flot: How to count the number of y-axes

2019-08-15 00:46发布

问题:

In my project I declare some y-axes (temperature, humidity, etc) for line-charts. but I can show\hide some of the series.

I want to highlight the y-axes of tooltip item only if there are some y-axes on the screen

I thought I could use

  var isMultyYaxes = ( $(".flot-y-axis").length > 1 );
  if (isMultyYaxes) {
    $(".y" + item.series.yaxis.n + "Axis").addClass("myActiveAxis"); 
  }

but it seems there are duplicate y-axes in $(".flot-y-axis").

Should I use -

  var isMultyYaxes = ( $(".flot-y-axis").length > 2 );

Is there another way to determine how many y-axes are currently used?

回答1:

If you have stored a reference to your plot-object (with var plot = $.plot(...);) then you can get the options and the axes like this:

var yAxesCountAll = plot.getOptions().yaxes.length;

Then you can check if the axes are shown like this:

var yAxesCountVisible = 0;
for (var i = 1; i <= yAxescountAll; i++) {
    if (plot.getAxes()['y' + (i == 1 ? '' : i.toString()) + 'axis'].show) {
        yAxesCountvisible++;
    }
}


标签: flot