Example usage for org.jfree.data.xy XYDataset getDomainOrder

List of usage examples for org.jfree.data.xy XYDataset getDomainOrder

Introduction

In this page you can find the example usage for org.jfree.data.xy XYDataset getDomainOrder.

Prototype

public DomainOrder getDomainOrder();

Source Link

Document

Returns the order of the domain (or X) values returned by the dataset.

Usage

From source file:org.jfree.data.general.DatasetUtils.java

/**
 * Finds the indices of the the items in the dataset that span the 
 * specified x-value.  There are three cases for the return value:
 * <ul>/*  w ww  . j  a  v  a2s  .co m*/
 * <li>there is an exact match for the x-value at index i 
 * (returns {@code int[] {i, i}});</li>
 * <li>the x-value falls between two (adjacent) items at index i and i+1 
 * (returns {@code int[] {i, i+1}});</li>
 * <li>the x-value falls outside the domain bounds, in which case the 
 *    method returns {@code int[] {-1, -1}}.</li>
 * </ul>
 * @param dataset  the dataset ({@code null} not permitted).
 * @param series  the series index.
 * @param x  the x-value.
 *
 * @return The indices of the two items that span the x-value.
 *
 * @since 1.0.16
 * 
 * @see #findYValue(org.jfree.data.xy.XYDataset, int, double) 
 */
public static int[] findItemIndicesForX(XYDataset dataset, int series, double x) {
    Args.nullNotPermitted(dataset, "dataset");
    int itemCount = dataset.getItemCount(series);
    if (itemCount == 0) {
        return new int[] { -1, -1 };
    }
    if (itemCount == 1) {
        if (x == dataset.getXValue(series, 0)) {
            return new int[] { 0, 0 };
        } else {
            return new int[] { -1, -1 };
        }
    }
    if (dataset.getDomainOrder() == DomainOrder.ASCENDING) {
        int low = 0;
        int high = itemCount - 1;
        double lowValue = dataset.getXValue(series, low);
        if (lowValue > x) {
            return new int[] { -1, -1 };
        }
        if (lowValue == x) {
            return new int[] { low, low };
        }
        double highValue = dataset.getXValue(series, high);
        if (highValue < x) {
            return new int[] { -1, -1 };
        }
        if (highValue == x) {
            return new int[] { high, high };
        }
        int mid = (low + high) / 2;
        while (high - low > 1) {
            double midV = dataset.getXValue(series, mid);
            if (x == midV) {
                return new int[] { mid, mid };
            }
            if (midV < x) {
                low = mid;
            } else {
                high = mid;
            }
            mid = (low + high) / 2;
        }
        return new int[] { low, high };
    } else if (dataset.getDomainOrder() == DomainOrder.DESCENDING) {
        int high = 0;
        int low = itemCount - 1;
        double lowValue = dataset.getXValue(series, low);
        if (lowValue > x) {
            return new int[] { -1, -1 };
        }
        double highValue = dataset.getXValue(series, high);
        if (highValue < x) {
            return new int[] { -1, -1 };
        }
        int mid = (low + high) / 2;
        while (high - low > 1) {
            double midV = dataset.getXValue(series, mid);
            if (x == midV) {
                return new int[] { mid, mid };
            }
            if (midV < x) {
                low = mid;
            } else {
                high = mid;
            }
            mid = (low + high) / 2;
        }
        return new int[] { low, high };
    } else {
        // we don't know anything about the ordering of the x-values,
        // so we iterate until we find the first crossing of x (if any)
        // we know there are at least 2 items in the series at this point
        double prev = dataset.getXValue(series, 0);
        if (x == prev) {
            return new int[] { 0, 0 }; // exact match on first item
        }
        for (int i = 1; i < itemCount; i++) {
            double next = dataset.getXValue(series, i);
            if (x == next) {
                return new int[] { i, i }; // exact match
            }
            if ((x > prev && x < next) || (x < prev && x > next)) {
                return new int[] { i - 1, i }; // spanning match
            }
        }
        return new int[] { -1, -1 }; // no crossing of x
    }
}

From source file:org.jfree.data.general.DatasetUtilities.java

/**
 * Finds the indices of the the items in the dataset that span the 
 * specified x-value.  There are three cases for the return value:
 * <ul>/*from w  w  w  .ja  v a2s  .c om*/
 * <li>there is an exact match for the x-value at index i 
 * (returns <code>int[] {i, i}</code>);</li>
 * <li>the x-value falls between two (adjacent) items at index i and i+1 
 * (returns <code>int[] {i, i+1}</code>);</li>
 * <li>the x-value falls outside the domain bounds, in which case the 
 *    method returns <code>int[] {-1, -1}</code>.</li>
 * </ul>
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param series  the series index.
 * @param x  the x-value.
 *
 * @return The indices of the two items that span the x-value.
 *
 * @since 1.0.16
 * 
 * @see #findYValue(org.jfree.data.xy.XYDataset, int, double) 
 */
public static int[] findItemIndicesForX(XYDataset dataset, int series, double x) {
    ParamChecks.nullNotPermitted(dataset, "dataset");
    int itemCount = dataset.getItemCount(series);
    if (itemCount == 0) {
        return new int[] { -1, -1 };
    }
    if (itemCount == 1) {
        if (x == dataset.getXValue(series, 0)) {
            return new int[] { 0, 0 };
        } else {
            return new int[] { -1, -1 };
        }
    }
    if (dataset.getDomainOrder() == DomainOrder.ASCENDING) {
        int low = 0;
        int high = itemCount - 1;
        double lowValue = dataset.getXValue(series, low);
        if (lowValue > x) {
            return new int[] { -1, -1 };
        }
        if (lowValue == x) {
            return new int[] { low, low };
        }
        double highValue = dataset.getXValue(series, high);
        if (highValue < x) {
            return new int[] { -1, -1 };
        }
        if (highValue == x) {
            return new int[] { high, high };
        }
        int mid = (low + high) / 2;
        while (high - low > 1) {
            double midV = dataset.getXValue(series, mid);
            if (x == midV) {
                return new int[] { mid, mid };
            }
            if (midV < x) {
                low = mid;
            } else {
                high = mid;
            }
            mid = (low + high) / 2;
        }
        return new int[] { low, high };
    } else if (dataset.getDomainOrder() == DomainOrder.DESCENDING) {
        int high = 0;
        int low = itemCount - 1;
        double lowValue = dataset.getXValue(series, low);
        if (lowValue > x) {
            return new int[] { -1, -1 };
        }
        double highValue = dataset.getXValue(series, high);
        if (highValue < x) {
            return new int[] { -1, -1 };
        }
        int mid = (low + high) / 2;
        while (high - low > 1) {
            double midV = dataset.getXValue(series, mid);
            if (x == midV) {
                return new int[] { mid, mid };
            }
            if (midV < x) {
                low = mid;
            } else {
                high = mid;
            }
            mid = (low + high) / 2;
        }
        return new int[] { low, high };
    } else {
        // we don't know anything about the ordering of the x-values,
        // so we iterate until we find the first crossing of x (if any)
        // we know there are at least 2 items in the series at this point
        double prev = dataset.getXValue(series, 0);
        if (x == prev) {
            return new int[] { 0, 0 }; // exact match on first item
        }
        for (int i = 1; i < itemCount; i++) {
            double next = dataset.getXValue(series, i);
            if (x == next) {
                return new int[] { i, i }; // exact match
            }
            if ((x > prev && x < next) || (x < prev && x > next)) {
                return new int[] { i - 1, i }; // spanning match
            }
        }
        return new int[] { -1, -1 }; // no crossing of x
    }
}