Skip to content

Commit 51eb1bf

Browse files
fix(MetricChart): draw area charts (#764)
1 parent 7239727 commit 51eb1bf

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@gravity-ui/axios-wrapper": "^1.4.1",
14-
"@gravity-ui/chartkit": "^4.20.1",
14+
"@gravity-ui/chartkit": "^4.23.0",
1515
"@gravity-ui/components": "^2.12.0",
1616
"@gravity-ui/date-utils": "^1.4.2",
1717
"@gravity-ui/i18n": "^1.2.0",
@@ -24,6 +24,7 @@
2424
"@reduxjs/toolkit": "^2.2.1",
2525
"axios": "^1.6.7",
2626
"bem-cn-lite": "^4.1.0",
27+
"colord": "^2.9.3",
2728
"copy-to-clipboard": "^3.3.3",
2829
"crc-32": "^1.2.2",
2930
"history": "^4.10.1",

src/components/MetricChart/MetricChart.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {useCallback, useEffect, useReducer, useRef} from 'react';
22

3-
import {RawSerieData, YagrPlugin, YagrWidgetData} from '@gravity-ui/chartkit/yagr';
3+
import {YagrPlugin, YagrSeriesData, YagrWidgetData} from '@gravity-ui/chartkit/yagr';
44
import ChartKit, {settings} from '@gravity-ui/chartkit';
55

66
import type {IResponseError} from '../../types/api/error';
77
import type {TimeFrame} from '../../utils/timeframes';
88
import {useAutofetcher} from '../../utils/hooks';
99

10-
import {COLORS} from '../../utils/versions';
1110
import {cn} from '../../utils/cn';
1211

1312
import {Loader} from '../Loader';
@@ -30,6 +29,7 @@ import {
3029
setChartDataWasNotLoaded,
3130
setChartError,
3231
} from './reducer';
32+
import {colorToRGBA, colors} from './colors';
3333
import i18n from './i18n';
3434

3535
import './MetricChart.scss';
@@ -47,13 +47,19 @@ const prepareWidgetData = (
4747

4848
const isDataEmpty = !data.metrics.length;
4949

50-
const graphs: RawSerieData[] = data.metrics.map((metric, index) => {
50+
const graphs: YagrSeriesData[] = data.metrics.map((metric, index) => {
51+
const lineColor = metric.color || colors[index];
52+
const color = colorToRGBA(lineColor, 0.1);
53+
5154
return {
5255
id: metric.target,
5356
name: metric.title || metric.target,
54-
color: metric.color || COLORS[index],
5557
data: metric.data,
5658
formatter: defaultDataFormatter,
59+
60+
lineColor,
61+
color,
62+
legendColorKey: 'lineColor',
5763
};
5864
});
5965

@@ -71,7 +77,8 @@ const prepareWidgetData = (
7177
padding: isDataEmpty ? [10, 0, 10, 0] : undefined,
7278
},
7379
series: {
74-
type: 'line',
80+
type: 'area',
81+
lineWidth: 1.5,
7582
},
7683
select: {
7784
zoom: false,
@@ -209,7 +216,7 @@ export const MetricChart = ({
209216
dispatch(setChartError(err as IResponseError));
210217
}
211218
},
212-
[metrics, timeFrame, width],
219+
[database, metrics, timeFrame, width],
213220
);
214221

215222
useAutofetcher(fetchChartData, [fetchChartData], autorefresh);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {colord} from 'colord';
2+
3+
// Grafana classic palette
4+
export const colors = [
5+
'#7EB26D', // 0: pale green
6+
'#EAB839', // 1: mustard
7+
'#6ED0E0', // 2: light blue
8+
'#EF843C', // 3: orange
9+
'#E24D42', // 4: red
10+
'#1F78C1', // 5: ocean
11+
'#BA43A9', // 6: purple
12+
'#705DA0', // 7: violet
13+
'#508642', // 8: dark green
14+
'#CCA300', // 9: dark sand
15+
];
16+
17+
export function colorToRGBA(initialColor: string, opacity: number) {
18+
const color = colord(initialColor);
19+
if (!color.isValid()) {
20+
throw new Error('Invalid color is passed');
21+
}
22+
return color.alpha(opacity).toRgbString();
23+
}

0 commit comments

Comments
 (0)