gpt4 book ai didi

javascript - 谷歌图表 : How to make differential timeline

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:59 28 4
gpt4 key购买 nike

我有一个使用 Google Chart 显示时间表的脚本,但是我无法修改它以显示差异图表。

我只需计算两个值之间的差值并显示它,而不是总数。我有 [12, 3000, 2500], [15, 4700, 5800]第一个值是时间,第二个值是 X 团队,第三个值是 Y 团队。我想显示:

  • 在时间 12 时,X 队比 X 队多 500 人。
  • 在时间 15,Y 队比 X 队多 1100 人。

非常感谢,蒂姆

google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawLineColors);

function drawLineColors() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Blue Team');
data.addColumn('number', 'Red Team');

data.addRows([
[0, 0, 0], [3, 1700, 1600], [6, 1800, 1700], [9, 2500, 2423],
[12, 3000, 2500], [15, 4700, 5800],
[18, 5200, 5900], [21, 5500, 6000],
[24, 6000, 6200], [27, 6800, 6700],
[30, 7500, 7000], [33, 7800, 8200],
[36, 7900, 9756], [39, 8000, 10752],
[42, 9000, 13753], [45, 15000, 17845],
[48, 19000, 13753], [55, 25000, 17845]
]);

var options = {

lineWidth: 5,
animation:{
duration: 5000,
easing: 'in',
startup : true
},
legend : {
position : 'top'
},
//fontName : 'Shentox 7',
enableInteractivity:false,
width: 712,
height: 156,
backgroundColor : {fill:'transparent'},
curveType: 'function',
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Team Gold'
},
chartArea:{width:'72px',height:'126px'},
colors: ['#FF0000', '#0000FF']
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<body>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
</body>

最佳答案

您可以使用DataView.setColumns提供列值的公式

然后使用DataView绘制图表

请参阅以下工作片段...

google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawLineColors);

function drawLineColors() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Blue Team');
data.addColumn('number', 'Red Team');
data.addRows([
[0, 0, 0], [3, 1700, 1600],
[6, 1800, 1700], [9, 2500, 2423],
[12, 3000, 2500], [15, 4700, 5800],
[18, 5200, 5900], [21, 5500, 6000],
[24, 6000, 6200], [27, 6800, 6700],
[30, 7500, 7000], [33, 7800, 8200],
[36, 7900, 9756], [39, 8000, 10752],
[42, 9000, 13753], [45, 15000, 17845],
[48, 19000, 13753], [55, 25000, 17845]
]);

var options = {
lineWidth: 5,
animation:{
duration: 5000,
easing: 'in',
startup : true
},
legend : {
position : 'top'
},
enableInteractivity:false,
width: 712,
height: 156,
backgroundColor : {fill:'transparent'},
curveType: 'function',
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Team Gold'
},
chartArea:{width:'72px',height:'126px'},
colors: ['#FF0000', '#0000FF']
};

var dataView = new google.visualization.DataView(data);
dataView.setColumns([0, {
calc: function (data, row) {
return data.getValue(row, 2) - data.getValue(row, 1);
},
type: 'number',
label: 'Variance'
}]);

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(dataView, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

关于javascript - 谷歌图表 : How to make differential timeline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40137119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com