barChart.vue 6.43 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,
        hasYaxisName,
        yAxisLineHide,
        legendBottom,
        seriesText,
        yAxisName,
        tooltipTitle,
        labelShow = false,
        labelPosition = '',
        hideLoading = false,
        reverseAxis = false,
        showBackground = false,
        usePercent = false,
        showCount = true,
        rotateVal = 0
      } = optionFormat.otherConf;
      let currentClickIndex = null
      optionFormat.legend.bottom = legendBottom || 0;
      optionFormat.legend.itemGap = 65;
      optionFormat.legend.show = false;
      optionFormat.color = _color;//this.getRandomColors(optionFormat.series.length);
      optionFormat.hideLoading = hideLoading;
      optionFormat.grid = grid || { top: 40, right: hasYaxisName?40:20, bottom: 20, left: 40 };
      optionFormat.tooltip.trigger = "axis";
      optionFormat.tooltip.axisPointer = {
        type: "line",
        lineStyle: {
          color: "#444"
        }
      };
      let yAxis = {
        type: reverseAxis?"category":"value",
        // nameRotate: 1,
        splitLine: {
          lineStyle: {
            color: "#f5f5f5"
          }
        },
        axisLine: {
          show:!yAxisLineHide,
          lineStyle: {
            color: "#888"
          }
        },
        axisTick: {
          show: false
        },
        axisLabel: {
          fontSize: 12,
          fontFamily: this.fontFamily,
          color: "#666"
        }
      };
      optionFormat.xAxis.type = reverseAxis?'value':'category';
      optionFormat.xAxis.axisLabel.interval = 0;
      optionFormat.xAxis.axisLabel.rotate = rotateVal;
      if(hasYaxisName){
          yAxis.name = yAxisName[0];
          let yAxis2 = Object.assign({},yAxis,{name:yAxisName[1],position:'right'})
          optionFormat.yAxis = [yAxis,yAxis2];
      }else{
          //optionFormat.yAxis = yAxis;
      }
      if(reverseAxis){
          optionFormat.grid.left = localStorage.getItem('lang')=='en_US'?120:80;
          //optionFormat.tooltip.show = false;
          optionFormat.tooltip.axisPointer.type = 'none';
          optionFormat.tooltip.axisPointer.show = false;
          optionFormat.xAxis.show = false;
          optionFormat.yAxis.axisLine = {
            show:false
          }
          optionFormat.yAxis.axisTick = {
            show:false
          }
      }else{
          optionFormat.yAxis = yAxis;
      }
      let totalNum = 0;
      optionFormat.series.forEach((item,index) => {
        if(usePercent&&item.name!="background"){
            item.data.forEach(dataItem => {
               totalNum += (dataItem instanceof Object?dataItem.value:dataItem);
            });
        }
        if(reverseAxis){
            item.showBackground = showBackground;
            item.label = {
               show:index>0?false:true,
               fontSize:14,
               //fontWeight:'bold',
               position:labelPosition||'insideRight',
            }
        }else if(labelShow){
            item.label = {
               show:true,
               fontSize:14,
               color:'#000',
               position:'top'
            }
        }
        item.label.formatter = function(params){
          if(usePercent){
            let  ratio = (params.value / totalNum * 100).toFixed(2) + '%';
            if(!showCount)return ratio;
            return ratio+(reverseAxis?'/':'\n')+params.value+painter.$t('format.perTime');
          }
          return params.value +painter.$t('format.perTime');
        }
        if (item.type === "line") {
          /*item.lineStyle = {
            normal: {
              color: '#FFC62E'
            }
          }
          item.itemStyle = {
            normal: {
              color: '#FFC62E'
            }
          }*/
          if(hasYaxisName)item.yAxisIndex = 0;
          item.symbolSize = 6;
        } else if (item.type === "bar") {
          if(hasYaxisName)item.yAxisIndex = 1;
          /*item.itemStyle = {
            normal: {
              color: (params) => {
                let key = params.dataIndex
                return key === currentClickIndex ? '#0069FF' : '#3BB8FF'
              }
            }
          }*/
        }
      });
      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}${tooltipTitle || ''}</div><div>`;
        params.forEach(item => {
          // if (item.seriesName === '去年环比' || item.seriesName === 'Last year ring ratio') {
          if (item.componentSubType === "line") {
            htmls += `
              <p style="font-size:13px;padding:4px 15px;color:#444444; text-align: left;">${
                item.marker
              } ${item.seriesName}: ${item.data || "--"}${
              typeof item.data === "string" && item.data.includes("%")
                ? ""
                : "%"
            }</p>
            `;
          } else {
            if(item.seriesName!='background'){
               let value = item.data instanceof Object?item.data.value:item.data;
               htmls += `<p style="font-size:13px;padding:4px 15px;color:#444444; text-align: left;">${painter.$t('echarts.traffic')}: <span style="color:#5093fa">${value || "--"}</span>${this.$t("format.perTime")}</p>`;
               if(usePercent){
                 let  ratio = (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;
      };
      this.chart.off('click')
      this.chart.on('click', (params) => {
         this.$emit('emitChart', params)
      });
      return option;
    }
  },
  created() {

  }
}

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

</style>