Skip to content

Commit 71b50df

Browse files
authored
Merge pull request #303 from west-soft-development/refactor-identical-code
Refactor identical code
2 parents b80b07e + 4297872 commit 71b50df

24 files changed

+236
-1377
lines changed

dist/vue-chartjs.js

Lines changed: 109 additions & 637 deletions
Large diffs are not rendered by default.

dist/vue-chartjs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BaseCharts.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import Chart from 'chart.js'
2+
3+
function generateChart (chartId, chartType) {
4+
return {
5+
render: function (createElement) {
6+
return createElement(
7+
'div', {
8+
style: this.styles,
9+
class: this.cssClasses
10+
},
11+
[
12+
createElement(
13+
'canvas', {
14+
attrs: {
15+
id: this.chartId,
16+
width: this.width,
17+
height: this.height
18+
},
19+
ref: 'canvas'
20+
}
21+
)
22+
]
23+
)
24+
},
25+
26+
props: {
27+
chartId: {
28+
default: chartId,
29+
type: String
30+
},
31+
width: {
32+
default: 400,
33+
type: Number
34+
},
35+
height: {
36+
default: 400,
37+
type: Number
38+
},
39+
cssClasses: {
40+
type: String,
41+
default: ''
42+
},
43+
styles: {
44+
type: Object
45+
},
46+
plugins: {
47+
type: Array,
48+
default () {
49+
return []
50+
}
51+
}
52+
},
53+
54+
data () {
55+
return {
56+
_chart: null,
57+
_plugins: this.plugins
58+
}
59+
},
60+
61+
methods: {
62+
addPlugin (plugin) {
63+
this.$data._plugins.push(plugin)
64+
},
65+
renderChart (data, options) {
66+
this.$data._chart = new Chart(
67+
this.$refs.canvas.getContext('2d'), {
68+
type: chartType,
69+
data: data,
70+
options: options,
71+
plugins: this.$data._plugins
72+
}
73+
)
74+
}
75+
},
76+
beforeDestroy () {
77+
if (this.$data._chart) {
78+
this.$data._chart.destroy()
79+
}
80+
}
81+
}
82+
}
83+
84+
export const Bar = generateChart('bar-chart', 'bar')
85+
export const HorizontalBar = generateChart('horizontalbar-chart', 'horizontalBar')
86+
export const Doughnut = generateChart('doughnut-chart', 'doughnut')
87+
export const Line = generateChart('line-chart', 'line')
88+
export const Pie = generateChart('pie-chart', 'pie')
89+
export const PolarArea = generateChart('polar-chart', 'polarArea')
90+
export const Radar = generateChart('radar-chart', 'radar')
91+
export const Bubble = generateChart('bubble-chart', 'bubble')
92+
export const Scatter = generateChart('scatter-chart', 'scatter')
93+
94+
export default {
95+
Bar,
96+
HorizontalBar,
97+
Doughnut,
98+
Line,
99+
Pie,
100+
PolarArea,
101+
Radar,
102+
Bubble,
103+
Scatter
104+
}

src/BaseCharts/Bar.js

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/BaseCharts/Bubble.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/BaseCharts/Doughnut.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)