List of usage examples for org.jfree.chart.axis CategoryLabelPositions CategoryLabelPositions
public CategoryLabelPositions(CategoryLabelPosition top, CategoryLabelPosition bottom,
CategoryLabelPosition left, CategoryLabelPosition right)
From source file:com.manydesigns.portofino.chart.Chart2DGenerator.java
public JFreeChart generate(ChartDefinition chartDefinition, Persistence persistence, Locale locale) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); java.util.List<Object[]> result; String query = chartDefinition.getQuery(); logger.info(query);/*from ww w .j a v a 2s . c o m*/ Session session = persistence.getSession(chartDefinition.getDatabase()); result = QueryUtils.runSql(session, query); for (Object[] current : result) { ComparableWrapper x = new ComparableWrapper((Comparable) current[0]); ComparableWrapper y = new ComparableWrapper((Comparable) current[1]); if (current.length > 3) { x.setLabel(current[3].toString()); } if (current.length > 4) { y.setLabel(current[4].toString()); } dataset.setValue((Number) current[2], x, y); } PlotOrientation plotOrientation = PlotOrientation.HORIZONTAL; if (chartDefinition.getActualOrientation() == ChartDefinition.Orientation.VERTICAL) { plotOrientation = PlotOrientation.VERTICAL; } JFreeChart chart = createChart(chartDefinition, dataset, plotOrientation); chart.setAntiAlias(antiAlias); // impostiamo il bordo invisibile // eventualmente e' il css a fornirne uno chart.setBorderVisible(borderVisible); // impostiamo il titolo TextTitle title = chart.getTitle(); title.setFont(titleFont); title.setMargin(10.0, 0.0, 0.0, 0.0); // ottieni il Plot CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryItemRenderer renderer = plot.getRenderer(); String urlExpression = chartDefinition.getUrlExpression(); if (!StringUtils.isBlank(urlExpression)) { CategoryURLGenerator urlGenerator = new ChartBarUrlGenerator(chartDefinition.getUrlExpression()); renderer.setBaseItemURLGenerator(urlGenerator); } else { renderer.setBaseItemURLGenerator(null); } renderer.setBaseOutlinePaint(Color.BLACK); // /////////////// if (renderer instanceof BarRenderer) { BarRenderer barRenderer = (BarRenderer) renderer; barRenderer.setDrawBarOutline(true); barRenderer.setShadowVisible(false); barRenderer.setBarPainter(new StandardBarPainter()); } // /////////////// // il plot ha sfondo e bordo trasparente // (quindi si vede il colore del chart) plot.setBackgroundPaint(transparentColor); plot.setOutlinePaint(transparentColor); // Modifico il toolTip // plot.setToolTipGenerator(new StandardPieToolTipGenerator("{0} = {1} ({2})")); // imposta il messaggio se non ci sono dati plot.setNoDataMessage(ElementsThreadLocals.getText("no.data.available")); plot.setRangeGridlinesVisible(true); plot.setRangeGridlinePaint(Color.GRAY); plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0)); // Category axis CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setAxisLinePaint(Color.BLACK); categoryAxis.setLabelFont(axisFont); categoryAxis.setAxisLineVisible(true); // impostiamo la rotazione dell'etichetta if (plot.getOrientation() == PlotOrientation.VERTICAL) { CategoryLabelPosition pos = new CategoryLabelPosition(RectangleAnchor.TOP_LEFT, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 4.0, CategoryLabelWidthType.CATEGORY, 100); CategoryLabelPositions positions = new CategoryLabelPositions(pos, pos, pos, pos); categoryAxis.setCategoryLabelPositions(positions); categoryAxis.setMaximumCategoryLabelWidthRatio(6.0f); height = 333; } else { categoryAxis.setMaximumCategoryLabelWidthRatio(0.4f); // recuperiamo 8 pixel a sinistra plot.setInsets(new RectangleInsets(4.0, 0.0, 4.0, 8.0)); height = 74; // contiamo gli elementi nel dataset height += 23 * dataset.getColumnCount(); height += 57; } Axis rangeAxis = plot.getRangeAxis(); rangeAxis.setAxisLinePaint(Color.BLACK); rangeAxis.setLabelFont(axisFont); DrawingSupplier supplier = new DesaturatedDrawingSupplier(plot.getDrawingSupplier()); plot.setDrawingSupplier(supplier); // ? plot.setForegroundAlpha(1.0f); // impostiamo il titolo della legenda String legendString = chartDefinition.getLegend(); Title subtitle = new TextTitle(legendString, legendFont, Color.BLACK, RectangleEdge.BOTTOM, HorizontalAlignment.CENTER, VerticalAlignment.CENTER, new RectangleInsets(0, 0, 0, 0)); subtitle.setMargin(0, 0, 5, 0); chart.addSubtitle(subtitle); // impostiamo la legenda LegendTitle legend = chart.getLegend(); legend.setBorder(0, 0, 0, 0); legend.setItemFont(legendItemFont); int legendMargin = 10; legend.setMargin(0.0, legendMargin, legendMargin, legendMargin); legend.setBackgroundPaint(transparentColor); // impostiamo un gradiente orizzontale Paint chartBgPaint = new GradientPaint(0, 0, new Color(255, 253, 240), 0, height, Color.WHITE); chart.setBackgroundPaint(chartBgPaint); return chart; }
From source file:com.manydesigns.portofino.chart.ChartStackedBarGenerator.java
public JFreeChart generate(ChartDefinition chartDefinition, Persistence persistence, Locale locale) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); java.util.List<Object[]> result; String query = chartDefinition.getQuery(); logger.info(query);//from w ww . j a v a 2s . c o m Session session = persistence.getSession(chartDefinition.getDatabase()); result = QueryUtils.runSql(session, query); for (Object[] current : result) { ComparableWrapper x = new ComparableWrapper((Comparable) current[0]); ComparableWrapper y = new ComparableWrapper((Comparable) current[1]); if (current.length > 3) { x.setLabel(current[3].toString()); } if (current.length > 4) { y.setLabel(current[4].toString()); } dataset.setValue((Number) current[2], x, y); } PlotOrientation plotOrientation = PlotOrientation.HORIZONTAL; if (chartDefinition.getActualOrientation() == ChartDefinition.Orientation.VERTICAL) { plotOrientation = PlotOrientation.VERTICAL; } JFreeChart chart = createChart(chartDefinition, dataset, plotOrientation); chart.setAntiAlias(antiAlias); // impostiamo il bordo invisibile // eventualmente e' il css a fornirne uno chart.setBorderVisible(borderVisible); // impostiamo il titolo TextTitle title = chart.getTitle(); title.setFont(titleFont); title.setMargin(10.0, 0.0, 0.0, 0.0); // ottieni il Plot CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryItemRenderer renderer = plot.getRenderer(); String urlExpression = chartDefinition.getUrlExpression(); if (!StringUtils.isBlank(urlExpression)) { CategoryURLGenerator urlGenerator = new ChartBarUrlGenerator(chartDefinition.getUrlExpression()); renderer.setBaseItemURLGenerator(urlGenerator); } else { renderer.setBaseItemURLGenerator(null); } renderer.setBaseOutlinePaint(Color.BLACK); // /////////////// if (renderer instanceof BarRenderer) { BarRenderer barRenderer = (BarRenderer) renderer; barRenderer.setDrawBarOutline(true); barRenderer.setShadowVisible(false); barRenderer.setBarPainter(new StandardBarPainter()); // hongliangpan add // ? barRenderer.setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator()); // bar??? barRenderer.setMinimumBarLength(0.02); // barRenderer.setMaximumBarWidth(0.06); } if (renderer instanceof StackedBarRenderer3D || renderer instanceof StackedBarRenderer) { BarRenderer barRenderer = (BarRenderer) renderer; barRenderer.setDrawBarOutline(true); barRenderer.setShadowVisible(false); barRenderer.setBarPainter(new StandardBarPainter()); // hongliangpan add // ? setSeriesItemLabelGenerator barRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); // bar??? barRenderer.setMinimumBarLength(0.02); // barRenderer.setMaximumBarWidth(0.06); // ????? ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT, TextAnchor.HALF_ASCENT_LEFT, -1.57D); // ??labelposition barRenderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback); barRenderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback); barRenderer.setItemLabelsVisible(true); barRenderer.setItemMargin(10); } // /////////////// // il plot ha sfondo e bordo trasparente // (quindi si vede il colore del chart) plot.setBackgroundPaint(transparentColor); plot.setOutlinePaint(transparentColor); // Modifico il toolTip // plot.setToolTipGenerator(new StandardPieToolTipGenerator("{0} = {1} ({2})")); // imposta il messaggio se non ci sono dati plot.setNoDataMessage(ElementsThreadLocals.getText("no.data.available")); plot.setRangeGridlinesVisible(true); plot.setRangeGridlinePaint(Color.GRAY); plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0)); // Category axis CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setAxisLinePaint(Color.BLACK); categoryAxis.setLabelFont(axisFont); categoryAxis.setAxisLineVisible(true); // impostiamo la rotazione dell'etichetta if (plot.getOrientation() == PlotOrientation.VERTICAL) { CategoryLabelPosition pos = new CategoryLabelPosition(RectangleAnchor.TOP_LEFT, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 4.0, CategoryLabelWidthType.CATEGORY, 100); CategoryLabelPositions positions = new CategoryLabelPositions(pos, pos, pos, pos); categoryAxis.setCategoryLabelPositions(positions); categoryAxis.setMaximumCategoryLabelWidthRatio(6.0f); height = 333; } else { categoryAxis.setMaximumCategoryLabelWidthRatio(0.4f); // recuperiamo 8 pixel a sinistra plot.setInsets(new RectangleInsets(4.0, 0.0, 4.0, 8.0)); height = 74; // contiamo gli elementi nel dataset height += 23 * dataset.getColumnCount(); height += 57; } Axis rangeAxis = plot.getRangeAxis(); rangeAxis.setAxisLinePaint(Color.BLACK); rangeAxis.setLabelFont(axisFont); DrawingSupplier supplier = new DesaturatedDrawingSupplier(plot.getDrawingSupplier()); plot.setDrawingSupplier(supplier); // impostiamo il titolo della legenda String legendString = chartDefinition.getLegend(); Title subtitle = new TextTitle(legendString, legendFont, Color.BLACK, RectangleEdge.BOTTOM, HorizontalAlignment.CENTER, VerticalAlignment.CENTER, new RectangleInsets(0, 0, 0, 0)); subtitle.setMargin(0, 0, 5, 0); chart.addSubtitle(subtitle); // impostiamo la legenda LegendTitle legend = chart.getLegend(); legend.setBorder(0, 0, 0, 0); legend.setItemFont(legendItemFont); int legendMargin = 10; legend.setMargin(0.0, legendMargin, legendMargin, legendMargin); legend.setBackgroundPaint(transparentColor); // impostiamo un gradiente orizzontale Paint chartBgPaint = new GradientPaint(0, 0, new Color(255, 253, 240), 0, height, Color.WHITE); chart.setBackgroundPaint(chartBgPaint); return chart; }
From source file:com.manydesigns.portofino.chart.ChartBarGenerator.java
public JFreeChart generate(ChartDefinition chartDefinition, Persistence persistence, Locale locale) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); java.util.List<Object[]> result; String query = chartDefinition.getQuery(); logger.info(query);//from w w w .j ava 2 s . c o m Session session = persistence.getSession(chartDefinition.getDatabase()); result = QueryUtils.runSql(session, query); for (Object[] current : result) { ComparableWrapper x = new ComparableWrapper((Comparable) current[0]); ComparableWrapper y = new ComparableWrapper((Comparable) current[1]); if (current.length > 3) { x.setLabel(current[3].toString()); } if (current.length > 4) { y.setLabel(current[4].toString()); } dataset.setValue((Number) current[2], x, y); } PlotOrientation plotOrientation = PlotOrientation.HORIZONTAL; if (chartDefinition.getActualOrientation() == ChartDefinition.Orientation.VERTICAL) { plotOrientation = PlotOrientation.VERTICAL; } JFreeChart chart = createChart(chartDefinition, dataset, plotOrientation); chart.setAntiAlias(antiAlias); // impostiamo il bordo invisibile // eventualmente e' il css a fornirne uno chart.setBorderVisible(borderVisible); // impostiamo il titolo TextTitle title = chart.getTitle(); title.setFont(titleFont); title.setMargin(10.0, 0.0, 0.0, 0.0); // ottieni il Plot CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryItemRenderer renderer = plot.getRenderer(); String urlExpression = chartDefinition.getUrlExpression(); if (!StringUtils.isBlank(urlExpression)) { CategoryURLGenerator urlGenerator = new ChartBarUrlGenerator(chartDefinition.getUrlExpression()); renderer.setBaseItemURLGenerator(urlGenerator); } else { renderer.setBaseItemURLGenerator(null); } renderer.setBaseOutlinePaint(Color.BLACK); // /////////////// if (renderer instanceof BarRenderer || renderer instanceof BarRenderer3D) { BarRenderer barRenderer = (BarRenderer) renderer; barRenderer.setDrawBarOutline(true); barRenderer.setShadowVisible(false); barRenderer.setBarPainter(new StandardBarPainter()); // hongliangpan add barRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); barRenderer.setBaseItemLabelsVisible(true); // ? setSeriesItemLabelGenerator barRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); // bar??? barRenderer.setMinimumBarLength(0.02); // barRenderer.setMaximumBarWidth(0.05); // ????? // ??90,??/3.14 ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT, TextAnchor.HALF_ASCENT_LEFT, -1.57D); // ??labelposition // barRenderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback); // barRenderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback); barRenderer.setItemLabelsVisible(true); // ? barRenderer.setBaseOutlinePaint(Color.BLACK); // ??? barRenderer.setDrawBarOutline(true); // ??? barRenderer.setItemMargin(0.0); // ? barRenderer.setIncludeBaseInRange(true); barRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); barRenderer.setBaseItemLabelsVisible(true); // ? plot.setForegroundAlpha(1.0f); } // /////////////// // il plot ha sfondo e bordo trasparente // (quindi si vede il colore del chart) plot.setBackgroundPaint(transparentColor); plot.setOutlinePaint(transparentColor); // Modifico il toolTip // plot.setToolTipGenerator(new StandardPieToolTipGenerator("{0} = {1} ({2})")); // imposta il messaggio se non ci sono dati plot.setNoDataMessage(ElementsThreadLocals.getText("no.data.available")); plot.setRangeGridlinesVisible(true); plot.setRangeGridlinePaint(Color.GRAY); plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0)); // Category axis CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setAxisLinePaint(Color.BLACK); categoryAxis.setLabelFont(axisFont); categoryAxis.setAxisLineVisible(true); // impostiamo la rotazione dell'etichetta if (plot.getOrientation() == PlotOrientation.VERTICAL) { CategoryLabelPosition pos = new CategoryLabelPosition(RectangleAnchor.TOP_LEFT, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 4.0, CategoryLabelWidthType.CATEGORY, 100); CategoryLabelPositions positions = new CategoryLabelPositions(pos, pos, pos, pos); categoryAxis.setCategoryLabelPositions(positions); categoryAxis.setMaximumCategoryLabelWidthRatio(6.0f); height = 333; } else { categoryAxis.setMaximumCategoryLabelWidthRatio(0.4f); // recuperiamo 8 pixel a sinistra plot.setInsets(new RectangleInsets(4.0, 0.0, 4.0, 8.0)); height = 74; // contiamo gli elementi nel dataset height += 23 * dataset.getColumnCount(); height += 57; } Axis rangeAxis = plot.getRangeAxis(); rangeAxis.setAxisLinePaint(Color.BLACK); rangeAxis.setLabelFont(axisFont); DrawingSupplier supplier = new DesaturatedDrawingSupplier(plot.getDrawingSupplier()); plot.setDrawingSupplier(supplier); // impostiamo il titolo della legenda String legendString = chartDefinition.getLegend(); Title subtitle = new TextTitle(legendString, legendFont, Color.BLACK, RectangleEdge.BOTTOM, HorizontalAlignment.CENTER, VerticalAlignment.CENTER, new RectangleInsets(0, 0, 0, 0)); subtitle.setMargin(0, 0, 5, 0); chart.addSubtitle(subtitle); // impostiamo la legenda LegendTitle legend = chart.getLegend(); legend.setBorder(0, 0, 0, 0); legend.setItemFont(legendItemFont); int legendMargin = 10; legend.setMargin(0.0, legendMargin, legendMargin, legendMargin); legend.setBackgroundPaint(transparentColor); // impostiamo un gradiente orizzontale Paint chartBgPaint = new GradientPaint(0, 0, new Color(255, 253, 240), 0, height, Color.WHITE); chart.setBackgroundPaint(chartBgPaint); return chart; }
From source file:de.fhbingen.wbs.wpOverview.tabs.APCalendarPanel.java
/** * Create the JFreeChart./*from w w w . ja v a2s . c o m*/ * @param dataset * task list for the JFreeChart. * @return JFreeChart of tasks. */ private JFreeChart createChart(final IntervalCategoryDataset dataset) { final JFreeChart chart = ChartFactory.createGanttChart("", "", "", dataset, true, false, false); chart.getCategoryPlot().getDomainAxis().setCategoryMargin(0.4); chart.getCategoryPlot().getDomainAxis().setLowerMargin(0); chart.getCategoryPlot().getDomainAxis().setUpperMargin(0); chart.getCategoryPlot().getDomainAxis().setTickLabelFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10)); chart.getCategoryPlot().getDomainAxis().setTickLabelInsets(new RectangleInsets(0, 0, 0, 0)); chart.getCategoryPlot().getDomainAxis() .setCategoryLabelPositions(new CategoryLabelPositions( new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, CategoryLabelWidthType.RANGE, 1), new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, CategoryLabelWidthType.RANGE, 1), new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, CategoryLabelWidthType.RANGE, 1), new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, CategoryLabelWidthType.RANGE, 1))); return chart; }
From source file:com.smhdemo.common.report.generate.factory.ChartFactory.java
public byte[] export(Chart report, Map<String, String> pageParams) throws Exception { if (report == null) { return null; }//w w w .ja v a 2 s . c o m DefaultCategoryDataset dataset = buildDataset(report, pageParams); java.awt.Font titleFont = new java.awt.Font(report.getFontName(), report.getFontStyle(), report.getFontSize()); String chartTitle = report.getChartTitle(); chartTitle = replaceParam(pageParams, report.getParameters(), chartTitle, false); String horizAxisLabel = report.getHorizAxisLabel(); horizAxisLabel = replaceParam(pageParams, report.getParameters(), horizAxisLabel, false); String vertAxisLabel = report.getVertAxisLabel(); vertAxisLabel = replaceParam(pageParams, report.getParameters(), vertAxisLabel, false); Boolean showLegend = report.getShowLegend(); Boolean showTooltips = report.getShowTooltips(); Boolean drillThroughEnabled = false; Chart.Type chartType = report.getType(); CategoryURLGenerator urlGenerator = null; JFreeChart chart = null; switch (chartType) { case VERTBAR: chart = ChartGenerationService.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTBAR3D: chart = ChartGenerationService.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZBAR: chart = ChartGenerationService.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZBAR3D: chart = ChartGenerationService.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDVERTBAR: chart = ChartGenerationService.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDVERTBAR3D: chart = ChartGenerationService.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDHORIZBAR: chart = ChartGenerationService.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDHORIZBAR3D: chart = ChartGenerationService.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTLINE: chart = ChartGenerationService.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZLINE: chart = ChartGenerationService.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTAREA: chart = ChartGenerationService.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZAREA: chart = ChartGenerationService.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTSTACKEDAREA: chart = ChartGenerationService.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZSTACKEDAREA: chart = ChartGenerationService.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case PIEBYCOLUMN: chart = ChartGenerationService.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN, showLegend, showTooltips, drillThroughEnabled, null); break; case PIEBYROW: chart = ChartGenerationService.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend, showTooltips, drillThroughEnabled, null); break; case PIEBYCOLUMN3D: chart = ChartGenerationService.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN, showLegend, showTooltips, drillThroughEnabled, null); break; case PIEBYROW3D: chart = ChartGenerationService.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend, showTooltips, drillThroughEnabled, null); break; default: throw new BaseException("", ""); } try { Integer bgColorR = report.getBgColorR(); Integer bgColorG = report.getBgColorG(); Integer bgColorB = report.getBgColorB(); chart.setBackgroundPaint(new java.awt.Color(bgColorR, bgColorG, bgColorB)); String axisFontName = report.getAxisFontName(); Integer axisFontStyle = report.getAxisFontStyle(); Integer axisFontSize = report.getAxisFontSize(); java.awt.Font axisFont = new java.awt.Font(axisFontName, axisFontStyle, axisFontSize); String axisTickFontName = report.getAxisTickFontName(); Integer axisTickFontStyle = report.getAxisTickFontStyle(); Integer axisTickFontSize = report.getAxisTickFontSize(); java.awt.Font axisTickFont = new java.awt.Font(axisTickFontName, axisTickFontStyle, axisTickFontSize); String legendFontName = report.getLegendFontName(); Integer legendFontStyle = report.getLegendFontStyle(); Integer legendFontSize = report.getLegendFontSize(); java.awt.Font legendFont = new java.awt.Font(legendFontName, legendFontStyle, legendFontSize); Integer tickLabelRotate = report.getTickLabelRotate(); String dataFontName = report.getDataFontName(); Integer dataFontStyle = report.getDataFontStyle(); Integer dataFontSize = report.getDataFontSize(); java.awt.Font dataFont = new java.awt.Font(dataFontName, dataFontStyle, dataFontSize); Plot plot = chart.getPlot(); if (!(plot instanceof MultiplePiePlot)) { CategoryPlot categoryPlot = chart.getCategoryPlot(); CategoryItemRenderer renderer = categoryPlot.getRenderer(); renderer.setBaseItemLabelGenerator(new LabelGenerator(0.0)); renderer.setBaseItemLabelFont(dataFont); renderer.setBaseItemLabelsVisible(true); if (chartType == Chart.Type.VERTLINE || chartType == Chart.Type.HORIZLINE) { LineAndShapeRenderer lineRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer(); lineRenderer.setBaseShapesVisible(true); lineRenderer.setDrawOutlines(true); lineRenderer.setUseFillPaint(true); } } if (plot instanceof CategoryPlot) { CategoryPlot catPlot = (CategoryPlot) plot; catPlot.getDomainAxis().setLabelFont(axisFont); catPlot.getRangeAxis().setLabelFont(axisFont); catPlot.getDomainAxis().setTickLabelFont(axisTickFont); catPlot.getRangeAxis().setTickLabelFont(axisTickFont); catPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(100.0f); double angle = -2.0 * Math.PI / 360.0 * (double) tickLabelRotate; CategoryLabelPositions oldp = catPlot.getDomainAxis().getCategoryLabelPositions(); CategoryLabelPositions newp = new CategoryLabelPositions(oldp.getLabelPosition(RectangleEdge.TOP), new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, angle, CategoryLabelWidthType.RANGE, 0.0f), oldp.getLabelPosition(RectangleEdge.LEFT), oldp.getLabelPosition(RectangleEdge.RIGHT)); catPlot.getDomainAxis().setCategoryLabelPositions(newp); } else if (plot instanceof PiePlot3D) { PiePlot3D piePlot = (PiePlot3D) plot; piePlot.setLabelFont(axisFont); piePlot.setDirection(org.jfree.util.Rotation.CLOCKWISE); piePlot.setForegroundAlpha(0.5f); piePlot.setNoDataMessage("?"); } else if (plot instanceof PiePlot) { PiePlot piePlot = (PiePlot) plot; piePlot.setLabelFont(axisFont); } LegendTitle legend = (LegendTitle) chart.getLegend(); if (legend != null) { legend.setItemFont(legendFont); RectangleEdge legendRectEdge = RectangleEdge.BOTTOM; Integer legendPosition = report.getLegendPosition(); switch (legendPosition) { case 0: legendRectEdge = RectangleEdge.LEFT; break; case 1: legendRectEdge = RectangleEdge.TOP; break; case 2: legendRectEdge = RectangleEdge.RIGHT; break; case 3: legendRectEdge = RectangleEdge.BOTTOM; break; } legend.setPosition(legendRectEdge); } } catch (Exception e) { logger.error("Chart Export Exception", e); } ByteArrayOutputStream out = new ByteArrayOutputStream(); ChartUtilities.writeChartAsPNG(out, chart, report.getChartWidth(), report.getChartHeight()); return out.toByteArray(); }
From source file:com.ewcms.plugin.report.generate.factory.ChartFactory.java
public byte[] export(ChartReport report, Map<String, String> pageParams) throws Exception { if (report == null) { return null; }//from w w w. ja v a2s . com DefaultCategoryDataset dataset = buildDataset(report, pageParams); java.awt.Font titleFont = new java.awt.Font(report.getFontName(), report.getFontStyle(), report.getFontSize()); String chartTitle = report.getChartTitle(); chartTitle = replaceParam(pageParams, report.getParameters(), chartTitle, false); String horizAxisLabel = report.getHorizAxisLabel(); horizAxisLabel = replaceParam(pageParams, report.getParameters(), horizAxisLabel, false); String vertAxisLabel = report.getVertAxisLabel(); vertAxisLabel = replaceParam(pageParams, report.getParameters(), vertAxisLabel, false); Boolean showLegend = report.getShowLegend(); Boolean showTooltips = report.getShowTooltips(); Boolean drillThroughEnabled = false; ChartReport.Type chartType = report.getType(); CategoryURLGenerator urlGenerator = null; JFreeChart chart = null; switch (chartType) { case VERTBAR: chart = ChartGenerationService.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTBAR3D: chart = ChartGenerationService.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZBAR: chart = ChartGenerationService.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZBAR3D: chart = ChartGenerationService.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDVERTBAR: chart = ChartGenerationService.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDVERTBAR3D: chart = ChartGenerationService.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDHORIZBAR: chart = ChartGenerationService.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case STACKEDHORIZBAR3D: chart = ChartGenerationService.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTLINE: chart = ChartGenerationService.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZLINE: chart = ChartGenerationService.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTAREA: chart = ChartGenerationService.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZAREA: chart = ChartGenerationService.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case VERTSTACKEDAREA: chart = ChartGenerationService.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case HORIZSTACKEDAREA: chart = ChartGenerationService.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case PIEBYCOLUMN: chart = ChartGenerationService.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN, showLegend, showTooltips, drillThroughEnabled, null); break; case PIEBYROW: chart = ChartGenerationService.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend, showTooltips, drillThroughEnabled, null); break; case PIEBYCOLUMN3D: chart = ChartGenerationService.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN, showLegend, showTooltips, drillThroughEnabled, null); break; case PIEBYROW3D: chart = ChartGenerationService.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend, showTooltips, drillThroughEnabled, null); break; default: throw new BaseException("", ""); } try { Integer bgColorR = report.getBgColorR(); Integer bgColorG = report.getBgColorG(); Integer bgColorB = report.getBgColorB(); chart.setBackgroundPaint(new java.awt.Color(bgColorR, bgColorG, bgColorB)); String axisFontName = report.getAxisFontName(); Integer axisFontStyle = report.getAxisFontStyle(); Integer axisFontSize = report.getAxisFontSize(); java.awt.Font axisFont = new java.awt.Font(axisFontName, axisFontStyle, axisFontSize); String axisTickFontName = report.getAxisTickFontName(); Integer axisTickFontStyle = report.getAxisTickFontStyle(); Integer axisTickFontSize = report.getAxisTickFontSize(); java.awt.Font axisTickFont = new java.awt.Font(axisTickFontName, axisTickFontStyle, axisTickFontSize); String legendFontName = report.getLegendFontName(); Integer legendFontStyle = report.getLegendFontStyle(); Integer legendFontSize = report.getLegendFontSize(); java.awt.Font legendFont = new java.awt.Font(legendFontName, legendFontStyle, legendFontSize); Integer tickLabelRotate = report.getTickLabelRotate(); String dataFontName = report.getDataFontName(); Integer dataFontStyle = report.getDataFontStyle(); Integer dataFontSize = report.getDataFontSize(); java.awt.Font dataFont = new java.awt.Font(dataFontName, dataFontStyle, dataFontSize); Plot plot = chart.getPlot(); if (!(plot instanceof MultiplePiePlot)) { CategoryPlot categoryPlot = chart.getCategoryPlot(); CategoryItemRenderer renderer = categoryPlot.getRenderer(); renderer.setBaseItemLabelGenerator(new LabelGenerator(0.0)); renderer.setBaseItemLabelFont(dataFont); renderer.setBaseItemLabelsVisible(true); if (chartType == ChartReport.Type.VERTLINE || chartType == ChartReport.Type.HORIZLINE) { LineAndShapeRenderer lineRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer(); lineRenderer.setBaseShapesVisible(true); lineRenderer.setDrawOutlines(true); lineRenderer.setUseFillPaint(true); } } if (plot instanceof CategoryPlot) { CategoryPlot catPlot = (CategoryPlot) plot; catPlot.getDomainAxis().setLabelFont(axisFont); catPlot.getRangeAxis().setLabelFont(axisFont); catPlot.getDomainAxis().setTickLabelFont(axisTickFont); catPlot.getRangeAxis().setTickLabelFont(axisTickFont); catPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(100.0f); double angle = -2.0 * Math.PI / 360.0 * (double) tickLabelRotate; CategoryLabelPositions oldp = catPlot.getDomainAxis().getCategoryLabelPositions(); CategoryLabelPositions newp = new CategoryLabelPositions(oldp.getLabelPosition(RectangleEdge.TOP), new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, angle, CategoryLabelWidthType.RANGE, 0.0f), oldp.getLabelPosition(RectangleEdge.LEFT), oldp.getLabelPosition(RectangleEdge.RIGHT)); catPlot.getDomainAxis().setCategoryLabelPositions(newp); } else if (plot instanceof PiePlot3D) { PiePlot3D piePlot = (PiePlot3D) plot; piePlot.setLabelFont(axisFont); piePlot.setDirection(org.jfree.util.Rotation.CLOCKWISE); piePlot.setForegroundAlpha(0.5f); piePlot.setNoDataMessage("?"); } else if (plot instanceof PiePlot) { PiePlot piePlot = (PiePlot) plot; piePlot.setLabelFont(axisFont); } LegendTitle legend = (LegendTitle) chart.getLegend(); if (legend != null) { legend.setItemFont(legendFont); RectangleEdge legendRectEdge = RectangleEdge.BOTTOM; Integer legendPosition = report.getLegendPosition(); switch (legendPosition) { case 0: legendRectEdge = RectangleEdge.LEFT; break; case 1: legendRectEdge = RectangleEdge.TOP; break; case 2: legendRectEdge = RectangleEdge.RIGHT; break; case 3: legendRectEdge = RectangleEdge.BOTTOM; break; } legend.setPosition(legendRectEdge); } } catch (Exception e) { logger.error("Chart Export Exception", e); } ByteArrayOutputStream out = new ByteArrayOutputStream(); ChartUtilities.writeChartAsPNG(out, chart, report.getChartWidth(), report.getChartHeight()); return out.toByteArray(); }
From source file:com.tonbeller.jpivot.chart.ChartComponent.java
/** * Entry point for producing charts, called by wcf render tag. * Produces a jfreechart dataset from olap model, then creates a chart and * writes it to the servlet container temp directory. * Returns a DOM document for Renderer to transform into html. * Requires that jfreechart servlet is installed in this application context. *//* ww w . j a v a2 s . c om*/ public Document render(RequestContext context) throws Exception { // check if we need to produce a new chart if (dirty) { // clear old listeners dispatcher.clear(); this.result = olapModel.getResult(); this.cellIterator = result.getCells().iterator(); this.dimCount = result.getAxes().length; DefaultCategoryDataset dataset = null; switch (dimCount) { case 1: logger.info("1-dim data"); dataset = build1dimDataset(); break; case 2: logger.info("2-dim data"); dataset = build2dimDataset(); break; default: logger.error("less than 1 or more than 2 dimensions"); throw new IllegalArgumentException("ChartRenderer requires a 1 or 2 dimensional result"); } // re-set dirty flag dirty = false; // create font titleFont = new java.awt.Font(fontName, fontStyle, fontSize); CategoryURLGenerator urlGenerator = new jpivotCategoryURLGenerator(webControllerURL); // Create the chart object JFreeChart chart = null; switch (chartType) { case 1: chart = ChartFactory.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 2: chart = ChartFactory.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 3: chart = ChartFactory.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 4: chart = ChartFactory.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 5: chart = ChartFactory.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 6: chart = ChartFactory.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 7: chart = ChartFactory.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 8: chart = ChartFactory.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 9: chart = ChartFactory.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 10: chart = ChartFactory.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 11: chart = ChartFactory.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 12: chart = ChartFactory.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 13: chart = ChartFactory.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 14: chart = ChartFactory.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator); break; case 15: chart = ChartFactory.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN, showLegend, showTooltips, drillThroughEnabled, new jpivotPieURLGenerator(TableOrder.BY_COLUMN, dataset, webControllerURL)); break; case 16: chart = ChartFactory.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend, showTooltips, drillThroughEnabled, new jpivotPieURLGenerator(TableOrder.BY_ROW, dataset, webControllerURL)); break; case 17: chart = ChartFactory.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN, showLegend, showTooltips, drillThroughEnabled, new jpivotPieURLGenerator(TableOrder.BY_COLUMN, dataset, webControllerURL)); break; case 18: chart = ChartFactory.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend, showTooltips, drillThroughEnabled, new jpivotPieURLGenerator(TableOrder.BY_ROW, dataset, webControllerURL)); break; default: throw new Exception("An unknown Chart Type was requested"); } try { chart.setBackgroundPaint(new java.awt.Color(bgColorR, bgColorG, bgColorB)); java.awt.Font slicerFont = new java.awt.Font(slicerFontName, slicerFontStyle, slicerFontSize); java.awt.Font axisFont = new java.awt.Font(axisFontName, axisFontStyle, axisFontSize); java.awt.Font axisTickFont = new java.awt.Font(axisTickFontName, axisTickFontStyle, axisTickFontSize); java.awt.Font legendFont = new java.awt.Font(legendFontName, legendFontStyle, legendFontSize); Plot plot = chart.getPlot(); if (plot instanceof CategoryPlot) { CategoryPlot catPlot = (CategoryPlot) plot; catPlot.getDomainAxis().setLabelFont(axisFont); catPlot.getRangeAxis().setLabelFont(axisFont); catPlot.getDomainAxis().setTickLabelFont(axisTickFont); catPlot.getRangeAxis().setTickLabelFont(axisTickFont); catPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(100.0f); double angle = -2.0 * Math.PI / 360.0 * (double) tickLabelRotate; CategoryLabelPositions oldp = catPlot.getDomainAxis().getCategoryLabelPositions(); CategoryLabelPositions newp = new CategoryLabelPositions( oldp.getLabelPosition(RectangleEdge.TOP), new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, angle, CategoryLabelWidthType.RANGE, 0.0f), oldp.getLabelPosition(RectangleEdge.LEFT), oldp.getLabelPosition(RectangleEdge.RIGHT)); catPlot.getDomainAxis().setCategoryLabelPositions(newp); } else if (plot instanceof PiePlot3D) { PiePlot3D piePlot = (PiePlot3D) plot; //piePlot.setSectionLabelFont(axisFont); piePlot.setLabelFont(axisFont); //piePlot.setSeriesLabelFont(axisTickFont); //piePlot.setSectionLabelType(piePlot.NO_LABELS); piePlot.setDirection(org.jfree.util.Rotation.CLOCKWISE); piePlot.setForegroundAlpha(0.5f); piePlot.setNoDataMessage("No data to display"); } else if (plot instanceof PiePlot) { PiePlot piePlot = (PiePlot) plot; //piePlot.setSectionLabelFont(axisFont); //piePlot.setSeriesLabelFont(axisTickFont); piePlot.setLabelFont(axisFont); //piePlot.setSectionLabelType(piePlot.NO_LABELS); } LegendTitle legend = (LegendTitle) chart.getLegend(); if (legend != null) { legend.setItemFont(legendFont); /* RectangleAnchor legendRectAnchor=RectangleAnchor.BOTTOM; switch (legendPosition){ case 0: legendRectAnchor = RectangleAnchor.LEFT; break; case 1: legendRectAnchor = RectangleAnchor.TOP; break; case 2: legendRectAnchor = RectangleAnchor.RIGHT; break; case 3: legendRectAnchor = RectangleAnchor.BOTTOM; break; } legend.setLegendItemGraphicAnchor(legendRectAnchor); */ RectangleEdge legendRectEdge = RectangleEdge.BOTTOM; switch (legendPosition) { case 0: legendRectEdge = RectangleEdge.LEFT; break; case 1: legendRectEdge = RectangleEdge.TOP; break; case 2: legendRectEdge = RectangleEdge.RIGHT; break; case 3: legendRectEdge = RectangleEdge.BOTTOM; break; } legend.setPosition(legendRectEdge); } if (showSlicer) { RectangleEdge slicerRectPos = RectangleEdge.BOTTOM; HorizontalAlignment slicerHorizAlignment = HorizontalAlignment.LEFT; switch (slicerPosition) { case 0: slicerRectPos = RectangleEdge.TOP; break; case 1: slicerRectPos = RectangleEdge.BOTTOM; break; case 2: slicerRectPos = RectangleEdge.RIGHT; break; case 3: slicerRectPos = RectangleEdge.LEFT; break; } switch (slicerAlignment) { case 4: slicerHorizAlignment = HorizontalAlignment.CENTER; break; case 3: slicerHorizAlignment = HorizontalAlignment.LEFT; break; case 2: slicerHorizAlignment = HorizontalAlignment.RIGHT; break; } TextTitle slicer = new TextTitle(buildSlicer(), slicerFont, Color.BLACK, slicerRectPos, slicerHorizAlignment, VerticalAlignment.CENTER, new RectangleInsets(0, 0, 0, 0)); slicer.setPosition(slicerRectPos); chart.addSubtitle(slicer); } info = new ChartRenderingInfo(new StandardEntityCollection()); // Write the chart image to the temporary directory HttpSession session = context.getSession(); filename = ServletUtilities.saveChartAsPNG(chart, chartWidth, chartHeight, info, session); } catch (Exception e) { filename = "public_error_500x300.png"; dirty = true; } } // new DOM document DocumentBuilder parser = XmlUtils.getParser(); // get an image map for the chart, wrap it in xchart tags String xchart = "<xchart>" + writeImageMap(filename, info, false) + "</xchart>"; /* if (logger.isDebugEnabled()) { logger.debug("Chart XML"); logger.debug(xchart); } */ // create an InputStream from the DOM document InputStream stream = new ByteArrayInputStream(xchart.getBytes("UTF-8")); document = parser.parse(stream); Element root = document.getDocumentElement(); // create url for img tag String graphURL = getGraphURL(context); Element img = document.createElement("img"); img.setAttribute("src", graphURL); img.setAttribute("width", new Integer(chartWidth).toString()); img.setAttribute("height", new Integer(chartHeight).toString()); img.setAttribute("style", "border:0;"); img.setAttribute("usemap", "#" + filename); root.appendChild(img); return document; }
From source file:hr.restart.util.chart.ChartXY.java
/** * Creates a BAR CHART// ww w. j ava2 s .c om * @param dataset The org.jfree.data.CategoryDataset * @param title The title * @return org.jfree.chart.JFreeChart */ private JFreeChart createBarChart(final CategoryDataset dataset, String title, PlotOrientation orientation) { final JFreeChart chart = ChartFactory.createBarChart(title, // chart title "", // domain axis label "", // range axis label dataset, // data orientation, // the plot orientation false, // include legend true, false); chart.setBackgroundPaint(Color.white); // the subtitle from the combobox if (jcb != null) chart.addSubtitle(new TextTitle(jcb.getSelectedItem().toString())); //subtitles setted by the user. if (getSubtitles() != null) for (int i = 0; i < getSubtitles().size(); i++) { chart.addSubtitle(new TextTitle(getSubtitles().get(i).toString())); } final Plot plot = chart.getPlot(); // get a reference to the plot for further customisation... final CategoryPlot categoryPlot = (CategoryPlot) plot; categoryPlot.setNoDataMessage("NO DATA!"); categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); final CategoryItemRenderer renderer = new CustomRenderer(new Paint[] { Color.red, Color.blue, Color.green, Color.yellow, Color.orange, Color.cyan, Color.magenta, Color.blue }); categoryPlot.setRenderer(renderer); renderer.setLabelGenerator(new StandardCategoryLabelGenerator()); renderer.setItemLabelsVisible(true); // inside //renderer.setBaseItemLabelPaint(Color.white); Font font = new Font("SansSerif", Font.PLAIN, 7); Font derive = font.deriveFont(Font.BOLD); renderer.setBaseItemLabelFont(derive); // margin final CategoryAxis domainAxis = categoryPlot.getDomainAxis(); domainAxis.setLowerMargin(0.0); domainAxis.setUpperMargin(0.0); //domainAxis.setBottomCategoryLabelPosition(new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER)); domainAxis.setCategoryLabelPositions(new CategoryLabelPositions( new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_CENTER), // TOP new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER), // BOTTOM new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, CategoryLabelWidthType.RANGE, 0.30f), // LEFT new CategoryLabelPosition(RectangleAnchor.RIGHT, TextBlockAnchor.CENTER_RIGHT, CategoryLabelWidthType.RANGE, 0.30f) // RIGHT) )); final ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 0.0); renderer.setPositiveItemLabelPosition(p); if (comboBoxOrientation != null) { if (comboBoxOrientation.getSelectedItem() == "Vertikalni") { domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0)); } } return chart; }
From source file:org.pentaho.plugin.jfreereport.reportcharts.CategoricalChartExpression.java
protected void configureChart(final JFreeChart chart) { super.configureChart(chart); final CategoryPlot cpl = chart.getCategoryPlot(); final CategoryItemRenderer renderer = cpl.getRenderer(); if (StringUtils.isEmpty(getTooltipFormula()) == false) { renderer.setBaseToolTipGenerator( new FormulaCategoryTooltipGenerator(getRuntime(), getTooltipFormula())); }/*from ww w. ja v a 2 s . c o m*/ if (StringUtils.isEmpty(getUrlFormula()) == false) { renderer.setBaseItemURLGenerator(new FormulaCategoryURLGenerator(getRuntime(), getUrlFormula())); } if (this.categoricalLabelFormat != null) { final StandardCategoryItemLabelGenerator scilg; if (categoricalLabelDecimalFormat != null) { final DecimalFormat numFormat = new DecimalFormat(categoricalLabelDecimalFormat, new DecimalFormatSymbols(getRuntime().getResourceBundleFactory().getLocale())); numFormat.setRoundingMode(RoundingMode.HALF_UP); scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, numFormat); } else if (categoricalLabelDateFormat != null) { scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, new SimpleDateFormat( categoricalLabelDateFormat, getRuntime().getResourceBundleFactory().getLocale())); } else { final DecimalFormat formatter = new DecimalFormat(); formatter.setDecimalFormatSymbols( new DecimalFormatSymbols(getRuntime().getResourceBundleFactory().getLocale())); scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, formatter); } renderer.setBaseItemLabelGenerator(scilg); } renderer.setBaseItemLabelsVisible(Boolean.TRUE.equals(getItemsLabelVisible())); if (getItemLabelFont() != null) { renderer.setBaseItemLabelFont(getItemLabelFont()); } if (categoricalItemLabelRotation != null) { final ItemLabelPosition orgPosItemLabelPos = renderer.getBasePositiveItemLabelPosition(); if (orgPosItemLabelPos == null) { final ItemLabelPosition pos2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER, TextAnchor.CENTER, categoricalItemLabelRotation.doubleValue()); renderer.setBasePositiveItemLabelPosition(pos2); } else { final ItemLabelPosition pos2 = new ItemLabelPosition(orgPosItemLabelPos.getItemLabelAnchor(), orgPosItemLabelPos.getTextAnchor(), orgPosItemLabelPos.getRotationAnchor(), categoricalItemLabelRotation.doubleValue()); renderer.setBasePositiveItemLabelPosition(pos2); } final ItemLabelPosition orgNegItemLabelPos = renderer.getBaseNegativeItemLabelPosition(); if (orgNegItemLabelPos == null) { final ItemLabelPosition pos2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER, TextAnchor.CENTER, categoricalItemLabelRotation.doubleValue()); renderer.setBaseNegativeItemLabelPosition(pos2); } else { final ItemLabelPosition neg2 = new ItemLabelPosition(orgNegItemLabelPos.getItemLabelAnchor(), orgNegItemLabelPos.getTextAnchor(), orgNegItemLabelPos.getRotationAnchor(), categoricalItemLabelRotation.doubleValue()); renderer.setBaseNegativeItemLabelPosition(neg2); } } final Font labelFont = Font.decode(getLabelFont()); final CategoryAxis categoryAxis = cpl.getDomainAxis(); categoryAxis.setLabelFont(labelFont); categoryAxis.setTickLabelFont(labelFont); if (getCategoryTitleFont() != null) { categoryAxis.setLabelFont(getCategoryTitleFont()); } if (getCategoryTickFont() != null) { categoryAxis.setTickLabelFont(getCategoryTickFont()); } if (maxCategoryLabelWidthRatio != null) { categoryAxis.setMaximumCategoryLabelWidthRatio(maxCategoryLabelWidthRatio.floatValue()); } cpl.setDomainGridlinesVisible(showGridlines); if (labelRotation != null) { double angle = labelRotation.doubleValue(); CategoryLabelPosition top = createUpRotationCategoryLabelPosition(PlaneDirection.TOP, angle); CategoryLabelPosition bottom = createUpRotationCategoryLabelPosition(PlaneDirection.BOTTOM, angle); CategoryLabelPosition left = createUpRotationCategoryLabelPosition(PlaneDirection.LEFT, angle); CategoryLabelPosition right = createUpRotationCategoryLabelPosition(PlaneDirection.RIGHT, angle); CategoryLabelPositions rotationLabelPositions = new CategoryLabelPositions(top, bottom, left, right); categoryAxis.setCategoryLabelPositions(rotationLabelPositions); } final String[] colors = getSeriesColor(); for (int i = 0; i < colors.length; i++) { renderer.setSeriesPaint(i, parseColorFromString(colors[i])); } if (lowerMargin != null) { categoryAxis.setLowerMargin(lowerMargin.doubleValue()); } if (upperMargin != null) { categoryAxis.setUpperMargin(upperMargin.doubleValue()); } if (categoryMargin != null) { categoryAxis.setCategoryMargin(categoryMargin.doubleValue()); } configureRangeAxis(cpl, labelFont); }