ํฐ์คํ ๋ฆฌ ๋ทฐ
[ Chartjs ] vue.js์์ ์ฐจํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ vue-chartjs ์ฌ์ฉํ๊ธฐ
devOhzl 2023. 6. 16. 17:16๐ฌ ๊ฐ์
์ฐจํธ๋ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ํ์๋ก ์์ฃผ ์ฌ์ฉํ์๋ค.
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>
'Chart' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Echarts] Pie Chart ์์ ์ด๋ฏธ์ง ๋ฃ๊ธฐ, ํด๋ฆญ์ url ์ด๋ํ๊ธฐ (0) | 2023.06.24 |
---|---|
[Chartjs] ๋ฒ๋ก์ ์ฐจํธ ๊ฐ๊ฒฉ ์กฐ์ ํ๊ธฐ (0) | 2023.06.23 |
[ Chartjs ] vue.js ์ฐจํธ ์์ - Bar&Line Chart, Min&Max (0) | 2023.06.19 |
[ Echarts ] ๊ธฐ๋ณธ Option ์ ๋ฆฌ (0) | 2023.06.15 |
[ Echarts ] vue.js ์์ ์ฐจํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ Echarts ์ฌ์ฉํ๊ธฐ (0) | 2023.06.14 |
- Total
- Today
- Yesterday
- ํ๊ฒฝ์ค์
- vscode
- ์์๋ณต์ฌ
- ๊น์๋ณต์ฌ
- Location
- package
- echarts
- chartjs
- SASS
- Figma ๋ฒํผ
- ๊ฐ์ฒด
- web
- package-lock
- Vscode๋จ์ถํค
- Chart
- piechart
- javascript
- Figma ๊ธฐ์ด
- frontend
- VUE
- Legend
- npm
- x์ถ์คํฌ๋กค
- Figma Style
- npm install
- SCSS
- ํ๋ก ํธ์๋
- BarChart
- ๊ฐ์ฒด๋ณต์ฌ
- figma
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |