英文:
Creating a dynamic column chart with Highcharts and Angular to visualize object data
问题
我有这个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) // 过滤掉空对象
.map((key) => key); // 提取名称
chartDataObject.data = Object.values(data.values)
.filter((value) => Object.keys(value).length !== 0) // 过滤掉空对象
.map((value) => (value ? Object.values(value)[0] : 0)); // 提取数值
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;
}
我尝试创建柱状图,X轴显示所有数据,Y轴显示对象。如果对象为空,绑定0;如果对象存在,我需要绑定其值。
英文:
I have this is json response
{
"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,
},
{}
],
}
can you help me how to bind object to column. currently I am doing like this:
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;
}
I am try to create column chart xAxis display all the data yAxis display object
that object we have name and value if object is empty bind 0 if object is there i need to bind
答案1
得分: 1
你需要创建一个带有x轴类别的堆叠柱状图。最重要的是要将数据适应Highcharts所需的系列结构。你可以像下面的方式进行:
const series = [];
repsonse.values.forEach((values, xVal) => {
Object.keys(values).forEach((key, index) => {
if (!series[index]) {
series.push({
data: []
});
}
series[index].data.push({
name: key,
x: xVal,
y: values[key]
});
});
});
Highcharts.chart('container', {
...,
series,
xAxis: {
categories: repsonse.dates,
min: 0,
max: repsonse.dates.length - 1
}
});
Live demo: http://jsfiddle.net/BlackLabel/mdv6rp2L/
API Reference: https://api.highcharts.com/highcharts/series.column.data
英文:
You need to create a stacked column chart with categories on x-axis. The most important thing is to adapt your data to the series structure required by Highcharts. You can do that for example in the way below:
const series = [];
repsonse.values.forEach((values, xVal) => {
Object.keys(values).forEach((key, index) => {
if (!series[index]) {
series.push({
data: []
});
}
series[index].data.push({
name: key,
x: xVal,
y: values[key]
});
});
});
Highcharts.chart('container', {
...,
series,
xAxis: {
categories: repsonse.dates,
min: 0,
max: repsonse.dates.length - 1
}
});
Live demo: http://jsfiddle.net/BlackLabel/mdv6rp2L/
API Reference: https://api.highcharts.com/highcharts/series.column.data
答案2
得分: 0
不确定 `container` 和 `options` 的值是什么,但根据你当前的代码,这应该解决问题。
如 @ppotaczek 的答案中所述,你需要以结构的方式提供值,以便 HighCharts 能够正确渲染图表。
总结下面的代码,
1. `keys` - 从 `data.values` 数组中的对象中提取所有属性名称。
2. `series` - 从 `keys` 数组转换为一个包含每个系列数据的对象数组,具有 `name` 和 `value` 属性。如果 `data.values` 数组中的对象不包含该属性 (`key`),则默认将 `value` 设置为 0。
3. `categories` - 类别是 x 轴的标签,应该使用 `data.dates` 作为值。
英文:
Unsure what are the values for container
and options
, but this should resolve the issue according to your current code.
As mentioned in @ppotaczek's answer, you need to provide the value in structure so that HighCharts is able to render the chart correctly.
In summary of the below code,
-
keys
- Extract all the property names from the object in thedata.values
array. -
series
- Transform from thekeys
array into an array of objects containing each series data withname
andvalue
properties. Default thevalue
as 0 if the object in thedata.values
array doesn't contain the property (key
). -
categories
- Categories are the label for the x-axis, which should usedata.dates
as a value.
createChart(container: any, options: any[], chartName = null) {
const chartOptions = this.getOptions();
const chartData: any = [];
const keys = this.data.values
.map((x: any) => Object.keys(x))
.reduce((acc: any[], cur: any[]) => [...acc, ...cur], [] as any[]);
chartOptions.series = keys.map(
(key: any) =>
({
name: key,
data: this.data.values.map((y: any) => y[key] ?? 0),
//color: /* Your color value */,
//fillColor: /* Your fillColor setting object */,
} as any)
);
(<AxisOptions>chartOptions.xAxis)!.categories = Array.from(this.data.dates);
const element = document.createElement('div');
container.appendChild(element);
chartOptions.chart!['renderTo'] = element;
this.chart = new Highcharts.Chart(chartOptions);
return this.chart;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论