Skip to content

Commit 55a4edb

Browse files
committed
fix: use referenceLines
1 parent 1eb141b commit 55a4edb

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

src/Shared/Components/Charts/Chart.component.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { DEVTRON_BASE_MAIN_ID } from '@Shared/constants'
2222
import { useTheme } from '@Shared/Providers'
2323

2424
import { LEGENDS_LABEL_CONFIG } from './constants'
25-
import { getAverageLinePlugin, getSeparatorLinePlugin } from './plugins'
25+
import { drawReferenceLine, getSeparatorLinePlugin } from './plugins'
2626
import { ChartProps, GetDefaultOptionsParams } from './types'
2727
import { buildChartTooltipFromContext, getChartJSType, getDefaultOptions, transformDataForChart } from './utils'
2828

@@ -159,7 +159,7 @@ const Chart = (props: ChartProps) => {
159159
hideAxis = false,
160160
onChartClick,
161161
separatorIndex,
162-
averageLineValue,
162+
referenceLines,
163163
hideXAxisLabels = false,
164164
xAxisMax,
165165
yAxisMax,
@@ -242,15 +242,15 @@ const Chart = (props: ChartProps) => {
242242
...defaultOptions,
243243
},
244244
plugins: [
245-
...(averageLineValue ? [getAverageLinePlugin(averageLineValue, appTheme)] : []),
245+
...(referenceLines ?? []).map((rl, idx) => drawReferenceLine(rl, `reference-line-${idx}`, appTheme)),
246246
...(separatorIndex ? [getSeparatorLinePlugin(separatorIndex, type, appTheme)] : []),
247247
],
248248
})
249249

250250
return () => {
251251
chartRef.current.destroy()
252252
}
253-
}, [type, datasets, labels, appTheme, hideAxis, averageLineValue, separatorIndex])
253+
}, [type, datasets, labels, appTheme, hideAxis, referenceLines, separatorIndex])
254254

255255
return (
256256
<div className="h-100 w-100 dc__position-rel">
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
export { default as Chart } from './Chart.component'
22
export { CHART_COLORS } from './constants'
3-
export type { ChartColorKey, ChartProps, ChartType, SimpleDataset, SimpleDatasetForPie } from './types'
3+
export type {
4+
ChartColorKey,
5+
ChartProps,
6+
ChartType,
7+
ReferenceLineConfigType,
8+
SimpleDataset,
9+
SimpleDatasetForPie,
10+
} from './types'
411
export { chartColorGenerator } from './utils'

src/Shared/Components/Charts/plugins.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import { Chart, Plugin } from 'chart.js'
33
import { AppThemeType } from '@Shared/Providers'
44

55
import { CHART_COLORS } from './constants'
6-
import { ChartType } from './types'
6+
import { ChartType, ReferenceLineConfigType } from './types'
77

8-
export const getAverageLinePlugin = (averageValue: number, appTheme: AppThemeType): Plugin => ({
9-
id: 'averageLine',
8+
export const drawReferenceLine = (config: ReferenceLineConfigType, id: string, appTheme: AppThemeType): Plugin => ({
9+
id,
1010
afterDraw: (chart) => {
1111
const { ctx, chartArea, scales } = chart
12-
if (!scales || !scales.y || !averageValue) {
12+
if (!scales || !scales.y || !config?.value) {
1313
return
1414
}
15-
const yValue = scales.y.getPixelForValue(averageValue)
15+
const yValue = scales.y.getPixelForValue(config.value)
1616
ctx.save()
1717
ctx.beginPath()
1818
ctx.setLineDash([6, 6])
19-
ctx.strokeStyle = CHART_COLORS[appTheme].CharcoalGray700
20-
ctx.lineWidth = 1
19+
ctx.strokeStyle = CHART_COLORS[appTheme][config.color ?? 'CharcoalGray700']
20+
ctx.lineWidth = config.strokeWidth ?? 1
2121
ctx.moveTo(chartArea.left, yValue)
2222
ctx.lineTo(chartArea.right, yValue)
2323
ctx.stroke()

src/Shared/Components/Charts/types.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ export interface SimpleDatasetForLine extends BaseSimpleDataset {
4242
borderColor: ChartColorKey
4343
}
4444

45+
export interface ReferenceLineConfigType {
46+
strokeWidth?: number
47+
color?: ChartColorKey
48+
value: number
49+
}
50+
4551
type XYAxisMax = {
4652
xAxisMax?: number
4753
yAxisMax?: number
54+
/**
55+
* Optional reference lines to draw across the chart
56+
*/
57+
referenceLines?: ReferenceLineConfigType[]
4858
}
4959

5060
type TypeAndDatasetsType =
@@ -55,25 +65,21 @@ type TypeAndDatasetsType =
5565
*/
5666
datasets: SimpleDatasetForPie
5767
separatorIndex?: never
58-
averageLineValue?: never
5968
} & Never<XYAxisMax>)
6069
| ({
6170
type: 'line'
6271
datasets: SimpleDatasetForLine[]
6372
separatorIndex?: never
64-
averageLineValue?: number
6573
} & XYAxisMax)
6674
| ({
6775
type: 'area'
6876
datasets: SimpleDataset[]
6977
separatorIndex?: never
70-
averageLineValue?: number
7178
} & XYAxisMax)
7279
| ({
7380
type: Exclude<ChartType, 'pie' | 'line' | 'area'>
7481
datasets: SimpleDataset[]
7582
separatorIndex?: number
76-
averageLineValue?: number
7783
} & XYAxisMax)
7884

7985
export type ChartProps = {

0 commit comments

Comments
 (0)