This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
forked from Tokyo-Metro-Gov/covid19
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvue-chart.ts
154 lines (140 loc) · 3.56 KB
/
vue-chart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import Vue, { PropType } from 'vue'
import { ChartData, ChartOptions } from 'chart.js'
import { Doughnut, Bar, Line, Pie, mixins } from 'vue-chartjs'
import { Plugin } from '@nuxt/types'
import { useDayjsAdapter } from './chartjs-adapter-dayjs'
type ChartVCData = { chartData: ChartData }
type ChartVCMethod = {
renderChart(chartData: ChartData, options: ChartOptions): void
}
type ChartVCComputed = unknown
type ChartVCProps = { options: Object }
const VueChartPlugin: Plugin = ({ app }) => {
useDayjsAdapter(app.i18n)
createCustomChart()
}
const createCustomChart = () => {
const { reactiveProp } = mixins
Vue.component<ChartVCData, ChartVCMethod, ChartVCComputed, ChartVCProps>(
'doughnut-chart',
{
extends: Doughnut,
mixins: [reactiveProp],
props: {
options: {
type: Object as PropType<ChartOptions>,
default: () => {}
}
},
mounted(): void {
this.renderChart(this.chartData, this.options)
}
}
)
Vue.component<ChartVCData, ChartVCMethod, ChartVCComputed, ChartVCProps>(
'bar',
{
extends: Bar,
mixins: [reactiveProp],
props: {
options: {
type: Object,
default: () => {}
}
},
mounted(): void {
this.renderChart(this.chartData, this.options)
}
}
)
Vue.component<ChartVCData, ChartVCMethod, ChartVCComputed, ChartVCProps>(
'line-chart',
{
extends: Line,
mixins: [reactiveProp],
props: {
options: {
type: Object,
default: () => {}
}
},
mounted(): void {
this.renderChart(this.chartData, this.options)
}
}
)
Vue.component<ChartVCData, ChartVCMethod, ChartVCComputed, ChartVCProps>(
'pie-chart',
{
extends: Pie,
mixins: [reactiveProp],
props: {
options: {
type: Object as PropType<ChartOptions>,
default: () => {}
}
},
mounted(): void {
this.renderChart(this.chartData, this.options)
}
}
)
}
export default VueChartPlugin
export const scrollPlugin: Chart.PluginServiceRegistrationOptions[] = [
{
beforeInit(chartInstance) {
const fn = () => {
if (
chartInstance &&
chartInstance.canvas &&
chartInstance.canvas.parentElement
) {
chartInstance.canvas.parentElement.scrollLeft! = chartInstance.width!
}
}
window.addEventListener('resize', fn)
fn()
}
}
]
const rgba0 = 'rgba(255,255,255,0)'
const rgba1 = 'rgba(255,255,255,1)'
export const yAxesBgPlugin: Chart.PluginServiceRegistrationOptions[] = [
{
beforeDraw(chartInstance) {
const ctx = chartInstance.ctx as CanvasRenderingContext2D
// プロットエリアマスク用
ctx.fillStyle = '#fff'
ctx.fillRect(
0,
0,
chartInstance.chartArea.left,
chartInstance.chartArea.bottom + 1
)
// 横軸マスク用
const gradient = ctx.createLinearGradient(
0,
0,
chartInstance.chartArea.left,
0
)
gradient.addColorStop(0, rgba1)
gradient.addColorStop(1, rgba0)
ctx.fillStyle = gradient
ctx.fillRect(
0,
chartInstance.chartArea.bottom + 1,
chartInstance.chartArea.left,
(chartInstance.height as number) - chartInstance.chartArea.bottom - 1
)
}
}
]
export interface DataSets<T = number> extends ChartData {
data: T[]
}
export interface DisplayData<T = number, U = string> {
labels?: U[]
datasets: DataSets<T>[]
}