[ Chartjs ] vue.js์์ ์ฐจํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ vue-chartjs ์ฌ์ฉํ๊ธฐ
๐ฌ ๊ฐ์
์ฐจํธ๋ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ํ์๋ก ์์ฃผ ์ฌ์ฉํ์๋ค.
echarts , chartjs๋ฅผ ์ฌ์ฉํด ๋ณด์๋๋ฐ, ๊ทธ์ค chartjs ์ฌ์ฉ๋ฒ์ ์ ๋ฆฌํด๋ณด๊ณ ์ ํ๋ค.
๐ ์ ์
vue-chartjs ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ chart.js๋ฅผ vue์์ ์ฌ์ฉํ ์ ์๋๋ก ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
chart.js๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ chart.js, vue-chartjs ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ํ๋ฉฐ,
๊ธฐํ ์ฐจํธ ๊ด๋ จ ํ๋ฌ๊ทธ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐ ์ค์นํ ์ ์๋ค.
๐ vue-chartjs ์ค์น
npm install vue-chartjs chart.js
๐ chartjs ํ๋ฌ๊ทธ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น (ํ์X)
โฉ chartjs-plugin-datalabels ์ค์น
: ์ฐจํธ ๋ด์ ๋ฐ์ดํฐ์ ๊ฐ์ ํ์ํ๊ธฐ ์ํ ํ๋ฌ๊ทธ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
npm install chartjs-plugin-datalabels --save
โฉ chartjs-plugin-datalabels ์ค์น
: ์ฐจํธ ๋ด์ ๊ธฐ์ค ์ ์ด๋ ๊ธฐ์ค ์์ญ์ ํ์ํ๊ธฐ ์ํ ํ๋ฌ๊ทธ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
npm install chartjs-plugin-annotation
โฉ chartjs-plugin-zoom์ค์น
: ์ฐจํธ ๋ด์์ ํ๋, ์ถ์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ํ ํ๋ฌ๊ทธ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
npm install chartjs-plugin-zoom
๐ ์ฐจํธ ์ปดํฌ๋ํธ( .js ) ์์ฑ
: ๋ค๋ฅธ ์ฐจํธ๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ๋๋ Line -> Pie / Bar ๋ฑ์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
// views/Component/LineChart
import { Line, mixins } from 'vue-chartjs';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import zoomPlugin from 'chartjs-plugin-zoom';
import chartjsPluginAnnotation from 'chartjs-plugin-annotation';
const { reactiveProp } = mixins;
export default {
extends: Line,
mixins: [reactiveProp],
props: ['options'],
mounted() {
this.addPlugin(ChartDataLabels,chartjsPluginAnnotation,zoomPlugin); // ํ๋ฌ๊ทธ์ธ ์ถ๊ฐ
this.renderChart(this.chartData, this.options); // ์ฐจํธ refresh ๊ธฐ๋ฅ ์ถ๊ฐ
},
watch: {
options () {
this.renderChart(this.chartData, this.options);
}
}
};
๐ ์ฐจํธ ์ฌ์ฉํ๊ธฐ
์ฐจํธ ์ปดํฌ๋ํธ๋ฅผ importํด์จ ํ ์ฌ์ฉํ๋ค.
<template>
<line-chart
class="graph-containter"
:options="options"
:chartData="chartDataArray">
</line-chart>
</template>
<script>
import LineChart from '@/views/Component/LineChart';
export default {
components: {
LineChart
},
computed: {
...rootComputed,
...serviceComputed,
options() {
let unit = 'mA';
return {
responsive: true,
maintainAspectRatio: false, // chart ๋์ด ์ง์ ์ ์ํด ํ์
plugins: {
datalabels: {
display: true,
anchor: 'start',
align : 'end',
},
zoom: {
pan: {
enabled: true,
mode: 'y',
},
zoom: {
enabled: true,
mode: 'y',
},
}
},
legend: {
display: false,
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
return `${tooltipItem.yLabel} ${unit}` ;
},
}
},
scales: {
yAxes: [
{
ticks: {
beginAtZero: false,
crossAlign : 'center',
minGridDistance: 8,
maxTicksLimit: 10,
max: this.trendData.current !== undefined ? Math.max(...this.trendData.current)+1 : 300,
min: this.trendData.current !== undefined ? Math.min(...this.trendData.current)-1 : 0,
callback: function(val, index){
return `${val.toFixed(1)} ${unit}`
}
},
scaleLabel: {
display: true,
labelString: `${this.getMessage_('EWMS000035')}(mA)` //์ ๋ฅ
},
}
],
xAxes: [ {
ticks:{
display:true,
fontSize:12,
fontcolor:'#A5A5A5',
padding:1,
callback: function(val, index) {
return val.slice(-8).substr(0,5)
},
},
gridLines:{
display:true,
fontcolor:'#A5A5A5',
},
scaleLabel: {
display: true,
labelString: this.getMessage_('EWMS000091') // ์๊ฐ
}
}],
}
}
},
chartDataArray() {
let dataList = {
labels: this.trendData.labels,
datasets: [{
data: this.trendData[this.selectedButton],
borderColor: 'rgba(186, 5, 0, 0.34)',
pointerColor:'rgba(199, 0, 25, 1)',
backgroundColor: '#00ff0000'
}]
}
return dataList
}
}
}
</script>