11import React from "react" ;
22import styled , { useTheme } from "styled-components" ;
33
4- import { Chart as ChartJS , CategoryScale , LinearScale , PointElement , LineElement , TimeScale , Tooltip } from "chart.js" ;
4+ import {
5+ Chart as ChartJS ,
6+ CategoryScale ,
7+ LinearScale ,
8+ PointElement ,
9+ LineElement ,
10+ TimeScale ,
11+ Tooltip ,
12+ ScriptableContext ,
13+ } from "chart.js" ;
514import { Line } from "react-chartjs-2" ;
615import "chartjs-adapter-moment" ;
716
@@ -51,6 +60,7 @@ const TimeSeriesChart: React.FC<ITimeSeriesChart> = ({ data }) => {
5160 } ,
5261 plugins : {
5362 tooltip : {
63+ position : "custom" ,
5464 backgroundColor : theme . whiteBackground ,
5565 titleColor : theme . primaryText ,
5666 borderColor : theme . stroke ,
@@ -74,18 +84,47 @@ const TimeSeriesChart: React.FC<ITimeSeriesChart> = ({ data }) => {
7484 datasets : [
7585 {
7686 data,
77- borderColor : theme . primaryBlue ,
87+ // borderColor: theme.primaryBlue,
7888 stepped : true ,
7989 cubicInterpolationMode : "monotone" ,
90+ borderColor : ( context : ScriptableContext < "line" > ) => {
91+ const ctx = context . chart . ctx ;
92+ const gradient = ctx . createLinearGradient ( 0 , 0 , 0 , 200 ) ;
93+ gradient . addColorStop ( 0 , theme . primaryBlue ) ;
94+ gradient . addColorStop ( 1 , theme . secondaryPurple ) ;
95+ return gradient ;
96+ } ,
8097 } ,
8198 ] ,
8299 } ,
83100 options,
84101 } }
102+ plugins = { [
103+ {
104+ id : "line-draw" ,
105+ afterDatasetsDraw : ( chart ) => {
106+ if ( chart . tooltip ?. _active ?. length ) {
107+ const x = chart . tooltip . _active [ 0 ] . element . x ;
108+ const y = chart . tooltip . _active [ 0 ] . element . y ;
109+ const yAxis = chart . scales . y ;
110+
111+ const ctx = chart . ctx ;
112+ ctx . save ( ) ;
113+ ctx . beginPath ( ) ;
114+ ctx . moveTo ( x , y ) ;
115+ ctx . lineTo ( x , yAxis . bottom ) ;
116+ ctx . lineWidth = 1 ;
117+ ctx . strokeStyle = theme . secondaryPurple ;
118+ ctx . setLineDash ( [ 4 , 4 ] ) ;
119+ ctx . stroke ( ) ;
120+ ctx . restore ( ) ;
121+ }
122+ } ,
123+ } ,
124+ ] }
85125 />
86126 }
87127 </ LineContainer >
88128 ) ;
89129} ;
90-
91130export default TimeSeriesChart ;
0 commit comments