Skip to content

Commit 9a58e06

Browse files
authored
Merge pull request #334 from apertureless/feature/custom_charts
Custom Charts
2 parents bfda218 + 29d511c commit 9a58e06

File tree

5 files changed

+101
-56
lines changed

5 files changed

+101
-56
lines changed

README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ Vue.component('line-chart', {
7171
})
7272
```
7373

74-
75-
### Browserify / Webpack 1
76-
77-
If you're using Gulp, Browserify or Webpack 1 the entry is `vue-chartjs.js` which is __transpiled__ and __bundled__ UMD Module.
78-
79-
However, Chart.js is a `peerDependencies` so you have to install it separately. In most projects This way, you can have different versions of Chart.js then in this package.
80-
81-
### Webpack 2
82-
If you're using Webpack 2 it will automatically use the `jsnext:main` / `module` entry point. Which is `es/index.js`
83-
It is a __transpiled__ es version of the source. And is not __bundled__ to a module. This way your tree shaking will work. Like in the bundled version, `Chart.js` is a `peerDependencies` and need to be installed.
84-
85-
8674
## How to use
8775

8876
You need to import the component and then either use `extends` or `mixins` and add it.

docs/README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ export default {
9999
After you add your component you can use it:
100100

101101
```html
102-
<line-chart :data="{your data object}" :options="{your options}"></line-chart>
102+
<line-chart :data="chartData" :options="chartOptions"></line-chart>
103103
```
104104

105105
If you want to overwrite width and height:
106106

107107
```html
108108
<line-chart
109-
:data="{your data object}"
109+
:data="chartData"
110110
:options="{responsive: false, maintainAspectRatio: false}"
111111
:width="400"
112112
:height="200"
@@ -279,6 +279,36 @@ mounted () {
279279
})
280280
}
281281
```
282+
## Custom / New Charts
283+
284+
Sometimes you need to extend the default Chart.js charts. There are a lot of examples how to extend and modify the default charts. Or you want to create a own chart type.
285+
286+
In `vue-chartjs` you can do this pretty much the same way.
287+
288+
```js
289+
// 1. Import Chart.js so you can use the global Chart object
290+
import Chart from 'chart.js'
291+
// 2. Import the `generateChart()` method to create the vue component.
292+
import { generateChart } from 'vue-chartjs'
293+
294+
// 3. Extend on of the default charts
295+
// http://www.chartjs.org/docs/latest/developers/charts.html
296+
Chart.defaults.LineWithLine = Chart.defaults.line;
297+
Chart.controllers.LineWithLine = Chart.controllers.line.extend({ /* custom magic here */})
298+
299+
// 4. Generate the vue-chartjs component
300+
// First argument is the chart-id, second the chart type.
301+
const CustomLine = generateChart('custom-line', 'LineWithLine')
302+
303+
// 5. Extend the CustomLine Component just like you do with the default vue-chartjs charts.
304+
305+
export default {
306+
extends: CustomLine,
307+
mounted () {
308+
// ....
309+
}
310+
}
311+
```
282312

283313
## Available Charts
284314

@@ -318,36 +348,6 @@ This chart has a different data structure then the others. Right now the reactiv
318348

319349
![Scatter](assets/scatter.png)
320350

321-
322-
## Explanation of Different Builds
323-
There are three different entry points. It depends on your build setup. The dependencies are bundled or required as a peerDependency.
324-
325-
- Browser
326-
- Browserify / Webpack 1
327-
- Webpack 2
328-
329-
330-
| Build | Chart.js | Vue.js |
331-
|---|---|---|
332-
| vue-chartjs.full.js | Bundled | Bundled |
333-
| vue-chartjs.full.min.js | Bundled | Bundled |
334-
| vue-chartjs.js | peerDependency | peerDependency |
335-
| vue-chartjs.min.js | peerDependency | peerDependency |
336-
| es/index* | peerDependency | peerDependency |
337-
338-
### Browser
339-
You can use `vue-chartjs` directly in the browser without any build setup, like in this [codepen](https://codepen.io/apertureless/pen/vxWbqB?editors=1010). In this case, please use the `vue-chartjs.full.min.js` which is the minified version. It has Vue.js and Chart.js bundled into it and is bundled to a UMD module, so you only need that one file.
340-
341-
342-
### Browserify / Webpack 1
343-
344-
If you're using Gulp, Browserify or Webpack 1 the entry is `vue-chartjs.js` which is __transpiled__ and __bundled__ UMD Module.
345-
346-
However Vue.js and Chart.js are `peerDependencies` so you have to install them separately. In most projects you will have Vue.js already installed anyways. This way, you can have different versions of Vue.js and Chart.js then in this package.
347-
348-
### Webpack 2
349-
If you're using Webpack 2 it will automatically use the `jsnext:main` / `module` entry point, which is `es/index.js`. It is a __transpiled__ es version of the source and is not __bundled__ to a module. This way your tree shaking will work. Like in the bundled version, Vue.js and Chart.js are `peerDependencies` and need to be installed.
350-
351351
## Resources
352352

353353
You can find here some resources like tutorials on how to use `vue-chartjs`

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)