VUE項目中封裝Echart折線圖的方法
本文實例為大家分享了VUE項目中封裝Echart折線圖的具體代碼,供大家參考,具體內容如下
封裝Echart折線圖,可顯示多條折線
1. 首先在項目中全局引入Echarts,main.js中:
import * as echarts from 'echarts' Vue.prototype.$echarts = echarts
2.components中新建組件baseLineChart.vue,以下代碼直接復制:
<template>
? ? <div
? ? ? id="baseLineChart"
? ? ? ref="baseLineChart"
? ? ? :style="{ width: chartWidth, height: chartHeight }"
? ? />
</template>
<script>
export default {
? props: {
? ? chartData: {
? ? ? type: Array,
? ? ? default: () => []
? ? },
? ? timeX: {
? ? ? type: Array,
? ? ? default: () => []
? ? },
? ? chartName: {
? ? ? type: String,
? ? ? default: ''
? ? },
? ? chartWidth: {
? ? ? type: String,
? ? ? default: ''
? ? },
? ? chartHeight: {
? ? ? type: String,
? ? ? default: ''
? ? },
? ? seriesName: {
? ? ? type: Array,
? ? ? default: () => []
? ? },
? ? chartUnit: {
? ? ? type: String,
? ? ? default: ''
? ? }
? },
? data() {
? ? return {
? ? ? baseLineChart: null,
? ? ? newChartData: {}
? ? }
? },
? computed: {
? ? chartOptions() {
? ? ? const options = {
? ? ? ? grid: {
? ? ? ? ? left: '4%',
? ? ? ? ? bottom: '8%',
? ? ? ? ? top: '15%',
? ? ? ? ? right: '0'
? ? ? ? },
? ? ? ? tooltip: {
? ? ? ? ? trigger: 'axis'
? ? ? ? },
? ? ? ? color: ['#1879BD', '#03B8DF', '#7B879A'],
? ? ? ? legend: {
? ? ? ? ? show: true,
? ? ? ? ? right: '0',
? ? ? ? ? top: '-1%',
? ? ? ? ? icon: 'circle'
? ? ? ? },
? ? ? ? xAxis: [
? ? ? ? ? {
? ? ? ? ? ? type: 'category',
? ? ? ? ? ? axisTick: { show: false },
? ? ? ? ? ? data: []
? ? ? ? ? }
? ? ? ? ],
? ? ? ? yAxis: [
? ? ? ? ? {
? ? ? ? ? ? type: 'value',
? ? ? ? ? ? name: '',
? ? ? ? ? ? nameTextStyle: {
? ? ? ? ? ? ? padding: [0, 15, 0, 0]
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ],
? ? ? ? series: []
? ? ? }
? ? ? return options
? ? }
? },
? watch: {
? ? chartData: {
? ? ? handler(newValue, oldValue) {
? ? ? ? this.newChartData = newValue
? ? ? ? this.initData()
? ? ? },
? ? ? deep: true
? ? }
? },
? mounted() {
? ? this.$nextTick(() => {
? ? ? this.initChart()
? ? ? if (this.chartData) {
? ? ? ? this.initData()
? ? ? }
? ? })
? },
? methods: {
? ? initChart() {
? ? ? const _this = this
? ? ? _this.baseLineChart = _this.$echarts.init(this.$refs.baseLineChart)
? ? ? window.addEventListener('resize', function () {
? ? ? ? _this.baseLineChart.resize()
? ? ? })
? ? },
? ? initData() {
? ? ? let sData = []
? ? ? if (this.chartData) {
? ? ? ? sData = this.chartData.map((val, index) => {
? ? ? ? ? return {
? ? ? ? ? ? data: val,
? ? ? ? ? ? name: this.seriesName[index],
? ? ? ? ? ? type: 'line'
? ? ? ? ? }
? ? ? ? })
? ? ? ? this.chartOptions.series = sData
? ? ? }
? ? ? this.chartOptions.xAxis[0].data = this.timeX
? ? ? this.chartOptions.yAxis[0].name = `單位(${this.chartUnit})`
? ? ? this.baseLineChart.setOption(this.chartOptions, true)
? ? }
? }
}
</script>配置項根據自身項目來定制。
3、在組件中引入:
<template>
? <div>
? ? ? <baseLine
? ? ? ? :chart-data="chartData"
? ? ? ? ?:time-x="timeX"
? ? ? ? ?chart-name="OEE"
? ? ? ? ?chart-width="1700px"
? ? ? ? ?chart-height="350px"
? ? ? ? ?:series-name="seriesName"
? ? ? ? ?chart-unit="%"
? ? ? ? ? />
? ? </div>
</template>
import baseLine from '@/components/charts/baseLineChart.vue'
<script>
export default {
?components: {
? ? baseLine
? },
?data() {
? ?return {
? ? ?timeX: [],
? ? ?chartData:[]
? ? ?seriesName: ['白班', '晚班']
? ?}
?},
}
</script>chart-width 圖表寬度
chart-height 圖表高度
chart-unit 圖表數據的顯示單位
timeX為X軸數據,一般為時間數組 [‘1-1’,‘1-2’,‘1-3’ ];
chartData為折線數據,多條數據格式 [ [1,2,3],[4,5,6] ]
seriesName為每條折線對應名稱
同理可封裝其他圖表
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue使用element-ui tabs切換echarts解決寬度100%方式
這篇文章主要介紹了vue使用element-ui tabs切換echarts解決寬度100%方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
前端XSS攻擊場景詳解與Vue.js處理XSS的方法(vue-xss)
這篇文章主要給大家介紹了關于前端XSS攻擊場景與Vue.js使用vue-xss處理XSS的方法,介紹了實際工作中渲染數據時遇到XSS攻擊時的防范措施,以及解決方案,需要的朋友可以參考下2024-02-02
vue2.0基于vue-cli+element-ui制作樹形treeTable
這篇文章主要介紹了vue2.0基于vue-cli+element-ui制作樹形treeTable,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

