protected void configurePlot(Plot plot, JRChartPlot jrPlot) { super.configurePlot(plot, jrPlot); if (plot instanceof CategoryPlot) { CategoryPlot categoryPlot = (CategoryPlot) plot; CategoryItemRenderer categoryRenderer = categoryPlot.getRenderer(); CategoryDataset categoryDataset = categoryPlot.getDataset(); if (categoryDataset != null) { for (int i = 0; i < categoryDataset.getRowCount(); i++) { categoryRenderer.setSeriesOutlinePaint(i, ChartThemesConstants.TRANSPARENT_PAINT); } } categoryPlot.setRangeGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); categoryPlot.setRangeGridlineStroke(new BasicStroke(0.5f)); categoryPlot.setDomainGridlinesVisible(false); categoryPlot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45); } else if (plot instanceof XYPlot) { XYPlot xyPlot = (XYPlot) plot; XYItemRenderer xyItemRenderer = xyPlot.getRenderer(); XYDataset xyDataset = xyPlot.getDataset(); if (xyDataset != null) { for (int i = 0; i < xyDataset.getSeriesCount(); i++) { xyItemRenderer.setSeriesOutlinePaint(i, ChartThemesConstants.TRANSPARENT_PAINT); } } xyPlot.setRangeGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); xyPlot.setRangeGridlineStroke(new BasicStroke(0.5f)); xyPlot.setDomainGridlinesVisible(false); xyPlot.setRangeZeroBaselineVisible(true); } }
/** * Returns a legend item for a series. * * @param datasetIndex the dataset index (zero-based). * @param series the series index (zero-based). * @return The legend item. */ public LegendItem getLegendItem(int datasetIndex, int series) { CategoryPlot p = getPlot(); if (p == null) { return null; } CategoryDataset dataset; dataset = p.getDataset(datasetIndex); String label = this.legendItemLabelGenerator.generateLabel(dataset, series); String description = label; String toolTipText = null; if (this.legendItemToolTipGenerator != null) { toolTipText = this.legendItemToolTipGenerator.generateLabel(dataset, series); } String urlText = null; if (this.legendItemURLGenerator != null) { urlText = this.legendItemURLGenerator.generateLabel(dataset, series); } Shape shape = getSeriesShape(series); Paint paint = getSeriesPaint(series); Paint outlinePaint = getSeriesOutlinePaint(series); Stroke outlineStroke = getSeriesOutlineStroke(series); // TODO: generate attributed label return new LegendItem( label, description, toolTipText, urlText, shape, paint, outlineStroke, outlinePaint); }
protected JFreeChart createStackedBar3DChart() throws JRException { JFreeChart jfreeChart = super.createStackedBar3DChart(); CategoryPlot categoryPlot = (CategoryPlot) jfreeChart.getPlot(); BarRenderer3D barRenderer3D = (BarRenderer3D) categoryPlot.getRenderer(); barRenderer3D.setWallPaint(ChartThemesConstants.TRANSPARENT_PAINT); barRenderer3D.setItemMargin(0); CategoryDataset categoryDataset = categoryPlot.getDataset(); if (categoryDataset != null) { for (int i = 0; i < categoryDataset.getRowCount(); i++) { barRenderer3D.setSeriesOutlinePaint(i, ChartThemesConstants.TRANSPARENT_PAINT); } } return jfreeChart; }
/** * Initialises the renderer. This method gets called once at the start of the process of drawing a * chart. * * @param g2 the graphics device. * @param dataArea the area in which the data is to be plotted. * @param plot the plot. * @param rendererIndex the renderer index. * @param info collects chart rendering information for return to caller. * @return The renderer state. */ public CategoryItemRendererState initialise( Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) { CategoryItemRendererState state = super.initialise(g2, dataArea, plot, rendererIndex, info); // calculate the box width CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex); CategoryDataset dataset = plot.getDataset(rendererIndex); if (dataset != null) { int columns = dataset.getColumnCount(); int rows = dataset.getRowCount(); double space = 0.0; PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { space = dataArea.getHeight(); } else if (orientation == PlotOrientation.VERTICAL) { space = dataArea.getWidth(); } double maxWidth = space * getMaximumBarWidth(); double categoryMargin = 0.0; double currentItemMargin = 0.0; if (columns > 1) { categoryMargin = domainAxis.getCategoryMargin(); } if (rows > 1) { currentItemMargin = getItemMargin(); } double used = space * (1 - domainAxis.getLowerMargin() - domainAxis.getUpperMargin() - categoryMargin - currentItemMargin); if ((rows * columns) > 0) { state.setBarWidth( Math.min(used / (dataset.getColumnCount() * dataset.getRowCount()), maxWidth)); } else { state.setBarWidth(Math.min(used, maxWidth)); } } return state; }
protected JFreeChart createGanttChart() throws JRException { JFreeChart jfreeChart = super.createGanttChart(); CategoryPlot categoryPlot = (CategoryPlot) jfreeChart.getPlot(); categoryPlot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.STANDARD); categoryPlot.setDomainGridlinesVisible(true); categoryPlot.setDomainGridlinePosition(CategoryAnchor.END); categoryPlot.setDomainGridlineStroke( new BasicStroke( 0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 50, new float[] {1}, 0)); categoryPlot.setDomainGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); categoryPlot.setRangeGridlinesVisible(true); categoryPlot.setRangeGridlineStroke( new BasicStroke( 0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 50, new float[] {1}, 0)); categoryPlot.setRangeGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); // JRBarPlot barPlot = (BarPlot)categoryPlot; // categoryPlot.getDomainAxis().setTickLabelsVisible( // categoryPlot.getShowTickLabels() == null ? true : barPlot.getShowTickLabels(). // true // ); CategoryItemRenderer categoryRenderer = categoryPlot.getRenderer(); categoryRenderer.setBaseItemLabelsVisible(true); BarRenderer barRenderer = (BarRenderer) categoryRenderer; List seriesPaints = (List) getDefaultValue(defaultChartPropertiesMap, ChartThemesConstants.SERIES_COLORS); barRenderer.setSeriesPaint(0, (Paint) seriesPaints.get(3)); barRenderer.setSeriesPaint(1, (Paint) seriesPaints.get(0)); CategoryDataset categoryDataset = categoryPlot.getDataset(); if (categoryDataset != null) { for (int i = 0; i < categoryDataset.getRowCount(); i++) { barRenderer.setSeriesItemLabelFont(i, categoryPlot.getDomainAxis().getTickLabelFont()); barRenderer.setSeriesItemLabelsVisible(i, true); // barRenderer.setSeriesPaint(i, GRADIENT_PAINTS[i]); // CategoryMarker categoryMarker = new // CategoryMarker(categoryDataset.getColumnKey(i),MARKER_COLOR, new BasicStroke(1f)); // categoryMarker.setAlpha(0.5f); // categoryPlot.addDomainMarker(categoryMarker, Layer.BACKGROUND); } } categoryPlot.setOutlinePaint(Color.DARK_GRAY); categoryPlot.setOutlineStroke(new BasicStroke(1.5f)); categoryPlot.setOutlineVisible(true); return jfreeChart; }
/** * Initialises the renderer and returns a state object that will be used for the remainder of the * drawing process for a single chart. The state object allows for the fact that the renderer may * be used simultaneously by multiple threads (each thread will work with a separate state * object). * * <p>Stores a reference to the {@link PlotRenderingInfo} object (which might be <code>null</code> * ), and then sets the useCategoriesPaint flag according to the special case conditions a) there * is only one series and b) the categoriesPaint array is not null. * * @param g2 the graphics device. * @param dataArea the data area. * @param plot the plot. * @param rendererIndex the renderer index. * @param info an object for returning information about the structure of the plot (<code>null * </code> permitted). * @return The renderer state. */ public CategoryItemRendererState initialise( Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) { setPlot(plot); CategoryDataset data = plot.getDataset(rendererIndex); if (data != null) { this.rowCount = data.getRowCount(); this.columnCount = data.getColumnCount(); } else { this.rowCount = 0; this.columnCount = 0; } return new CategoryItemRendererState(info); }
/** * Returns a legend item for a series. * * @param datasetIndex the dataset index (zero-based). * @param series the series index (zero-based). * @return The legend item. */ @Override public LegendItem getLegendItem(int datasetIndex, int series) { // if there is no plot, there is no dataset to access... CategoryPlot cp = getPlot(); if (cp == null) { return null; } // check that a legend item needs to be displayed... if (!isSeriesVisible(series) || !isSeriesVisibleInLegend(series)) { return null; } CategoryDataset dataset = cp.getDataset(datasetIndex); String label = getLegendItemLabelGenerator().generateLabel(dataset, series); String description = label; String toolTipText = null; if (getLegendItemToolTipGenerator() != null) { toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series); } String urlText = null; if (getLegendItemURLGenerator() != null) { urlText = getLegendItemURLGenerator().generateLabel(dataset, series); } Shape shape = lookupLegendShape(series); Paint paint = lookupSeriesPaint(series); Paint outlinePaint = lookupSeriesOutlinePaint(series); Stroke outlineStroke = lookupSeriesOutlineStroke(series); LegendItem result = new LegendItem( label, description, toolTipText, urlText, shape, paint, outlineStroke, outlinePaint); result.setLabelFont(lookupLegendTextFont(series)); Paint labelPaint = lookupLegendTextPaint(series); if (labelPaint != null) { result.setLabelPaint(labelPaint); } result.setDataset(dataset); result.setDatasetIndex(datasetIndex); result.setSeriesKey(dataset.getRowKey(series)); result.setSeriesIndex(series); return result; }
/** * Returns a legend item for a series. * * @param datasetIndex the dataset index (zero-based). * @param series the series index (zero-based). * @return The legend item. */ public LegendItem getLegendItem(int datasetIndex, int series) { CategoryPlot p = getPlot(); if (p == null) { return null; } // check that a legend item needs to be displayed... if (!isSeriesVisible(series) || !isSeriesVisibleInLegend(series)) { return null; } CategoryDataset dataset = p.getDataset(datasetIndex); String label = getLegendItemLabelGenerator().generateLabel(dataset, series); String description = label; String toolTipText = null; if (getLegendItemToolTipGenerator() != null) { toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series); } String urlText = null; if (getLegendItemURLGenerator() != null) { urlText = getLegendItemURLGenerator().generateLabel(dataset, series); } Shape shape = lookupLegendShape(series); Paint paint = lookupSeriesPaint(series); LegendItem item = new LegendItem(label, description, toolTipText, urlText, shape, paint); item.setLabelFont(lookupLegendTextFont(series)); Paint labelPaint = lookupLegendTextPaint(series); if (labelPaint != null) { item.setLabelPaint(labelPaint); } item.setSeriesKey(dataset.getRowKey(series)); item.setSeriesIndex(series); item.setDataset(dataset); item.setDatasetIndex(datasetIndex); return item; }
/** * Draws the category labels and returns the updated axis state. * * @param g2 the graphics device (<code>null</code> not permitted). * @param plotArea the plot area (<code>null</code> not permitted). * @param dataArea the area inside the axes (<code>null</code> not permitted). * @param edge the axis location (<code>null</code> not permitted). * @param state the axis state (<code>null</code> not permitted). * @param plotState collects information about the plot (<code>null</code> permitted). * @return The updated axis state (never <code>null</code>). */ protected AxisState drawSubCategoryLabels( Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state, PlotRenderingInfo plotState) { if (state == null) { throw new IllegalArgumentException("Null 'state' argument."); } g2.setFont(this.subLabelFont); g2.setPaint(this.subLabelPaint); CategoryPlot plot = (CategoryPlot) getPlot(); CategoryDataset dataset = plot.getDataset(); int categoryCount = dataset.getColumnCount(); double maxdim = getMaxDim(g2, edge); for (int categoryIndex = 0; categoryIndex < categoryCount; categoryIndex++) { double x0 = 0.0; double x1 = 0.0; double y0 = 0.0; double y1 = 0.0; if (edge == RectangleEdge.TOP) { x0 = getCategoryStart(categoryIndex, categoryCount, dataArea, edge); x1 = getCategoryEnd(categoryIndex, categoryCount, dataArea, edge); y1 = state.getCursor(); y0 = y1 - maxdim; } else if (edge == RectangleEdge.BOTTOM) { x0 = getCategoryStart(categoryIndex, categoryCount, dataArea, edge); x1 = getCategoryEnd(categoryIndex, categoryCount, dataArea, edge); y0 = state.getCursor(); y1 = y0 + maxdim; } else if (edge == RectangleEdge.LEFT) { y0 = getCategoryStart(categoryIndex, categoryCount, dataArea, edge); y1 = getCategoryEnd(categoryIndex, categoryCount, dataArea, edge); x1 = state.getCursor(); x0 = x1 - maxdim; } else if (edge == RectangleEdge.RIGHT) { y0 = getCategoryStart(categoryIndex, categoryCount, dataArea, edge); y1 = getCategoryEnd(categoryIndex, categoryCount, dataArea, edge); x0 = state.getCursor(); x1 = x0 + maxdim; } Rectangle2D area = new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0)); int subCategoryCount = this.subCategories.size(); float width = (float) ((x1 - x0) / subCategoryCount); float height = (float) ((y1 - y0) / subCategoryCount); float xx = 0.0f; float yy = 0.0f; for (int i = 0; i < subCategoryCount; i++) { if (RectangleEdge.isTopOrBottom(edge)) { xx = (float) (x0 + (i + 0.5) * width); yy = (float) area.getCenterY(); } else { xx = (float) area.getCenterX(); yy = (float) (y0 + (i + 0.5) * height); } String label = this.subCategories.get(i).toString(); TextUtilities.drawRotatedString( label, g2, xx, yy, TextAnchor.CENTER, 0.0, TextAnchor.CENTER); } } if (edge.equals(RectangleEdge.TOP)) { double h = maxdim; state.cursorUp(h); } else if (edge.equals(RectangleEdge.BOTTOM)) { double h = maxdim; state.cursorDown(h); } else if (edge == RectangleEdge.LEFT) { double w = maxdim; state.cursorLeft(w); } else if (edge == RectangleEdge.RIGHT) { double w = maxdim; state.cursorRight(w); } return state; }
/** * Draws a marker for the domain axis. * * @param g2 the graphics device (not <code>null</code>). * @param plot the plot (not <code>null</code>). * @param axis the range axis (not <code>null</code>). * @param marker the marker to be drawn (not <code>null</code>). * @param dataArea the area inside the axes (not <code>null</code>). */ public void drawDomainMarker( Graphics2D g2, CategoryPlot plot, CategoryAxis axis, CategoryMarker marker, Rectangle2D dataArea) { Comparable category = marker.getKey(); CategoryDataset dataset = plot.getDataset(plot.getIndexOf(this)); int columnIndex = dataset.getColumnIndex(category); if (columnIndex < 0) { return; } PlotOrientation orientation = plot.getOrientation(); Rectangle2D bounds = null; if (marker.getDrawAsLine()) { double v = axis.getCategoryMiddle( columnIndex, dataset.getColumnCount(), dataArea, plot.getDomainAxisEdge()); Line2D line = null; if (orientation == PlotOrientation.HORIZONTAL) { line = new Line2D.Double(dataArea.getMinX(), v, dataArea.getMaxX(), v); } else if (orientation == PlotOrientation.VERTICAL) { line = new Line2D.Double(v, dataArea.getMinY(), v, dataArea.getMaxY()); } g2.setPaint(marker.getPaint()); g2.setStroke(marker.getStroke()); g2.draw(line); bounds = line.getBounds2D(); } else { double v0 = axis.getCategoryStart( columnIndex, dataset.getColumnCount(), dataArea, plot.getDomainAxisEdge()); double v1 = axis.getCategoryEnd( columnIndex, dataset.getColumnCount(), dataArea, plot.getDomainAxisEdge()); Rectangle2D area = null; if (orientation == PlotOrientation.HORIZONTAL) { area = new Rectangle2D.Double(dataArea.getMinX(), v0, dataArea.getWidth(), (v1 - v0)); } else if (orientation == PlotOrientation.VERTICAL) { area = new Rectangle2D.Double(v0, dataArea.getMinY(), (v1 - v0), dataArea.getHeight()); } g2.setPaint(marker.getPaint()); g2.fill(area); bounds = area; } String label = marker.getLabel(); RectangleAnchor anchor = marker.getLabelAnchor(); if (label != null) { Font labelFont = marker.getLabelFont(); g2.setFont(labelFont); g2.setPaint(marker.getLabelPaint()); Point2D coordinates = calculateDomainMarkerTextAnchorPoint( g2, orientation, dataArea, bounds, marker.getLabelOffset(), marker.getLabelOffsetType(), anchor); TextUtilities.drawAlignedString( label, g2, (float) coordinates.getX(), (float) coordinates.getY(), marker.getLabelTextAnchor()); } }