123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <template>
- <div class="grid-chart" :id="id">
- </div>
- </template>
- <script>
- import {copyObj, uuid} from '@/utils';
- import * as echarts from 'echarts';
- const basicSerie = {
- type: 'line',
- };
- const opts = {
- legend: {
- top: '5%',
- },
- grid: {
- top: 10,
- bottom: 20,
- },
- tooltip: {
- show: true,
- trigger: 'axis',
- },
- xAxis: {
- type: 'category',
- boundaryGap: false,
- },
- yAxis: {
- type: 'value',
- name: 'kW·h',
- minInterval: 1,
- nameLocation: 'middle',
- nameTextStyle: {
- color: '#000',
- fontSize: '14px',
- lineHeight: 56,
- },
- },
- series: [],
- };
- export default {
- props: {
- title: {
- default: '标题',
- type: String,
- },
- optCfg: {
- type: Object,
- default: () => {
- return {};
- },
- },
- },
- watch: {
- optCfg: {
- handler(val) {
- this.initChart();
- },
- deep: true,
- },
- },
- data() {
- return {
- id: uuid(),
- refCharts: null,
- };
- },
- mounted() {
- this.$nextTick(this.initChart);
- },
- created() {
- },
- methods: {
- initChart() {
- this.refCharts && this.refCharts.clear();
- const chartDom = document.getElementById(this.id);
- this.refCharts = echarts.init(chartDom);
- const option = copyObj(opts);
- const legend = [];
- this.optCfg.series.forEach((item, index) => {
- const serie = copyObj(basicSerie);
- Object.assign(serie, item);
- const {name} = item;
- option.series.push(serie);
- if (name) {
- legend.push(name);
- }
- });
- option.legend = {
- data: legend,
- };
- option.xAxis = this.optCfg.xAxis;
- option.yAxis.name = this.optCfg.unit || "kW·h"
- option && this.refCharts && this.refCharts.setOption(option);
- this.refCharts.resize();
- },
- },
- };
- </script>
- <style src="./index.scss" lang="scss"/>
|