1
1
import React from "react" ;
2
2
import styled , { useTheme } from "styled-components" ;
3
3
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" ;
5
14
import { Line } from "react-chartjs-2" ;
6
15
import "chartjs-adapter-moment" ;
7
16
@@ -51,6 +60,7 @@ const TimeSeriesChart: React.FC<ITimeSeriesChart> = ({ data }) => {
51
60
} ,
52
61
plugins : {
53
62
tooltip : {
63
+ position : "custom" ,
54
64
backgroundColor : theme . whiteBackground ,
55
65
titleColor : theme . primaryText ,
56
66
borderColor : theme . stroke ,
@@ -74,18 +84,47 @@ const TimeSeriesChart: React.FC<ITimeSeriesChart> = ({ data }) => {
74
84
datasets : [
75
85
{
76
86
data,
77
- borderColor : theme . primaryBlue ,
87
+ // borderColor: theme.primaryBlue,
78
88
stepped : true ,
79
89
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
+ } ,
80
97
} ,
81
98
] ,
82
99
} ,
83
100
options,
84
101
} }
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
+ ] }
85
125
/>
86
126
}
87
127
</ LineContainer >
88
128
) ;
89
129
} ;
90
-
91
130
export default TimeSeriesChart ;
0 commit comments