barsChart.vue 2.99 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 = true,
        labelPosition = "",
        hideLoading = false,
        reverseAxis = false,
        showBackground = false,
        isSex = false,
        total = [],
      } = optionFormat.otherConf;
      optionFormat.color = _color; //this.getRandomColors(optionFormat.series.length);
      optionFormat.hideLoading = hideLoading;
      optionFormat.animation = true;
      optionFormat.grid = grid || [
        { top: 10, right: hasYaxisName ? 40 : 20, bottom: 20, left: 40 },
      ];
      let that = this;
      optionFormat.series.forEach((item, index) => {
        let count = 0;
        if(!isSex){
          count = item.data.reduce((count,it)=>count+it,0);
        }
        item.showBackground = showBackground;
        item.label = {
          show: labelShow,
          fontSize: 12,
          color:'#6a696c',
          //fontWeight:'bold',
          position: labelPosition || "insideRight",
          formatter(param){
             return parseInt(param.value*100/count)+'%/'+param.value+that.$t('format.perNum');
          }
        };
        if (isSex) {
          item.itemStyle = {
            color: _color[index],
          };
          item.label.show = true;
          item.label.formatter = (param)=>{
             return total[param.dataIndex][index];
          }
          if (index == 0) {
            item.itemStyle.barBorderRadius = [20, 0, 0, 20];
            item.label.position = 'left';
          } else if (index == 1) {
            item.itemStyle.barBorderRadius = [0, 20, 20, 0];
            item.label.position = 'right';
          }
        }
      });
      if (isSex) {
        optionFormat.yAxis.forEach((item, index) => {
          item.axisLabel = {
            color: _color[index],
            formatter(label, i) {
              let per = optionFormat.series[index].data[i] + "%";
              return (
                "{a|" + per + "}" + (index == 0 ? "{b|" + label + "}" : "")
              );
            },
            rich: {
              a: {
                width: 40,
              },
              b: {
                width: this.$el.offsetWidth * 0.3 - 100,
                color: "#000",
                align: "center",
              },
            },
          };
        });
      }
      let option = Object.assign({}, optionFormat);
      return option;
    },
  },
  created() {},
};
</script>
<style scoped>
.chart {
  position: relative;
  height: 500px;
}
</style>