• echarts 折线图 柱图 封装


    components/echarts/index.vue 组件内容

    <!-- 重叠柱状图 -->
    <template>
      <div ref="chart" :style="styleCfg" />
    </template>
    <script>
    export default {
    name: 'EchartIndex',
    props: {
      option: {
        default: () => {},
        type: Object
      },
      width: {
        default: 'auto',
        type: String | Number
      },
      height: {
        default: '300',
        type: String
      },
      autoHeight: {
        default: false,
        type: Boolean
      },
      minWidth:{
        default:'',
        type:String
      }
    },
    data() {
      return {
        myChart: ''
      };
    },
    computed: {
      styleCfg() {
        let width = isNaN(Number(this.width)) ? this.width : `${this.width}px`;
        let height = isNaN(Number(this.height))
          ? this.height
          : `${this.height}px`;
        let returnStyle=`width: ${width}; height: ${height}; `
        if(this.minWidth){
          returnStyle+='min-width:'+this.minWidth;
        }
        return returnStyle;
      }
    },
    watch: {
      height(newval) {
        this.$refs.chart.style.height = newval;
        this.resize();
      },
      option: {
        handler(a, b) {
          this.$forceUpdate();
          this.myChart && this.myChart.setOption(a,true);
        },
        immediate: true,
        deep: true
      }
    },
    created() {},
    mounted() {
      this.draw();
      window.addEventListener('resize', this.cuntHeight);
    },
    methods: {
      cuntHeight() {
        if (!this.autoHeight) return;
        this.$nextTick(() => {
          if (!this.$refs.chart || !this.$refs.chart.parentNode) return;
          let num = this.$refs.chart.parentNode.clientHeight - 10;
          // console.log(num, 1234);
          this.$refs.chart.style.height = num + 'px';
          this.resize();
        });
      },
      draw() {
        this.myChart = this.$echarts.init(this.$refs.chart);
        this.myChart.getZr().on('click', param => { // 解决折线图没有拐点也有点击事件
          const point = [param.offsetX, param.offsetY]
          if (this.myChart.containPixel('grid', point)) { // 判断点击事件是否在图形区域内
            let xIndex = this.myChart.convertFromPixel({seriesIndex: 0}, point)[0] // 拿到x轴坐标
            let option = this.myChart.getOption()
            this.$emit('zrOnClick', option, xIndex);
          }
        })
        this.myChart.on('click', param => {
          this.echartOnClick(param);
        });
        this.myChart.on('mouseover', param => {
          this.echartOnMouseover(param);
        });
        this.myChart.on('mouseout', param => {
          this.echartOnMouseout(param);
        });
        this.myChart.on('legendselectchanged',param =>{
          this.echartOnLegendselectchanged(param);
        })
        this.myChart.setOption(this.option);
        this.cuntHeight()
      },
      echartOnClick(param) {
        this.$emit('echartOnClick', param);
      },
      echartOnLegendselectchanged(param) {
        this.$emit('echartOnLegendse', param);
      },
      echartOnMouseover(param) {
        this.$emit('echartOnMouseover', param);
      },
      echartOnMouseout(param) {
        this.$emit('echartOnMouseout', param);
      },
      setOption(param) {
        this.myChart && this.myChart.setOption(param);
      },
      init() {
        this.myChart && this.myChart.setOption(this.option);
      },
      resize() {
        this.myChart = this.$echarts.init(this.$refs.chart);
        this.myChart.resize();
      },
    }
    };
    </script>
    
    <style scoped>
    .no-data{
      position: absolute;
      display: flex;
      width: 100%;
      height: 100%;
      align-items: center;
      text-align: center;
      
    }
    p{
        flex: 1;
      }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143

    引用

    <div style="padding:10px 20px">
     <chartline ref="myCharts" :option="_options2" width="100%" :minWidth="'360px'" :height="'200px'" :autoHeight="false"></chartline>
    </div>
    
    
    computed:{
          _options() {
            let custSerises=[]
            this.busbar_data.length&&this.busbar_data.forEach((item,index)=>{
              custSerises.push({
                name: item.name,
                type: 'line',
                //stack:"test",
                emphasis:{
                  lineStyle:{
                    // width:3,
                    // color:"red"
                  },
                  itemStyle:{
                    borderWidth:4
                  }
                },
                symbolSize:1,
                lineStyle:{
                  width:2
                },
                data: item.data_value,
                smooth:true
              })
            })
    
            const op = {
    
              tooltip: {
                trigger: 'axis',
                // axisPointer: {
                //   type: 'shadow'
                // },
                //解决折线图tooltip展示不全问题
                position:(pos,params,el,elRect,size)=>{
                  var obj = {top:10};
                  obj[['left,right'][+(pos[0] < size.viewSize[0] / 2)]] = 30
                  return obj
    
                },
                backgroundColor: "#fff", // 提示框背景颜色
                textStyle: {
                  fontSize: 14,
                  color: "#505D6F",
                },
                // formatter:(params)=>{
    
                //   let str =""
                //   params.length&¶ms.forEach(e=>{
                //     str+=`
    // ${e.marker} ${e.seriesName} : ${e.value} //
    `
    // }) // // console.log(params) // return `
    //
    ${params[0].name}
    // ${str} //
    `
    // } }, // areaStyle:{ // normal:{ // color: new echarts.graphic.LinearGradient( // 0,0,0,1,[ // {offset:0.98,color:'rgba(25,163,223,0)'}, // {offset:0,color:'rgba(25,163,223,0.3)'}, // // {offset:0.98,color:'rgba(5,213,255,1)'}, // // {offset:0,color:'rgba(5,213,255,0)'}, // ],false // ), // } // }, legend: { data: ["A相电压","B相电压","C相电压"], show:true, left: '50px', top:"0px", // width:"100%", orient:"horizontal", itemWidth:14, itemHeight:4, icon :"rect", padding:10 }, grid: [ { right: '10px', bottom:"20px", left:"60px", top:"10px" } ], toolbox: { show: true, feature: { saveAsImage: {} } }, color:["#3C8782","#3A82D3","#68CD99","#E6A55D","#43D8FD","#1562B8"], xAxis: { type: 'category', boundaryGap: true, // axisLabel :{ // interval:0, // rotate:'70',//旋转度数 // }, axisLabel: { showMaxLabel: true, color:"rgb(119,119,119)", }, data: this.busbar_data.time || [] , axisPointer: { snap: true }, axisTick: { show: false }, axisLine: { show: false, lineStyle: { color: "rgb(60,74,85)", type:"dashed" } }, splitLine: { show: false, lineStyle: { color: "rgb(60,74,85)", type:"dashed" } } }, yAxis: { name:"V", nameLocation:"end", max:(value)=>{ return parseInt(value.max+(value.max*0.05)) }, min: function (value) { return parseInt(value.min - (value.min*0.05)); }, type: 'value', minInterval:1, axisLabel: { showMaxLabel: true, color:"rgb(31,31,31)", // axisLabel:{ formatter:"{value} V" // } }, nameTextStyle:{ color:"rgb(31,31,31)" }, axisPointer: { snap: true }, axisTick: { show: false }, axisLine: { show: false, lineStyle: { color: "rgb(60,74,85)" } }, splitLine: { show: false, lineStyle: { color: "rgb(60,74,85)", type:"dashed" } }, }, series: [ { name: "A相电压", type: 'line', //stack:"test", emphasis:{ lineStyle:{ // width:3, // color:"red" }, itemStyle:{ borderWidth:4 } }, symbolSize:1, lineStyle:{ width:2 }, data: this.busbar_data.ua, smooth:true }, { name: "B相电压", type: 'line', //stack:"test", emphasis:{ lineStyle:{ // width:3, // color:"red" }, itemStyle:{ borderWidth:4 } }, symbolSize:1, lineStyle:{ width:2 }, data: this.busbar_data.ub, smooth:true }, { name: "C相电压", type: 'line', //stack:"test", emphasis:{ lineStyle:{ // width:3, // color:"red" }, itemStyle:{ borderWidth:4 } }, symbolSize:1, lineStyle:{ width:2 }, data: this.busbar_data.uc, smooth:true }, ] }; return _.merge(op, this.options); },
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249

    }

  • 相关阅读:
    ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
    第20章 Netty
    月影下的时光机:Python中的日期、时间、农历、节气和时区探秘
    面试完我感觉五味杂陈,八年 Android 开发这种水平要20k?
    QGIS制作精美地图
    Lazada本土店怎么运营?Lazada本土店发货流程——站斧浏览器
    【排序24:俄罗斯套娃信封问题】(动态规划 / 二维最长公共子序列 / 基于二分查找的动态规划)
    前端研习录(09)——CSS浮动与清除浮动
    独角数卡安装前后常见问题汇总
    2000-2022年上市公司CEO 高管及董事会环保背景数据(5W+ )(原始数据+处理代码Stata do文档)
  • 原文地址:https://blog.csdn.net/qq_42197013/article/details/134500196