VUE + elementui-table component draws miniature echarts diagram in table-cell

Demand rendering example
Insert picture description here

Actually completed renderings
Insert picture description here** code implementation

  • Note: The table table is a sub-component of the secondary package-
    the id of the element is dynamically set according to scope. $ Index in the table table, which is convenient for the initialization of the echarts of the specified cell;

-Trigger a method in the cell, pass in the current scope.row data or specify other data, and pass in scope. $ Index and a string to identify which data is currently the charts

-Draw echarts in the method **

   		 <el-table-column align="center">
       	  <template slot="header" slot-scope="scope">
           <div v-if="tableTitle == 'sale'">
             <p v-if="dateType != '1'">
               近{{ dateType }}天累计
               <br />
               / 日均销量
             </p>
             <p v-if="dateType == '1'">昨日累计销量</p>
           </div>
           <div v-if="tableTitle == 'fav'">
             <p v-if="dateType != '1'">
               近{{ dateType }}天累计
               <br />
               / 日均收藏量
             </p>
             <p v-if="dateType == '1'">昨日累计收藏</p>
           </div>
         </template>
         <div slot-scope="scope" style="white-space:nowrap;">
           <span class="xiao-red-color">{{ realRowName(scope.row, '0') || '-' }}</span>
           <span v-if="dateType != '1'" class="xiao-red-color">
             /
             {{ isNaN(realRowName(scope.row, '0')) ? '-' : (realRowName(scope.row, '0') / (parseInt(dateType) - calcShelfTime(scope.row.real_created_time, '0') &lt; 0 ? parseInt(dateType): calcShelfTime(scope.row.real_created_time, '0'))).toFixed(0)}}
           </span>
         </div>
       </el-table-column>
       <el-table-column label="每日销量趋势" align="center" v-if="dateType != '1'">
         <template slot-scope="scope">
           {{ drawEcharts(scope.row, scope.$index, 'sale') }}
           <div :id="`tiger-sale-trend-index` + scope.$index" class="tiger-trend-charts"></div>
         </template>
       </el-table-column>

The method of drawing echarts (the data is only an example, and the actual development is based on the scope.row data passed in). Note that the VUE's this. $ NextTick method is used here when initializing the echarts object, in case unrepresented node elements cannot be obtained .

 drawEcharts() {
      //绘制趋势echarts
      // console.log(arguments)
      let option = {
        tooltip: {
          trigger: 'axis'
        },
        // legend: {
        //   data: ['每日30天销量分析']
        // },
        grid: {
          left: '10px',
          right: '30px',
          top: '40px',
          bottom: '10px',
          containLabel: true
        },
        xAxis: {
          show: false,
          type: 'category',
          boundaryGap: false,
          data: ['03-21', '03-22', '03-23', '03-24', '03-25', '03-26', '03-27']
        },
        yAxis: {
          show: false,
          type: 'value'
        },
        series: [
          {
            name: '每日30天销量分析',
            type: 'line',
            data: [120, 500, 101, 86, 173, 230, 6]
          }
        ]
      };
      let chartId = 'tiger-' + arguments[2] + '-trend-index' + arguments[1];
      this.$nextTick(() => {
        let myChart = this.echarts.init(document.getElementById(chartId), 'macarons');
        myChart.setOption(option);
        myChart.resize();
      });
    },

AND Do n’t forget to set the height of echarts , otherwise the graphics will not come out in a lifetime (example, adjusted according to implementation development)

&-frame {
    display: flex;
    flex-flow: column nowrap;
    justify-content: space-between;

    .price-bar {
      color: red !important;
    }
    .tiger-trend-charts {
      height: 60px;
      min-width: 100px;
    }
  }
Published 12 original articles · Like 22 · Visits 3234

Guess you like

Origin blog.csdn.net/bosslay/article/details/105601840