• echarts-动态加载桑吉图数据


    先看示意图:
    在这里插入图片描述
    以某个商场业态数据举例,默认展示第一层级,有多层级品类字体设置了特殊的蓝色,点击展示对应的下一层级,这种设计思路也是防止全部展示导致图表重叠,而且全部展示也无法突出数据重点。
    代码是:

      let box = document.querySelector('.box');
      let e = echarts.init(box);
      let colors = ['#FF9656', '#19C09B', '#4387F0', '#9474E2', '#E96E93', '#FFD300'];
      let data1 = {
        firstList: [
          {
            source: 'all',
            target: ['餐饮', '零售', '休闲娱乐', '生活服务', '文化创意'],
            value: [14, 12, 20, 22, 16]
          }
        ],
        otherList: [
          {
            source: '餐饮',
            target: ['餐1', '餐2'],
            value: [15, 20]
          },
          {
            source: '休闲娱乐',
            target: ['乐1', '乐2'],
            value: [18, 21]
          },
          {
            source: '餐1',
            target: ['餐1_1', '餐1_2'],
            value: [12, 20]
          },
          {
            source: '乐1',
            target: ['乐1_1', '乐1_2'],
            value: [30, 15]
          }
        ]
      };
    
      let allName = [];
      let links = [];
      let labelStatus = {};
    
      // 添加第一层数据
      addData(data1.firstList, true);
    
      let option = {
        color: [
          // '#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#d1e5f0', '#92c5de', '#4393c3', '#2166ac', '#053061'
        ],
        tooltip: {
          trigger: 'item',
          // triggerOn: 'mousemove'
        },
        animation: false,
        series: [
          {
            type: 'sankey',
            bottom: 90,
            top: 20,
            right: 20,
            left: 20,
            nodeWidth: 20,
            nodeAlign: 'left',
            draggable: false,
            focusNodeAdjacency: false,
            orient: 'vertical',
            data: allName,
            links: links,
            label: {
              position: 'bottom',
              formatter: function (val) {
                if (val.name === 'all') return '';
                if (labelStatus[val.name]['hasNextStep']) {
                  return [
                    '{value| '+labelStatus[val.name]['value']+'% }',
                    '{hasNextStepName| ' + labelStatus[val.name]['name'] + '}'
                  ].join('\n')
                } else {
                  return [
                    '{value| '+labelStatus[val.name]['value']+'% }',
                    '{notNextStepName| ' + labelStatus[val.name]['name'] + '}'
                  ].join('\n')
                }
              },
              rich: {
                value: {
                  fontSize: 18,
                  height: 25,
                  align: "center"
                },
                hasNextStepName: {
                  fontSize: 14,
                  align: "center",
                  color: '#4A90E2'
                },
                notNextStepName: {
                  fontSize: 14,
                  align: "center",
                  color: '#000'
                }
              }
            }
          }
        ]
      };
    
      // resetCharts();
    
      e.setOption(option);
    
      e.on('click', function (obj) {
    
        if (obj.name === 'all') return;
    
        // 判断是否在图表显示,显示就隐藏
        let hasin = removeList(obj.name, data1.otherList);
        resetCharts();
        if (hasin !== -1) return;
    
        // 判断有没有下一级
        let status = data1.otherList.findIndex((val, index) => {
          return val.source === obj.name;
        });
        if (status !== -1) {
          // 加入下一级
          addData([data1.otherList[status]], false);
    
          // 如果点击第二级 计算排它
          let isSecond = data1.firstList[0].target.findIndex((val, index) => {
            return val === obj.name;
          });
          if (isSecond !== -1) {
            data1.firstList[0].target.forEach((v, i) => {
              if (v === obj.name) return;
              removeList(v, data1.otherList);
            });
          }
    
          box.style.height = '700px';
          resetCharts();
        }
    
      });
    
      function unique(points) {
        let result = [];
        let obj = {};
        for (let i = 0; i < points.length; i++) {
          if (!obj[points[i].name]) {
            result.push(points[i]);
            obj[points[i].name] = true;
          }
        }
        return result;
      }
    
      function removeList(name, otherList) {
        let statusHas = links.findIndex((val, index) => {
          return val.source === name;
        });
    
        if (statusHas !== -1) {
          links = links.filter((val, index) => {
            return val.source !== name;
          });
    
          let indexPos = otherList.findIndex((val, index) => {
            return val.source === name;
          });
    
          allName = allName.filter((val, index) => {
            return otherList[indexPos].target.indexOf(val.name) === -1;
          });
    
          // 判断子集的子集是否在 links中 有删除
          otherList[indexPos].target.forEach((v, i) => {
            removeList(v, otherList);
          });
        }
        return statusHas;
      }
    
      function resetCharts() {
        option.series[0].data = allName;
        option.series[0].links = links;
        e.resize();
        e.setOption(option);
      }
    
      function addData(arr, status) {
        arr.forEach((v, i) => {
          // 首次加载
          if (status) {
            allName.push({
              name: v.source,
              itemStyle: {
                color: '#C1CAE1',
                borderColor: 'transparent'
              }
            });
            labelStatus[v.source] = {
              name: v.source,
              hasNextStep: true,
              value: ''
            };
          }
          v.target.forEach((val, index) => {
    
            // 判断是否有下一级
            let hasEle = data1.otherList.findIndex((value, ind) => {
              return value.source === val
            });
    
            if (hasEle === -1) {
              labelStatus[val] = {
                name: val,
                hasNextStep: false,
                value: v.value[index]
              }
            } else {
              labelStatus[val] = {
                name: val,
                hasNextStep: true,
                value: v.value[index]
              }
            }
    
            allName.push({
              name: val,
              itemStyle: {
                color: colors[index],
                borderColor: 'transparent'
              }
            });
            links.push({
              source: v.source,
              target: val,
              value: v.value[index],
              lineStyle: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'rgba(181, 191, 214, 0.5)' // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: 'rgba(226, 234, 249, 0.3)' // 100% 处的颜色
                    }
                  ],
                  global: false // 缺省为 false
                },
                opacity: 1,
                curveness: 0.5
              }
            });
          })
        });
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261

    全部代码都在这了,直接复制到项目中即可看到效果。

  • 相关阅读:
    Linux 下进程间通讯之内存映射详解
    计算机毕业论文基于Python实现的电脑硬件配置推荐官网
    3-2 中阶API示范
    LeetCode知识点总结 - 343
    存内计算技术在边缘计算、物联网设备中的应用及前景
    论文阅读《Omnidirectional DSO: Direct Sparse Odometry with Fisheye Cameras》
    长沙周边最美露营基地:安化云台山风景区星空露营公园
    享元模式 & 基于享元模式的对象池设计与开发应用(设计模式与开发实践 P12)
    探索亚马逊大语言模型:开启人工智能时代的语言创作新篇章
    devops学习(三) K8环境部署jenkins
  • 原文地址:https://blog.csdn.net/weixin_44384273/article/details/132992272