-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Chart component using chart.js #858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5789fb7
feat: add Chart component
Elessar1802 4f3d6d2
refactor: chart types
Elessar1802 f7642c7
refactor: chart & colors
Elessar1802 a9ce9a6
feat: add line chart
Elessar1802 98f1ebe
feat: add gradient to area chart
Elessar1802 a417de2
Merge branch 'kubecon-2025' of github.com:devtron-labs/devtron-fe-com…
Elessar1802 f9add16
chore: add documentation comment to chart component
Elessar1802 bfa7e76
refactor: improve types
Elessar1802 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { | ||
ArcElement, | ||
BarController, | ||
BarElement, | ||
CategoryScale, | ||
Chart as ChartJS, | ||
DoughnutController, | ||
Filler, | ||
Legend, | ||
LinearScale, | ||
LineController, | ||
LineElement, | ||
PointElement, | ||
Title, | ||
Tooltip, | ||
} from 'chart.js' | ||
|
||
import { LEGENDS_LABEL_CONFIG } from './constants' | ||
import { ChartProps } from './types' | ||
import { getChartJSType, getDefaultOptions, transformDataForChart } from './utils' | ||
|
||
// Register Chart.js components | ||
ChartJS.register( | ||
CategoryScale, | ||
LinearScale, | ||
BarController, | ||
BarElement, | ||
LineController, | ||
LineElement, | ||
PointElement, | ||
DoughnutController, | ||
ArcElement, | ||
Title, | ||
Tooltip, | ||
Legend, | ||
Filler, | ||
) | ||
|
||
ChartJS.overrides.doughnut.plugins.legend.labels = { | ||
...ChartJS.overrides.doughnut.plugins.legend.labels, | ||
...LEGENDS_LABEL_CONFIG, | ||
} | ||
|
||
const Chart = ({ id, type, labels, datasets, className, style }: ChartProps) => { | ||
Elessar1802 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const canvasRef = useRef<HTMLCanvasElement>(null) | ||
const chartRef = useRef<ChartJS | null>(null) | ||
|
||
useEffect(() => { | ||
AbhishekA1509 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const ctx = canvasRef.current.getContext('2d') | ||
|
||
// Get Chart.js type and transform data | ||
const chartJSType = getChartJSType(type) | ||
const transformedData = transformDataForChart(labels, datasets, type) | ||
const defaultOptions = getDefaultOptions(type) | ||
|
||
// Create new chart | ||
chartRef.current = new ChartJS(ctx, { | ||
type: chartJSType, | ||
data: transformedData, | ||
options: defaultOptions, | ||
}) | ||
AbhishekA1509 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return () => { | ||
chartRef.current?.destroy() | ||
} | ||
}, [type, datasets, labels]) | ||
AbhishekA1509 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return ( | ||
<div className="flex" style={style}> | ||
<canvas id={id} ref={canvasRef} className={className} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Chart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const LEGENDS_LABEL_CONFIG = { | ||
usePointStyle: true, | ||
pointStyle: 'rectRounded', | ||
pointStyleWidth: 0, | ||
font: { | ||
family: "'IBM Plex Sans', 'Open Sans', 'Roboto'", | ||
size: 13, | ||
lineHeight: '150%', | ||
weight: 400, | ||
}, | ||
} as const |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as Chart } from './Chart.component' | ||
export type { ChartProps, SimpleDataset } from './types' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export type ChartType = 'area' | 'pie' | 'stackedBar' | 'stackedBarHorizontal' | ||
|
||
export interface SimpleDataset { | ||
label: string | ||
data: number[] | ||
AbhishekA1509 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
export interface ChartProps { | ||
id: string | ||
type: ChartType | ||
labels: string[] | ||
AbhishekA1509 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
datasets: SimpleDataset[] | ||
className?: string | ||
style?: React.CSSProperties | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
import { ChartData, ChartDataset, ChartOptions, ChartType as ChartJSChartType } from 'chart.js' | ||
|
||
import { LEGENDS_LABEL_CONFIG } from './constants' | ||
import { ChartType, SimpleDataset } from './types' | ||
|
||
const getCSSVariableValue = (variableName: string) => { | ||
const value = getComputedStyle(document.querySelector('#devtron-base-main-identifier')).getPropertyValue( | ||
AbhishekA1509 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
variableName, | ||
) | ||
|
||
if (!value) { | ||
// eslint-disable-next-line no-console | ||
console.error(`CSS variable "${variableName}" not found`) | ||
} | ||
|
||
return value ?? 'transparent' | ||
AbhishekA1509 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
// Map our chart types to Chart.js types | ||
export const getChartJSType = (type: ChartType): ChartJSChartType => { | ||
switch (type) { | ||
case 'area': | ||
return 'line' | ||
case 'pie': | ||
return 'doughnut' | ||
case 'stackedBar': | ||
case 'stackedBarHorizontal': | ||
return 'bar' | ||
default: | ||
return type as ChartJSChartType | ||
} | ||
} | ||
|
||
// Get default options based on chart type | ||
export const getDefaultOptions = (type: ChartType): ChartOptions => { | ||
const baseOptions: ChartOptions = { | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
devicePixelRatio: 3, | ||
plugins: { | ||
legend: { | ||
position: 'bottom' as const, | ||
labels: LEGENDS_LABEL_CONFIG, | ||
}, | ||
title: { | ||
display: false, | ||
}, | ||
}, | ||
elements: { | ||
line: { | ||
fill: true, | ||
tension: 0.4, | ||
}, | ||
bar: { | ||
borderSkipped: 'start' as const, | ||
borderWidth: 2, | ||
borderColor: 'transparent', | ||
borderRadius: 4, | ||
}, | ||
arc: { | ||
spacing: 2, | ||
}, | ||
}, | ||
} | ||
|
||
const gridConfig = { | ||
color: getCSSVariableValue('--N50'), | ||
} | ||
|
||
switch (type) { | ||
case 'area': | ||
return { | ||
...baseOptions, | ||
plugins: { | ||
...baseOptions.plugins, | ||
tooltip: { | ||
mode: 'index', | ||
}, | ||
}, | ||
interaction: { | ||
mode: 'nearest', | ||
axis: 'x', | ||
intersect: false, | ||
}, | ||
scales: { | ||
y: { | ||
stacked: true, | ||
beginAtZero: true, | ||
grid: gridConfig, | ||
}, | ||
x: { | ||
grid: gridConfig, | ||
}, | ||
}, | ||
} as ChartOptions<'line'> | ||
case 'stackedBar': | ||
return { | ||
...baseOptions, | ||
scales: { | ||
x: { | ||
stacked: true, | ||
grid: gridConfig, | ||
}, | ||
y: { | ||
stacked: true, | ||
beginAtZero: true, | ||
grid: gridConfig, | ||
}, | ||
}, | ||
} | ||
case 'stackedBarHorizontal': | ||
return { | ||
...baseOptions, | ||
indexAxis: 'y' as const, | ||
scales: { | ||
x: { | ||
stacked: true, | ||
beginAtZero: true, | ||
grid: gridConfig, | ||
}, | ||
y: { | ||
stacked: true, | ||
grid: gridConfig, | ||
}, | ||
}, | ||
} | ||
case 'pie': | ||
return { | ||
...baseOptions, | ||
plugins: { | ||
...baseOptions.plugins, | ||
legend: { | ||
position: 'right', | ||
align: 'center', | ||
}, | ||
}, | ||
} | ||
default: | ||
return baseOptions | ||
} | ||
} | ||
|
||
// Generates a palette of pastel HSL colors | ||
const generateColors = (count: number): string[] => { | ||
const colors: string[] = [] | ||
for (let i = 0; i < count; i++) { | ||
const hue = (i * 360) / count | ||
const saturation = 50 // Pastel: 40-60% | ||
const lightness = 75 // Pastel: 80-90% | ||
colors.push(`hsl(${hue}, ${saturation}%, ${lightness}%)`) | ||
} | ||
return colors | ||
} | ||
|
||
// Generates a slightly darker shade for a given HSL color string | ||
const generateCorrespondingBorderColor = (hsl: string): string => { | ||
// Parse hsl string: hsl(hue, saturation%, lightness%) | ||
const match = hsl.match(/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/) | ||
AbhishekA1509 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (!match) throw new Error('Invalid HSL color format') | ||
const hue = Number(match[1]) | ||
const saturation = Number(match[2]) | ||
let lightness = Number(match[3]) | ||
lightness = Math.max(0, lightness - 15) // Clamp to 0 | ||
return `hsl(${hue}, ${saturation}%, ${lightness}%)` | ||
} | ||
|
||
// Transform simple data to Chart.js format with consistent styling | ||
export const transformDataForChart = (labels: string[], datasets: SimpleDataset[], type: ChartType): ChartData => { | ||
const colors = generateColors(type === 'pie' ? datasets[0].data.length : datasets.length) | ||
AbhishekA1509 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const transformedDatasets = datasets.map((dataset, index) => { | ||
const colorIndex = index % colors.length | ||
const baseDataset = { | ||
label: dataset.label, | ||
data: dataset.data, | ||
backgroundColor: colors[colorIndex], | ||
} | ||
|
||
switch (type) { | ||
case 'area': | ||
return { | ||
...baseDataset, | ||
fill: true, | ||
pointRadius: 0, | ||
pointHoverRadius: 10, | ||
pointHitRadius: 20, | ||
pointStyle: 'rectRounded', | ||
pointBorderWidth: 0, | ||
borderWidth: 2, | ||
borderColor: generateCorrespondingBorderColor(colors[colorIndex]), | ||
} as ChartDataset<'line'> | ||
case 'pie': | ||
return { | ||
...baseDataset, | ||
backgroundColor: colors.slice(0, dataset.data.length), | ||
} | ||
case 'stackedBar': | ||
case 'stackedBarHorizontal': | ||
default: | ||
return baseDataset | ||
} | ||
}) | ||
|
||
return { | ||
labels, | ||
datasets: transformedDatasets, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.