From: <mu...@us...> - 2007-09-27 10:06:15
|
Revision: 194 http://jfreechart.svn.sourceforge.net/jfreechart/?rev=194&view=rev Author: mungady Date: 2007-09-27 03:06:13 -0700 (Thu, 27 Sep 2007) Log Message: ----------- 2007-09-27 David Gilbert <dav...@ob...> * source/org/jfree/chart/axis/CategoryAxis.java (getCategorySeriesMiddle): New method, * source/org/jfree/chart/renderer/category/LineAndShapeRenderer.java (useSeriesOffset): New field, (itemMargin): Likewise, (LineAndShapeRenderer(boolean, boolean)): Initialise new fields, (getUseSeriesOffset): New method, (setUseSeriesOffset): Likewise, (getItemMargin): Likewise, (setItemMargin): Likewise, (drawItem): Apply new offset option to x-coordinates, (equals): Check new fields, * tests/org/jfree/chart/renderer/category/junit/LineAndShapeRendererTests.java (testEquals): Updated for new fields. Modified Paths: -------------- branches/jfreechart-1.0.x-branch/ChangeLog branches/jfreechart-1.0.x-branch/source/org/jfree/chart/axis/CategoryAxis.java branches/jfreechart-1.0.x-branch/source/org/jfree/chart/renderer/category/LineAndShapeRenderer.java Modified: branches/jfreechart-1.0.x-branch/ChangeLog =================================================================== --- branches/jfreechart-1.0.x-branch/ChangeLog 2007-09-26 16:04:29 UTC (rev 193) +++ branches/jfreechart-1.0.x-branch/ChangeLog 2007-09-27 10:06:13 UTC (rev 194) @@ -1,3 +1,20 @@ +2007-09-27 David Gilbert <dav...@ob...> + + * source/org/jfree/chart/axis/CategoryAxis.java + (getCategorySeriesMiddle): New method, + * source/org/jfree/chart/renderer/category/LineAndShapeRenderer.java + (useSeriesOffset): New field, + (itemMargin): Likewise, + (LineAndShapeRenderer(boolean, boolean)): Initialise new fields, + (getUseSeriesOffset): New method, + (setUseSeriesOffset): Likewise, + (getItemMargin): Likewise, + (setItemMargin): Likewise, + (drawItem): Apply new offset option to x-coordinates, + (equals): Check new fields, + * tests/org/jfree/chart/renderer/category/junit/LineAndShapeRendererTests.java + (testEquals): Updated for new fields. + 2007-09-26 David Gilbert <dav...@ob...> * source/org/jfree/chart/renderer/category/StatisticalLineAndShapeRenderer.java Modified: branches/jfreechart-1.0.x-branch/source/org/jfree/chart/axis/CategoryAxis.java =================================================================== --- branches/jfreechart-1.0.x-branch/source/org/jfree/chart/axis/CategoryAxis.java 2007-09-26 16:04:29 UTC (rev 193) +++ branches/jfreechart-1.0.x-branch/source/org/jfree/chart/axis/CategoryAxis.java 2007-09-27 10:06:13 UTC (rev 194) @@ -83,6 +83,7 @@ * 30-Oct-2006 : Updated refreshTicks() method to account for possibility of * multiple domain axes (DG); * 07-Mar-2007 : Fixed bug in axis label positioning (DG); + * 27-Sep-2007 : Added getCategorySeriesMiddle() method (DG); * */ @@ -110,6 +111,7 @@ import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.Plot; import org.jfree.chart.plot.PlotRenderingInfo; +import org.jfree.data.category.CategoryDataset; import org.jfree.io.SerialUtilities; import org.jfree.text.G2TextMeasurer; import org.jfree.text.TextBlock; @@ -645,6 +647,43 @@ + calculateCategorySize(categoryCount, area, edge); } + + /** + * Returns the middle coordinate (in Java2D space) for a series within a + * category. + * + * @param category the category (<code>null</code> not permitted). + * @param seriesKey the series key (<code>null</code> not permitted). + * @param dataset the dataset (<code>null</code> not permitted). + * @param itemMargin the item margin (0.0 <= itemMargin < 1.0); + * @param area the area (<code>null</code> not permitted). + * @param edge the edge (<code>null</code> not permitted). + * + * @return The coordinate in Java2D space. + * + * @since 1.0.7 + */ + public double getCategorySeriesMiddle(Comparable category, + Comparable seriesKey, CategoryDataset dataset, double itemMargin, + Rectangle2D area, RectangleEdge edge) { + + int categoryIndex = dataset.getColumnIndex(category); + int categoryCount = dataset.getColumnCount(); + int seriesIndex = dataset.getRowIndex(seriesKey); + int seriesCount = dataset.getRowCount(); + double start = getCategoryStart(categoryIndex, categoryCount, area, + edge); + double end = getCategoryEnd(categoryIndex, categoryCount, area, edge); + double width = end - start; + if (seriesCount == 1) { + return start + width / 2.0; + } + else { + double gap = (width * itemMargin) / (seriesCount - 1); + double ww = (width * (1 - itemMargin)) / seriesCount; + return start + (seriesIndex * (ww + gap)) + ww / 2.0; + } + } /** * Calculates the size (width or height, depending on the location of the Modified: branches/jfreechart-1.0.x-branch/source/org/jfree/chart/renderer/category/LineAndShapeRenderer.java =================================================================== --- branches/jfreechart-1.0.x-branch/source/org/jfree/chart/renderer/category/LineAndShapeRenderer.java 2007-09-26 16:04:29 UTC (rev 193) +++ branches/jfreechart-1.0.x-branch/source/org/jfree/chart/renderer/category/LineAndShapeRenderer.java 2007-09-27 10:06:13 UTC (rev 194) @@ -86,6 +86,7 @@ * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG); * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); * 24-Sep-2007 : Deprecated redundant fields/methods (DG); + * 27-Sep-2007 : Added option to offset series x-position within category (DG); * */ @@ -189,8 +190,24 @@ * outlines - if not, the regular series paint is used. */ private boolean useOutlinePaint; + + /** + * A flag that controls whether or not the x-position for each item is + * offset within the category according to the series. + * + * @since 1.0.7 + */ + private boolean useSeriesOffset; /** + * The item margin used for series offsetting - this allows the positioning + * to match the bar positions of the {@link BarRenderer} class. + * + * @since 1.0.7 + */ + private double itemMargin; + + /** * Creates a renderer with both lines and shapes visible by default. */ public LineAndShapeRenderer() { @@ -217,6 +234,8 @@ this.useFillPaint = false; this.drawOutlines = true; this.useOutlinePaint = false; + this.useSeriesOffset = false; // preserves old behaviour + this.itemMargin = 0.0; } // LINES VISIBLE @@ -722,6 +741,73 @@ } /** + * Returns the flag that controls whether or not the x-position for each + * data item is offset within the category according to the series. + * + * @return A boolean. + * + * @see #setUseSeriesOffset(boolean) + * + * @since 1.0.7 + */ + public boolean getUseSeriesOffset() { + return this.useSeriesOffset; + } + + /** + * Sets the flag that controls whether or not the x-position for each + * data item is offset within its category according to the series, and + * sends a {@link RendererChangeEvent} to all registered listeners. + * + * @param offset the offset. + * + * @see #getUseSeriesOffset() + * + * @since 1.0.7 + */ + public void setUseSeriesOffset(boolean offset) { + this.useSeriesOffset = offset; + notifyListeners(new RendererChangeEvent(this)); + } + + /** + * Returns the item margin, which is the gap between items within a + * category (expressed as a percentage of the overall category width). + * This can be used to match the offset alignment with the bars drawn by + * a {@link BarRenderer}). + * + * @return The item margin. + * + * @see #setItemMargin(double) + * @see #getUseSeriesOffset() + * + * @since 1.0.7 + */ + public double getItemMargin() { + return this.itemMargin; + } + + /** + * Sets the item margin, which is the gap between items within a category + * (expressed as a percentage of the overall category width), and sends + * a {@link RendererChangeEvent} to all registered listeners. + * + * @param margin the margin (0.0 <= margin < 1.0). + * + * @see #getItemMargin() + * @see #getUseSeriesOffset() + * + * @since 1.0.7 + */ + public void setItemMargin(double margin) { + if (margin < 0.0 || margin >= 1.0) { + throw new IllegalArgumentException("Requires 0.0 <= margin < 1.0."); + } + this.itemMargin = margin; + notifyListeners(new RendererChangeEvent(this)); + } + + /** * Returns a legend item for a series. * * @param datasetIndex the dataset index (zero-based). @@ -824,8 +910,16 @@ PlotOrientation orientation = plot.getOrientation(); // current data point... - double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), - dataArea, plot.getDomainAxisEdge()); + double x1; + if (this.useSeriesOffset) { + x1 = domainAxis.getCategorySeriesMiddle(dataset.getColumnKey( + column), dataset.getRowKey(row), dataset, this.itemMargin, + dataArea, plot.getDomainAxisEdge()); + } + else { + x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), + dataArea, plot.getDomainAxisEdge()); + } double value = v.doubleValue(); double y1 = rangeAxis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge()); @@ -836,9 +930,19 @@ if (previousValue != null) { // previous data point... double previous = previousValue.doubleValue(); - double x0 = domainAxis.getCategoryMiddle(column - 1, - getColumnCount(), dataArea, - plot.getDomainAxisEdge()); + double x0; + if (this.useSeriesOffset) { + x0 = domainAxis.getCategorySeriesMiddle( + dataset.getColumnKey(column - 1), + dataset.getRowKey(row), dataset, + this.itemMargin, dataArea, + plot.getDomainAxisEdge()); + } + else { + x0 = domainAxis.getCategoryMiddle(column - 1, + getColumnCount(), dataArea, + plot.getDomainAxisEdge()); + } double y0 = rangeAxis.valueToJava2D(previous, dataArea, plot.getRangeAxisEdge()); @@ -958,6 +1062,12 @@ if (this.useOutlinePaint != that.useOutlinePaint) { return false; } + if (this.useSeriesOffset != that.useSeriesOffset) { + return false; + } + if (this.itemMargin != that.itemMargin) { + return false; + } return super.equals(obj); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |