lineChart.vue 5.41 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 {
        normalXAxisLabel,
        grid,
        _color,
        hasYaxisName,
        yAxisLineHide,
        legendBottom,
        seriesText,
        yAxisName,
        isCylindrical,
        tooltipTitle,
        usePercent = false,
        legendShow = false,
        legendSelected={},
        legendObj,
        barWidth
      } = optionFormat.otherConf;
      let currentClickIndex = null
      optionFormat.legend.bottom = legendBottom || 0;
      optionFormat.legend.selected = legendSelected;
      //optionFormat.legend.itemGap = 65;
      optionFormat.legend.show = legendShow;
      optionFormat.legend = legendObj||optionFormat.legend;
      /*if(optionFormat.series.length==1)*/optionFormat.color = _color;
      let areaColor = _color.map(item => {
        return {
          type: "linear",
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [{
              offset: 0,
              color: item // 0% 处的颜色
            },
            {
              offset: 1,
              color: "#fff" // 100% 处的颜色
            }
          ]
        };
      });
      optionFormat.grid = grid || { top: yAxisName?40:20, right: hasYaxisName?40:0, bottom: legendShow?60:20, left: 60 };
      optionFormat.tooltip.trigger = "axis";
      optionFormat.tooltip.axisPointer = {
        type: "line",
        lineStyle: {
          color: "#444"
        }
      };
      let yAxis = {
        type: "value",
        name:yAxisName||'',
        // nameRotate: 1,
        splitLine: {
          lineStyle: {
            color: "#f5f5f5"
          }
        },
        axisLine: {
          show:!yAxisLineHide,
          lineStyle: {
            color: "#888"
          }
        },
        axisTick: {
          show: false
        },
        axisLabel: {
          fontSize: 12,
          fontFamily: this.fontFamily,
          color: "#666"
        }
      };
      if(hasYaxisName){
          yAxis.name = yAxisName[0];
          let yAxis2 = Object.assign({},yAxis,{name:yAxisName[1],position:'right'})
          optionFormat.yAxis = [yAxis,yAxis2];
      }else{
          optionFormat.yAxis = yAxis;
      }
      optionFormat.xAxis.boundaryGap = true;
      optionFormat.series.forEach((item,index) => {
        if (item.type === "line") {
          if(hasYaxisName)item.yAxisIndex = 0;
          item.symbol = 'circle';
          item.smooth = true;
          item.symbolSize = 4;
          item.emphasis = {
            scale:true
          }
          item.areaStyle = {
            normal: {
              color: areaColor[index]
            }
          };
        } else if (item.type === "bar") {
          if(hasYaxisName)item.yAxisIndex = 1;
          /*item.itemStyle = {
            normal: {
              color: (params) => {
                let key = params.dataIndex
                return key === currentClickIndex ? '#0069FF' : '#3BB8FF'
              }
            }
          }*/
          if(barWidth&& optionFormat.series.length==1)item.barWidth = barWidth;
         // if (isCylindrical) {
          //  item.barWidth = 14;
            //item.barGap = "100%";
         // }
        }
      });
      let option = Object.assign({}, optionFormat);
      let that = this;
      option.series.forEach(item => {
        if (item.type === "line" && item.name === "growthRate") {
          item.name = this.$t("echarts.growthRate");
        } else if (item.type === "bar") {
          item.name =
            item.name === "avgWeekDay"
              ? this.$t("echarts.workdayAverageInnum")
              : item.name === "avgWeekend"
              ? this.$t("echarts.weekendAverageInnum")
              : item.name;
        }
      });
      if (isCylindrical) {
        option.legend.data = option.series.map(item => item.name);
      }
      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 || "--"}${yAxisName||painter.$t('format.perTime')}</p>
            `;
          } else {
            // ${this.$t('format.TRAFFIC')}
            htmls += `
              <p style="font-size:13px;padding:4px 15px;color:#444444; text-align: left;">
                ${item.marker} ${item.seriesName}: ${that.toThousands(item.data|| "--")}${this.$t("format.perTime")}
              </p>
            `;
          }
        });
        htmls += "</div>";
        return htmls;
      };
      return option;
    }
  },
  created() {

  }
}

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

</style>