G2
G2
G6
F2
L7
墨者学院
关于 G2
图表示例
API 文档
使用教程
English
折线图
基础折线图
双折线图
多折线图
其他折线图
条形图
分组条形图
堆叠条形图
基础条形图
柱状图
基础柱状图
分组柱状图
堆叠柱状图
直方图
饼图
环图
玫瑰图
基础饼图
嵌套饼图
点图
散点图
面积图
基础面积图
堆叠面积图
区间面积图
箱形图
箱型图
烛形图
烛形图
热力图
热力图
仪表盘
仪表盘
漏斗图
漏斗图
地图
地图
雷达图
雷达图
分面
分面
关系图
关系图
其他图表
其他
迷你图
组件使用
组件
组件
legend-html
源码
复制成功
复制失败
全屏
复制
运行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,height=device-height"> <title>legend-html</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/assets/lib/jquery-3.2.1.min.js"></script> <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.data-set-0.10.1/dist/data-set.min.js"></script> <style type="text/css"> #canvas .g2-tooltip { position:absolute; visibility:hidden; border-style:solid; white-space:nowrap; z-index:9; transition:left 0.4s cubic-bezier(0.23, 1, 0.32, 1), top 0.4s cubic-bezier(0.23, 1, 0.32, 1); background-color:rgb(255, 255, 255); border-width:1px; border-color: #999; border-radius:4px; color: #444; font-style:normal; font-variant:normal; font-weight:normal; font-stretch:normal; font-size:14px; font-family:sans-serif; line-height:21px; padding:5px; } #canvas .g2-tooltip-list { margin: 5px ; list-style-type:none; padding:0; } #canvas .g2-legend { top: 45%!important; } </style> <script> var data = [{ country: 'Lithuania', litres: 501.9 }, { country: 'Czech', litres: 301.9 }, { country: 'Ireland', litres: 201.1 }, { country: 'Germany', litres: 165.8 }, { country: 'Australia', litres: 139.9 }, { country: 'Austria', litres: 128.3 }, { country: 'UK', litres: 99 }, { country: 'Belgium', litres: 60 }, { country: 'Netherlands', litres: 50 }]; var ds = new DataSet(); var dv = ds.createView().source(data).transform({ type: 'percent', field: 'litres', dimension: 'country', as: 'percent' }); var chart = new G2.Chart({ container: 'mountNode', forceFit: true, height: window.innerHeight, padding: [80, 300, 80, 80] }); chart.source(dv, { percent: { formatter: function formatter(val) { val = (val * 100).toFixed(2) + '%'; return val; } }, nice: false }); chart.coord('theta', { innerRadius: 0.3, radius: 0.95 }); chart.tooltip({ showTitle: false, // 不展示 tooltip containerTpl: '<div class="g2-tooltip"><ul class="g2-tooltip-list"></ul></div>', itemTpl: '<li data-index={index}><span style="color:{color}">{name}:</span>{value}</li>' }); chart.legend({ useHtml: true, position: 'right', reactive: true, containerTpl: '<div class="g2-legend">' + '<table class="g2-legend-list"></table>' + '</div>', itemTpl: function itemTpl(value, color, checked, index) { var obj = dv.rows[index]; var percent = (obj.percent * 100).toFixed(2) + '%'; checked = checked ? 'checked' : 'unChecked'; return '<tr class="g2-legend-list-item item-' + index + ' ' + checked + '" data-value="' + value + '" data-color=' + color + ' >' + '<td style="width:120px;"><i class="g2-legend-marker" style="width:10px;height:10px;display:inline-block;margin-right:10px;background-color:' + color + ';"></i>' + '<span class="g2-legend-text" style="color: #666">' + value + '</span></td>' + '<td style="text-align: right">' + percent + '</td>' + '<td style="text-align: right;color: #666;width:80px">' + obj.litres + '</td>' + '</tr>'; }, offset: [15, 0], 'g2-legend': { position: 'absolute' }, 'g2-legend-list': { listStyleType: 'none', margin: 0, padding: 0 }, 'g2-legend-list-item': { cursor: 'pointer', fontSize: '14px' } }); chart.filter('country', function(val) { return val !== 'UK'; }); var interval = chart.intervalStack().position('percent').color('country', ['#67b7dc', '#84b761', '#fdd400', '#cc4748', '#cd82ad', '#2f4074', '#448e4d', '#b7b83f', '#b9783f']).label('percent', { formatter: function formatter(val, item) { return item.country + ': ' + val; } }).style({ lineWidth: 2, stroke: '#fff' }).select(false).active(false); chart.render(); chart.on('interval:mouseenter', function(ev) { var shape = ev.shape; interval.setShapeSelected(shape); }); chart.on('interval:mouseleave', function() { interval.clearSelected(); }); </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,height=device-height"> <title>legend-html</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/assets/lib/jquery-3.2.1.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> <style type="text/css"> #canvas .g2-tooltip { position:absolute; visibility:hidden; border-style:solid; white-space:nowrap; z-index:9; transition:left 0.4s cubic-bezier(0.23, 1, 0.32, 1), top 0.4s cubic-bezier(0.23, 1, 0.32, 1); background-color:rgb(255, 255, 255); border-width:1px; border-color: #999; border-radius:4px; color: #444; font-style:normal; font-variant:normal; font-weight:normal; font-stretch:normal; font-size:14px; font-family:sans-serif; line-height:21px; padding:5px; } #canvas .g2-tooltip-list { margin: 5px ; list-style-type:none; padding:0; } #canvas .g2-legend { top: 45%!important; } </style> <script> var data = [{ country: 'Lithuania', litres: 501.9 }, { country: 'Czech', litres: 301.9 }, { country: 'Ireland', litres: 201.1 }, { country: 'Germany', litres: 165.8 }, { country: 'Australia', litres: 139.9 }, { country: 'Austria', litres: 128.3 }, { country: 'UK', litres: 99 }, { country: 'Belgium', litres: 60 }, { country: 'Netherlands', litres: 50 }]; var ds = new DataSet(); var dv = ds.createView().source(data).transform({ type: 'percent', field: 'litres', dimension: 'country', as: 'percent' }); var chart = new G2.Chart({ container: 'mountNode', forceFit: true, height: window.innerHeight, padding: [80, 300, 80, 80] }); chart.source(dv, { percent: { formatter: function formatter(val) { val = (val * 100).toFixed(2) + '%'; return val; } }, nice: false }); chart.coord('theta', { innerRadius: 0.3, radius: 0.95 }); chart.tooltip({ showTitle: false, // 不展示 tooltip containerTpl: '<div class="g2-tooltip"><ul class="g2-tooltip-list"></ul></div>', itemTpl: '<li data-index={index}><span style="color:{color}">{name}:</span>{value}</li>' }); chart.legend({ useHtml: true, position: 'right', reactive: true, containerTpl: '<div class="g2-legend">' + '<table class="g2-legend-list"></table>' + '</div>', itemTpl: function itemTpl(value, color, checked, index) { var obj = dv.rows[index]; var percent = (obj.percent * 100).toFixed(2) + '%'; checked = checked ? 'checked' : 'unChecked'; return '<tr class="g2-legend-list-item item-' + index + ' ' + checked + '" data-value="' + value + '" data-color=' + color + ' >' + '<td style="width:120px;"><i class="g2-legend-marker" style="width:10px;height:10px;display:inline-block;margin-right:10px;background-color:' + color + ';"></i>' + '<span class="g2-legend-text" style="color: #666">' + value + '</span></td>' + '<td style="text-align: right">' + percent + '</td>' + '<td style="text-align: right;color: #666;width:80px">' + obj.litres + '</td>' + '</tr>'; }, offset: [15, 0], 'g2-legend': { position: 'absolute' }, 'g2-legend-list': { listStyleType: 'none', margin: 0, padding: 0 }, 'g2-legend-list-item': { cursor: 'pointer', fontSize: '14px' } }); chart.filter('country', function(val) { return val !== 'UK'; }); var interval = chart.intervalStack().position('percent').color('country', ['#67b7dc', '#84b761', '#fdd400', '#cc4748', '#cd82ad', '#2f4074', '#448e4d', '#b7b83f', '#b9783f']).label('percent', { formatter: function formatter(val, item) { return item.country + ': ' + val; } }).style({ lineWidth: 2, stroke: '#fff' }).select(false).active(false); chart.render(); chart.on('interval:mouseenter', function(ev) { var shape = ev.shape; interval.setShapeSelected(shape); }); chart.on('interval:mouseleave', function() { interval.clearSelected(); }); </script> </body> </html>
相关链接
BizCharts
Viser