G2
G2
G6
F2
L7
墨者学院
关于 G2
图表示例
API 文档
使用教程
English
折线图
基础折线图
双折线图
多折线图
其他折线图
条形图
分组条形图
堆叠条形图
基础条形图
柱状图
基础柱状图
分组柱状图
堆叠柱状图
直方图
饼图
环图
玫瑰图
基础饼图
嵌套饼图
点图
散点图
面积图
基础面积图
堆叠面积图
区间面积图
箱形图
箱型图
烛形图
烛形图
热力图
热力图
仪表盘
仪表盘
漏斗图
漏斗图
地图
地图
雷达图
雷达图
分面
分面
关系图
关系图
其他图表
其他
迷你图
组件使用
组件
堆叠条形图
带标识的百分比堆叠条形图
堆叠条形图,带标识
源码
复制成功
复制失败
全屏
复制
运行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,height=device-height"> <title>带标识的百分比堆叠条形图</title> <style>::-webkit-scrollbar{display:none;}html,body{overflow:hidden;height:100%;margin:0;}</style> </head> <body> <div id="mountNode"></div> <script>/*Fixing iframe window.innerHeight 0 issue in Safari*/document.body.clientHeight;</script> <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.g2-3.5.1/dist/g2.min.js"></script> <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.data-set-0.10.1/dist/data-set.min.js"></script> <script> var measureKeys = ['Europe', 'Asia', 'Africa']; var originalData = [{ year: '1750', Europe: 20, Asia: 30, Africa: 50 }, { year: '1800', Europe: 30, Asia: 40, Africa: 30 }, { year: '1850', Europe: 50, Asia: 20, Africa: 30 }]; var colorSet = { Europe: '#4FAAEB', Asia: '#9AD681', Africa: '#FED46B' }; // 计算每个柱子的占比 var ds = new DataSet(); var dv = ds.createView().source(originalData).transform({ type: 'fold', fields: measureKeys, key: 'key', value: 'value' }).transform({ type: 'percent', field: 'value', dimension: 'key', groupBy: ['year'], as: 'percent' }); // 初始化图表实例 var chart = new G2.Chart({ container: 'mountNode', forceFit: true, height: window.innerHeight }); chart.source(dv, { percent: { min: 0, formatter: function formatter(val) { return (val * 100).toFixed(2) + '%'; } } }); // 是否水平翻转 // 改这个参数看效果! var transposeCoord = true; if (transposeCoord) { chart.coord().transpose(); } chart.intervalStack().position('year*percent').color('key', function(value) { return colorSet[value]; }); var totalValues = originalData.map(function(data) { return measureKeys.map(function(key) { return data[key]; }).reduce(function(a, b) { return a + b; }, 0); }); originalData.slice(1).forEach(function(data, dataIndex) { measureKeys.forEach(function(key, keyIndex) { var sliceArgs = transposeCoord ? [0, keyIndex + 1] : [keyIndex, measureKeys.length]; var getY = function getY(dataIndex) { return measureKeys.slice.apply(measureKeys, sliceArgs).map(function(key) { return originalData[dataIndex][key]; }).reduce(function(a, b) { return a + b; }, 0) / totalValues[dataIndex]; }; var startPoint = { x: dataIndex + 0.25, y: getY(dataIndex) }; var endPoint = { x: dataIndex + 0.75, y: getY(dataIndex + 1) }; var percent = (originalData[dataIndex + 1][key] - originalData[dataIndex][key]) / totalValues[dataIndex]; var symbol = percent === 0 ? '' : percent > 0 ? '+' : '-'; var color = percent === 0 ? '#000' : percent > 0 ? 'red' : 'green'; chart.guide().line({ start: [startPoint.x, startPoint.y], end: [endPoint.x, endPoint.y], lineStyle: { stroke: colorSet[key] }, text: { position: 'center', autoRotate: false, offsetY: 0, style: { fill: color }, content: '' + symbol + (Math.abs(percent) * 100).toFixed(2) + '%' } }); }); }); chart.render(); </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,height=device-height"> <title>带标识的百分比堆叠条形图</title> <style>::-webkit-scrollbar{display:none;}html,body{overflow:hidden;height:100%;margin:0;}</style> </head> <body style="background: #1f1f1f;"> <div id="mountNode"></div> <script>/*Fixing iframe window.innerHeight 0 issue in Safari*/document.body.clientHeight;</script> <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.g2-3.5.1/dist/g2.min.js"></script> <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.data-set-0.10.1/dist/data-set.min.js"></script> <script>G2.Global.setTheme('dark');</script> <script> var measureKeys = ['Europe', 'Asia', 'Africa']; var originalData = [{ year: '1750', Europe: 20, Asia: 30, Africa: 50 }, { year: '1800', Europe: 30, Asia: 40, Africa: 30 }, { year: '1850', Europe: 50, Asia: 20, Africa: 30 }]; var colorSet = { Europe: '#4FAAEB', Asia: '#9AD681', Africa: '#FED46B' }; // 计算每个柱子的占比 var ds = new DataSet(); var dv = ds.createView().source(originalData).transform({ type: 'fold', fields: measureKeys, key: 'key', value: 'value' }).transform({ type: 'percent', field: 'value', dimension: 'key', groupBy: ['year'], as: 'percent' }); // 初始化图表实例 var chart = new G2.Chart({ container: 'mountNode', forceFit: true, height: window.innerHeight }); chart.source(dv, { percent: { min: 0, formatter: function formatter(val) { return (val * 100).toFixed(2) + '%'; } } }); // 是否水平翻转 // 改这个参数看效果! var transposeCoord = true; if (transposeCoord) { chart.coord().transpose(); } chart.intervalStack().position('year*percent').color('key', function(value) { return colorSet[value]; }); var totalValues = originalData.map(function(data) { return measureKeys.map(function(key) { return data[key]; }).reduce(function(a, b) { return a + b; }, 0); }); originalData.slice(1).forEach(function(data, dataIndex) { measureKeys.forEach(function(key, keyIndex) { var sliceArgs = transposeCoord ? [0, keyIndex + 1] : [keyIndex, measureKeys.length]; var getY = function getY(dataIndex) { return measureKeys.slice.apply(measureKeys, sliceArgs).map(function(key) { return originalData[dataIndex][key]; }).reduce(function(a, b) { return a + b; }, 0) / totalValues[dataIndex]; }; var startPoint = { x: dataIndex + 0.25, y: getY(dataIndex) }; var endPoint = { x: dataIndex + 0.75, y: getY(dataIndex + 1) }; var percent = (originalData[dataIndex + 1][key] - originalData[dataIndex][key]) / totalValues[dataIndex]; var symbol = percent === 0 ? '' : percent > 0 ? '+' : '-'; var color = percent === 0 ? '#000' : percent > 0 ? 'red' : 'green'; chart.guide().line({ start: [startPoint.x, startPoint.y], end: [endPoint.x, endPoint.y], lineStyle: { stroke: colorSet[key] }, text: { position: 'center', autoRotate: false, offsetY: 0, style: { fill: color }, content: '' + symbol + (Math.abs(percent) * 100).toFixed(2) + '%' } }); }); }); chart.render(); </script> </body> </html>
图表简介
堆叠条形图,带标识
了解更多
图表用法
在百分比堆叠图中加入标识,增强了能力,能显示柱子之间的数值变化趋势
相关链接
BizCharts
Viser