ingo@2237: package de.intevation.flys.exports;
ingo@2237:
ingo@2237: import org.jfree.data.Range;
ingo@2237: import org.jfree.data.xy.XYDataset;
ingo@2237:
ingo@2241: import org.apache.log4j.Logger;
ingo@2241:
ingo@2237:
ingo@2237: /**
ingo@2237: * @author Ingo Weinzierl
ingo@2237: */
ingo@2237: public class ChartHelper {
ingo@2237:
ingo@2241: private static final Logger logger = Logger.getLogger(ChartHelper.class);
ingo@2241:
ingo@2241:
ingo@2237: /**
ingo@2237: * This method returns the ranges of the XYDataset dataset as array
ingo@2237: * with [xrange, yrange].
ingo@2237: *
ingo@2237: * @param dataset The dataset which should be evaluated.
ingo@2237: *
ingo@2237: * @return an array with x and y ranges.
ingo@2237: */
ingo@2237: public static Range[] getRanges(XYDataset dataset) {
ingo@2241: int seriesCount = dataset != null ? dataset.getSeriesCount() : 0;
ingo@2241:
ingo@2241: if (seriesCount == 0) {
ingo@2241: logger.warn("Dataset is empty or has no Series set.");
ingo@2237: return null;
ingo@2237: }
ingo@2237:
ingo@2241: boolean foundValue = false;
ingo@2241:
ingo@2237: double minX = Double.MAX_VALUE;
ingo@2237: double maxX = -Double.MAX_VALUE;
ingo@2237: double minY = Double.MAX_VALUE;
ingo@2237: double maxY = -Double.MAX_VALUE;
ingo@2237:
ingo@2241: for (int i = 0, m = seriesCount; i < m; i++) {
ingo@2237: for (int j = 0, n = dataset.getItemCount(i); j < n; j++) {
ingo@2237: double x = dataset.getXValue(i, j);
ingo@2237: double y = dataset.getYValue(i, j);
ingo@2237:
ingo@2241: if (Double.isNaN(x) || Double.isNaN(y)) {
ingo@2241: logger.warn("Item " + j + " in Series " + i + " is broken");
ingo@2241: continue;
ingo@2241: }
ingo@2241:
ingo@2241: foundValue = true;
ingo@2241:
ingo@2237: if (x < minX) {
ingo@2237: minX = x;
ingo@2237: }
ingo@2237:
ingo@2237: if (x > maxX) {
ingo@2237: maxX = x;
ingo@2237: }
ingo@2237:
ingo@2237: if (y < minY) {
ingo@2237: minY = y;
ingo@2237: }
ingo@2237:
ingo@2237: if (y > maxY) {
ingo@2237: maxY = y;
ingo@2237: }
ingo@2237: }
ingo@2237: }
ingo@2237:
ingo@2241: return foundValue
ingo@2241: ? new Range[] { new Range(minX, maxX), new Range(minY, maxY) }
ingo@2241: : null;
ingo@2241: }
ingo@2241:
ingo@2241:
ingo@2241: /**
ingo@2241: * Expand range by percent.
ingo@2241: *
ingo@2241: * @param range The range to expand.
ingo@2241: * @param percent The percentage to expand.
ingo@2241: *
ingo@2241: * @param an expanded range.
ingo@2241: */
ingo@2241: public static Range expandRange(Range range, double percent) {
ingo@2241: if (range == null) {
ingo@2241: return null;
ingo@2241: }
ingo@2241:
ingo@2241: double value = range.getLowerBound();
ingo@2241: double expand = Math.abs(value / 100 * percent);
ingo@2241:
ingo@2241: return expand != 0
ingo@2241: ? new Range(value-expand, value+expand)
ingo@2241: : new Range(-0.01 * percent, 0.01 * percent);
ingo@2237: }
ingo@2237: }
ingo@2237: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :