List of usage examples for org.jfree.chart.title LegendTitle setPosition
public void setPosition(RectangleEdge position)
From source file:uk.ac.lkl.cram.ui.chart.TLALearningTypeChartFactory.java
private static JFreeChart createChart(PieDataset dataset) { //Create a pie chart from the chart factory with no title, a legend and no tooltips JFreeChart chart = ChartFactory.createPieChart(null, dataset, true, false, false); //Set the background colour of the chart Paint backgroundPaint = Color.white; chart.setBackgroundPaint(backgroundPaint); //Get the plot from the chart PiePlot plot = (PiePlot) chart.getPlot(); //Set the background colour of the plot to be the same as the chart plot.setBackgroundPaint(backgroundPaint); //Remove shadows from the plot plot.setShadowXOffset(0);//from w ww . ja v a 2 s . com plot.setShadowYOffset(0); //Remove the outline from the plot plot.setOutlineVisible(false); //Remove the labels from the plot plot.setLabelGenerator(null); //Set the colours for the segments plot.setSectionPaint(LearningTypeChartMaker.ACQUISITION, LearningTypeChartMaker.ACQUISITION_COLOR); plot.setSectionPaint(LearningTypeChartMaker.COLLABORATION, LearningTypeChartMaker.COLLABORATION_COLOR); plot.setSectionPaint(LearningTypeChartMaker.DISCUSSION, LearningTypeChartMaker.DISCUSSION_COLOR); plot.setSectionPaint(LearningTypeChartMaker.INQUIRY, LearningTypeChartMaker.INQUIRY_COLOR); plot.setSectionPaint(LearningTypeChartMaker.PRACTICE, LearningTypeChartMaker.PRACTICE_COLOR); plot.setSectionPaint(LearningTypeChartMaker.PRODUCTION, LearningTypeChartMaker.PRODUCTION_COLOR); //Get the legend from the chart LegendTitle legend = chart.getLegend(); //Set the font of the legend to be the same as the platform UI legend.setItemFont(UIManager.getFont("Label.font")); //Set the background colour of the legend to be the same as the chart legend.setBackgroundPaint(backgroundPaint); //Remove the border from the legend legend.setFrame(BlockBorder.NONE); //Locate the legend to the right of the plot legend.setPosition(RectangleEdge.RIGHT); return chart; }
From source file:org.exist.xquery.modules.jfreechart.JFreeChartFactory.java
/** * Create JFreeChart graph using the supplied parameters. * * @param chartType One of the many chart types. * @param conf Chart configuration/*from w w w . ja v a 2 s. co m*/ * @param is Inputstream containing chart data * @return Initialized chart or NULL in case of issues. * @throws IOException Thrown when a problem is reported while parsing XML data. */ public static JFreeChart createJFreeChart(String chartType, Configuration conf, InputStream is) throws XPathException { logger.debug("Generating " + chartType); // Currently two dataset types supported CategoryDataset categoryDataset = null; PieDataset pieDataset = null; try { if ("PieChart".equals(chartType) || "PieChart3D".equals(chartType) || "RingChart".equals(chartType)) { logger.debug("Reading XML PieDataset"); pieDataset = DatasetReader.readPieDatasetFromXML(is); } else { logger.debug("Reading XML CategoryDataset"); categoryDataset = DatasetReader.readCategoryDatasetFromXML(is); } } catch (IOException ex) { throw new XPathException(ex.getMessage()); } finally { try { is.close(); } catch (IOException ex) { // } } // Return chart JFreeChart chart = null; // Big chart type switch if ("AreaChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createAreaChart(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("BarChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createBarChart(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("BarChart3D".equalsIgnoreCase(chartType)) { chart = ChartFactory.createBarChart3D(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("LineChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createLineChart(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("LineChart3D".equalsIgnoreCase(chartType)) { chart = ChartFactory.createLineChart3D(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("MultiplePieChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createMultiplePieChart(conf.getTitle(), categoryDataset, conf.getOrder(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setPieChartParameters(chart, conf); } else if ("MultiplePieChart3D".equalsIgnoreCase(chartType)) { chart = ChartFactory.createMultiplePieChart3D(conf.getTitle(), categoryDataset, conf.getOrder(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setPieChartParameters(chart, conf); } else if ("PieChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createPieChart(conf.getTitle(), pieDataset, conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setPieChartParameters(chart, conf); } else if ("PieChart3D".equalsIgnoreCase(chartType)) { chart = ChartFactory.createPieChart3D(conf.getTitle(), pieDataset, conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setPieChartParameters(chart, conf); } else if ("RingChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createRingChart(conf.getTitle(), pieDataset, conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setPieChartParameters(chart, conf); } else if ("SpiderWebChart".equalsIgnoreCase(chartType)) { SpiderWebPlot plot = new SpiderWebPlot(categoryDataset); if (conf.isGenerateTooltips()) { plot.setToolTipGenerator(new StandardCategoryToolTipGenerator()); } chart = new JFreeChart(conf.getTitle(), JFreeChart.DEFAULT_TITLE_FONT, plot, false); if (conf.isGenerateLegend()) { LegendTitle legend = new LegendTitle(plot); legend.setPosition(RectangleEdge.BOTTOM); chart.addSubtitle(legend); } else { TextTitle subTitle = new TextTitle(" "); subTitle.setPosition(RectangleEdge.BOTTOM); chart.addSubtitle(subTitle); } setCategoryChartParameters(chart, conf); } else if ("StackedAreaChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createStackedAreaChart(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("StackedBarChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createStackedBarChart(conf.getTitle(), conf.getDomainAxisLabel(), conf.getRangeAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("StackedBarChart3D".equalsIgnoreCase(chartType)) { chart = ChartFactory.createStackedBarChart3D(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else if ("WaterfallChart".equalsIgnoreCase(chartType)) { chart = ChartFactory.createWaterfallChart(conf.getTitle(), conf.getCategoryAxisLabel(), conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls()); setCategoryChartParameters(chart, conf); } else { logger.error("Illegal chartype. Choose one of " + "AreaChart BarChart BarChart3D LineChart LineChart3D " + "MultiplePieChart MultiplePieChart3D PieChart PieChart3D " + "RingChart SpiderWebChart StackedAreaChart StackedBarChart " + "StackedBarChart3D WaterfallChart"); } setCommonParameters(chart, conf); return chart; }
From source file:com.hmsinc.epicenter.webapp.chart.ChartService.java
/** * @param chart/*from ww w .j ava 2s .co m*/ * @return */ private static void configureTitleAndLegend(final JFreeChart chart, final AbstractChart adapter) { final TextTitle title = chart.getTitle(); if (title != null) { title.setFont(new Font("Arial", Font.BOLD, 12)); // title.setBackgroundPaint(Color.CYAN); title.setTextAlignment(HorizontalAlignment.LEFT); title.setHorizontalAlignment(HorizontalAlignment.CENTER); title.setMargin(0, 4, 5, 6); } if (chart.getLegend() != null) { chart.removeLegend(); final LegendTitle legend = new LegendTitle(chart.getPlot(), new SNColumnArrangement(0, 0), new ColumnArrangement(HorizontalAlignment.CENTER, VerticalAlignment.CENTER, 0, 0)); legend.setItemFont(LEGEND_FONT); legend.setPosition(RectangleEdge.BOTTOM); legend.setHorizontalAlignment(HorizontalAlignment.CENTER); legend.setBackgroundPaint(Color.WHITE); legend.setFrame(new LineBorder()); legend.setMargin(0, 4, 5, 6); chart.addLegend(legend); // Now we'll try to remove any duplicate items from the legend.. final Map<String, Integer> keys = new HashMap<String, Integer>(); final LegendItemCollection items = new LegendItemCollection(); for (LegendItemSource source : legend.getSources()) { for (int i = 0; i < source.getLegendItems().getItemCount(); i++) { final LegendItem item = source.getLegendItems().get(i); if (!keys.containsKey(item.getLabel())) { keys.put(item.getLabel(), i); items.add(item); } } } legend.setSources(new LegendItemSource[] { new LegendItemSource() { /* * (non-Javadoc) * * @see org.jfree.chart.LegendItemSource#getLegendItems() */ public LegendItemCollection getLegendItems() { return items; } } }); } }
From source file:de.laures.cewolf.util.Renderer.java
/** * Renders a legend/*from w ww. j av a 2 s . co m*/ * @param cd the chart iamge to be rendred * @return the rendered image * @throws CewolfException */ private static RenderedImage renderLegend(ChartImage cd, Object c) throws CewolfException { try { JFreeChart chart = (JFreeChart) c; final int width = cd.getWidth(); final int height = cd.getHeight(); LegendTitle legend = getLegend(chart); boolean haslegend = true; // with JFreeChart v0.9.20, the only way to get a valid legend, // is either to retrieve it from the chart or to assign a new // one to the chart. In the case where the chart has no legend, // a new one must be assigned, but just for rendering. After, we // have to reset the legend to null in the chart. if (null == legend) { haslegend = false; legend = new LegendTitle(chart.getPlot()); } legend.setPosition(RectangleEdge.BOTTOM); BufferedImage bimage = ImageHelper.createImage(width, height); Graphics2D g = bimage.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); legend.arrange(g, new RectangleConstraint(width, height)); legend.draw(g, new Rectangle(width, height)); ByteArrayOutputStream out = new ByteArrayOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); param.setQuality(1.0f, true); encoder.encode(bimage, param); out.close(); // if the chart had no legend, reset it to null in order to give back the // chart in the state we received it. if (!haslegend) { removeLegend(chart); } return new RenderedImage(out.toByteArray(), "image/jpeg", new ChartRenderingInfo(new StandardEntityCollection())); } catch (IOException ioex) { log.error(ioex); throw new ChartRenderingException(ioex.getMessage(), ioex); } }
From source file:org.jfree.chart.demo.LegendWrapperDemo1.java
private static JFreeChart createChart(PieDataset piedataset) { JFreeChart jfreechart = ChartFactory.createPieChart("Legend Wrapper Demo 1", piedataset, false, true, false);//from w w w . ja va 2 s .c om PiePlot pieplot = (PiePlot) jfreechart.getPlot(); pieplot.setLabelFont(new Font("SansSerif", 0, 12)); pieplot.setNoDataMessage("No data available"); pieplot.setCircular(true); pieplot.setLabelGap(0.02D); LegendTitle legendtitle = new LegendTitle(jfreechart.getPlot()); BlockContainer blockcontainer = new BlockContainer(new BorderArrangement()); blockcontainer.setFrame(new BlockBorder(1.0D, 1.0D, 1.0D, 1.0D)); LabelBlock labelblock = new LabelBlock("Legend Items:", new Font("SansSerif", 1, 12)); labelblock.setPadding(5D, 5D, 5D, 5D); blockcontainer.add(labelblock, RectangleEdge.TOP); LabelBlock labelblock1 = new LabelBlock("Source: http://www.jfree.org"); labelblock1.setPadding(8D, 20D, 2D, 5D); blockcontainer.add(labelblock1, RectangleEdge.BOTTOM); BlockContainer blockcontainer1 = legendtitle.getItemContainer(); blockcontainer1.setPadding(2D, 10D, 5D, 2D); blockcontainer.add(blockcontainer1); legendtitle.setWrapper(blockcontainer); legendtitle.setPosition(RectangleEdge.RIGHT); legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT); jfreechart.addSubtitle(legendtitle); return jfreechart; }
From source file:sernet.gs.ui.rcp.main.bsi.views.chart.LebenszyklusBarChart.java
protected JFreeChart createSpiderChart(Object dataset) { final int startAngle = 54; final double interiorGap = 0.4d; SpiderWebPlot plot = new SpiderWebPlot((CategoryDataset) dataset); plot.setStartAngle(startAngle);/* www. j a va2 s. c om*/ plot.setInteriorGap(interiorGap); plot.setToolTipGenerator(new StandardCategoryToolTipGenerator()); JFreeChart chart = new JFreeChart(Messages.LebenszyklusBarChart_1, TextTitle.DEFAULT_FONT, plot, false); LegendTitle legend = new LegendTitle(plot); legend.setPosition(RectangleEdge.BOTTOM); chart.addSubtitle(legend); return chart; }
From source file:net.sf.jasperreports.components.spiderchart.SpiderChartDesignEvaluator.java
/** * //from w w w .j a v a 2 s .c o m */ public static JRRenderable evaluateRenderer(ReportConverter reportConverter, JRComponentElement element) { SpiderChartComponent chartComponent = (SpiderChartComponent) element.getComponent(); ChartSettings chartSettings = chartComponent.getChartSettings(); SpiderPlot plot = chartComponent.getPlot(); SpiderWebPlot spiderWebPlot = new SpiderWebPlot(getSampleDataset()); if (plot.getAxisLineColor() != null) { spiderWebPlot.setAxisLinePaint(plot.getAxisLineColor()); } if (plot.getAxisLineWidth() != null) { spiderWebPlot.setAxisLineStroke(new BasicStroke(plot.getAxisLineWidth())); } if (plot.getBackcolor() != null) { spiderWebPlot.setBackgroundPaint(plot.getBackcolor()); } if (plot.getBackgroundAlpha() != null) { spiderWebPlot.setBackgroundAlpha(plot.getBackgroundAlpha()); } if (plot.getForegroundAlpha() != null) { spiderWebPlot.setForegroundAlpha(plot.getForegroundAlpha()); } if (plot.getHeadPercent() != null) { spiderWebPlot.setHeadPercent(plot.getHeadPercent()); } if (plot.getInteriorGap() != null) { spiderWebPlot.setInteriorGap(plot.getInteriorGap()); } if (plot.getLabelColor() != null) { spiderWebPlot.setLabelPaint(plot.getLabelColor()); } if (plot.getLabelFont() != null) { spiderWebPlot.setLabelFont(JRFontUtil.getAwtFont(plot.getLabelFont(), Locale.getDefault())); } if (plot.getLabelGap() != null) { spiderWebPlot.setAxisLabelGap(plot.getLabelGap()); } spiderWebPlot.setMaxValue(MAX_VALUE); if (plot.getRotation() != null) { spiderWebPlot.setDirection(plot.getRotation().getRotation()); } if (plot.getStartAngle() != null) { spiderWebPlot.setStartAngle(plot.getStartAngle()); } if (plot.getTableOrder() != null) { spiderWebPlot.setDataExtractOrder(plot.getTableOrder().getOrder()); } if (plot.getWebFilled() != null) { spiderWebPlot.setWebFilled(plot.getWebFilled()); } spiderWebPlot.setToolTipGenerator(new StandardCategoryToolTipGenerator()); spiderWebPlot.setLabelGenerator(new StandardCategoryItemLabelGenerator()); String titleText = JRExpressionUtil.getExpressionText(chartSettings.getTitleExpression()); Font titleFont = chartSettings.getTitleFont() != null ? JRFontUtil.getAwtFont(chartSettings.getTitleFont(), Locale.getDefault()) : TextTitle.DEFAULT_FONT; JFreeChart jfreechart = new JFreeChart(titleText, titleFont, spiderWebPlot, true); if (chartSettings.getBackcolor() != null) { jfreechart.setBackgroundPaint(chartSettings.getBackcolor()); } RectangleEdge titleEdge = getEdge(chartSettings.getTitlePosition(), RectangleEdge.TOP); if (titleText != null) { TextTitle title = jfreechart.getTitle(); title.setText(titleText); if (chartSettings.getTitleColor() != null) { title.setPaint(chartSettings.getTitleColor()); } title.setFont(titleFont); title.setPosition(titleEdge); jfreechart.setTitle(title); } String subtitleText = JRExpressionUtil.getExpressionText(chartSettings.getSubtitleExpression()); if (subtitleText != null) { TextTitle subtitle = new TextTitle(subtitleText); subtitle.setText(subtitleText); if (chartSettings.getSubtitleColor() != null) { subtitle.setPaint(chartSettings.getSubtitleColor()); } if (chartSettings.getSubtitleColor() != null) { Font subtitleFont = chartSettings.getSubtitleFont() != null ? JRFontUtil.getAwtFont(chartSettings.getSubtitleFont(), Locale.getDefault()) : TextTitle.DEFAULT_FONT; subtitle.setFont(subtitleFont); } subtitle.setPosition(titleEdge); jfreechart.addSubtitle(subtitle); } // Apply all of the legend formatting options LegendTitle legend = jfreechart.getLegend(); if (legend != null) { legend.setVisible((chartSettings.getShowLegend() == null || chartSettings.getShowLegend())); if (legend.isVisible()) { if (chartSettings.getLegendColor() != null) { legend.setItemPaint(chartSettings.getLegendColor()); } if (chartSettings.getLegendBackgroundColor() != null) { legend.setBackgroundPaint(chartSettings.getLegendBackgroundColor()); } if (chartSettings.getLegendFont() != null) { legend.setItemFont(JRFontUtil.getAwtFont(chartSettings.getLegendFont(), Locale.getDefault())); } legend.setPosition(getEdge(chartSettings.getLegendPosition(), RectangleEdge.BOTTOM)); } } Rectangle2D rectangle = new Rectangle2D.Double(0, 0, element.getWidth(), element.getHeight()); String renderType = chartSettings.getRenderType(); if (renderType == null) { renderType = JRProperties.getProperty(reportConverter.getReport(), JRChart.PROPERTY_CHART_RENDER_TYPE); } return ChartUtil.getChartRendererFactory(renderType).getRenderer(jfreechart, null, rectangle); }
From source file:sernet.gs.ui.rcp.main.bsi.views.chart.SchichtenBarChart.java
protected JFreeChart createSpiderChart(Object dataset) { SpiderWebPlot plot = new SpiderWebPlot((CategoryDataset) dataset); plot.setToolTipGenerator(new StandardCategoryToolTipGenerator()); JFreeChart chart = new JFreeChart(Messages.SchichtenBarChart_3, TextTitle.DEFAULT_FONT, plot, false); LegendTitle legend = new LegendTitle(plot); legend.setPosition(RectangleEdge.BOTTOM); chart.addSubtitle(legend);// w w w .j a v a 2 s. co m return chart; }
From source file:uk.ac.ed.epcc.webapp.charts.jfreechart.JFreePieChartData.java
@Override public JFreeChart getJFreeChart() { DefaultPieDataset pieDataset = makeDataSet(); JFreeChart chart = ChartFactory.createPieChart(title, // Title pieDataset, // Dataset true // Show legend , false, false);//from w w w .j a va2 s . c o m PiePlot plot = (PiePlot) chart.getPlot(); StandardPieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0} ({2})"); plot.setLabelGenerator(gen); plot.setMaximumLabelWidth(0.07); plot.setLabelFont(plot.getLabelFont().deriveFont(9.0F)); LegendTitle leg = chart.getLegend(); leg.setPosition(RectangleEdge.RIGHT); if (!colours.isEmpty()) { String legends[] = ds.getLegends(); for (int i = 0; i < ds.getNumSets(); i++) { if (legends != null && legends.length > i && colours.size() > i) { Color c = colours.get(i); plot.setSectionPaint(legends[i], c); } } } return chart; }
From source file:com.thalesgroup.hudson.plugins.klocwork.graph.KloTrendGraph.java
@Override protected JFreeChart createGraph() { final JFreeChart chart = ChartFactory.createLineChart(null, // chart title null, // unused yLabel, // range axis label categoryDataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls );//ww w . j a va2 s. c o m // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... final LegendTitle legend = chart.getLegend(); legend.setPosition(RectangleEdge.RIGHT); chart.setBackgroundPaint(Color.white); final CategoryPlot plot = chart.getCategoryPlot(); // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); plot.setBackgroundPaint(Color.WHITE); plot.setOutlinePaint(null); plot.setRangeGridlinesVisible(true); plot.setRangeGridlinePaint(Color.black); CategoryAxis domainAxis = new ShiftedCategoryAxis(null); plot.setDomainAxis(domainAxis); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); domainAxis.setLowerMargin(0.0); domainAxis.setUpperMargin(0.0); domainAxis.setCategoryMargin(0.0); final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setLowerBound(0); rangeAxis.setAutoRange(true); final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setBaseStroke(new BasicStroke(2.0f)); ColorPalette.apply(renderer); // crop extra space around the graph plot.setInsets(new RectangleInsets(5.0, 0, 0, 5.0)); return chart; }