简体   繁体   English

根据 ajax 更新和 Highcharts 中的数据点动态调整 Y 轴最小值/最大值

[英]Dynamically adjust Y axis min/max based on ajax update and data points in Highcharts

Currently, my graphs are created and then I have the following ajax call that creates the plotLines on the graph:目前,我的图表已创建,然后我有以下 ajax 调用,该调用在图表上创建 plotLines:

             $.ajax({
                type: "GET",
                url: "/limits",
                dataType: 'json',
                success: function (limits) {
                    console.log(limits);
                    graph1.yAxis[0].update({
                        plotLines: [{
                            value: limits[7].max,
                            color: 'orange',
                            dashStyle: 'dash',
                            width: 2,
                            label: {
                                text: 'USL'
                            }
                        }, {
                            value: limits[7].min,
                            color: 'orange',
                            dashStyle: 'dash',
                            width: 2,
                            label: {
                                text: 'LSL'
                            }
                        }]
                    });

The problem is that for some graphs the plotLines are out of Highcharts determined y-axis range (created from the data) so the plotLines do not show on the graphs.问题在于,对于某些图表,plotLines 超出了 Highcharts 确定的 y 轴范围(根据数据创建),因此 plotLines 不会显示在图表上。

I know I could do this to fix that problem:我知道我可以这样做来解决这个问题:

             $.ajax({
                    type: "GET",
                    url: "/limits",
                    dataType: 'json',
                    success: function (limits) {
                        console.log(limits);
                        graph1.yAxis[0].update({
                            min: limits[7].min,
                            max: limits[7].max,
                            plotLines: [{
                                value: limits[7].max,
                                color: 'orange',
                                dashStyle: 'dash',
                                width: 2,
                                label: {
                                    text: 'USL'
                                }
                            }, {
                                value: limits[7].min,
                                color: 'orange',
                                dashStyle: 'dash',
                                width: 2,
                                label: {
                                    text: 'LSL'
                                }
                            }]
                        });

However.. if I was to do that solution then if the graph has points outside of the plotLines, then those points would not be shown.但是..如果我要执行该解决方案,那么如果图形在 plotLines 之外有点,则不会显示这些点。

I figure I could do something like this:我想我可以做这样的事情:

chart: {
  events: {
    load: function() {
      var min = yAxis[0].min;
      var max = yAxis[0].max;
      if (limits[7].max > max) {
        yAxis[0].setExtremes(null, (limits[7].max);
      }
    }
  }
}

I'm not entirely sure how to incorporate that type of logic for both the max and min plotLines into the update function.我不完全确定如何将最大和最小 plotLines 的这种类型的逻辑合并到更新 function 中。

Any help would be very appreciated.任何帮助将不胜感激。 Thanks.谢谢。

You can compare the plot line value to the actual axis extremes and change min and max properties if necessary.您可以将 plot 线值与实际轴极值进行比较,并在必要时更改最小值和最大值属性。 For example:例如:

yAxis.update({
  min: yAxis.min > limits.min ? limits.min : null,
  max: yAxis.max < limits.max ? limits.max : null,
  plotLines: [{
    value: limits.max,
    width: 2,
    color: 'orange'
  }, {
    value: limits.min,
    width: 2,
    color: 'blue'
  }]
});

Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/4989/现场演示: http://jsfiddle.net/BlackLabel/6m4e8x0y/4989/

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

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