class SeriesUtils {

    protected static int iBoundsMax(XYSeries series, double visibleMax, float step) {
        int max = series.size() - 1;
        final int seriesSize = series.size();
        final int steps = (int) Math.ceil(seriesSize / step);
        for (int stepIndex = steps; stepIndex >= 0; stepIndex--) {
            final int i = stepIndex * (int) step;
            for (int ii = 0; ii < step; ii++) {
                final int iii = i + ii;
                if(iii < seriesSize) {
                    final Number thisX = series.getX(iii);
                    if (thisX != null) {
                        final double thisDouble = thisX.doubleValue();
                        if (thisDouble > visibleMax) {
                            // this is the smallest non-null value in this block, so skip
                            // to the next block:
                            max = iii;
                            break;
                        } else if (thisDouble == visibleMax) {
                            return iii;
                        } else {
                            return max;
                        }
                    }
                }
            }
        }
        return max;
    }

}