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

    }
  },
  methods: {
    /**
     * 预处理option参数
     * */
    preChartData(cdata) {
       return cdata;
    },
    /**
     * 处理option参数
     * */
    dealChartData(optionFormat) {
      // 特殊配置项
      let { kpiName, grid, _color } = optionFormat.otherConf;
      //optionFormat.xAxis.name = this.$t("table.date");
      optionFormat.xAxis.type = 'value';
      optionFormat.xAxis.splitLine = {
         show:false
      }
      optionFormat.yAxis.name = "女\n性\n占\n比";
      optionFormat.yAxis.nameLocation  = 'middle';
      optionFormat.yAxis.nameGap  = 25;
      optionFormat.yAxis.splitNumber = 2;
      optionFormat.yAxis.offset = 0;
      optionFormat.yAxis.nameRotate = 0;
      optionFormat.yAxis.axisLine = {
         show:true,
         lineStyle:{color:'#ccc'}
      }
      optionFormat.yAxis.axisTick = {
          show:false
      }
      optionFormat.yAxis.axisLabel = {
         inside:true,
         show:true,
         showMaxLabel:true
      }
      optionFormat.grid = grid || { top: 40, right: 20, left: 40,bottom:20 };
      optionFormat.legend.top = 26;
      optionFormat.legend.fontSize = 14;
      let tooltipData = [],
        dataNumArr = [],
        maxNum = null,
        minNum = null,
        maxSize = 100,
        minSize = 30,
        symbolsizeArr = [],
        sessionLocalWeather = "",
        legendName = "",
        legendData = [],
        newSeries = [];
        optionFormat.series.forEach((item, index) => {
        tooltipData[legendName] = item.data;
        item.name = legendName;
        item.type = "scatter";
        item.itemStyle = {
          normal: {
           // color: _color[index],
           // borderColor: _color[index],
            borderWidth: 2,
            opacity: 0.6
          },

        };
        symbolsizeArr[index] = [];
        item.data.forEach((dataItem, dataIndex) => {
          if (dataItem) {
            symbolsizeArr[index][dataIndex] = dataItem.value1;
            dataNumArr.push(dataItem.value1);
          } else {
            symbolsizeArr[index][dataIndex] = 0;
          }
        });
      });
      newSeries = Object.assign([], optionFormat.series);
      maxNum = _.max(optionFormat.xAxis.data);
      minNum =  _.min(optionFormat.xAxis.data);
      optionFormat.xAxis.min = minNum;
      optionFormat.xAxis.max = maxNum;
      let option = Object.assign({}, optionFormat); // coverTemp
      
      option.series = newSeries.map((item, index) => {
        item.label =  {
          show: true,
          color:'#444',
          //fontWeight:'bold',
          //position: 'inside',
          formatter(data){
            return data.data[3]
          }
        };
        let arr = _.map(item.data,(it=>it[2]));
        let min  = _.min(arr);
        let max = _.max(arr);
        item.symbolSize = (data, param) => {
          let _size = minSize + (maxSize-minSize)*(data[2]-min)/(max-min);
          return parseInt(_size);  
        };
        let colorArr = _.map(this.getRandomColors(item.data.length),(color,idx)=>{
            return {
              value: item.data[idx][2],
              label: item.data[idx][3],
              color:color
            };
        });
        option.visualMap = {
          show:false,
          type: 'piecewise',
          top: 'middle',
          splitNumber: item.data.length,
          min: min,
          dimension: 2,
          max: max,
          pieces:colorArr
        }
        return item;
      });

      option.legend.data = legendData;
      option.legend.show = false;
      
      option.tooltip.formatter = params => {
        
        let data = params.data;
        let htmls = '';
        htmls+=`<div>
          <p>${data[3]}</p>
          <p>年龄中位数:<span style="color:#5093fa">${data[0]}</span>岁</p>
          <p>女性占比:<span style="color:#5093fa">${(data[1]*100).toFixed(0)}</span>%</p>
          <p>到访人数:<span style="color:#5093fa">${this.toThousands(data[2])}</span>人</p>
        </div>`
        return htmls;
      };
 
      return option;
    }
  },
  created() {

  }
}

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

</style>