我有这个是json的回应
{
"dates": [
"2023-05-01",
"2023-05-02",
"2023-05-03",
"2023-05-04",
"2023-05-05"
],
"values": [
{},
{},
{
"Apple": 454,
"SIM-Free, Model A19211 6.5-inch Super": 191,
"Samsung's new variant which goes beyond": 3
},
{
"OPPO F19 is officially announced on April": 4980,
"Huawei’s re-badged P30 Pro New Edition": 41424,
"MacBook Pro 2021 with mini-LED display": 2535,
"Basic Data": 293,
"Samsung Galaxy Book S (2020)": 1181,
"Style and speed. Stand out on HD video calls": 378,
},
{}
],
}
我想要这样:
你能告诉我如何绑定对象到列。目前我是这样做的:
public createChart(container, options, chartName = null) {
const chartOptions = this.getOptions();
const chartData = [];
options.forEach((data) => {
const chartDataObject = {
name: [],
data: [],
color: '',
fillColor: {}
};
chartDataObject.name = Object.keys(data.values)
.filter((key) => Object.keys(data.values[key]).length !== 0) // Filter out empty objects
.map((key) => key); // Extract the names
chartDataObject.data = Object.values(data.values)
.filter((value) => Object.keys(value).length !== 0) // Filter out empty objects
.map((value) => (value ? Object.values(value)[0] : 0)); // Extract the names
chartDataObject.color = data.color;
chartDataObject.fillColor = {
linearGradient: [0, 0, 0, 150],
stops: [
[0, data.color],
[1, Highcharts.color(data.color).setOpacity(0).get('rgba')]
]
};
chartOptions.xAxis.categories = data.dates;
console.log(chartDataObject);
chartData.push(chartDataObject);
});
chartOptions.series = chartData;
const element = document.createElement('div');
container.appendChild(element);
chartOptions.chart['renderTo'] = element;
this.chart = new Highcharts.Chart(chartOptions);
return this.chart;
}
我正在尝试创建柱形图xAxis显示所有数据yAxis显示对象我们有名称和值如果对象为空bind 0如果对象在那里我需要绑定
2条答案
按热度按时间14ifxucb1#
您需要创建一个x轴为类别的堆叠柱形图。最重要的是使您的数据适应Highcharts所需的系列结构。你可以用下面的方式来实现:
现场演示:http://jsfiddle.net/BlackLabel/mdv6rp2L/
API引用:https://api.highcharts.com/highcharts/series.column.data
lqfhib0f2#
不确定
container
和options
的值是什么,但根据您当前的代码,这应该可以解决问题。正如@ppotaczek的回答中提到的,您需要在结构中提供值,以便HighCharts能够正确呈现图表。
在下面代码的总结中,
keys
-从data.values
数组中的对象中提取所有属性名称。series
-从keys
数组转换为包含具有name
和value
属性的每个系列数据的对象数组。如果data.values
数组中的对象不包含属性(key
),则将value
默认为0。categories
- Categories是x轴的标签,应该使用data.dates
作为值。Demo @ StackBlitz