当前位置:   article > 正文

Echarts+D3气泡图

Echarts+D3气泡图

Echarts+D3气泡图(相邻效果,气泡之间不叠加)

在这里插入图片描述

<template>
  <div ref="chart" style="width: 500px; height: 500px"></div>
</template>

<script setup>
import * as echarts from 'echarts/core'
import { DatasetComponent, TooltipComponent, VisualMapComponent } from 'echarts/components'
import { CustomChart } from 'echarts/charts'
import { CanvasRenderer } from 'echarts/renderers'
echarts.use([DatasetComponent, TooltipComponent, VisualMapComponent, CustomChart, CanvasRenderer])
import * as d3 from 'd3'
import { ref, onMounted } from 'vue'

const chart = ref(null)

const colorList = [
  '#5470c6',
  '#91cc75',
  '#fac858',
  '#ee6666',
  '#73c0de',
  '#3ba272',
  '#fc8452',
  '#9a60b4',
  '#ea7ccc'
]

let option = {}
let seriesData = [
  {
    depth: 1,
    id: 'city',
    index: 0,
    value: 2
  },
  {
    depth: 1,
    id: 'city.孝感',
    index: 6,
    value: 6
  },
  {
    depth: 1,
    id: 'city.武汉',
    index: 1,
    value: 36
  },
  {
    depth: 1,
    id: 'city.荆州',
    index: 2,
    value: 26
  },
  {
    depth: 1,
    id: 'city.咸宁',
    index: 3,
    value: 16
  },
  {
    depth: 1,
    id: 'city.仙桃',
    index: 4,
    value: 6
  },
  {
    depth: 1,
    id: 'city.潜江',
    index: 5,
    value: 10
  },
  {
    depth: 1,
    id: 'city.十堰',
    index: 6,
    value: 8
  }
]

let displayRoot = stratify1()

function stratify1() {
  return d3
    .stratify()
    .parentId(function (d) {
      return d.id.substring(0, d.id.lastIndexOf('.'))
    })(seriesData)
    .sum(function (d) {
      return d.value || 0
    })
    .sort(function (a, b) {
      return b.value - a.value
    })
}

function overallLayout(params, api) {
  let context = params.context
  d3
    .pack()
    .size([api.getWidth() - 2, api.getHeight() - 2])
    .padding(0)(displayRoot)
  context.nodes = {}

  displayRoot.descendants().forEach(function (node) {
    context.nodes[node.id] = node
  })
}

function renderItem(params, api) {
  let context = params.context
  let idx = params.dataIndex

  // Only do that layout once in each time `setOption` called.
  // 每次调用“setOption”时,只能进行一次布局。
  if (!context.layout) {
    context.layout = true
    overallLayout(params, api)
  }

  let nodePath = api.value('id')
  let nodeName = nodePath
    .slice(nodePath.lastIndexOf('.') + 1)
    .split(/(?=[A-Z][^A-Z])/g)
    .join('\n')
  let node = context.nodes[nodePath]
  if (node.id === 'city') {
    node.r = 0
  }

  if (!node) {
    // Reder nothing.
    return
  }

  let colorTop = colorList[idx % colorList.length]
  let colorBottom = colorList[(idx + 1) % colorList.length]

  // 创建线性渐变对象
  let linearGradient = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    { offset: 0, color: colorTop },
    { offset: 1, color: colorBottom }
  ])

  //   // 创建线性渐变对象
  //   let linearGradient = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  //     {
  //       offset: 0,
  //       color: 'rgb(55, 255, 255)' // 渐变起始颜色
  //     },
  //     {
  //       offset: 0.5,
  //       color: 'rgb(155, 105, 255)' // 渐变起始颜色
  //     },
  //     {
  //       offset: 1,
  //       color: 'rgb(255, 0, 255)' // 渐变结束颜色
  //     }
  //   ])

  let z2 = api.value('depth') * 2
  return {
    type: 'circle',
    shape: {
      cx: node.x,
      cy: node.y,
      r: node.r
    },
    // transition: ['shape'],
    z2: z2,
    textContent: {
      type: 'text',
      style: {
        // transition: isLeaf ? 'fontSize' : null,
        text: nodeName,
        fill: '#fff',
        fontFamily: 'Arial',
        width: node.r * 1.3,
        overflow: 'truncate',
        fontSize: node.r / 3
      },
      emphasis: {
        style: {
          overflow: null,
          fontSize: Math.max(node.r / 3, 12)
        }
      }
    },
    textConfig: {
      position: 'inside'
    },
    style: {
      fill: linearGradient
    },
    emphasis: {
      style: {
        fontFamily: 'Arial',
        fontSize: 12,
        shadowBlur: 20,
        shadowOffsetX: 3,
        shadowOffsetY: 5,
        shadowColor: 'rgba(0,0,0,0.3)'
      }
    }
  }
}

option = {
  dataset: {
    source: seriesData
  },
  tooltip: {},
  hoverLayerThreshold: Infinity,
  series: [
    {
      type: 'custom',
      colorBy: 'data',
      renderItem: renderItem,
      progressive: 0,
      coordinateSystem: 'none',
      encode: {
        tooltip: 'value',
        itemName: 'id'
      }
    }
  ]
}

const initEcharts = () => {
  const myChart = echarts.init(chart.value)
  myChart.setOption(option)
}

onMounted(() => {
  initEcharts()
})
</script>
  • 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
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读