Skip to content

Commit 50e5644

Browse files
committed
feat(charts): Export generateChart to create custom charts
1 parent bfda218 commit 50e5644

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

src/BaseCharts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Chart from 'chart.js'
22

3-
function generateChart (chartId, chartType) {
3+
export function generateChart (chartId, chartType) {
44
return {
55
render: function (createElement) {
66
return createElement(

src/examples/App.vue

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,61 @@
22
<div class="container">
33
<div class="Chart">
44
<h1 style="text-align:center;">Barchart</h1>
5-
<bar-example></bar-example>
5+
<bar-example/>
66
</div>
77

88
<div class="Chart">
99
<h1 style="text-align:center;">Horizontal Barchart</h1>
10-
<horizontal-bar-example></horizontal-bar-example>
10+
<horizontal-bar-example/>
1111
</div>
1212

1313
<div class="Chart">
1414
<h1 style="text-align:center;">Barchart with reactive mixing for live data</h1>
15-
<reactive-example></reactive-example>
15+
<reactive-example/>
1616
</div>
1717

1818
<div class="Chart">
1919
<h1 style="text-align:center;">Barchart with reactive mixing for live data as props</h1>
20-
<reactive-prop-example :chart-data="dataPoints"></reactive-prop-example>
20+
<reactive-prop-example :chart-data="dataPoints"/>
2121
</div>
2222

2323
<div class="Chart">
2424
<h1 style="text-align:center;">Linechart</h1>
25-
<line-example></line-example>
25+
<line-example/>
2626
</div>
2727

2828
<div class="Chart">
2929
<h1 style="text-align:center;">Doughnutchart</h1>
30-
<doughnut-example></doughnut-example>
30+
<doughnut-example/>
3131
</div>
3232

3333
<div class="Chart">
3434
<h1 style="text-align:center;">Piechart</h1>
35-
<pie-example></pie-example>
35+
<pie-example/>
3636
</div>
3737

3838
<div class="Chart">
3939
<h1 style="text-align:center;">Radarchart</h1>
40-
<radar-example></radar-example>
40+
<radar-example/>
4141
</div>
4242

4343
<div class="Chart">
4444
<h1 style="text-align:center;">Polararea</h1>
45-
<polar-area-example></polar-area-example>
45+
<polar-area-example/>
4646
</div>
4747

4848
<div class="Chart">
4949
<h1 style="text-align:center;">Bubblechart</h1>
50-
<bubble-example></bubble-example>
50+
<bubble-example />
5151
</div>
5252

5353
<div class="Chart">
5454
<h1 style="text-align:center;">Scatter Chart</h1>
55-
<scatter-example></scatter-example>
55+
<scatter-example />
56+
</div>
57+
<div class="Chart">
58+
<h1 style="text-align:center;">Custom Line Chart</h1>
59+
<custom-line />
5660
</div>
5761
</div>
5862
</template>
@@ -69,12 +73,14 @@
6973
import ReactivePropExample from './ReactivePropExample'
7074
import ScatterExample from './ScatterExample'
7175
import HorizontalBarExample from './HorizontalBarExample'
76+
import CustomLine from './CustomExample'
7277
7378
export default {
7479
components: {
7580
BarExample,
7681
LineExample,
7782
DoughnutExample,
83+
CustomLine,
7884
PieExample,
7985
RadarExample,
8086
PolarAreaExample,

src/examples/CustomExample.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Chart from 'chart.js'
2+
import { generateChart } from '../BaseCharts'
3+
4+
Chart.defaults.LineWithLine = Chart.defaults.line
5+
Chart.controllers.LineWithLine = Chart.controllers.line.extend({
6+
draw: function (ease) {
7+
Chart.controllers.line.prototype.draw.call(this, ease)
8+
9+
if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
10+
let activePoint = this.chart.tooltip._active[0]
11+
let ctx = this.chart.ctx
12+
let x = activePoint.tooltipPosition().x
13+
let topY = this.chart.scales['y-axis-0'].top
14+
let bottomY = this.chart.scales['y-axis-0'].bottom
15+
16+
// draw line
17+
ctx.save()
18+
ctx.beginPath()
19+
ctx.moveTo(x, topY)
20+
ctx.lineTo(x, bottomY)
21+
ctx.lineWidth = 2
22+
ctx.strokeStyle = '#07C'
23+
ctx.stroke()
24+
ctx.restore()
25+
}
26+
}
27+
})
28+
29+
const LineWithLine = generateChart('line-with-chart', 'LineWithLine')
30+
31+
export default {
32+
extends: LineWithLine,
33+
mounted () {
34+
this.renderChart({
35+
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
36+
datasets: [
37+
{
38+
label: 'Data One',
39+
backgroundColor: '#f87979',
40+
data: [40, 39, 10, 40, 39, 80, 40]
41+
}
42+
]
43+
}, {
44+
responsive: true,
45+
maintainAspectRatio: false,
46+
tooltips: {
47+
intersect: false
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)