/** * Creates a vertical 3D-effect bar chart with default settings. * * @param title the chart title. * @param categoryAxisLabel the label for the category axis. * @param valueAxisLabel the label for the value axis. * @param data the dataset for the chart. * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * @return a vertical 3D-effect bar chart. */ public static JFreeChart createBarChart3D( String title, java.awt.Font titleFont, String categoryAxisLabel, String valueAxisLabel, CategoryDataset data, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls, CategoryURLGenerator urlGenerator) { CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel); ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel); BarRenderer3D renderer = new BarRenderer3D(); // renderer.setLabelGenerator(new StandardCategoryLabelGenerator()); if (tooltips) { renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); } if (urls) { renderer.setItemURLGenerator(urlGenerator); } CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); plot.setOrientation(orientation); plot.setForegroundAlpha(0.75f); JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); return chart; }
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; }
public static String getBarSeries(Map dataSource, String objectName, HttpSession session) throws Exception { DefaultKeyedValues barValues = new DefaultKeyedValues(); DefaultKeyedValues seriesValues = new DefaultKeyedValues(); Element chartObject = XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName); Element barField = chartObject.getChild("BarFields").getChild("Field"); Element seriesField = chartObject.getChild("SeriesFields").getChild("Field"); for (int i = 0; i < dataSource.size(); i++) { Map rec = (Map) dataSource.get("ROW" + i); barValues.addValue( DataFilter.show(rec, chartObject.getChildText("ColumnLabel")), Double.parseDouble(rec.get(barField.getAttributeValue("name")).toString())); seriesValues.addValue( DataFilter.show(rec, chartObject.getChildText("ColumnLabel")), Double.parseDouble(rec.get(seriesField.getAttributeValue("name")).toString())); } CategoryDataset dataset = DatasetUtilities.createCategoryDataset(barField.getAttributeValue("label"), barValues); PlotOrientation plotOrientation = chartObject.getAttributeValue("plotOrientation").equalsIgnoreCase("VERTICAL") ? PlotOrientation.VERTICAL : PlotOrientation.HORIZONTAL; JFreeChart chart = ChartFactory.createBarChart3D( chartObject.getAttributeValue("title"), chartObject.getAttributeValue("categoryAxisLabel"), chartObject.getAttributeValue("valueAxisLabel"), dataset, plotOrientation, chartObject.getAttribute("showLegend").getBooleanValue(), chartObject.getAttribute("showToolTips").getBooleanValue(), chartObject.getAttribute("urls").getBooleanValue()); CategoryPlot categoryplot = chart.getCategoryPlot(); LineRenderer3D lineRenderer = new LineRenderer3D(); CategoryDataset datasetSeries = DatasetUtilities.createCategoryDataset( seriesField.getAttributeValue("label"), seriesValues); categoryplot.setDataset(1, datasetSeries); categoryplot.setRangeAxis(1, new NumberAxis3D(seriesField.getAttributeValue("label"))); categoryplot.setRenderer(1, lineRenderer); categoryplot.mapDatasetToRangeAxis(1, 1); BarRenderer3D barrenderer = (BarRenderer3D) categoryplot.getRenderer(); barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator()); barrenderer.setItemLabelsVisible(true); barrenderer.setPositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER)); // lineRenderer.setLabelGenerator(new StandardCategoryLabelGenerator()); // lineRenderer.setItemLabelsVisible(true); // lineRenderer.setPositiveItemLabelPosition( // new ItemLabelPosition(ItemLabelAnchor.OUTSIDE10, TextAnchor.CENTER)); float alpha = 0.7F; if (chartObject.getAttribute("alpha") != null) { alpha = chartObject.getAttribute("alpha").getFloatValue(); } categoryplot.setForegroundAlpha(alpha); int width, height; if (chartObject.getAttributeValue("width").equalsIgnoreCase("auto")) { width = (50 * dataSource.size()) + 100; } else { width = Integer.parseInt(chartObject.getAttributeValue("width")); } if (chartObject.getAttributeValue("height").equalsIgnoreCase("auto")) { height = (50 * dataSource.size()) + 100; } else { height = Integer.parseInt(chartObject.getAttributeValue("height")); } return ServletUtilities.saveChartAsPNG(chart, width, height, session); }
public static String getSimpleBarChart(Map dataSource, String objectName, HttpSession session) throws Throwable { Element chartObject = XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName); List invokeFields = chartObject.getChild("InvokeFields").getChildren("Field"); double[][] data = new double[invokeFields.size()][dataSource.size()]; String[] rowKeys = new String[invokeFields.size()]; String[] columnKeys = new String[dataSource.size()]; String columnLabel = chartObject.getChildText("ColumnLabel"); for (int i = 0; i < dataSource.size(); i++) { Map rec = (Map) dataSource.get("ROW" + i); columnKeys[i] = DataFilter.show(rec, columnLabel); for (int j = 0; j < invokeFields.size(); j++) { data[j][i] = Double.parseDouble( rec.get(((Element) invokeFields.get(j)).getAttributeValue("name")).toString()); } } for (int i = 0; i < invokeFields.size(); i++) { rowKeys[i] = ((Element) invokeFields.get(i)).getAttributeValue("label"); } CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); PlotOrientation plotOrientation = chartObject.getAttributeValue("plotOrientation").equalsIgnoreCase("VERTICAL") ? PlotOrientation.VERTICAL : PlotOrientation.HORIZONTAL; JFreeChart chart = ChartFactory.createBarChart3D( chartObject.getAttributeValue("title"), chartObject.getAttributeValue("categoryAxisLabel"), chartObject.getAttributeValue("valueAxisLabel"), dataset, plotOrientation, chartObject.getAttribute("showLegend").getBooleanValue(), chartObject.getAttribute("showToolTips").getBooleanValue(), chartObject.getAttribute("urls").getBooleanValue()); CategoryPlot C3dplot = (CategoryPlot) chart.getPlot(); if (chartObject.getAttribute("alpha") != null) { C3dplot.setForegroundAlpha(chartObject.getAttribute("alpha").getFloatValue()); } BarRenderer3D barrenderer = (BarRenderer3D) C3dplot.getRenderer(); barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator()); barrenderer.setItemLabelsVisible(true); barrenderer.setPositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER)); int width, height; if (chartObject.getAttributeValue("width").equalsIgnoreCase("auto")) { width = (50 * dataSource.size()) * invokeFields.size() + 100; } else { width = Integer.parseInt(chartObject.getAttributeValue("width")); } if (chartObject.getAttributeValue("height").equalsIgnoreCase("auto")) { height = (50 * dataSource.size()) * invokeFields.size() + 100; } else { height = Integer.parseInt(chartObject.getAttributeValue("height")); } return ServletUtilities.saveChartAsPNG(chart, width, height, session); }