• Echarts-实现3D柱状图显示,并单个柱子变色


    效果如下:

    在这里插入图片描述

    DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <title>模拟3D柱状图+渐变色柱子title>
    
        <script
          type="text/javascript"
          src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"
        >script>
        <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">script>
      head>
      <body>
        <div id="bar" style="width: 1000px; height: 500px">div>
        <script type="text/javascript">
          let myBarChart = echarts.init(document.getElementById('bar'))
          // 得到的数据
          let seriesData1 = [
            {
              name: '国君',
              value: 20,
            },
            {
              name: '中信',
              value: 100,
            },
            {
              name: '中金',
              value: 50,
            },
            {
              name: '华泰',
              value: 44,
            },
            {
              name: '海通',
              value: 12,
            },
          ]
          // 柱子降序排列
          seriesData1.sort((a, b) => b.value - a.value)
          console.log(seriesData1)
    
          // x轴证券公司名称
          let xData = []
          seriesData1.forEach((item) => {
            xData.push(item.name)
          })
          // 数据
          let seriesData = []
          seriesData1.forEach((item) => {
            seriesData.push(item.value)
          })
    
          // 颜色组
          let linearArr = [
            '#4D89FF',
            '#84BEFF',
            '#3C6CCC',
            '#FFA95D',
            '#FF7F0E',
            '#CC650B',
            '#E46F07',
          ]
    
          // 绘制左侧面
          const offsetX = 10,
            sliderWidth = 7,
            offsetTick = 10
          const CubeLeft = echarts.graphic.extendShape({
            shape: {
              x: 0,
              y: 0,
            },
            buildPath: function (ctx, shape) {
              // shape是从custom传入的
              const api = shape.api
              const xAxisPoint = api.coord([shape.xValue, 0])
              const c0 = [shape.x - offsetTick, shape.y]
              const c1 = [shape.x - offsetTick + offsetX, shape.y]
              const c2 = [xAxisPoint[0] - offsetTick + offsetX, xAxisPoint[1]]
              const c3 = [xAxisPoint[0] - offsetTick, xAxisPoint[1]]
              ctx
                .moveTo(c0[0], c0[1])
                .lineTo(c1[0], c1[1])
                .lineTo(c2[0], c2[1])
                .lineTo(c3[0], c3[1])
                .closePath()
            },
          })
          // 绘制右侧面
          const CubeRight = echarts.graphic.extendShape({
            shape: {
              x: 0,
              y: 0,
            },
            buildPath: function (ctx, shape) {
              const api = shape.api
              const xAxisPoint = api.coord([shape.xValue, 0])
              const c1 = [shape.x - offsetTick + offsetX, shape.y]
              const c2 = [
                shape.x - offsetTick + offsetX + sliderWidth,
                shape.y - sliderWidth,
              ]
              const c3 = [
                xAxisPoint[0] - offsetTick + offsetX + sliderWidth,
                xAxisPoint[1] - sliderWidth + 4,
              ]
              const c4 = [shape.x - offsetTick + offsetX, xAxisPoint[1]]
              ctx
                .moveTo(c1[0], c1[1])
                .lineTo(c2[0], c2[1])
                .lineTo(c3[0], c3[1])
                .lineTo(c4[0], c4[1])
                .closePath()
            },
          })
          // 绘制顶面
          const CubeTop = echarts.graphic.extendShape({
            shape: {
              x: 0,
              y: 0,
            },
            buildPath: function (ctx, shape) {
              const c1 = [shape.x - offsetTick, shape.y]
              const c2 = [shape.x - offsetTick + offsetX, shape.y] // 右点
              const c3 = [
                shape.x - offsetTick + offsetX + sliderWidth,
                shape.y - sliderWidth,
              ]
              const c4 = [shape.x - offsetTick + sliderWidth, shape.y - sliderWidth]
              ctx
                .moveTo(c1[0], c1[1])
                .lineTo(c2[0], c2[1])
                .lineTo(c3[0], c3[1])
                .lineTo(c4[0], c4[1])
                .lineTo(c1[0], c1[1])
                .closePath()
            },
          })
    
          // 注册三个面图形
          echarts.graphic.registerShape('CubeLeft', CubeLeft)
          echarts.graphic.registerShape('CubeRight', CubeRight)
          echarts.graphic.registerShape('CubeTop', CubeTop)
    
          option = {
            xAxis: {
              axisTick: {
                // 不显示坐标刻度
                show: false,
              },
              axisLine: {
                show: false, // 不显示坐标轴线
              },
              data: xData,
            },
            yAxis: {
              type: 'value',
    
              // 不显示网格
              splitLine: {
                show: false,
              },
            },
            series: [
              {
                type: 'custom',
                data: seriesData,
                itemStyle: {
                  shadowColor: '#000',
                  shadowBlur: 100,
                },
                renderItem: function (params, api) {
                  let location = api.coord([api.value(0), api.value(1)])
                  console.log(seriesData[params.dataIndex])
                  return {
                    type: 'group',
    
                    children: [
                      // 柱子前面
                      {
                        type: 'CubeLeft',
                        // shape 属性描述了这个矩形的像素位置和大小
                        shape: {
                          api,
                          xValue: api.value(0), //表示取出当前 dataItem 中第一个维度的数值
                          yValue: api.value(0),
                          x: location[0],
                          y: location[1],
                        },
                        style: {
                          //柱子阴影
                          shadowColor:
                            xData[params.dataIndex] === '海通'
                              ? '#FF7F0E'
                              : '#8DD0FA',
                          shadowOffsetX: 0,
                          shadowOffsetY: 0,
                          shadowBlur: 7,
                          // 颜色
                          fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            {
                              offset: 0,
    
                              color:
                                xData[params.dataIndex] === '海通'
                                  ? linearArr[4]
                                  : linearArr[0],
                            },
                            {
                              offset: 1,
                              color:
                                xData[params.dataIndex] === '海通'
                                  ? linearArr[3]
                                  : linearArr[1],
                            },
                          ]),
                        },
                      },
                      // 柱子侧面
                      {
                        type: 'CubeRight',
                        shape: {
                          api,
                          xValue: api.value(0),
                          yValue: api.value(1),
                          x: location[0],
                          y: location[1],
                        },
                        style: {
                          //柱子阴影
                          shadowColor:
                            xData[params.dataIndex] === '海通'
                              ? '#FF7F0E'
                              : '#8DD0FA',
                          shadowOffsetX: 0,
                          shadowOffsetY: 0,
                          shadowBlur: 7,
                          //颜色
                          fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            {
                              offset: 0,
                              color:
                                xData[params.dataIndex] === '海通'
                                  ? linearArr[6]
                                  : linearArr[2],
                            },
                            {
                              offset: 1,
                              color:
                                xData[params.dataIndex] === '海通'
                                  ? linearArr[5]
                                  : linearArr[2],
                            },
                          ]), //平面颜色
                        },
                      },
                      // 柱子顶部
                      {
                        type: 'CubeTop',
                        shape: {
                          api,
                          xValue: api.value(0),
                          yValue: api.value(1),
                          x: location[0],
                          y: location[1],
                        },
                        style: {
                          //柱子阴影
                          shadowColor:
                            xData[params.dataIndex] === '海通'
                              ? '#FF7F0E'
                              : '#8DD0FA',
                          shadowOffsetX: 0,
                          shadowOffsetY: 0,
                          shadowBlur: 7,
                          fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            {
                              offset: 0,
                              color:
                                xData[params.dataIndex] === '海通'
                                  ? linearArr[4]
                                  : linearArr[0],
                            },
                            {
                              offset: 1,
                              color:
                                xData[params.dataIndex] === '海通'
                                  ? linearArr[3]
                                  : linearArr[1],
                            },
                          ]), //平面颜色
                        },
                      },
                    ],
                  }
                },
              },
            ],
          }
          myBarChart.setOption(option)
        script>
      body>
    html>
    
    
    • 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
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
  • 相关阅读:
    为什么HashMap不使用B、B+树
    Linux运维
    leetcode_2300 咒语和药水的成功对数
    C++学习笔记(四): 类、头文件、对象
    lamada List对象属性值转数组array
    Bug解决:出现C++:internal compiler error: killed(program cc1plus)
    vue脚手架安装
    虹科分享 | 网络仿真器 | 预测云中对象存储系统的实际性能
    Platform—企业通讯录
    21.4 Python 使用GeoIP2地图定位
  • 原文地址:https://blog.csdn.net/qq_41675812/article/details/128020860