11import React , { RefObject , useEffect , useRef } from "react" ;
2- import { Chart , ChartItem , ChartConfiguration , LineController , LineElement , PointElement , LinearScale , Title , CategoryScale , ScatterController , ChartData , ChartDataset , ChartType , BarController , BarElement } from "chart.js" ;
3- Chart . register ( LineController , BarController , BarElement , ScatterController , LineElement , PointElement , LinearScale , Title , CategoryScale ) ;
2+ import { Chart , ChartItem , ChartConfiguration , LineController , LineElement , PointElement , LinearScale , Title , CategoryScale , ChartType , Legend } from "chart.js" ;
3+ Chart . register ( LineController , LineElement , PointElement , LinearScale , Title , Legend , CategoryScale ) ;
44
55// ArrayLike<any> is used for data because chartjs supports TypedArrays and we
66// might use those too down the line
77interface CardLineChartProps {
88 title : string ;
9- color : string ;
10- datasetNames ?: string [ ] ; // optional list of names to give each y-value set
119 numRows : number ; // total number of rows of data. Used to rerender the chart on updates
1210 dataX : ArrayLike < any > ; // eg. timestamps
1311 dataY : ArrayLike < any > [ ] ; // 1 or more y-value sets
12+ datasetNames ?: string [ ] ; // optional list of names to give each y-value set
13+ dataXUnits : string ,
14+ dataYUnits : string ,
1415}
1516
1617const CHART_TYPE : ChartType = 'line'
1718
18- function initChart ( chartRef : RefObject < ChartItem > , { title, color, dataY, datasetNames : names } : CardLineChartProps ) {
19+ const unique_colors : ( keyof DefaultColors ) [ ] = [
20+ "red" , "amber" , "lime" , "cyan" , "blue" , "violet" , "fuchsia" , "pink"
21+ ]
22+ import colors from "tailwindcss/colors" ;
23+ import { DefaultColors } from "tailwindcss/types/generated/colors" ;
24+
25+ function initChart ( chartRef : RefObject < ChartItem > , { title, dataY, datasetNames, dataXUnits, dataYUnits } : CardLineChartProps ) {
1926
2027 const config : ChartConfiguration < ChartType , typeof dataY [ 0 ] , number > = {
2128 type : CHART_TYPE ,
2229 data : {
2330 datasets : dataY . map ( ( _ , i ) => ( {
2431 data : [ ] ,
25- label : names ? names [ i ] : ( dataY . length > 1 ? `Dataset ${ i } ` : title ) ,
32+ label : datasetNames ? datasetNames [ i ] : ( dataY . length > 1 ? `Dataset ${ i } ` : title ) ,
33+ backgroundColor : `${ colors [ unique_colors [ i ] ] [ '900' ] } ` ,
34+ borderColor : `${ colors [ unique_colors [ i ] ] [ '700' ] } ` ,
2635 } ) ) ,
2736 // labels: [],
2837 } ,
2938 options : {
3039 datasets : {
3140 line : {
32- backgroundColor : color ,
33- borderColor : color ,
3441 pointRadius : 0 , // too noisy
3542 borderWidth : 2 ,
3643 spanGaps : true ,
@@ -40,16 +47,50 @@ function initChart(chartRef: RefObject<ChartItem>, { title, color, dataY, datase
4047 parsing : false , // requires data to be in internal obj format, should improve performance
4148 responsive : true ,
4249 animation : false ,
50+ interaction : {
51+ mode : 'nearest'
52+ } ,
53+ plugins : {
54+ legend : {
55+ display : true ,
56+ position : 'top' ,
57+ align : 'center' ,
58+ labels : {
59+ font : { size : 12 } ,
60+ boxWidth : 8 ,
61+ boxHeight : 8 ,
62+ }
63+ } ,
64+ title : {
65+ display : false ,
66+ text : title ,
67+ } ,
68+ tooltip : {
69+ enabled : true ,
70+ intersect : false ,
71+ }
72+ } ,
73+ aspectRatio : 1.3 ,
4374 scales : {
4475 x : {
76+ title : {
77+ display : dataXUnits != null ,
78+ text : dataXUnits ,
79+ font : { size : 14 } ,
80+ color : colors [ "neutral" ] [ 500 ] ,
81+ } ,
4582 type : 'linear' ,
46- // grace: '1%',
4783 ticks : { color : "rgba(255,255,255,.7)" } ,
4884 grid : { display : false } ,
4985 } ,
5086 y : {
87+ title : {
88+ display : dataYUnits != null ,
89+ text : dataYUnits ,
90+ font : { size : 14 } ,
91+ color : colors [ "neutral" ] [ 500 ] ,
92+ } ,
5193 type : 'linear' ,
52- // grace: '10%',
5394 ticks : { color : "rgba(255,255,255,.7)" } ,
5495 grid : { color : "rgba(255, 255, 255, 0.15)" } ,
5596 } ,
@@ -118,19 +159,15 @@ export default function CardLineChart(props: CardLineChartProps) {
118159
119160 return (
120161 < >
121- < div className = "flex flex-col break-words rounded " >
122- < h6 className = "uppercase text-blueGray-100 text-lg font-semibold" >
162+ < div className = "break-words" >
163+ < h6 className = "text-lg font-semibold" >
123164 { title }
124165 </ h6 >
125- < div className = "flex-auto" >
126- { /* Chart */ }
166+ < div className = "" >
127167 < div className = "relative " >
128168 { /* for some reason tsserver gets confused here... not idea why */ }
129169 < canvas ref = { chartRef as any } > </ canvas >
130170 </ div >
131- < p className = { "transition-transform duration-500 font-mono select-none" } > Current
132- Value: { dataY [ 0 ] && dataY [ 0 ] . length > 2 ? dataY [ 0 ] [ dataY [ 0 ] . length - 1 ] . toFixed ( 2 ) : 0.000 }
133- </ p >
134171 </ div >
135172 </ div >
136173 </ >
0 commit comments