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
4cc0be25
Commit
4cc0be25
authored
Jan 18, 2019
by
Hugo Häggmark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Redid logic for fontcolor and thresholds in Gauge and added tests
parent
c17ccf22
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
20 deletions
+62
-20
packages/grafana-ui/src/components/Gauge/Gauge.test.tsx
+32
-4
packages/grafana-ui/src/components/Gauge/Gauge.tsx
+30
-16
No files found.
packages/grafana-ui/src/components/Gauge/Gauge.test.tsx
View file @
4cc0be25
...
@@ -45,7 +45,7 @@ describe('Get font color', () => {
...
@@ -45,7 +45,7 @@ describe('Get font color', () => {
expect
(
instance
.
getFontColor
(
49
)).
toEqual
(
'#7EB26D'
);
expect
(
instance
.
getFontColor
(
49
)).
toEqual
(
'#7EB26D'
);
});
});
it
(
'should get the
next
threshold color if value is same as a threshold'
,
()
=>
{
it
(
'should get the threshold color if value is same as a threshold'
,
()
=>
{
const
{
instance
}
=
setup
({
const
{
instance
}
=
setup
({
thresholds
:
[
thresholds
:
[
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
...
@@ -54,10 +54,10 @@ describe('Get font color', () => {
...
@@ -54,10 +54,10 @@ describe('Get font color', () => {
],
],
});
});
expect
(
instance
.
getFontColor
(
50
)).
toEqual
(
'#
6ED0E0
'
);
expect
(
instance
.
getFontColor
(
50
)).
toEqual
(
'#
EAB839
'
);
});
});
it
(
'should get the nearest threshold color'
,
()
=>
{
it
(
'should get the nearest threshold color
between thresholds
'
,
()
=>
{
const
{
instance
}
=
setup
({
const
{
instance
}
=
setup
({
thresholds
:
[
thresholds
:
[
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
...
@@ -66,7 +66,35 @@ describe('Get font color', () => {
...
@@ -66,7 +66,35 @@ describe('Get font color', () => {
],
],
});
});
expect
(
instance
.
getFontColor
(
6.5
)).
toEqual
(
'#EAB839'
);
expect
(
instance
.
getFontColor
(
55
)).
toEqual
(
'#EAB839'
);
});
});
describe
(
'Get thresholds formatted'
,
()
=>
{
it
(
'should return first thresholds color for min and max'
,
()
=>
{
const
{
instance
}
=
setup
({
thresholds
:
[{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
}]
});
expect
(
instance
.
getFormattedThresholds
()).
toEqual
([
{
value
:
0
,
color
:
'#7EB26D'
},
{
value
:
100
,
color
:
'#7EB26D'
},
]);
});
it
(
'should get the correct formatted values when thresholds are added'
,
()
=>
{
const
{
instance
}
=
setup
({
thresholds
:
[
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
],
});
expect
(
instance
.
getFormattedThresholds
()).
toEqual
([
{
value
:
0
,
color
:
'#7EB26D'
},
{
value
:
50
,
color
:
'#7EB26D'
},
{
value
:
75
,
color
:
'#EAB839'
},
{
value
:
100
,
color
:
'#6ED0E0'
},
]);
});
});
});
});
...
...
packages/grafana-ui/src/components/Gauge/Gauge.tsx
View file @
4cc0be25
...
@@ -149,16 +149,42 @@ export class Gauge extends PureComponent<Props> {
...
@@ -149,16 +149,42 @@ export class Gauge extends PureComponent<Props> {
return
thresholds
[
0
].
color
;
return
thresholds
[
0
].
color
;
}
}
const
atThreshold
=
thresholds
.
filter
(
threshold
=>
(
value
as
number
)
<
threshold
.
value
);
const
atThreshold
=
thresholds
.
filter
(
threshold
=>
(
value
as
number
)
===
threshold
.
value
)[
0
];
if
(
atThreshold
)
{
return
atThreshold
.
color
;
}
const
belowThreshold
=
thresholds
.
filter
(
threshold
=>
(
value
as
number
)
>
threshold
.
value
);
if
(
at
Threshold
.
length
>
0
)
{
if
(
below
Threshold
.
length
>
0
)
{
const
nearestThreshold
=
atThreshold
.
sort
((
t1
,
t2
)
=>
t1
.
value
-
t2
.
value
)[
0
];
const
nearestThreshold
=
belowThreshold
.
sort
((
t1
,
t2
)
=>
t2
.
value
-
t1
.
value
)[
0
];
return
nearestThreshold
.
color
;
return
nearestThreshold
.
color
;
}
}
return
BasicGaugeColor
.
Red
;
return
BasicGaugeColor
.
Red
;
}
}
getFormattedThresholds
()
{
const
{
maxValue
,
minValue
,
thresholds
}
=
this
.
props
;
const
thresholdsSortedByIndex
=
[...
thresholds
].
sort
((
t1
,
t2
)
=>
t1
.
index
-
t2
.
index
);
const
lastThreshold
=
thresholdsSortedByIndex
[
thresholdsSortedByIndex
.
length
-
1
];
const
formattedThresholds
=
[
...
thresholdsSortedByIndex
.
map
(
threshold
=>
{
if
(
threshold
.
index
===
0
)
{
return
{
value
:
minValue
,
color
:
threshold
.
color
};
}
const
previousThreshold
=
thresholdsSortedByIndex
[
threshold
.
index
-
1
];
return
{
value
:
threshold
.
value
,
color
:
previousThreshold
.
color
};
}),
{
value
:
maxValue
,
color
:
lastThreshold
.
color
},
];
return
formattedThresholds
;
}
draw
()
{
draw
()
{
const
{
const
{
maxValue
,
maxValue
,
...
@@ -166,7 +192,6 @@ export class Gauge extends PureComponent<Props> {
...
@@ -166,7 +192,6 @@ export class Gauge extends PureComponent<Props> {
timeSeries
,
timeSeries
,
showThresholdLabels
,
showThresholdLabels
,
showThresholdMarkers
,
showThresholdMarkers
,
thresholds
,
width
,
width
,
height
,
height
,
stat
,
stat
,
...
@@ -190,17 +215,6 @@ export class Gauge extends PureComponent<Props> {
...
@@ -190,17 +215,6 @@ export class Gauge extends PureComponent<Props> {
const
thresholdMarkersWidth
=
gaugeWidth
/
5
;
const
thresholdMarkersWidth
=
gaugeWidth
/
5
;
const
thresholdLabelFontSize
=
fontSize
/
2.5
;
const
thresholdLabelFontSize
=
fontSize
/
2.5
;
const
formattedThresholds
=
[
{
value
:
minValue
,
color
:
thresholds
.
length
===
1
?
thresholds
[
0
].
color
:
BasicGaugeColor
.
Green
},
...
thresholds
.
map
((
threshold
,
index
)
=>
{
return
{
value
:
threshold
.
value
,
color
:
thresholds
[
index
].
color
,
};
}),
{
value
:
maxValue
,
color
:
thresholds
.
length
===
1
?
thresholds
[
0
].
color
:
BasicGaugeColor
.
Red
},
];
const
options
=
{
const
options
=
{
series
:
{
series
:
{
gauges
:
{
gauges
:
{
...
@@ -217,7 +231,7 @@ export class Gauge extends PureComponent<Props> {
...
@@ -217,7 +231,7 @@ export class Gauge extends PureComponent<Props> {
layout
:
{
margin
:
0
,
thresholdWidth
:
0
},
layout
:
{
margin
:
0
,
thresholdWidth
:
0
},
cell
:
{
border
:
{
width
:
0
}
},
cell
:
{
border
:
{
width
:
0
}
},
threshold
:
{
threshold
:
{
values
:
formattedThresholds
,
values
:
this
.
getFormattedThresholds
()
,
label
:
{
label
:
{
show
:
showThresholdLabels
,
show
:
showThresholdLabels
,
margin
:
thresholdMarkersWidth
+
1
,
margin
:
thresholdMarkersWidth
+
1
,
...
...
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