| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- line.vue<template>
- <div>
- <!-- <div class="chart-title">{{title}}</div> -->
- <div id="echarts"></div>
- </div>
- </template>
- <script>
- import * as echarts from 'echarts';
- export default {
- name: 'Echarts',
- props: {
- 'width': {
- type: Number,
- default: 800
- },
- 'height': {
- type: Number,
- default: 400
- },
- 'title': {
- type: String,
- default: '表格标题'
- },
- 'source': {
- type: Object,
- default: function () {
- return {
- rows: [],
- data: []
- }
- }
- }
- },
- data() {
- return {}
- },
- watch: {
- 'source': {
- deep: true,
- handler: function () {
- this.$nextTick(() => {
- this.initEcharts()
- })
- }
- }
- },
- mounted() {
- this.initEcharts()
- },
- computed: {
- 'seriesParse': function () {
- if (this.source.data.length == 0) {
- return []
- }
- let result2 = this.source.data.map((item) => {
- return {
- name: item.title,
- data: [...item.data],
- type: 'line',
- }
- })
- result2.shift()
- return result2
- },
- 'xAxisParse': function () {
- return this.source.rows
- },
- 'legendSelectArr': function () {
- let selectedArr = this.seriesParse.map((item) => {
- return item.name
- })
- return selectedArr
- }
- },
- methods: {
- initEcharts() {
- if (!this.myChart) {
- this.myChart = echarts.init(document.getElementById('echarts'));
- this.myChart.off("legendselectchanged");
- this.myChart.on('legendselectchanged', (params) => {
- // console.log(params)
- this.selectedItem(params.name)
- })
- }
- this.myChart.resize({
- height: this.height
- });
- let selected = {}
- this.legendSelectArr.forEach((item, index) => {
- if (index == 0) {
- selected[item] = true
- } else {
- selected[item] = false
- }
- })
- this.myChart.setOption({
- legend: {
- orient: 'vertical',
- y: 'top',
- x: 'right',
- selected: selected,
- selector: true
- },
- title: {
- text: this.title,
- show: !!this.title,
- textAlign: 'auto'
- },
- tooltip: {
- trigger: 'axis'
- },
- xAxis: {
- data: [...this.xAxisParse]
- },
- yAxis: {},
- series: [...this.seriesParse]
- }, true);
- },
- selectedItem(name) {
- let selected = {}
- this.legendSelectArr.forEach((item, index) => {
- if (item == name) {
- selected[item] = true
- } else {
- selected[item] = false
- }
- })
- this.myChart.setOption({
- legend: {
- orient: 'vertical',
- y: 'top',
- x: 'right',
- selected: selected,
- selector: true
- },
- title: {
- text: this.title,
- show: !!this.title,
- textAlign: 'auto'
- },
- tooltip: {
- trigger: 'axis'
- },
- xAxis: {
- data: [...this.xAxisParse]
- },
- yAxis: {},
- series: [...this.seriesParse]
- }, true);
- }
- }
- }
- </script>
- <style>
- #echarts {
- width: 100%;
- height: 400px;
- margin-left: auto;
- margin-right: auto;
- /* float: left; */
- }
- .chart-title {
- text-align: center;
- }
- </style>
|