List of usage examples for org.jfree.chart.axis NumberAxis setRangeWithMargins
public void setRangeWithMargins(double lower, double upper)
From source file:no.met.jtimeseries.chart.ChartPlotter.java
/** * Add a line chart in the plot/*from www . j a va2 s . com*/ * * @param timeBase * The base time of the data: second, minute, hour, year or auto * @param phenomenon * Phenomenon to plot. * @param plotStyle * The style preference for the plot * */ public void addLineChart(TimeBase timeBase, NumberPhenomenon ph, PlotStyle plotStyle) { NumberPhenomenon phenomenon = ph.clone(); if (phenomenon.getItems().size() > 0) { XYItemRenderer renderer = RendererFactory.createRenderer(SplineStyle.STANDARD); // if using hybrid spline if (plotStyle.getSplineStyle().equalsIgnoreCase(SplineStyle.HYBRID)) { phenomenon.doHybridSpline(0.5d, 4); renderer = RendererFactory.createRenderer(SplineStyle.NONE); } if (plotStyle.getNonNegative()) { phenomenon.removeNegativeValues(); } XYDataset dataset = phenomenon.getTimeSeries(plotStyle.getTitle(), timeBase); renderer.setSeriesPaint(0, plotStyle.getSeriesColor()); renderer.setSeriesStroke(0, plotStyle.getStroke()); // hidden the legend by default renderer.setSeriesVisibleInLegend(0, false); plot.setDataset(plotIndex, dataset); plot.setRenderer(plotIndex, renderer); // render the range axis NumberAxis numberAxis; // null check for number axis if (plotStyle.getNumberAxis() == null) { numberAxis = new NumberAxis(plotStyle.getTitle()); numberAxis.setAutoRangeIncludesZero(false); numberAxis.setLabelPaint(plotStyle.getLabelColor()); numberAxis.setTickLabelPaint(plotStyle.getLabelColor()); // ugly calculation double max = phenomenon.getMaxValue(); double min = phenomenon.getMinValue(); // increase and decrease max, min respectiivly to get the // difference pf atleast 50 while ((max - min) <= plotStyle.getDifference()) { max++; min--; } int tUnit = (int) ((max - min) / plotStyle.getTotalTicks()); if (tUnit <= 1) { tUnit = 2; } int[] range = calculateAxisMaxMin(phenomenon, tUnit, plotStyle.getTotalTicks()); NumberTickUnit ntu = new NumberTickUnit(tUnit); numberAxis.setTickUnit(ntu); numberAxis.setRangeWithMargins(range[1], range[0]); } else { numberAxis = plotStyle.getNumberAxis(); } plot.setRangeAxis(rangeAxisIndex, numberAxis); Date minDate = phenomenon.getStartTime(); Date maxDate = phenomenon.getEndTime(); DateAxis domainAxis = (DateAxis) plot.getDomainAxis(); domainAxis.setRange(minDate, maxDate); plot.mapDatasetToRangeAxis(plotIndex, rangeAxisIndex); plotIndex++; rangeAxisIndex++; } }