我开始使用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>
我想念什么?
答案 0 :(得分:0)
我不是100%肯定我理解此问题,但是无论如何我都会尽力帮助您,我检查了您的代码,并认为主要问题发生了,因为您将整个数组推到了chart.data
上,而不仅仅是对象你想改变。请查看我的代码:
function changeData() {
chart.data.push({
"country": "Lithuania",
"litres": "501.9"
})
chart.validateData();
}