<template> <div :id="id" :style="`width:${curWidth};height:${curHeight}`" > </div> </template> <script> /** * @author 王晓颖 * @date 2020/08/21 * @Description 简单的散点图 * @props 数据格式:{ * id: 'traffic_govern_line_id', width: 0, height: 0, seriesData: [ [10.0, 8.04], [8.0, 6.95], [13.0, 7.58], [9.0, 8.81], [11.0, 8.33], [14.0, 9.96], [6.0, 7.24], [4.0, 4.26], [12.0, 10.84], [7.0, 4.82], [5.0, 5.68] ] } * */ import { countSize } from '@/utils/utils' export default { name: 'SimpleScatterChart', props: { id: { // id type: String, default: 'simpleBarChart' }, width: { // 宽 type: Number | String, default: '100%' }, height: { // 高 type: Number | String, default: '100%' }, unit: { // 单位 type: String, default: '' }, colors: { // 颜色 type: Array, default: () => { return ['#01b4ff', '#0337ff'] } }, axisLineColor: { // 轴线颜色 type: String, default: '#16277e' }, fontColor: { // 轴线上文字颜色 type: String, default: '#ffffff' }, labelColor: { // 图形上文本标签颜色 type: String, default: '#47c8b2' }, smooth: { type: Boolean, default: true }, // 平滑 seriesData: { // 数据 type: Array, default: () => { return [] } }, xAxisData: { // X轴数据 type: Array, default: () => { return [] } } }, data () { return { curWidth: this.width, curHeight: this.height, option: { grid: { left: 10, right: 30, bottom: 5, top: 20, containLabel: true }, legend: { orient: 'horizontal', data: [], align: 'left', right: '5%', itemWidth: 8, itemHeight: 8, icon: 'rect', textStyle: { width: '300', color: '#fff', fontSize: 10 } }, tooltip: { trigger: 'value', textStyle: { fontSize: '13' }, axisPointer: { type: 'cross', label: { fontSize: '13' } } }, symbolSize: 7, // 标记大小 itemStyle: { shadowBlur: 10, shadowColor: 'rgba(120, 36, 50, 0.5)', shadowOffsetY: 5, color: new this.$echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(251, 118, 123)' }, { offset: 1, color: 'rgb(204, 46, 72)' }]) // 颜色 }, xAxis: [ { type: 'value', boundaryGap: false, data: [], axisLine: { lineStyle: { color: this.axisLineColor // 轴线的颜色 } }, splitLine: { show: false, lineStyle: { color: ['#101641'], type: 'dashed' } }, axisLabel: { color: this.fontColor, // X轴名称颜色 fontSize: 8 } } ], yAxis: [ { name: this.unit, type: 'value', nameTextStyle: { color: '#fff', fontSize: 9, verticalAlign: 'middle' }, splitLine: { show: false, lineStyle: { color: ['#101641'], type: 'dashed' } }, axisLine: { lineStyle: { color: this.axisLineColor } }, axisLabel: { color: this.fontColor, fontSize: 8 } } ], series: [ { type: 'scatter', data: [] } ] } } }, watch: { width (newVal, oldVal) { this.curWidth = newVal this.refreshEchart() }, height (newVal, oldVal) { this.curHeight = newVal this.refreshEchart() }, xAxisData (newVal, oldVal) { this.option.xAxis[0].data = newVal this.refreshEchart() }, legend (newVal, oldVal) { this.option.legend.data = newVal this.refreshEchart() }, unit (newVal, oldVal) { this.option.yAxis[0].name = newVal this.refreshEchart() }, seriesData: { handler (newVal, oldValue) { let newSeries = [] for (let item of newVal) { let series = { name: item.name, type: 'line', symbol: 'none', // 去掉折线节点 smooth: this.smooth, // 曲线变平滑 itemStyle: { color: `rgb(${item.color})` }, lineStyle: { width: countSize(0.03) }, data: item.data } newSeries.push(series) } this.$set(this.option, 'series', newSeries) this.refreshEchart() }, deep: true } }, mounted () { // this.unit && (this.option.yAxis[0].name = this.unit) if (this.seriesData.length) { this.option.legend.data = this.legend this.option.series[0].data = this.seriesData this.initEchart() } }, methods: { refreshEchart () { if (this.curWidth && this.curHeight && this.xAxisData.length) { this.initEchart() } }, initEchart () { const _div = document.getElementById(this.id) setTimeout(() => { let myChart = this.$echarts.init(_div) myChart.setOption(this.option, true) window.onresize = myChart.resize }, 500) } } } </script> <style scoped> </style>