gpt4 book ai didi

javascript - Amcharts:更改整个数据集

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:49 25 4
gpt4 key购买 nike

我开始使用 amcharts 4,但遇到了问题:根据文档更改图表数据集,只需将新的对象数组分配给 chart.data:

chart.data = [{ some data object }, {}......]

它似乎对我有用,请参阅此代码笔:https://codepen.io/anon/pen/LMgEjV

/**
* ---------------------------------------
* This demo was created using amCharts 4.
*
* For more information visit:
* https://www.amcharts.com/
*
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
*/

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.PieChart);

// Add data
chart.data = [{
"country": "Lithuania",
"litres": 501.9
}, {
"country": "Czech Republic",
"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": "The Netherlands",
"litres": 50
}];

// Add and configure Series
var pieSeries = chart.series.push(new am4charts.PieSeries());
pieSeries.dataFields.value = "litres";
pieSeries.dataFields.category = "country";

function changeData() {
chart.data = [{
"country": "Lithuania",
"litres": 501.9
}, {
"country": "Czech Republic",
"litres": 301.9
}
];

chart.validateData();
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

#chartdiv {
width: 100%;
height: 400px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<button id="btn-change" onClick="changeData()">Click Me</button>
<div id="chartdiv"></div>

我错过了什么?

最佳答案

我不是 100% 确定我理解问题,但无论如何我会尽力帮助你,我检查了你的代码,我认为主要问题发生了,因为你将整个数组推送到 chart.data而不仅仅是您要更改的对象。请看我的代码:

function changeData() {
chart.data.push({
"country": "Lithuania",
"litres": "501.9"
})

chart.validateData();
}

关于javascript - Amcharts:更改整个数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54143537/

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