简体   繁体   English

创建图表后需要精确设置 Highcharts 轴最小值/最大值

[英]Need to precisely set Highcharts axis min/max after chart creation

Most of the time, our app wants to let Highcharts decide the y axis tick positions for a chart.大多数时候,我们的应用程序想让 Highcharts 决定图表的 y 轴刻度位置。 But the user can select a "Customize Range" command and specify custom min and max for the y axis after the chart has been created and shown.但用户可以选择“自定义范围”命令,并在创建并显示图表后为 y 轴指定自定义最小值和最大值。 Purpose: user may want to export charts that line up with other charts they've made.目的:用户可能想要导出与他们制作的其他图表对齐的图表。 I have tried a number of things including the following code, but Highcharts continues to follow its own preferences for the min, max and tickInterval (eg it does not respect {5,25} or {-5,35}. And my custom tickPositioner callback is not being called.我已经尝试了很多事情,包括以下代码,但 Highcharts 继续遵循自己对 min、max 和 tickInterval 的偏好(例如,它不尊重 {5,25} 或 {-5,35}。我的自定义 tickPositioner没有调用回调。

okClicked() {  
    this.axis1.startOnTick = false;
    this.axis1.endOnTick = false;   
    this.axis1.alignTicks = false;
    this.axis1.tickPositioner = this.getCustomTicks.bind(this, this.axis1, this.yMin1, this.yMax1);
    this.axis1.setExtremes(this.yMin1, this.yMax1);
    this.axis1.chart.redraw();
}

You can fire chart.update function after the button is clicked, and set axes options to whatever you want them to be:您可以在单击按钮后触发 chart.update 函数,并将轴选项设置为您想要的任何选项:

$('.btn').click(function() {
    Highcharts.each(Highcharts.charts, function(chart, i) {
        chart.yAxis[0].update({
            max: 25,
            min: 5,
            startOnTick: false,
            tickInterval: 5
        });
    });
});

http://jsfiddle.net/BlackLabel/obfudy23/ http://jsfiddle.net/BlackLabel/obfudy23/

API Reference: https://api.highcharts.com/class-reference/Highcharts.Chart#update API 参考: https : //api.highcharts.com/class-reference/Highcharts.Chart#update

It is possible to solve this issue by adding a simple input field to your html like this:可以通过向 html 添加一个简单的输入字段来解决这个问题,如下所示:

Min: <input id="min"> <br>
Max: <input id="max">
<button id="update">Update y-axis </button>

Then we need a function which activates on click of the button.然后我们需要一个通过点击按钮激活的功能。 Inside this function we will update the Highcharts chart.在此函数中,我们将更新 Highcharts 图表。

$('#update').click(function () {
    chart.update({
       yAxis: {
           min: document.getElementById("min").value,
           max: document.getElementById("max").value,
           tickInterval: 5 //do calculations here to get needed tickInterval
       }
    })
});

As shown in this JSFiddle , it alters the y-axis.如这个JSFiddle所示,它改变了 y 轴。 You can extend the update function however you like, just override the previous settings like happened in the code shown above.您可以随心所欲地扩展更新功能,只需像上面显示的代码中那样覆盖以前的设置即可。

On a sidenote: It has to be noted that in the fiddle the y-axis can be off at times, depending on the inserted values.旁注:必须注意的是,在小提琴中,y 轴有时可能会关闭,具体取决于插入的值。 The tickInterval should be dynamically calculated for this to work.应动态计算 tickInterval 以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM