使用G2创建箱型图/How To Use G2 To Make Box Chart

in utopian-io •  7 years ago  (edited)

Summary:

G2 is a set of graphically-based graphical syntax that is data-driven and highly user-friendly and extensible, allowing users to build a wide variety of interactive statistics without having to worry about detailed implementation details chart.
G2 是一套基于可视化编码的图形语法,以数据驱动,具有高度的易用性和扩展性,用户无需关注各种繁琐的实现细节,一条语句即可构建出各种各样的可交互的统计图表。

您将从这个教程中学到什么

  • 如何引入js文件
  • 如何定义容器
  • 如何定义数据
  • 如何引用数据
  • 如何定义提示框
  • 如何渲染图表

学习此教程的必备条件

教程难度

  • 容易

教程内容

演示效果
demo.gif

1. 知识点A - 如何引入js文件

<script src="https://gw.alipayobjects.com/os/antv/assets/g2/3.0.4-beta.2/g2.min.js"></script>
<script src="https://gw.alipayobjects.com/os/antv/assets/data-set/0.8.3/data-set.min.js"></script>
<script src="https://gw.alipayobjects.com/os/antv/assets/lib/jquery-3.2.1.min.js"></script>

使用内嵌script对js文件进行引入,用于后期图表使用。


2. 知识点B - 如何定义容器

<div id="boxchart"></div>

使用div定义容器,用于展示图表。容器名:boxchart


3. 知识点C - 如何定义数据

  const data = [
    { x: 'Demo-A', xdiz: 1, xdx: 9, xzx: 16, xsx: 22, xdingz: 24 },
    { x: 'Demo-B', xdiz: 1, xdx: 5, xzx: 8, xsx: 12, xdingz: 16 },
    { x: 'Demo-C', xdiz: 1, xdx: 8, xzx: 12, xsx: 19, xdingz: 26 },
    { x: 'Demo-D', xdiz: 2, xdx: 8, xzx: 12, xsx: 21, xdingz: 28 },
    { x: 'Demo-E', xdiz: 1, xdx: 8, xzx: 14, xsx: 18, xdingz: 24 },
    { x: 'Demo-F', xdiz: 3, xdx: 10, xzx: 17, xsx: 28, xdingz: 30 },
    { x: 'Demo-G', xdiz: 1, xdx: 7, xzx: 10, xsx: 17, xdingz: 22 },
    { x: 'Demo-H', xdiz: 1, xdx: 6, xzx: 8, xsx: 13, xdingz: 16 }
  ];
  • const: 用于定义数组。
  • data:定义为数组名。
  • 格式:[x:'x轴值', xdiz: y轴起点值, xdx:y轴箱底值, xzx:y轴箱中间值, xsx:y轴箱顶值, xdingz: y轴终点值 ]

4. 知识点D - 如何引用数据

  const dv = new DataView().source(data);
  dv.transform({
    type: 'map',
    callback: (obj) => {
      obj.range = [ obj.xdiz, obj.xdx, obj.xzx, obj.xsx, obj.xdingz ];
      return obj;
    }
  });
  const chart = new G2.Chart({
    container: 'boxchart',
    forceFit: true,
    height: window.innerHeight,
  });
  chart.source(dv, {
    range: {
      max: 35
    }
  });
  • obj.range:定义从数组中取值规则。
  • chart:创建一个chart实例,返回一个Chart对象,建议在单个容器上只初始化一个Chart实例。
  • container:定于数据从boxchart数组取值。
  • forceFit: 定义图表的宽度自适应开关,默认为 false,设置为 true 时表示自动取 dom(实例容器)的宽度。
  • height: 定义图表高度。
  • window.innerHeight: 获取页面可用高度。
  • source:定义为chart装载数据,返回chart对象。
  • range:输出数据的范围,默认[0, 1],格式为 [min, max],min 和 max 均为 0 至 1 范围的数据。

5. 知识点E - 如何定义提示框

  chart.tooltip({
    showTitle: false,
    crosshairs: {
      type: 'rect'
    },
    itemTpl: '<li data-index={index} style="margin-bottom:4px;">'
        + '<span style="background-color:{color};" class="g2-tooltip-marker"></span>'
        + '{name}<br/>'
        + '<span style="padding-left: 16px">最大值:{xdingz}</span><br/>'
        + '<span style="padding-left: 16px">上四分位数:{xsx}</span><br/>'
        + '<span style="padding-left: 16px">中位数:{xzx}</span><br/>'
        + '<span style="padding-left: 16px">下四分位数:{xdx}</span><br/>'
        + '<span style="padding-left: 16px">最小值:{xdiz}</span><br/>'
        + '</li>'
  });
  • tooltip:图表的tooltip配置,G2图表的tooltip使用html渲染。
  • crosshairs:是一个对象类型,用于设置 tooltip 的辅助线或者辅助框。
  • rect: 定义为矩形框

6. 知识点F - 如何渲染图表

  chart.schema().position('x*range')
  .shape('box')
  .tooltip('x*xdiz*xdx*xzx*xsx*xdingz', (x, xdiz, xdx, xzx, xsx, xdingz) => {
    return {
      name: x,
      xdiz,
      xdx,
      xzx,
      xsx,
      xdingz
    };
  })
  .style({
    stroke: '#545454',
    fill: '#1890FF',
    fillOpacity: 0.3
  });
  chart.render();
  • shape:将数据值映射到图形的形状上的方法。
  • position:将数据值映射到图形的位置上的方法。position 属性会对多个字段进行数据的映射。以chart.interval().position('xy') 为例,interval代表柱状图,即最后需要生成柱状图,而position代表位置,position('x*y')代表数据在图形中的位置由x和y这两个维度的变量决定,(x1,y1) 这样的数值对,最后就会被转换为画布上对应的坐标点。
  • tooltip:图表的tooltip配置,G2图表的tooltip使用html渲染。
  • stroke:定义线的颜色
  • fill:定义文本的颜色
  • render:用于将图表渲染至画布。

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>基础箱型图</title>
</head>
<body>
<div id="boxchart"></div>
<script src="https://gw.alipayobjects.com/os/antv/assets/g2/3.0.4-beta.2/g2.min.js"></script>
<script src="https://gw.alipayobjects.com/os/antv/assets/data-set/0.8.3/data-set.min.js"></script>
<script src="https://gw.alipayobjects.com/os/antv/assets/lib/jquery-3.2.1.min.js"></script>
<script>
  const { DataView } = DataSet;
  const data = [
    { x: 'Demo-A', xdiz: 1, xdx: 9, xzx: 16, xsx: 22, xdingz: 24 },
    { x: 'Demo-B', xdiz: 1, xdx: 5, xzx: 8, xsx: 12, xdingz: 16 },
    { x: 'Demo-C', xdiz: 1, xdx: 8, xzx: 12, xsx: 19, xdingz: 26 },
    { x: 'Demo-D', xdiz: 2, xdx: 8, xzx: 12, xsx: 21, xdingz: 28 },
    { x: 'Demo-E', xdiz: 1, xdx: 8, xzx: 14, xsx: 18, xdingz: 24 },
    { x: 'Demo-F', xdiz: 3, xdx: 10, xzx: 17, xsx: 28, xdingz: 30 },
    { x: 'Demo-G', xdiz: 1, xdx: 7, xzx: 10, xsx: 17, xdingz: 22 },
    { x: 'Demo-H', xdiz: 1, xdx: 6, xzx: 8, xsx: 13, xdingz: 16 }
  ];
  const dv = new DataView().source(data);
  dv.transform({
    type: 'map',
    callback: (obj) => {
      obj.range = [ obj.xdiz, obj.xdx, obj.xzx, obj.xsx, obj.xdingz ];
      return obj;
    }
  });
  const chart = new G2.Chart({
    container: 'boxchart',
    forceFit: true,
    height: window.innerHeight,
  });
  chart.source(dv, {
    range: {
      max: 35
    }
  });
  chart.tooltip({
    showTitle: false,
    crosshairs: {
      type: 'rect'
    },
    itemTpl: '<li data-index={index} style="margin-bottom:4px;">'
        + '<span style="background-color:{color};" class="g2-tooltip-marker"></span>'
        + '{name}<br/>'
        + '<span style="padding-left: 16px">最大值:{xdingz}</span><br/>'
        + '<span style="padding-left: 16px">上四分位数:{xsx}</span><br/>'
        + '<span style="padding-left: 16px">中位数:{xzx}</span><br/>'
        + '<span style="padding-left: 16px">下四分位数:{xdx}</span><br/>'
        + '<span style="padding-left: 16px">最小值:{xdiz}</span><br/>'
        + '</li>'
  });
  chart.schema().position('x*range')
  .shape('box')
  .tooltip('x*xdiz*xdx*xzx*xsx*xdingz', (x, xdiz, xdx, xzx, xsx, xdingz) => {
    return {
      name: x,
      xdiz,
      xdx,
      xzx,
      xsx,
      xdingz
    };
  })
  .style({
    stroke: '#545454',
    fill: '#1890FF',
    fillOpacity: 0.3
  });
  chart.render();
</script>
</body>
</html>

最终效果
demo.gif

系列课程

  • 如果您喜欢我的教程,可以在我的个人档案页面,获取更多信息。
    If you like my tutorial , You can check out your profile for more such tutorials.
  • 您可以使用zqz-tutorial标签快速查看我发布的所有教程
    You can use the "zqz-tutorial" tag to see all the tutorials I've posted.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

thank you very much~

Your contribution cannot be approved because it does not follow the Utopian Rules.

This is the same as in https://antv.alipay.com/zh-cn/g2/3.x/demo/box/basic.html . You are taking the official examples and present them as yours.

You can contact us on Discord.
[utopian-moderator]