javascript - 如何创建包含动态数据的柱形图
问题描述
我正在使用谷歌图表,我想获得一个具有固定周期但动态值的柱形图。这是一个柱形图的示例,具有固定值,我想使用注释后代码中的变量(我想使用的变量),考虑到列和值的大小并不总是相同。
function drawChart() {
// Simple example
var data = google.visualization.arrayToDataTable(
[ ['Year', 'Example 1', 'Example 2', 'Example 3'],
['2004', 1000, 400, 100],
['2005', 1170, 460, 500],
]);
// variables i would like to use
// These variables are fixed
var periodOne = '2004';
var periodTwo = '2005';
// non-fixed variables, variables that I will receive and that will not always be the same size.
var columns = ['Example 1', 'Example 2', 'Example 3'];
var valuesP1 = [1000, 400, 100];
var valuesP2 = [1170, 460, 500];
var options = {
title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: 'red'}},
tooltip: {legend:'none',isHtml:true, textStyle: {color: '#FF0000'}, showColorCode: true}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
解决方案
要动态构建,请从空白数据表开始...
// create blank data table
var data = new google.visualization.DataTable();
为期间/年添加一列...
// add period column
data.addColumn('string', 'Year');
添加动态列...
// add columns
columns.forEach(function (label) {
data.addColumn('number', label);
});
将期间/年添加到行值...
// add period to data
valuesP1.splice(0, 0, periodOne);
valuesP2.splice(0, 0, periodTwo);
将行值添加到数据表...
// add data
data.addRow(valuesP1);
data.addRow(valuesP2);
请参阅以下工作片段...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
// These variables are fixed
var periodOne = '2004';
var periodTwo = '2005';
// non-fixed variables, variables that I will receive and that will not always be the same size.
var columns = ['Example 1', 'Example 2', 'Example 3'];
var valuesP1 = [1000, 400, 100];
var valuesP2 = [1170, 460, 500];
// create blank data table
var data = new google.visualization.DataTable();
// add period column
data.addColumn('string', 'Year');
// add columns
columns.forEach(function (label) {
data.addColumn('number', label);
});
// add period to data
valuesP1.splice(0, 0, periodOne);
valuesP2.splice(0, 0, periodTwo);
// add data
data.addRow(valuesP1);
data.addRow(valuesP2);
// draw chart
var options = {
title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: 'red'}},
tooltip: {legend:'none',isHtml:true, textStyle: {color: '#FF0000'}, showColorCode: true}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
window.addEventListener('resize', function () {
chart.draw(data, options);
});
chart.draw(data, options);
});
html, body {
height: 100%;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
#chart_div {
height: 100%;
min-height: 400px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
推荐阅读
- python - 小数据集不适用于先知
- oracle - oracle中的时间戳转换问题
- java - 带有 Spring Boot 的 Javers 为 @Transient 字段返回 ENTITY_INSTANCE_WITH_NULL_ID
- vb.net - 使用 VB.net 的数据库连接维护内存的示例
- javascript - 如何防止在 Backstretch 中延迟显示第一张图像
- node.js - 在之前块中定义的文档完成之前运行的 Mocha 测试
- azure-devops - Azure devops 代理池 - 交互过程
- android - 从另一个 Activity (Kotlin) 将项目添加到 RecyclerView
- java - 如何使用 JBotsim 库实现领导选举的分布式算法
- python-3.x - 即使我在每个循环的开头搜索元素,for 循环中的陈旧元素