-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(connection): replace chart.js to echart
- Loading branch information
Showing
9 changed files
with
316 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<template> | ||
<div :id="id" :style="{ width, height }"></div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Component, Vue, Prop, Watch } from 'vue-property-decorator' | ||
import { Getter } from 'vuex-class' | ||
import * as echarts from 'echarts/core' | ||
import { TitleComponent, ToolboxComponent, TooltipComponent, GridComponent, LegendComponent } from 'echarts/components' | ||
import { LineChart } from 'echarts/charts' | ||
import { UniversalTransition } from 'echarts/features' | ||
import { CanvasRenderer } from 'echarts/renderers' | ||
echarts.use([ | ||
TitleComponent, | ||
ToolboxComponent, | ||
TooltipComponent, | ||
GridComponent, | ||
LegendComponent, | ||
LineChart, | ||
CanvasRenderer, | ||
UniversalTransition, | ||
]) | ||
@Component | ||
export default class AreaLine extends Vue { | ||
@Prop({ required: true }) public id!: string | ||
@Prop({ default: '450px' }) public height!: string | ||
@Prop({ default: '100%' }) public width!: string | ||
@Prop({ default: 'transparent' }) public backgroundColor!: string | ||
@Prop({ default: '' }) public chartTitle!: string | ||
@Prop({ | ||
default: () => ({ | ||
xData: [], | ||
seriesData: [], | ||
}), | ||
}) | ||
public chartData!: AreaLineSeriesData | ||
@Getter('currentTheme') private theme!: Theme | ||
@Watch('chartData', { deep: true }) | ||
onChartDataChanged(val: any) { | ||
this.updateChart() | ||
} | ||
private areaLineChart: echarts.ECharts | null = null | ||
private generateChartOption() { | ||
return { | ||
backgroundColor: this.backgroundColor, | ||
color: this.chartData.seriesData.map((item) => item.areaStyle.colorFrom), | ||
title: { | ||
text: this.chartTitle, | ||
}, | ||
tooltip: { | ||
trigger: 'axis', | ||
axisPointer: { | ||
type: 'cross', | ||
label: { | ||
backgroundColor: '#6a7985', | ||
}, | ||
}, | ||
}, | ||
legend: { | ||
data: this.chartData.seriesData.map((item) => item.name), | ||
}, | ||
toolbox: { | ||
feature: { | ||
saveAsImage: {}, | ||
}, | ||
}, | ||
grid: { | ||
left: '3%', | ||
right: '4%', | ||
bottom: '3%', | ||
containLabel: true, | ||
}, | ||
xAxis: [ | ||
{ | ||
type: 'category', | ||
boundaryGap: false, | ||
data: this.chartData.xData, | ||
}, | ||
], | ||
yAxis: [ | ||
{ | ||
type: 'value', | ||
}, | ||
], | ||
series: this.chartData.seriesData.map((item) => { | ||
return { | ||
name: item.name, | ||
type: 'line', | ||
stack: 'Total', | ||
smooth: true, | ||
lineStyle: { | ||
width: 0, | ||
}, | ||
showSymbol: false, | ||
areaStyle: { | ||
opacity: 0.8, | ||
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ | ||
{ | ||
offset: 0, | ||
color: item.areaStyle.colorFrom, | ||
}, | ||
{ | ||
offset: 1, | ||
color: item.areaStyle.colorTo, | ||
}, | ||
]), | ||
}, | ||
emphasis: { | ||
focus: 'series', | ||
}, | ||
data: item.data, | ||
} | ||
}), | ||
} | ||
} | ||
initChart() { | ||
const chartDom = document.getElementById(this.id) as HTMLElement | ||
this.areaLineChart = echarts.init(chartDom, this.theme !== 'light' ? 'dark' : 'light') | ||
this.updateChart() | ||
} | ||
updateChart() { | ||
if (this.areaLineChart) { | ||
const option = this.generateChartOption() | ||
this.areaLineChart.setOption(option) | ||
} | ||
} | ||
mounted() { | ||
this.initChart() | ||
window.addEventListener('resize', () => { | ||
if (this.areaLineChart) { | ||
this.areaLineChart.resize() | ||
} | ||
}) | ||
} | ||
beforeDestroy() { | ||
if (this.areaLineChart) { | ||
this.areaLineChart.dispose() | ||
} | ||
window.removeEventListener('resize', () => { | ||
if (this.areaLineChart) { | ||
this.areaLineChart.resize() | ||
} | ||
}) | ||
} | ||
} | ||
</script> |
Oops, something went wrong.