javascript - Highcharts 图表的默认值

标签 javascript highcharts

我们正在使用Highcharts用于仪表板 Web 应用程序中的图表。我们正在用 javaScript 创建图形对象。大多数图表都有相似的选项。例如。一张图是由这样的东西创建的:

Highcharts.chart('boiler-temp', {
    chart: {
        type: 'spline'
    },
    title: {
        text: 'Boiler temperatures'
    },         
    legend: { 
        layout: 'horizontal',
        align: 'center',
        verticalAlign: 'bottom'
    },
    yAxis: [{
        title: {
            text: 'Inner temperature'
        }
    }, {
        title: {
            text: 'Outer temperature',
        },
        opposite: true,
        min: 0,
        max: 100
    }],
    data: {
        rowsURL: dataUrl,
    },
    series: [{
        yAxis: 0,
    }, {
        yAxis: 1,
    }],
    plotOptions: {
        series: {
        connectNulls: true
        }
    },
    exporting: {
        fallbackToExportServer: false
    }
}

另一个图表的创建方式非常类似:

Highcharts.chart('boiler-temp', {
    chart: {
        type: 'spline'
    },
    title: {
        text: 'Boiler pressure'
    },         
    legend: { 
        layout: 'horizontal',
        align: 'center',
        verticalAlign: 'bottom'
    },
    yAxis: [{
        title: {
            text: 'Inner pressure'
        }
    }, {
        title: {
            text: 'Outer outer pressure',
        },
        opposite: true,
        min: 0,
        max: 100
    }],
    data: {
        rowsURL: dataUrl,
    },
    series: [{
        yAxis: 0,
    }, {
        yAxis: 1,
    }],
    plotOptions: {
        series: {
        connectNulls: true
        }
    },
    exporting: {
        fallbackToExportServer: false
    }
}

为了可维护性,我想定义一组默认选项为

{
    chart: {
        type: 'spline'
    },
    legend: { 
        layout: 'horizontal',
        align: 'center',
        verticalAlign: 'bottom'
    },
    yAxis: [{
    }, {
        opposite: true,
        min: 0,
        max: 100
    }],
    series: [{
        yAxis: 0,
    }, {
        yAxis: 1,
    }],
    plotOptions: {
        series: {
        connectNulls: true
        }
    },
    exporting: {
        fallbackToExportServer: false
    }
}

然后,当我实例化图表时,覆盖更改的内容。有没有办法用 Highcharts/JavaScript 做到这一点?

最佳答案

可以使用 Highcharts 全局选项来完成 - 同一页面上所有图表的选项集。

Highcharts.setOptions({
  chart: {
    type: 'spline'
  },
  legend: {
    layout: 'horizontal',
    align: 'center',
    verticalAlign: 'bottom'
  },
  yAxis: [{}, {
    opposite: true,
    min: 0,
    max: 100
  }],
  series: [{
    yAxis: 0,
  }, {
    yAxis: 1,
  }],
  plotOptions: {
    series: {
      connectNulls: true
    }
  },
  exporting: {
    fallbackToExportServer: false
  }
});

演示:

文档:

<小时/>

另一种方法是在应用程序中的某个位置创建 defaultOptions 并使用例如将它们与图表选项合并。 Highcharts.merge()

const defualtData = {
  chart: {
    type: 'spline'
  },
  legend: {
    layout: 'horizontal',
    align: 'center',
    verticalAlign: 'bottom'
  },
  yAxis: [{}, {
    opposite: true,
    min: 0,
    max: 100
  }],
  series: [{
    yAxis: 0,
  }, {
    yAxis: 1,
  }],
  plotOptions: {
    series: {
      connectNulls: true
    }
  },
  exporting: {
    fallbackToExportServer: false
  }
};

Highcharts.chart('boiler-temp1', Highcharts.merge(defualtData, {
  title: {
    text: 'Boiler temperatures'
  }
}));

演示:

API引用:

关于javascript - Highcharts 图表的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56286875/

相关文章:

jquery - HighCharts 通过 Json 加载数据

javascript - Highcharts:显示峰值

javascript - Highcharts 错误 #13 : www. highcharts.com/errors/13 如何解决此错误

javascript - 将 Highstock 缩放按钮放置在 <div> 内的图表区域之外

javascript - JSP x 谷歌图表 X MySQL : Multiple Charts Not Displaying Entire Information

javascript - 转换后的 JSON 数组的数量

javascript - 与过滤器的一次性绑定(bind)不起作用

javascript - Highcharts - 主详细图表中的多个主图表

javascript - 如何从 Meteor/React 的选择选项中获取值?

javascript - 未捕获的类型错误 : Cannot read property 'arc' of undefined