如何在AMchart中向json对象添加额外数据?
当我的obj是简单的时候,一切都被解析:var data = [ {year: "1930",italy: 4,germany: 5.1,uk: 3}, {year: "1934",italy: 1,germany: 2,uk: 6}, {year: "1938",italy: 2,germany: 3,uk: 1} ];
但当它看起来像这样时,它并不:var data = [ {year:"1930", italy:[{aa:20,bb:"21"}], germany:[{aa:30,bb:"44"}], uk:[{aa:40,bb:"77"}] } ];
这是负责解析的代码:
var series = chart.series.push(
am5xy.LineSeries.new(root, {
name: name,
xAxis: xAxis,
yAxis: yAxis,
valueYField: field, // << -- issue
categoryXField: "year",
tooltip: am5.Tooltip.new(root, {
pointerOrientation: "horizontal",
labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
})
})
);
我可以使用此方法获得正确的值,但当我输入值并替换上面的field
时,代码无法正确解析,
if(chart.series._values[0]){
var data1 = chart.series._values[0]._dataItems[0].dataContext;
if(data1[field][0])
var valY = data1[field][0].aa;
//console.log( valY );
}
下面是我完整代码:
am5.ready(function() {
var root = am5.Root.new("chartdiv");
root._logo.dispose();
root.setThemes([
am5themes_Animated.new(root)
]);
var chart = root.container.children.push(
am5xy.XYChart.new(root, {
panX: true,
panY: true,
wheelX: "panX",
wheelY: "zoomX",
layout: root.verticalLayout,
pinchZoomX:true
})
);
// Add cursor
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
behavior: "none"
}));
cursor.lineY.set("visible", false);
// The data
var data = [ {year:"1930", italy:[{aa:20,bb:"21"}], germany:[{aa:30,bb:"44"}], uk:[{aa:40,bb:"77"}] } ];
/* var data = [ {year: "1930",italy: 4,germany: 5.1,uk: 3},
{year: "1934",italy: 1,germany: 2,uk: 6},
{year: "1938",italy: 2,germany: 3,uk: 1} ]; */
// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xRenderer = am5xy.AxisRendererX.new(root, {});
xRenderer.grid.template.set("location", 0.5);
xRenderer.labels.template.setAll({
location: 0.5,
multiLocation: 0.5
});
var xAxis = chart.xAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: "year",
renderer: xRenderer,
tooltip: am5.Tooltip.new(root, {})
})
);
xAxis.data.setAll(data);
var yAxis = chart.yAxes.push(
am5xy.ValueAxis.new(root, {
maxPrecision: 1,
renderer: am5xy.AxisRendererY.new(root, {
inversed: false
})
})
);
function createSeries(name, field) {
if(chart.series._values[0]){
var data1 = chart.series._values[0]._dataItems[0].dataContext;
if(data1[field][0])
var valY = data1[field][0].aa;
//console.log( valY );
}
var series = chart.series.push(
am5xy.LineSeries.new(root, {
name: name,
xAxis: xAxis,
yAxis: yAxis,
valueYField: field, // issue when replced with: valY
categoryXField: "year",
tooltip: am5.Tooltip.new(root, {
pointerOrientation: "horizontal",
labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
})
})
);
series.bullets.push(function() {
return am5.Bullet.new(root, {
sprite: am5.Circle.new(root, {
radius: 5,
fill: series.get("fill")
})
});
});
series.set("setStateOnChildren", true);
series.states.create("hover", {});
series.mainContainer.set("setStateOnChildren", true);
series.mainContainer.states.create("hover", {});
series.strokes.template.states.create("hover", {
strokeWidth: 4
});
series.data.setAll(data);
series.show(1000);
}
createSeries("Italy", "italy");
createSeries("Germany", "germany");
createSeries("UK", "uk");
var legend = chart.children.push(
am5.Legend.new(root, {
centerX: am5.percent(90),
x: am5.percent(90),
centerY: am5.percent(105),
y: am5.percent(105),
})
);
// Make series change state when legend item is hovered
legend.itemContainers.template.states.create("hover", {});
legend.itemContainers.template.events.on("pointerover", function(e) {
e.target.dataItem.dataContext.hover();
});
legend.itemContainers.template.events.on("pointerout", function(e) {
e.target.dataItem.dataContext.unhover();
});
legend.data.setAll(chart.series.values);
chart.appear(1000, 100);
});
1条答案
按热度按时间6jygbczu1#
看来amcharts并不提供通过其数据字段对数据中的子对象进行结构化访问的功能。
因此,唯一可用的解决方案是预处理数据;对于您的数据结构,为了涵盖这两种情况,您可以使用以下内容
片段: