-
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: placeholder flag metrics chart (#8197)
- Loading branch information
Showing
5 changed files
with
212 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
32 changes: 32 additions & 0 deletions
32
frontend/src/component/common/Chart/customHighlightPlugin.ts
This file contains 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,32 @@ | ||
import type { Chart } from 'chart.js'; | ||
|
||
export const customHighlightPlugin = { | ||
id: 'customLine', | ||
beforeDraw: (chart: Chart) => { | ||
const width = 46; | ||
if (chart.tooltip?.opacity && chart.tooltip.x) { | ||
const x = chart.tooltip.caretX; | ||
const yAxis = chart.scales.y; | ||
const ctx = chart.ctx; | ||
ctx.save(); | ||
const gradient = ctx.createLinearGradient( | ||
x, | ||
yAxis.top, | ||
x, | ||
yAxis.bottom + 34, | ||
); | ||
gradient.addColorStop(0, 'rgba(129, 122, 254, 0)'); | ||
gradient.addColorStop(1, 'rgba(129, 122, 254, 0.12)'); | ||
ctx.fillStyle = gradient; | ||
ctx.roundRect( | ||
x - width / 2, | ||
yAxis.top, | ||
width, | ||
yAxis.bottom - yAxis.top + 34, | ||
5, | ||
); | ||
ctx.fill(); | ||
ctx.restore(); | ||
} | ||
}, | ||
}; |
This file contains 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,16 @@ | ||
import type { Tick } from 'chart.js'; | ||
|
||
export const formatTickValue = ( | ||
tickValue: string | number, | ||
index: number, | ||
ticks: Tick[], | ||
) => { | ||
if (typeof tickValue === 'string') { | ||
return tickValue; | ||
} | ||
const value = Number.parseInt(tickValue.toString()); | ||
if (value > 999999) { | ||
return `${value / 1000000}M`; | ||
} | ||
return value > 999 ? `${value / 1000}k` : value; | ||
}; |
126 changes: 126 additions & 0 deletions
126
frontend/src/component/personalDashboard/FlagMetricsChart.tsx
This file contains 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,126 @@ | ||
import { | ||
BarElement, | ||
CategoryScale, | ||
Chart as ChartJS, | ||
type ChartOptions, | ||
Legend, | ||
LinearScale, | ||
Title, | ||
Tooltip, | ||
} from 'chart.js'; | ||
import annotationPlugin from 'chartjs-plugin-annotation'; | ||
import { Bar } from 'react-chartjs-2'; | ||
import type { Theme } from '@mui/material/styles/createTheme'; | ||
import useTheme from '@mui/material/styles/useTheme'; | ||
import { useMemo } from 'react'; | ||
import { formatTickValue } from 'component/common/Chart/formatTickValue'; | ||
|
||
const defaultYes = [ | ||
45_000_000, 28_000_000, 28_000_000, 25_000_000, 50_000_000, 27_000_000, | ||
26_000_000, 50_000_000, 32_000_000, 12_000_000, 13_000_000, 31_000_000, | ||
12_000_000, 47_000_000, 29_000_000, 46_000_000, 45_000_000, 28_000_000, | ||
28_000_000, 25_000_000, 50_000_000, 27_000_000, 26_000_000, 50_000_000, | ||
32_000_000, 12_000_000, 13_000_000, 31_000_000, 12_000_000, 47_000_000, | ||
]; | ||
const defaultNo = [ | ||
5_000_000, 8_000_000, 3_000_000, 2_000_000, 2_000_000, 5_000_000, 9_000_000, | ||
3_000_000, 7_000_000, 2_000_000, 5_000_000, 8_000_000, 3_000_000, 2_000_000, | ||
2_000_000, 5_000_000, 1_000_000, 3_000_000, 12_000_000, 2_000_000, | ||
1_000_000, 1_000_000, 3_000_000, 2_000_000, 2_000_000, 5_000_000, 1_000_000, | ||
3_000_000, 8_000_000, 2_000_000, | ||
]; | ||
|
||
const data = { | ||
labels: Array.from({ length: 30 }, (_, i) => i + 1), | ||
datasets: [ | ||
{ | ||
data: defaultYes, | ||
label: 'yes', | ||
backgroundColor: '#BEBEBE', | ||
hoverBackgroundColor: '#BEBEBE', | ||
}, | ||
{ | ||
data: defaultNo, | ||
label: 'no', | ||
backgroundColor: '#9A9A9A', | ||
hoverBackgroundColor: '#9A9A9A', | ||
}, | ||
], | ||
}; | ||
|
||
const createBarChartOptions = (theme: Theme): ChartOptions<'bar'> => ({ | ||
plugins: { | ||
legend: { | ||
position: 'bottom', | ||
labels: { | ||
color: theme.palette.text.primary, | ||
pointStyle: 'circle', | ||
usePointStyle: true, | ||
boxHeight: 6, | ||
padding: 15, | ||
boxPadding: 5, | ||
}, | ||
}, | ||
tooltip: { | ||
enabled: false, | ||
}, | ||
}, | ||
responsive: true, | ||
scales: { | ||
x: { | ||
stacked: true, | ||
ticks: { | ||
color: theme.palette.text.secondary, | ||
}, | ||
grid: { | ||
display: false, | ||
}, | ||
}, | ||
y: { | ||
stacked: true, | ||
ticks: { | ||
color: theme.palette.text.secondary, | ||
maxTicksLimit: 5, | ||
callback: formatTickValue, | ||
}, | ||
grid: { | ||
drawBorder: false, | ||
}, | ||
}, | ||
}, | ||
elements: { | ||
bar: { | ||
borderRadius: 5, | ||
}, | ||
}, | ||
interaction: { | ||
mode: 'index', | ||
intersect: false, | ||
}, | ||
}); | ||
|
||
export const PlaceholderFlagMetricsChart = () => { | ||
const theme = useTheme(); | ||
|
||
const options = useMemo(() => { | ||
return createBarChartOptions(theme); | ||
}, [theme]); | ||
|
||
return ( | ||
<Bar | ||
data={data} | ||
options={options} | ||
aria-label='A bar chart with a single feature flag exposure metrics' | ||
/> | ||
); | ||
}; | ||
|
||
ChartJS.register( | ||
annotationPlugin, | ||
CategoryScale, | ||
LinearScale, | ||
BarElement, | ||
Title, | ||
Tooltip, | ||
Legend, | ||
); |
This file contains 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