DOCUMENT:
https://www.chartjs.org/docs/latest/charts/line.html
DEMO:
http://demo.vue-chartjs.org/
API:
https://vue-chartjs.org/zh-cn/
demo code:
//chart.vue
<script>
import { Line, mixins } from "vue-chartjs";
const { reactiveProp } = mixins;
export default {
extends: Line,
mixins: [reactiveProp],
props: ["options"],
mounted() {
this.renderChart(this.chartData, this.options);
},
watch: {
chartData: function(val, oldVal) {
console.log("watcher invoked");
this.$data._chart.destroy();
this.renderChart(this.chartData, this.options);
}
}
};
</script>
//useChart.vue
<template>
<line-chart class="w-100 plineChart" :chart-data="chartdata" :options="options"></line-chart>
</template>
<script>
import lineChart from "@/components/chart";
export default {
data(){
labels:[2, 4, 6, 8, 10],
aLineArr:[0, 1, 2, 3, 4, 5],
bLineArr:[10, 20, 30, 40, 50],
chartData:null,
options:{
legend: {
position: "bottom"
},
scales: {
yAxes: [
{
id: "A",
type: "linear",
position: "left"
}
]
},
elements: {
line: {
fill: false
}
},
responsive: true,
maintainAspectRatio: false
}
},
components: {
lineChart: lineChart
},
methods:{
getChartData(){
this.chartdata = {
labels: labels,
datasets: [
{
label: "A Line",
lineTension: 0, //偏移度 0是直线
backgroundColor: "#41c7db",
borderColor: "#41c7db",
data: aLineArr,
yAxisID: "A"
},
{
label: "B Line",
lineTension: 0, //偏移度 0是直线
backgroundColor: "green",
borderColor: "green",
data: bLineArr,
yAxisID: "A"
}
]
}
}
}
}
</script>