Add scatter chart

This commit is contained in:
Jakub Juszczak
2017-07-02 16:40:38 +02:00
parent 80799b7bd5
commit d304a2c5ab
3 changed files with 127 additions and 1 deletions

69
src/BaseCharts/Scatter.js Normal file
View File

@@ -0,0 +1,69 @@
import Vue from 'vue'
import Chart from 'chart.js'
import { mergeOptions } from '../helpers/options'
export default Vue.extend({
render: function (createElement) {
return createElement(
'div',
[
createElement(
'canvas', {
attrs: {
id: this.chartId,
width: this.width,
height: this.height
},
ref: 'canvas'
}
)
]
)
},
props: {
chartId: {
default: 'scatter-chart',
type: String
},
width: {
default: 400,
type: Number
},
height: {
default: 400,
type: Number
}
},
data () {
return {
defaultOptions: {
scales: {
xAxes: [{
type: 'linear',
position: 'bottom'
}]
}
}
}
},
methods: {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)
this._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'scatter',
data: data,
options: chartOptions
}
)
this._chart.generateLegend()
}
},
beforeDestroy () {
this._chart.destroy()
}
})

View File

@@ -18,6 +18,8 @@
<polar-area-example></polar-area-example>
<h1 style="text-align:center;margin:40px 0;">Bubblechart</h1>
<bubble-example></bubble-example>
<h1 style="text-align:center;margin:40px 0;">Scatter Chart</h1>
<scatter-example></scatter-example>
</div>
</template>
@@ -31,6 +33,7 @@
import BubbleExample from './BubbleExample'
import ReactiveExample from './ReactiveExample'
import ReactivePropExample from './ReactivePropExample'
import ScatterExample from './ScatterExample'
export default {
components: {
@@ -42,7 +45,8 @@
PolarAreaExample,
BubbleExample,
ReactiveExample,
ReactivePropExample
ReactivePropExample,
ScatterExample
},
data () {
return {

View File

@@ -0,0 +1,53 @@
import Scatter from '../BaseCharts/Scatter'
export default Scatter.extend({
mounted () {
this.renderChart({
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Scatter Dataset 1',
fill: false,
borderColor: '#f87979',
backgroundColor: '#f87979',
data: [{
x: -2,
y: 4
}, {
x: -1,
y: 1
}, {
x: 0,
y: 0
}, {
x: 1,
y: 1
}, {
x: 2,
y: 4
}]
},
{
label: 'Scatter Dataset 2',
fill: false,
borderColor: '#7acbf9',
backgroundColor: '#7acbf9',
data: [{
x: -2,
y: -4
}, {
x: -1,
y: -1
}, {
x: 0,
y: 1
}, {
x: 1,
y: -1
}, {
x: 2,
y: -4
}]
}]
}, {responsive: true, maintainAspectRatio: false})
}
})