pictorialBar.vue 3.53 KB
<template>
  <div>
     <basic ref="basic" :chartId="chartId" :chartData="chartData"></basic>
  </div>
</template>
<script>
import mixin from './mixin';
export default {
  mixins: [mixin],
  data() {
    return {

    }
  },
  methods: {
    /**
     * 预处理option参数
     * */
    preChartData(cdata) {
       return cdata;
    },
    /**
     * 处理option参数
     * */
    dealChartData(optionFormat) {
      // 特殊配置项
      let {
        grid,
        _color,
        usePercent = true
      } = optionFormat.otherConf;
      optionFormat.color = _color||[new this.$echarts.graphic.LinearGradient(0,0,0,1,[
          {
            offset:0,
            color:'#f4cedd'
          },
          {
            offset:1,
            color:'#f16595'
          }
        ])];//this.getRandomColors(optionFormat.series.length);
      optionFormat.grid = grid || { top: 20, right: 0, bottom: 20, left: 40 };
      optionFormat.legend = {show:false};
      optionFormat.tooltip.trigger = "axis";
      optionFormat.tooltip.axisPointer = {
        type: "line",
        lineStyle: {
          color: "#444"
        }
      };
      let yAxis = {
        type: "value",
        splitLine: { 
          show: true, 
          lineStyle:{
            color:'#eee'
          }
        },
        axisTick: { show: false },
        axisLine: { show: false },
        axisLabel: { show: false }
      };
      optionFormat.xAxis = Object.assign({},optionFormat.xAxis,{
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: {
            color: '#000',
             interval:0
          }
      }); 
      optionFormat.yAxis = yAxis;
      let totalNum = 0; 
      optionFormat.series.forEach((item,index) => {
          if(usePercent){
              item.data.forEach(dataItem => {
                 totalNum += (dataItem instanceof Object?parseInt(dataItem.value):dataItem);
              });
          }
          item.type = 'pictorialBar';
          item.symbol = 'path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z';
          //item.barCategoryGap = '-130%';
          item.label = {
            position: 'top',
            distance:20,
            verticalAlign:'top',
            show:true,
            formatter:function(params){
                if(usePercent){
                  let  ratio = (params.value / totalNum * 100).toFixed(2) + '%';
                  return ratio+'/'+params.value;
                }
                return params.value;
            },
            color:'#f16595',
            fontSize:14
          }
      });
      let option = Object.assign({}, optionFormat);
      option.tooltip.formatter = params => {
        let htmls = "";
        htmls += `<div style="font-size:14px;height:31px;color:#333;border-radius:4px;line-height:31px;padding-left:15px;padding-right:15px;text-align: left;">${params[0].name}</div><div>`;
        params.forEach((item,idx) => {
          htmls += `<p style="font-size:13px;padding:4px 15px;color:#444444; text-align: left;">${painter.$t('echarts.traffic')}:<span style="color:#5093fa">${item.value|| "--"}</span></p>`;
          let  ratio = (item.value / totalNum * 100).toFixed(2) + '%';
          htmls += `<p style="font-size:13px;padding:4px 15px;color:#444444; text-align: left;">${painter.$t('echarts.proportion')}:<span style="color:#5093fa">${ratio|| "--"}</span></p>`;
        });
        htmls += "</div>";

        return htmls;
      };
      return option;
    }
  },
  created() {

  }
}

</script>
<style scoped>
.chart {
  position: relative;
  height: 500px;
}

</style>