Highcharts 配置语法

Highcharts 配置语法详解

Highcharts 的图表是通过一个大的 JavaScript 对象(options)来配置的,传入 Highcharts.chart(container, options) 方法中。这个对象结构清晰、层级分明,几乎所有图表外观和行为都通过它控制。

基本配置结构

Highcharts.chart('container', {
    // 1. 图表类型和全局设置
    chart: {
        type: 'line',              // 图表类型:line, column, pie, area, scatter 等
        backgroundColor: '#ffffff',// 背景色
        borderWidth: 1,            // 边框宽度
        zoomType: 'xy',            // 缩放类型:x, y, xy
        animation: true,           // 是否开启动画
        // ...
    },

    // 2. 标题
    title: {
        text: '主标题',
        align: 'center',           // left, center, right
        style: { fontSize: '20px' }
    },
    subtitle: {
        text: '副标题',
        align: 'center'
    },

    // 3. X 轴配置
    xAxis: {
        categories: ['一月', '二月', '三月', /* ... */],  // 分类轴标签
        title: { text: '月份' },
        labels: { rotation: -45 }, // 标签旋转
        // 或数值轴时用 type: 'linear', min, max 等
    },

    // 4. Y 轴配置(可多个)
    yAxis: {
        title: { text: '温度 (°C)' },
        opposite: false,           // 是否显示在右侧
        min: 0,
        // ...
    },

    // 5. 数据提示框(tooltip)
    tooltip: {
        shared: true,              // 多系列共享一个提示框
        crosshairs: true,          // 显示十字准线
        formatter: function () {   // 自定义格式
            return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '°C';
        }
    },

    // 6. 图例(legend)
    legend: {
        enabled: true,
        align: 'center',           // left, center, right
        verticalAlign: 'bottom',   // top, middle, bottom
        layout: 'horizontal'       // horizontal 或 vertical
    },

    // 7. 数据系列(最核心部分)
    series: [{
        name: '东京',
        data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6],
        color: '#ff0000',          // 系列颜色
        marker: { enabled: true }  // 数据点标记
    }, {
        name: '伦敦',
        data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
    }],

    // 8. 导出模块(需引入 exporting.js)
    exporting: {
        enabled: true,
        buttons: {
            contextButton: {
                menuItems: ['downloadPNG', 'downloadJPEG', 'downloadPDF', 'downloadSVG']
            }
        }
    },

    // 9. 响应式规则
    responsive: {
        rules: [{
            condition: { maxWidth: 500 },
            chartOptions: {
                legend: { enabled: false }
            }
        }]
    }
});

常用配置项速查表

配置项位置常见选项示例说明
chart.typechart‘line’, ‘column’, ‘pie’, ‘area’, ‘spline’图表类型
chart.eventschartload: function() { … }图表加载完成事件
plotOptions根级series: { animation: false }所有系列通用设置
series.type每个 seriestype: ‘column’覆盖全局类型,混合图表常用
dataLabelsplotOptions 或 seriesenabled: true, format: ‘{y} °C’数据标签显示
credits根级enabled: false隐藏右下角 Highcharts.com 水印
accessibility根级enabled: true无障碍支持

高级技巧

  1. 混合图表(不同系列不同类型):
   series: [{
       type: 'column',
       name: '柱状'
   }, {
       type: 'line',
       name: '折线'
   }]
  1. 多 Y 轴
   yAxis: [{
       title: { text: '温度' }
   }, {
       title: { text: '降雨量' },
       opposite: true
   }],
   series: [{ yAxis: 0 }, { yAxis: 1 }]
  1. 动态更新数据
   chart.series[0].setData([新数据数组]);
   chart.redraw();

推荐学习路径

  1. 先掌握以上核心结构(title、axes、series、tooltip、legend)。
  2. 查看官方 API 参考:https://api.highcharts.com/highcharts/
  • 每个配置项都有详细说明、默认值和示例。
  1. 使用 Highcharts 在线编辑器练习:https://www.highcharts.com/docs/working-with-highcharts/online-editor

如果你有具体需求(如配置饼图、仪表盘、热力图、时间轴等),或者想实现某个效果(如自定义主题、钻取功能),告诉我,我可以给出完整配置代码示例!

文章已创建 3511

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部