You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于分组数据集创建动态Highcharts图表的技术问询

实现每个国家独立的Highcharts图表方案

需求说明

现有如下格式的数据集:

var data = [{
    "France": {
        "val1": [10, 20, 30, 40],
        "val2": [10, 20, 30, 4]
    },
    "Croatia": {
        "val1": [50, 60, 70, 80],
        "val2": [50, 60, 70, 8]}
}];

需要为France和Croatia分别创建独立的Highcharts图表,每个图表展示对应国家的val1和val2数据。

原代码的问题分析

  1. 循环逻辑错误:原代码遍历data数组(长度为1),但循环条件i <= data.length会导致i=1时访问data[1],出现undefined报错;且遍历目标错误,应该遍历的是数据中的国家列表,而非外层数组。
  2. 图表数据未对应单个国家:每个图表的series同时包含两个国家的数据,不符合“每个国家独立图表”的需求。
  3. 容器样式缺失:CSS仅设置了#container的高度,未对#container1和#container2设置高度,导致图表无法正常显示。

正确实现代码

HTML部分

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>

<div id="container1"></div>
<div id="container2"></div>

CSS部分

.highcharts-figure,
.highcharts-data-table table {
    min-width: 310px;
    max-width: 800px;
    margin: 1em auto;
}

/* 为所有图表容器设置高度 */
#container1, #container2 {
    height: 400px;
}

.highcharts-data-table table {
    font-family: Verdana, sans-serif;
    border-collapse: collapse;
    border: 1px solid #ebebeb;
    margin: 10px auto;
    text-align: center;
    width: 100%;
    max-width: 500px;
}

.highcharts-data-table caption {
    padding: 1em 0;
    font-size: 1.2em;
    color: #555;
}

.highcharts-data-table th {
    font-weight: 600;
    padding: 0.5em;
}

.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
    padding: 0.5em;
}

.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
    background: #f8f8f8;
}

.highcharts-data-table tr:hover {
    background: #f1f7ff;
}

JavaScript部分

var data = [{
    "France": {
        "val1": [10, 20, 30, 40],
        "val2": [10, 20, 30, 4]
    },
    "Croatia": {
        "val1": [50, 60, 70, 80],
        "val2": [50, 60, 70, 8]}
}];

// 获取数据中的国家列表
const countries = Object.keys(data[0]);

// 遍历每个国家创建图表
countries.forEach((country, index) => {
    const containerName = `container${index + 1}`;
    const countryData = data[0][country];

    Highcharts.chart(containerName, {
        chart: {
            type: 'column'
        },
        title: {
            text: `${country} Monthly Average Rainfall`
        },
        subtitle: {
            text: 'Source: WorldClimate.com'
        },
        xAxis: {
            // 只保留前4个月份,匹配数据长度
            categories: ['Jan', 'Feb', 'Mar', 'Apr'],
            crosshair: true
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Rainfall (mm)'
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: [
            {
                name: `${country} val1`,
                data: countryData.val1
            },
            {
                name: `${country} val2`,
                data: countryData.val2
            }
        ]
    });
});

关键修改点

  • 改用Object.keys(data[0])获取国家列表,遍历每个国家生成独立图表。
  • 每个图表的标题、series数据都对应当前遍历的国家,确保数据独立。
  • 调整x轴的categories数量,匹配数据数组的长度(原数据只有4个值,对应前4个月)。
  • 修正CSS,为所有容器设置高度,保证图表正常渲染。

内容的提问来源于stack exchange,提问作者user8512043

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:40:34