javascript - Highcharts - 具有通用代码和唯一数据和标题的全局配置

标签 javascript charts highcharts

我正在使用 Highcharts。

在单个页面中,我有多个具有不同数据和标题以及相同图表属性配置的Area 图表。

Online Demo

图一到图二、图三的变化如下:

  • 系列名称
  • 系列数据
  • 传奇
  • 工具提示数据

除了上面,其他都是一样的...

如何避免为 #chart-1、#chart-2、#chart-3 等编写重复代码...并仅配置以上元素?

HTML:

<div id="chart-1" style="height: 200px; width: 500px"></div>
<div id="chart-2" style="height: 200px; width: 500px"></div>
<div id="chart-3" style="height: 200px; width: 500px"></div>

全局初始化:

jQuery(function () {

    Highcharts.setOptions({
        colors: ['#fe5758', '#99cc03', '#33b5e6'],
        chart: {
            style: {
                fontFamily: 'Roboto Light',
                fontWeight: 'normal',
                fontSize: '12px',
                color: '#585858',
            }
        },      
        title: { text: null },
        subtitle: { text:null },
        credits: { enabled: false },
        exporting: { enabled: false },
......

图表 1:

$(function () {
    $('#chart-1').highcharts({
        chart: {
          type: 'area'
        },
        xAxis: {
            ....

图表 2:

$(function () {
    $('#chart-2').highcharts({
        chart: {
          type: 'area'
        },
        xAxis: {
            ....

最佳答案

您可以为共同的选项创建一个变量,例如:

var commonOptions = {
    chart: {
        type: 'area'
    },
    // ...
};

然后像这样合并每个图表的特定选项:

$('#container').highcharts(Highcharts.merge(commonOptions, {
    series: [{
        name: 'Tokyo',
        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]
    }]
    // ...
}));

参见 this JSFiddle example带有图例、工具提示和系列选项。

关于javascript - Highcharts - 具有通用代码和唯一数据和标题的全局配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36423854/

相关文章:

javascript - 我想在使用 amCharts 创建的图表中显示菱形图标

javascript - 如何在选择过程中禁用工具提示?

highcharts - 如何设置 xAxis pointInterval(更新 : tickInterval) in highcharts

javascript - highcharts 如何自动扩展列

javascript - 搜索表的特定列以突出显示表数据

javascript - 提交时重置 div 内容

javascript - 如何使用 for 循环将 Json 对象推送到 Json 数组?

javascript - 如何远程模拟下拉选择

javascript - 将鼠标悬停在 chartjs 饼图上时 Canvas 填充文本消失

java - 请在 Charts4j 上发布一些教程链接