diff --git a/src/BaseCharts/Scatter.js b/src/BaseCharts/Scatter.js new file mode 100644 index 0000000..ef6d0d3 --- /dev/null +++ b/src/BaseCharts/Scatter.js @@ -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() + } +}) diff --git a/src/examples/App.vue b/src/examples/App.vue index 61f5cee..cfb9016 100644 --- a/src/examples/App.vue +++ b/src/examples/App.vue @@ -18,6 +18,8 @@

Bubblechart

+

Scatter Chart

+ @@ -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 { diff --git a/src/examples/ScatterExample.js b/src/examples/ScatterExample.js new file mode 100644 index 0000000..a6ea91f --- /dev/null +++ b/src/examples/ScatterExample.js @@ -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}) + } +})