gpt4 book ai didi

javascript - 谷歌可视化 : Animated Line Graph --incremental rather than all at once?

转载 作者:行者123 更新时间:2023-12-05 07:41:50 25 4
gpt4 key购买 nike

现在我的代码是这样的:

function drawChart() {

var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Revenue');

data.addRows([
['', 0],
['2008', 123],
['2010', 213],
['2012', 654]
]);

var options = {
hAxis: {textStyle:{color: '#FFF'}},
vAxis: { baseline:0, baselineColor: '#FFF', gridlineColor: '#FFF', textStyle:{color: '#FFF'} },
backgroundColor: 'transparent',
legend: { position: 'none' },
colors: ['#FFF'],
textStyle:{color: '#FFF'},
pointSize: 10,
series: {
0: { pointShape: 'star'}
},
animation: {startup: true, duration: 5000, easing: 'linear',}

};



var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

chart.draw(data, options);
}

我想要的是让我的动画逐渐显示每一行。我该怎么做?

如有任何帮助,我们将不胜感激。

最佳答案

必须绘制图表才能发生动画

保留数据,一次只绘制一行

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

google.charts.load('current', {
callback: function () {
var rawData = [
[0, 0],
[1, 2],
[2, 1],
[3, 4],
[4, 2],
[5, 8],
[6, 3],
[7, 16],
[8, 4],
[9, 32]
];

var data = new google.visualization.DataTable({
"cols": [
{"id":"","label":"X","type":"number"},
{"id":"","label":"Y","type":"number"}
]
});

var options = {
pointSize: 4,
animation:{
startup: true,
duration: 600,
easing: 'in'
},
legend: 'none',
hAxis: {
viewWindow: {
min: 0,
max: 9
}
},
vAxis: {
viewWindow: {
min: 0,
max: 32
}
}
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

drawChart();
setInterval(drawChart, 1200);

var rowIndex = 0;
function drawChart() {
if (rowIndex < rawData.length) {
data.addRow(rawData[rowIndex++]);
chart.draw(data, options);
}
}
},
packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

编辑

为了更流畅的操作,等待图表的 'ready' 事件,然后再次绘制。

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

google.charts.load('current', {
packages:['corechart']
}).then(function () {
var rawData = [
[0, 0],
[1, 2],
[2, 1],
[3, 4],
[4, 2],
[5, 8],
[6, 3],
[7, 16],
[8, 4],
[9, 32]
];

var data = new google.visualization.DataTable({
"cols": [
{"id":"","label":"X","type":"number"},
{"id":"","label":"Y","type":"number"}
]
});

var options = {
pointSize: 4,
animation:{
startup: true,
duration: 600,
easing: 'in'
},
legend: 'none',
hAxis: {
viewWindow: {
min: 0,
max: 9
}
},
vAxis: {
viewWindow: {
min: 0,
max: 32
}
}
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

google.visualization.events.addListener(chart, 'ready', function () {
drawChart();
});

var rowIndex = 0;
drawChart();
function drawChart() {
if (rowIndex < rawData.length) {
data.addRow(rawData[rowIndex++]);
chart.draw(data, options);
}
}
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

关于javascript - 谷歌可视化 : Animated Line Graph --incremental rather than all at once?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45077939/

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