Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
a431efa0
Commit
a431efa0
authored
Jan 14, 2019
by
Hugo Häggmark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored logic in ThresholdEditor
parent
030d20d9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
36 deletions
+68
-36
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.test.tsx
+46
-13
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.tsx
+22
-23
No files found.
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.test.tsx
View file @
a431efa0
...
@@ -15,25 +15,56 @@ const setup = (propOverrides?: object) => {
...
@@ -15,25 +15,56 @@ const setup = (propOverrides?: object) => {
return
shallow
(<
ThresholdsEditor
{
...
props
}
/>).
instance
()
as
ThresholdsEditor
;
return
shallow
(<
ThresholdsEditor
{
...
props
}
/>).
instance
()
as
ThresholdsEditor
;
};
};
describe
(
'Initialization'
,
()
=>
{
it
(
'should add a base threshold if missing'
,
()
=>
{
const
instance
=
setup
();
expect
(
instance
.
state
.
thresholds
).
toEqual
([{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
}]);
});
});
describe
(
'Add threshold'
,
()
=>
{
describe
(
'Add threshold'
,
()
=>
{
it
(
'should add threshold'
,
()
=>
{
it
(
'should add threshold'
,
()
=>
{
const
instance
=
setup
();
const
instance
=
setup
();
instance
.
onAddThreshold
(
0
);
instance
.
onAddThreshold
(
1
);
expect
(
instance
.
state
.
thresholds
).
toEqual
([{
index
:
0
,
value
:
50
,
color
:
'rgb(127, 115, 64)'
}]);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
]);
});
});
it
(
'should add another threshold above a first'
,
()
=>
{
it
(
'should add another threshold above a first'
,
()
=>
{
const
instance
=
setup
({
const
instance
=
setup
({
thresholds
:
[{
index
:
0
,
value
:
50
,
color
:
'rgb(127, 115, 64)
'
}],
thresholds
:
[{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839
'
}],
});
});
instance
.
onAddThreshold
(
1
);
instance
.
onAddThreshold
(
2
);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
]);
});
it
(
'should add another threshold between first and second index'
,
()
=>
{
const
instance
=
setup
({
thresholds
:
[
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
],
});
instance
.
onAddThreshold
(
2
);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
1
,
value
:
75
,
color
:
'rgb(170, 95, 61)'
},
{
index
:
3
,
value
:
75
,
color
:
'#EF843C'
},
{
index
:
0
,
value
:
50
,
color
:
'rgb(127, 115, 64)'
},
{
index
:
2
,
value
:
62.5
,
color
:
'#6ED0E0'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
]);
]);
});
});
});
});
...
@@ -41,23 +72,25 @@ describe('Add threshold', () => {
...
@@ -41,23 +72,25 @@ describe('Add threshold', () => {
describe
(
'change threshold value'
,
()
=>
{
describe
(
'change threshold value'
,
()
=>
{
it
(
'should update value and resort rows'
,
()
=>
{
it
(
'should update value and resort rows'
,
()
=>
{
const
instance
=
setup
();
const
instance
=
setup
();
const
mockThresholds
=
[
const
thresholds
=
[
{
index
:
0
,
value
:
50
,
color
:
'rgba(237, 129, 40, 0.89)'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
75
,
color
:
'rgba(237, 129, 40, 0.89)'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
];
];
instance
.
state
=
{
instance
.
state
=
{
baseColor
:
BasicGaugeColor
.
Green
,
baseColor
:
BasicGaugeColor
.
Green
,
thresholds
:
mockThresholds
,
thresholds
,
};
};
const
mockEvent
=
{
target
:
{
value
:
78
}
};
const
mockEvent
=
{
target
:
{
value
:
78
}
};
instance
.
onChangeThresholdValue
(
mockEvent
,
mockThresholds
[
0
]);
instance
.
onChangeThresholdValue
(
mockEvent
,
thresholds
[
1
]);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
0
,
value
:
78
,
color
:
'rgba(237, 129, 40, 0.89)'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
75
,
color
:
'rgba(237, 129, 40, 0.89)'
},
{
index
:
1
,
value
:
78
,
color
:
'#EAB839'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
]);
]);
});
});
});
});
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.tsx
View file @
a431efa0
import
React
,
{
PureComponent
}
from
'react'
;
import
React
,
{
PureComponent
}
from
'react'
;
import
tinycolor
,
{
ColorInput
}
from
'tinycolor2'
;
//
import tinycolor, { ColorInput } from 'tinycolor2';
import
{
Threshold
,
BasicGaugeColor
}
from
'../../types'
;
import
{
Threshold
,
BasicGaugeColor
}
from
'../../types'
;
import
{
ColorPicker
}
from
'../ColorPicker/ColorPicker'
;
import
{
ColorPicker
}
from
'../ColorPicker/ColorPicker'
;
import
{
PanelOptionsGroup
}
from
'../PanelOptionsGroup/PanelOptionsGroup'
;
import
{
PanelOptionsGroup
}
from
'../PanelOptionsGroup/PanelOptionsGroup'
;
import
{
colors
}
from
'../../utils'
;
export
interface
Props
{
export
interface
Props
{
thresholds
:
Threshold
[];
thresholds
:
Threshold
[];
...
@@ -19,43 +20,41 @@ export class ThresholdsEditor extends PureComponent<Props, State> {
...
@@ -19,43 +20,41 @@ export class ThresholdsEditor extends PureComponent<Props, State> {
constructor
(
props
:
Props
)
{
constructor
(
props
:
Props
)
{
super
(
props
);
super
(
props
);
this
.
state
=
{
thresholds
:
props
.
thresholds
,
baseColor
:
BasicGaugeColor
.
Green
};
const
thresholds
:
Threshold
[]
=
props
.
thresholds
.
length
>
0
?
props
.
thresholds
:
[{
index
:
0
,
value
:
-
Infinity
,
color
:
colors
[
0
]
}];
this
.
state
=
{
thresholds
,
baseColor
:
BasicGaugeColor
.
Green
};
}
}
onAddThreshold
=
(
index
:
number
)
=>
{
onAddThreshold
=
(
index
:
number
)
=>
{
const
maxValue
=
100
;
// hardcoded for now before we add the base threshold
const
minValue
=
0
;
// hardcoded for now before we add the base threshold
const
{
thresholds
}
=
this
.
state
;
const
{
thresholds
}
=
this
.
state
;
const
maxValue
=
100
;
const
minValue
=
0
;
if
(
index
===
0
)
{
return
;
}
const
newThresholds
=
thresholds
.
map
(
threshold
=>
{
const
newThresholds
=
thresholds
.
map
(
threshold
=>
{
if
(
threshold
.
index
>=
index
)
{
if
(
threshold
.
index
>=
index
)
{
const
index
=
threshold
.
index
+
1
;
threshold
=
{
threshold
=
{
...
threshold
,
...
threshold
,
index
:
threshold
.
index
+
1
,
index
,
color
:
colors
[
index
],
};
};
}
}
return
threshold
;
return
threshold
;
});
});
// Setting value to a value between the previous thresholds
// Setting value to a value between the previous thresholds
let
value
;
const
beforeThreshold
=
newThresholds
.
filter
(
threshold
=>
threshold
.
index
===
index
-
1
)[
0
];
const
afterThreshold
=
newThresholds
.
filter
(
threshold
=>
threshold
.
index
===
index
+
1
)[
0
];
if
(
index
===
0
&&
thresholds
.
length
===
0
)
{
const
beforeThresholdValue
=
beforeThreshold
!==
undefined
?
Math
.
max
(
beforeThreshold
.
value
,
minValue
)
:
minValue
;
value
=
maxValue
-
(
maxValue
-
minValue
)
/
2
;
const
afterThresholdValue
=
afterThreshold
!==
undefined
?
Math
.
min
(
afterThreshold
.
value
,
maxValue
)
:
maxValue
;
}
else
if
(
index
===
0
&&
thresholds
.
length
>
0
)
{
const
value
=
afterThresholdValue
-
(
afterThresholdValue
-
beforeThresholdValue
)
/
2
;
value
=
newThresholds
[
index
+
1
].
value
-
(
newThresholds
[
index
+
1
].
value
-
minValue
)
/
2
;
}
else
if
(
index
>
newThresholds
[
newThresholds
.
length
-
1
].
index
)
{
// Set a color
value
=
maxValue
-
(
maxValue
-
newThresholds
[
index
-
1
].
value
)
/
2
;
const
color
=
colors
[
index
];
}
// Set a color that lies between the previous thresholds
let
color
;
if
(
index
===
0
&&
thresholds
.
length
===
0
)
{
color
=
tinycolor
.
mix
(
BasicGaugeColor
.
Green
,
BasicGaugeColor
.
Red
,
50
).
toRgbString
();
}
else
{
color
=
tinycolor
.
mix
(
thresholds
[
index
-
1
].
color
as
ColorInput
,
BasicGaugeColor
.
Red
,
50
).
toRgbString
();
}
this
.
setState
(
this
.
setState
(
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment