Antv/G2 分组柱状图

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>自定义图例legend</title>
	<style>
		#chartcontainer {
      
      
			margin: 50px auto;
			text-align: center;
		}
	</style>
</head>
<!-- 引入G2在线资源 -->
<script src="https://gw.alipayobjects.com/os/lib/antv/g2/3.4.10/dist/g2.min.js"></script>
<body>
	<div id="chartcontainer"></div>
	<script>
        // G2 对数据源格式的要求:JSON 数组,数组的每个元素是一个标准 JSON 对象。
        const data =  [
        {
      
       month: 'Jan', city: 'Tokyo', temperature: 7 },
        {
      
       month: 'Jan', city: 'London', temperature: 3.9 },
        {
      
       month: 'Feb', city: 'Tokyo', temperature: 6.9 },
        {
      
       month: 'Feb', city: 'London', temperature: 4.2 },
        {
      
       month: 'Mar', city: 'Tokyo', temperature: 9.5 },
        {
      
       month: 'Mar', city: 'London', temperature: 5.7 },
        {
      
       month: 'Apr', city: 'Tokyo', temperature: 14.5 },
        {
      
       month: 'Apr', city: 'London', temperature: 8.5 },
        {
      
       month: 'May', city: 'Tokyo', temperature: 18.4 },
        {
      
       month: 'May', city: 'London', temperature: 11.9 },
        {
      
       month: 'Jun', city: 'Tokyo', temperature: 21.5 },
        {
      
       month: 'Jun', city: 'London', temperature: 15.2 },
        {
      
       month: 'Jul', city: 'Tokyo', temperature: 25.2 },
        {
      
       month: 'Jul', city: 'London', temperature: 17 },
        {
      
       month: 'Aug', city: 'Tokyo', temperature: 26.5 },
        {
      
       month: 'Aug', city: 'London', temperature: 16.6 },
        {
      
       month: 'Sep', city: 'Tokyo', temperature: 23.3 },
        {
      
       month: 'Sep', city: 'London', temperature: 14.2 },
        {
      
       month: 'Oct', city: 'Tokyo', temperature: 18.3 },
        {
      
       month: 'Oct', city: 'London', temperature: 10.3 },
        {
      
       month: 'Nov', city: 'Tokyo', temperature: 13.9 },
        {
      
       month: 'Nov', city: 'London', temperature: 6.6 },
        {
      
       month: 'Dec', city: 'Tokyo', temperature: 9.6 },
        {
      
       month: 'Dec', city: 'London', temperature: 4.8 },
        ] 
        
        // Step 1: 创建 Chart 对象(使用 script 标签引入 G2 资源时,挂载在 window 上的变量名为 G2,所以需要加上 G2 的前缀。)
        const chart = new G2.Chart({
      
      
            container: 'chartcontainer', // 指定图表容器 ID:chart
            width: 800, // 指定图表宽度
            height: 500 // 指定图表高度
        });
        
        // Step 2: 载入数据源
        chart.source(data);
        
        chart.axis('temperature', {
      
      
        	position: 'right',
        	label: {
      
      
              formatter: function formatter(val) {
      
       // 格式化
            	  return val + '°C';
              }
           }
    	});
        chart.axis('month', {
      
      
        	label: {
      
      
        		offset: 12
        	}
        });
        chart.coord().transpose().scale(1, -1);
        // 自定义图例
        chart.legend('city',{
      
      
          position: 'top' // 设置图例的显示位置       
        })
        // Step 3:创建图形语法,绘制柱状图,由 month 和 temperature 两个属性决定图形位置,month 映射至 x ,temperature 映射至 y 轴
        chart.interval().position('month*temperature').color('city')
        .adjust([{
      
      
        	type: 'dodge',
        	marginRatio: 1 / 32
        }]);
        
        // Step 4: 渲染图表
        chart.render();
    </script>
</body>
</html>

页面效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/HH18700418030/article/details/134124664
今日推荐