D3 二维图表的绘制系列(十四)气泡图

上一篇: 多符号散点图 https://blog.youkuaiyun.com/zjw_python/article/details/98483989

下一篇: 雷达图 https://blog.youkuaiyun.com/zjw_python/article/details/98486655

代码结构和初始化画布的Chart对象介绍,请先看 https://blog.youkuaiyun.com/zjw_python/article/details/98182540

本图完整的源码地址: https://github.com/zjw666/D3_demo/tree/master/src/scatterChart/bubbleChart

1 图表效果

在这里插入图片描述

2 数据

x,y,size
24,12,64
39,20,14
78,15,67
15,80,34
36,70,14
7,11,64
36,13,34
34,89,74
9,95,6
72,35,25
26,65,80
16,36,10
99,65,45
46,78,65
85,42,55
32,14,20
43,12,31
73,95,8
8,6,16
81,62,11

3 关键代码

导入数据

d3.csv('./data.csv', function(d){
   
    return {
   
        x: +d.x,
        y: +d.y,
        r: +d.size
    };
}).then(function(data){
   
.....

一些样式参数配置

const config = {
   
        margins: {
   top: 80, left: 80, bottom: 50, right: 80},
        textColor: 'black',
        gridColor: 'gray',
        ShowGridX: [10, 20, 30, 40, 50, 60, 70 ,80, 90, 100],
        ShowGridY: [10, 20, 30, 40, 50, 60, 70 ,80, 90, 100],
        title: '气泡图',
        pointMaxSize: 20,
        hoverColor: 'white',
        animateDuration: 1000,
        pointCenterColor: 'white',
        pointEdgeColor: chart._colors(0)
    }

尺度转换,气泡图相比基础散点图,新增了一个数据点的大小作为新的维度

/* ----------------------------尺度转换------------------------  */
    chart.scaleX = d3.scaleLinear()
                    .domain([0, Math.ceil(d3.max(data, (d) => d.x)/10)*10])
                    .range([0, chart.getBodyWidth()]);
    
    chart.scaleY = d3.scaleLinear()
                    .domain([0, Math.ceil(d3.max(data, (d) => d.y)/10)*10])
                    .range([chart.getBodyHeight(), 0]);
    
    chart.scaleSize = d3.scaleLinear()
                        .domain(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值