G2 图表示例
源码
g2-react
x
 
1
<!DOCTYPE html>
2
<html>
3
  <head>
4
    <meta charset="utf-8">
5
    <title>各国脂肪和糖分摄入量对比</title>
6
    <script src="https://a.alipayobjects.com/jquery/jquery/1.11.1/jquery.js"></script>
7
    <script src="https://gw.alipayobjects.com/as/g/datavis/g2/2.3.13/index.js"></script>
8
  </head>
9
  <body>
10
    <div id="c1"></div>
11
    <script>
12
      $('<h2 style="text-align: center;margin-bottom: 5px;">Sugar and fat intake per country</h2>').appendTo($('#c1'))
13
      // 设置鼠标 hove 至气泡的样式
14
      G2.Global.activeShape.point = {
15
        lineWidth: 2,
16
        shadowBlur: 12,
17
        shadowColor: '#3182bd'
18
      };
19
      var data = [
20
        { x: 95, y: 95, z: 13.8, name: 'BE', country: 'Belgium' },
21
        { x: 86.5, y: 102.9, z: 14.7, name: 'DE', country: 'Germany' },
22
        { x: 80.8, y: 91.5, z: 15.8, name: 'FI', country: 'Finland' },
23
        { x: 80.4, y: 102.5, z: 12, name: 'NL', country: 'Netherlands' },
24
        { x: 80.3, y: 86.1, z: 11.8, name: 'SE', country: 'Sweden' },
25
        { x: 78.4, y: 70.1, z: 16.6, name: 'ES', country: 'Spain' },
26
        { x: 74.2, y: 68.5, z: 14.5, name: 'FR', country: 'France' },
27
        { x: 73.5, y: 83.1, z: 10, name: 'NO', country: 'Norway' },
28
        { x: 71, y: 93.2, z: 24.7, name: 'UK', country: 'United Kingdom' },
29
        { x: 69.2, y: 57.6, z: 10.4, name: 'IT', country: 'Italy' },
30
        { x: 68.6, y: 20, z: 16, name: 'RU', country: 'Russia' },
31
        { x: 65.5, y: 126.4, z: 35.3, name: 'US', country: 'United States' },
32
        { x: 65.4, y: 50.8, z: 28.5, name: 'HU', country: 'Hungary' },
33
        { x: 63.4, y: 51.8, z: 15.4, name: 'PT', country: 'Portugal' },
34
        { x: 64, y: 82.9, z: 31.3, name: 'NZ', country: 'New Zealand' }
35
      ];
36
      var chart = new G2.Chart({
37
        id: 'c1',
38
        forceFit: true,
39
        height: 450,
40
        plotCfg: {
41
          margin: [20, 80, 90, 60],
42
          background: {
43
            stroke: '#ccc', // 边颜色
44
            lineWidth: 1, // 边框粗细
45
          } // 绘图区域背景设置
46
        }
47
      });
48
      chart.source(data, {
49
        x: {
50
          alias: 'Daily fat intake', // 定义别名
51
          tickInterval: 5, // 自定义刻度间距
52
          nice: false, // 不对最大最小值优化
53
          max: 96, // 自定义最大值
54
          min: 62 // 自定义最小值
55
        },
56
        y: {
57
          alias: 'Daily sugar intake',
58
          tickInterval: 50,
59
          nice: false,
60
          max: 165,
61
          min: 0
62
        },
63
        z: {
64
          alias: 'Obesity(adults) %'
65
        }
66
      });
67
      // 开始配置坐标轴
68
      chart.axis('x', {
69
        formatter: function(val) {
70
          return val + ' gr'; // 格式化坐标轴显示文本
71
        },
72
        grid: {
73
          line: {
74
            stroke: '#d9d9d9',
75
            lineWidth: 1,
76
            lineDash: [2,2]
77
          }
78
        }
79
      });
80
      chart.axis('y', {
81
        titleOffset: 80, // 设置标题距离坐标轴的距离
82
        formatter: function(val) {
83
          if (val > 0) {
84
            return val + ' gr';
85
          }
86
        }
87
      });
88
      chart.legend(false);
89
      chart.tooltip({
90
        map: {
91
          title: 'country'
92
        }
93
      });
94
      chart.point().position('x*y').size('z', 40, 10).label('name*country', {
95
        offset:0, // 文本距离图形的距离
96
        label: {
97
          fill: '#000',
98
          fontWeight: 'bold', // 文本粗细
99
          shadowBlur: 5, // 文本阴影模糊
100
          shadowColor: '#fff' // 阴影颜色
101
        },
102
      }).color('#3182bd').opacity(0.5).shape('circle').tooltip('x*y*z');
103
      chart.guide().tag([65, 'min'], [65, 'max'], 'Safe fat intake 65g/day');
104
      chart.guide().tag(['min', 50], ['max', 50], 'Safe sugar intake 50g/day');
105
      chart.render();
106
    </script>
107
  </body>
108
</html>
109

各国脂肪和糖分摄入量对比

气泡图是一种多变量图表,是散点图的变体,也可以认为是散点图和百分比区域图的组合。 了解更多