Chart

[ 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>