Skip to content

Commit 6bbb438

Browse files
authored
Merge pull request #47 from apertureless/feature/documentation
Feature/documentation
2 parents 938b8f3 + 0c96745 commit 6bbb438

25 files changed

+402
-35
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
**vue-chartjs** is a wrapper for [Chart.js](https://github.com/chartjs/Chart.js) in vue. You can easily create reuseable chart components.
1515

16-
## Demo
16+
## Demo & Docs
1717

18-
[Demo](https://apertureless.github.io/vue-chartjs/)
18+
- [Demo](http://demo.vue-chartjs.org/)
19+
- [Docs](http://www.vue-chartjs.org/)
1920

2021
### Compatibility
2122

assets/bar.png

25.2 KB
Loading

assets/bubble.png

1.11 KB
Loading

assets/doughnut.png

13.1 KB
Loading

assets/line.png

49.7 KB
Loading

assets/pie.png

36.3 KB
Loading

assets/polar.png

60.1 KB
Loading

assets/radar.png

32.1 KB
Loading

docs/CNAME

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vue-chartjs.org

docs/README.md

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
---
2+
search: english
3+
---
4+
5+
# vue-chartjs
6+
**vue-chartjs** is a wrapper for [Chart.js](https://github.com/chartjs/Chart.js) in vue. You can easily create reuseable chart components.
7+
8+
## Introduction
9+
`vue-chartjs` let you use chart.js without much hassle inside vue. It's perfect for people who need simple charts up and running as fast as possible.
10+
11+
It abstracts the basic logic but exposes the chart.js object to give you the most possible flexibility.
12+
13+
## Installation
14+
If you are working with Vue.js 2+ simple run:
15+
16+
`yarn add vue-chartjs -S`
17+
18+
If you are using vue 1.x please use the `legancy` tag. However the vue 1 version is not maintained anymore.
19+
20+
`yarn add vue-chartjs@legacy -S`
21+
22+
## Quick Start
23+
24+
You need to import the base chart and extend it. This gives much more flexibility when working with different data.
25+
You can encapsulate your components and use props to pass data or you can directly imput them inside the component. However this way, your component is not reuseable.
26+
27+
You can import the whole package or each module individual.
28+
29+
```javascript
30+
// CommitChart.js
31+
import { Bar } from 'vue-chartjs'
32+
33+
export default Bar.extend({
34+
mounted () {
35+
// Overwriting base render method with actual data.
36+
this.renderChart(data, options)
37+
}
38+
})
39+
```
40+
41+
You can pass the `renderChart()` method, two arguments:
42+
43+
- Data object
44+
- Options object
45+
46+
### Data object
47+
48+
The data object looks like this:
49+
50+
```javascript
51+
{
52+
labels: ['January', 'February'],
53+
datasets: [
54+
{
55+
label: 'GitHub Commits',
56+
backgroundColor: '#f87979',
57+
data: [40, 20]
58+
}
59+
]
60+
}
61+
```
62+
63+
For more information take a look at the [Chart.js](http://www.chartjs.org/docs/#chart-configuration-chart-data) docs.
64+
65+
## Props
66+
67+
There are some basic props defined in the BaseCharts. Because you `extend()` them, they are *invisible*, but you can overwrite them:
68+
69+
| Prop | Description |
70+
|---|---|
71+
| width | chart width |
72+
| height | chart height |
73+
| id | id of the canvas |
74+
75+
76+
## Examples
77+
78+
Here are some exmaples
79+
80+
### Chart with props
81+
82+
You can create the data and options props to pass data to the chart.
83+
84+
```javascript
85+
// LineChart.js
86+
import { Line } from 'vue-chartjs'
87+
88+
export default Line.extend({
89+
props: ['data', 'options'],
90+
mounted () {
91+
this.renderChart(this.data, this.options)
92+
}
93+
})
94+
```
95+
96+
After you add your component you can use it:
97+
98+
```html
99+
<line-chart :data="{your data object}" :options="{your options}"></line-chart>
100+
```
101+
102+
If you want to overwrite the width and height:
103+
104+
```html
105+
<line-chart
106+
:data="{your data object}"
107+
:options="{responsive: false, maintainAspectRatio: false}"
108+
:width="400"
109+
:height="200"
110+
>
111+
</line-chart>
112+
```
113+
114+
<p class="warning">
115+
Please keep in mind, that you have to set `responsive: false` to be able to set a fix `width` and `height.
116+
</p>
117+
118+
### Chart with local data
119+
120+
```javascript
121+
import {Bar} from 'vue-chartjs'
122+
123+
export default Bar.extend({
124+
data () {
125+
return {
126+
datacollection: {
127+
labels: ['January', 'February'],
128+
datasets: [
129+
{
130+
label: 'Data One',
131+
backgroundColor: '#f87979',
132+
data: [40, 20]
133+
}
134+
]
135+
}
136+
}
137+
}
138+
mounted () {
139+
this.renderChart(this.datacollection, {responsive: true, maintainAspectRatio: false})
140+
}
141+
})
142+
```
143+
144+
### Reusebale Components
145+
146+
If you want to keep your chart components reuseable, it's the best to add a wrapper to them. This way the chart component is only responsable for the pure data representation and the wrapper component for the logic behind it. There are many different usecases and it is different if you're running a Single Page Application or integrate it in for example laravel.
147+
148+
## Reactive Data
149+
150+
Chart.js does not provide a live update if you change the datasets. However `vue-chartjs` provides two mixins to achive this.
151+
152+
- `reactiveProp`
153+
- `reactiveData`
154+
155+
Both mixins to actually the same. Most of the time you will use `reactiveProp`. It extends the logic of your chart component and automatically creates a prop names `chartData` and add a `vue watch` on this prop. On data change, it will either call `update()` if only the data inside the datasets has changed or `renderChart()` if new datasets were added.
156+
157+
`reactiveData` simply creates a local chartData variable which is not a prop! And add a watcher.
158+
This is only usefull, if you need single purpose charts and make an API call inside your chart component.
159+
160+
```javascript
161+
data () {
162+
return {
163+
chartData: null
164+
}
165+
}
166+
```
167+
168+
### Example
169+
170+
**LineChart.js**
171+
```javascript
172+
import { Line, mixins } from 'vue-chartjs'
173+
const { reactiveProp } = mixins
174+
175+
export default Line.extend({
176+
mixins: [reactiveProp],
177+
props: ['options'],
178+
mounted () {
179+
// this.chartData is created in the mixin
180+
this.renderChart(this.chartData, this.options)
181+
}
182+
})
183+
```
184+
185+
**RandomChart.vue**
186+
187+
```javascript
188+
<template>
189+
<div class="small">
190+
<line-chart :chart-data="datacollection"></line-chart>
191+
<button @click="fillData()">Randomize</button>
192+
</div>
193+
</template>
194+
195+
<script>
196+
import LineChart from './LineChart.js'
197+
198+
export default {
199+
components: {
200+
LineChart
201+
},
202+
data () {
203+
return {
204+
datacollection: null
205+
}
206+
},
207+
mounted () {
208+
this.fillData()
209+
},
210+
methods: {
211+
fillData () {
212+
this.datacollection = {
213+
labels: [this.getRandomInt(), this.getRandomInt()],
214+
datasets: [
215+
{
216+
label: 'Data One',
217+
backgroundColor: '#f87979',
218+
data: [this.getRandomInt(), this.getRandomInt()]
219+
}, {
220+
label: 'Data One',
221+
backgroundColor: '#f87979',
222+
data: [this.getRandomInt(), this.getRandomInt()]
223+
}
224+
]
225+
}
226+
},
227+
getRandomInt () {
228+
return Math.floor(Math.random() * (50 - 5 + 1)) + 5
229+
}
230+
}
231+
}
232+
</script>
233+
234+
<style>
235+
.small {
236+
max-width: 600px;
237+
margin: 150px auto;
238+
}
239+
</style>
240+
```
241+
242+
## Chart.js object
243+
244+
Sometimes you need more control over chart.js. Thats why you can access the chart.js instance over `this._chart`
245+
246+
## Available Charts
247+
248+
### Bar Chart
249+
<p class="tip">
250+
The bar chart has an **optional** third parameter, which is the type.
251+
The default type is `bar` but you can pass `horizontalBar` if you want horizontal bars.
252+
253+
`renderChart (data, options, type) {}`
254+
255+
</p>
256+
257+
![Bar](assets/bar.png)
258+
### Line Chart
259+
260+
![Line](assets/line.png)
261+
262+
### Doughnut
263+
264+
![Doughnut](assets/doughnut.png)
265+
266+
### Pie
267+
268+
![Pie](assets/pie.png)
269+
270+
### Radar
271+
272+
![Pie](assets/radar.png)
273+
274+
### Polar Area
275+
276+
![Pie](assets/polar.png)
277+
278+
### Bubble
279+
280+
![Bubble](assets/bubble.png)

0 commit comments

Comments
 (0)