graphChart.vue 3.02 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) {
      /*********格式化自定义的 y 轴*****************/
      optionFormat.legend = Object.assign({},{
        show:true,
        orient: "horizontal",
        itemWidth: 12,
        itemHeight: 12,
        itemGap: 10,
        bottom: 0
      });
      optionFormat.tooltip =  optionFormat.tooltip||{
        show:true
      };
      //optionFormat.tooltip.formatter = '{a}, {b},{c},{d},{e}';
      optionFormat.series = _.map(optionFormat.series, item => {
        _.each(item.data.nodes,(node,index)=>{
            let source = _.pluck(_.filter(item.data.links,{sourceId:node.id}),'value');
            let target = _.pluck(_.filter(item.data.links,{targetId:node.id}),'value');
            node.source = _.reduce(source, function(memo, num){ return memo + num; }, 0);
            node.target = _.reduce(target, function(memo, num){ return memo + num; }, 0);
            node.value = node.source + node.target;
        });
        let maxNVal = _.max(item.data.nodes,(node)=>node.value).value;
        _.each(item.data.nodes,(node,index)=>{
            let min = 6,max=30;
            let size = Math.round(max*node.value/maxNVal);
            let symbolSize = size<min?min:size>max?max:size;
            node.symbolSize = symbolSize;
        })
        let maxVal = _.max(item.data.links,(link)=>link.value).value;
        _.each(item.data.links,(link,index)=>{
            let max = 15;
            let width =  Math.round(max*link.value/maxVal);
            link.lineStyle={
               width:width||1
            }
        });
        return {
          name: item.title,
          type: 'graph',
          layout: 'circular',
          force:{
            repulsion:100,
            gravity:0.1,
            edgeLength: [10, 50],
            layoutAnimation:false
          },
          circular: {
            rotateLabel: true
          },
          data: item.data.nodes,
          links: item.data.links,
          categories: item.data.categories,
          roam: true,
          label: {
            show:true,
            position: 'right',
            formatter: '{b}',
          },
          lineStyle: {
            width:1,
            color: 'source',
            curveness: 0.3
          }
        }
      });
      // 覆盖格式化后的`option`数据 按需修改
      let coverTemp = {
        grid: {
          left: 0,
          right: 0,
          top: 0,
          bottom: 20,
        }
      };
      const _that = this;
      let option = JSON.parse(
        JSON.stringify({ ...optionFormat, ...coverTemp })
      );
      return option;
    },
  },
  created() { },
};
</script>
<style scoped>
</style>