List of usage examples for org.jfree.chart JFreeChart getLegend
public LegendTitle getLegend()
From source file:net.sf.dynamicreports.test.jasper.chart.ChartTest.java
@Override public void test() { super.test(); numberOfPagesTest(1);//from w ww. ja va 2 s . c om chartCountTest("summary.chart1", 1); JFreeChart chart = getChart("summary.chart1", 0); TextTitle title = chart.getTitle(); Assert.assertEquals("title", "title", title.getText()); Assert.assertEquals("title color", Color.BLUE, title.getPaint()); Assert.assertEquals("title font", new Font("Arial", Font.BOLD, 10), title.getFont()); Assert.assertEquals("title position", RectangleEdge.RIGHT, title.getPosition()); TextTitle subtitle = (TextTitle) chart.getSubtitle(1); Assert.assertEquals("subtitle", "subtitle", subtitle.getText()); Assert.assertEquals("subtitle color", Color.CYAN, subtitle.getPaint()); Assert.assertEquals("subtitle font", new Font("Arial", Font.PLAIN, 10), subtitle.getFont()); LegendTitle legend = (LegendTitle) chart.getSubtitle(0); Assert.assertEquals("legend color", Color.BLUE, legend.getItemPaint()); Assert.assertEquals("legend backgroundcolor", Color.LIGHT_GRAY, legend.getBackgroundPaint()); Assert.assertEquals("legend font", new Font("Courier New", Font.PLAIN, 10), legend.getItemFont()); Assert.assertEquals("legend position", RectangleEdge.LEFT, legend.getPosition()); chartCountTest("summary.chart2", 1); chart = getChart("summary.chart2", 0); Assert.assertNull("legend", chart.getLegend()); Assert.assertEquals("plot orientation", PlotOrientation.HORIZONTAL, chart.getCategoryPlot().getOrientation()); Assert.assertEquals("plot series colors", Color.BLUE, chart.getPlot().getDrawingSupplier().getNextPaint()); Assert.assertEquals("plot series colors", Color.GREEN, chart.getPlot().getDrawingSupplier().getNextPaint()); Assert.assertEquals("plot series colors", Color.RED, chart.getPlot().getDrawingSupplier().getNextPaint()); }
From source file:chartsPK.LineChart.java
private JPanel createChartPanel(String color1, String color2) { XYDataset dataset = createDataset(); boolean showLegend = false; boolean createURL = false; boolean createTooltip = false; JFreeChart chart = ChartFactory.createXYLineChart(getMyTitle(), getAxis().getXLabel(), getAxis().getYLabel(), dataset, PlotOrientation.VERTICAL, true, true, true); Color color;//from w ww. j av a2 s .co m XYPlot plot = (XYPlot) chart.getPlot(); try { Field field = Class.forName("java.awt.Color").getField(color1); color = (Color) field.get(null); } catch (Exception e) { color = null; // Not defined } plot.setBackgroundPaint(color); try { Field field = Class.forName("java.awt.Color").getField(color2); color = (Color) field.get(null); } catch (Exception e) { color = null; // Not defined } chart.setBackgroundPaint(color); for (int i = 0; i < dataSeries.size(); i++) { try { Field field = Class.forName("java.awt.Color").getField(dataSeries.get(i).getColor()); color = (Color) field.get(null); } catch (Exception e) { color = null; // Not defined } plot.getRenderer().setSeriesPaint(i, color); } RectangleEdge p = RectangleEdge.BOTTOM; LegendTitle legend = chart.getLegend(); switch (getLegend().getPosition().toLowerCase()) { case "top": p = RectangleEdge.TOP; break; case "bottom": p = RectangleEdge.BOTTOM; break; case "left": p = RectangleEdge.LEFT; break; case "right": p = RectangleEdge.RIGHT; break; } legend.setPosition(p); try { Field field = Class.forName("java.awt.Color").getField(getLegend().getColor()); color = (Color) field.get(null); } catch (Exception e) { color = null; // Not defined } legend.setBackgroundPaint(color); return new ChartPanel(chart); }
From source file:io.github.mzmine.modules.plots.chromatogram.ChromatogramPlotWindowController.java
@FXML public void initialize() { final JFreeChart chart = chartNode.getChart(); final XYPlot plot = chart.getXYPlot(); // Do not set colors and strokes dynamically. They are instead provided // by the dataset and configured in configureRenderer() plot.setDrawingSupplier(null);// w w w. j a va2s . c om plot.setDomainGridlinePaint(JavaFXUtil.convertColorToAWT(gridColor)); plot.setRangeGridlinePaint(JavaFXUtil.convertColorToAWT(gridColor)); plot.setBackgroundPaint(JavaFXUtil.convertColorToAWT(backgroundColor)); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); plot.setDomainCrosshairPaint(JavaFXUtil.convertColorToAWT(crossHairColor)); plot.setRangeCrosshairPaint(JavaFXUtil.convertColorToAWT(crossHairColor)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); // chart properties chart.setBackgroundPaint(JavaFXUtil.convertColorToAWT(backgroundColor)); // legend properties LegendTitle legend = chart.getLegend(); // legend.setItemFont(legendFont); legend.setFrame(BlockBorder.NONE); // set the X axis (retention time) properties NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); xAxis.setLabel("Retention time (min)"); xAxis.setUpperMargin(0.03); xAxis.setLowerMargin(0.03); xAxis.setRangeType(RangeType.POSITIVE); xAxis.setTickLabelInsets(new RectangleInsets(0, 0, 20, 20)); // set the Y axis (intensity) properties NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setLabel("Intensity"); yAxis.setRangeType(RangeType.POSITIVE); yAxis.setAutoRangeIncludesZero(true); // set the fixed number formats, because otherwise JFreeChart sometimes // shows exponent, sometimes it doesn't DecimalFormat mzFormat = MZmineCore.getConfiguration().getMZFormat(); xAxis.setNumberFormatOverride(mzFormat); DecimalFormat intensityFormat = MZmineCore.getConfiguration().getIntensityFormat(); yAxis.setNumberFormatOverride(intensityFormat); chartTitle = chartNode.getChart().getTitle(); chartTitle.setMargin(5, 0, 0, 0); chartTitle.setFont(titleFont); chartTitle.setText("Chromatogram"); chartNode.setCursor(Cursor.CROSSHAIR); // Remove the dataset if it is removed from the list datasets.addListener((Change<? extends ChromatogramPlotDataSet> c) -> { while (c.next()) { if (c.wasRemoved()) { for (ChromatogramPlotDataSet ds : c.getRemoved()) { int index = plot.indexOf(ds); plot.setDataset(index, null); } } } }); itemLabelsVisible.addListener((prop, oldVal, newVal) -> { for (ChromatogramPlotDataSet dataset : datasets) { int datasetIndex = plot.indexOf(dataset); XYItemRenderer renderer = plot.getRenderer(datasetIndex); renderer.setBaseItemLabelsVisible(newVal); } }); legendVisible.addListener((prop, oldVal, newVal) -> { legend.setVisible(newVal); }); }
From source file:fr.amap.lidar.amapvox.chart.VoxelsToChart.java
public JFreeChart createChart(String title, XYSeriesCollection dataset, String xAxisLabel, String yAxisLabel) { JFreeChart chart = ChartFactory.createXYLineChart(title, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, true, true, false); String fontName = "Palatino"; chart.getTitle().setFont(new Font(fontName, Font.BOLD, 18)); XYPlot plot = (XYPlot) chart.getPlot(); plot.setDomainPannable(true);/* w w w . j a v a 2s. co m*/ plot.setRangePannable(true); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); plot.getDomainAxis().setLowerMargin(0.0); plot.getDomainAxis().setLabelFont(new Font(fontName, Font.BOLD, 14)); plot.getDomainAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12)); plot.getRangeAxis().setLabelFont(new Font(fontName, Font.BOLD, 14)); plot.getRangeAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12)); chart.getLegend().setItemFont(new Font(fontName, Font.PLAIN, 14)); chart.getLegend().setFrame(BlockBorder.NONE); LegendTitle subtitle = (LegendTitle) chart.getSubtitles().get(0); subtitle.setHorizontalAlignment(HorizontalAlignment.LEFT); XYItemRenderer r = plot.getRenderer(); if (r instanceof XYLineAndShapeRenderer) { XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; renderer.setBaseShapesVisible(true); Ellipse2D.Float shape = new Ellipse2D.Float(-2.5f, -2.5f, 5.0f, 5.0f); for (int i = 0; i < voxelFiles.length; i++) { renderer.setSeriesShape(i, shape); renderer.setSeriesPaint(i, voxelFiles[i].getSeriesParameters().getColor()); renderer.setLegendTextPaint(i, voxelFiles[i].getSeriesParameters().getColor()); } } return chart; }
From source file:com.jaxzin.iraf.forecast.swing.JForecaster.java
@SuppressWarnings({ "FieldRepeatedlyAccessedInMethod" }) private void customizeChart(final JFreeChart chart) { // Set the transparency of the histogram bars // chart.getXYPlot().setForegroundAlpha(0.5f); // Lock the y-axis to 0.0->0.5 // Customize the y-logAxis logAxis = new LogarithmicAxis("Account Value"); logAxis.setAutoRange(true);/*from w w w .j a v a2 s . c o m*/ logAxis.setAllowNegativesFlag(true); logAxis.setNumberFormatOverride(NumberFormat.getCurrencyInstance()); linearAxis = new NumberAxis("Account Value"); linearAxis.setAutoRange(true); linearAxis.setNumberFormatOverride(NumberFormat.getCurrencyInstance()); //noinspection ConditionalExpression chart.getXYPlot().setRangeAxis(controlPanel.isLogScale() ? logAxis : linearAxis); // Customize the legend (add title, reverse order, attach to right side) final BlockContainer legendWrap = new BlockContainer(); final Block title = new LabelBlock("Percentiles"); legendWrap.setArrangement(new ColumnArrangement()); legendWrap.add(title); final LegendTitle legendTitle = new LegendTitle(new ReversedLegendItemSource(chart.getXYPlot()), new ColumnArrangement(), new ColumnArrangement()); legendWrap.add(legendTitle); chart.getLegend().setWrapper(legendWrap); chart.getLegend().setPosition(RectangleEdge.RIGHT); // Customize the format of the tooltips chart.getXYPlot().getRenderer().setBaseToolTipGenerator( new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("yyyy"), NumberFormat.getCurrencyInstance())); }
From source file:com.mxgraph.examples.swing.chart.BarChartDemo1.java
/** * Creates a sample chart./*ww w .j a v a 2 s. c o m*/ * * @param dataset the dataset. * * @return The chart. */ public static JFreeChart createChart(final CategoryDataset dataset) { // create the chart... JFreeChart chart = ChartFactory.createBarChart("", // chart title "X-value", // domain axis label "Y-value", // range axis label dataset, // data PlotOrientation.HORIZONTAL, // orientation true, // include legend true, false); chart.addSubtitle(new TextTitle("http://www.bupt.edu.cn", new Font("", Font.ITALIC, 10))); // chart.setBackgroundPaint(Color.white); // chart.setBackgroundPaint(Color.getHSBColor(2, 29, 100)); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setRangeGridlinePaint(Color.white); plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setBaseItemLabelsVisible(true); // renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setCategoryMargin(0.1);// categoryAxis.setUpperMargin(0.02); categoryAxis.setLowerMargin(0.02); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setUpperMargin(0.10); // set up gradient paints for series... GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 0, 64)); GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(0, 64, 0)); GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(64, 0, 0)); renderer.setSeriesPaint(0, gp0); renderer.setSeriesPaint(1, gp1); renderer.setSeriesPaint(2, gp2); chart.getTitle().setFont(new Font("", Font.PLAIN, 16));// CategoryAxis domainAxis = plot.getDomainAxis(); // domainAxis.setLabelFont(new Font("", Font.PLAIN, 14)); // domainAxis.setTickLabelFont(new Font("", Font.PLAIN, 10)); // rangeAxis.setLabelFont(new Font("", Font.PLAIN, 15)); chart.getLegend().setItemFont(new Font("", Font.PLAIN, 15)); return chart; }
From source file:mil.tatrc.physiology.biogears.verification.ScenarioPlotTool.java
public void formatXYPlot(JFreeChart chart, Paint bgColor) { XYPlot plot = (XYPlot) chart.getPlot(); //For Scientific notation NumberFormat formatter = new DecimalFormat("0.######E0"); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setNumberFormatOverride(formatter); //White background outside of plottable area chart.setBackgroundPaint(bgColor);/* w w w. ja va2 s . c om*/ plot.setBackgroundPaint(Color.white); plot.setDomainGridlinePaint(Color.black); plot.setRangeGridlinePaint(Color.black); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); //Changing font sizes so they are readable. plot.getDomainAxis().setLabelFont(largeFont); plot.getRangeAxis().setLabelFont(largeFont); plot.getDomainAxis().setTickLabelFont(smallFont); plot.getRangeAxis().setTickLabelFont(smallFont); plot.getDomainAxis().setLabelPaint(bgColor == Color.red ? Color.white : Color.black); plot.getRangeAxis().setLabelPaint(bgColor == Color.red ? Color.white : Color.black); plot.getDomainAxis().setTickLabelPaint(bgColor == Color.red ? Color.white : Color.black); plot.getRangeAxis().setTickLabelPaint(bgColor == Color.red ? Color.white : Color.black); chart.getLegend().setItemFont(smallFont); chart.getTitle().setFont(largeFont); chart.getTitle().setPaint(bgColor == Color.red ? Color.white : Color.black); }
From source file:hr.restart.util.chart.ChartXYZ.java
/** * Creates a LINE CHART./* ww w.j a va 2 s .c o m*/ * * @param dataset The org.jfree.data.CategoryDataset * @param title The title * @return org.jfree.chart.JFreeChart */ final private JFreeChart createLineChart(final CategoryDataset dataset, String title) { // create the chart... final JFreeChart chart = ChartFactory.createLineChart(title, // chart title "", // domain axis label "", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); chart.setBackgroundPaint(Color.white); //print the subtitles java.util.List subs = getSubtitles(); if (subs != null) for (int i = 0; i < subs.size(); i++) { chart.addSubtitle(new TextTitle(subs.get(i).toString())); } // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... final StandardLegend legend = (StandardLegend) chart.getLegend(); legend.setDisplaySeriesShapes(true); legend.setShapeScaleX(1.5); legend.setShapeScaleY(1.5); legend.setDisplaySeriesLines(true); final CategoryPlot plot = (CategoryPlot) chart.getPlot(); //plot.setBackgroundPaint(Color.lightGray); //plot.setRangeGridlinePaint(Color.white); // customise the range axis... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setAutoRangeIncludesZero(true); // customise the renderer... final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setDrawShapes(true); adjustLineRenderer(renderer); // renderer.setSeriesStroke( // 0, new BasicStroke( // 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, // 1.0f, new float[] {10.0f, 6.0f}, 0.0f // ) // ); // renderer.setSeriesStroke( // 1, new BasicStroke( // 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, // 1.0f, new float[] {6.0f, 6.0f}, 0.0f // ) // ); // renderer.setSeriesStroke( // 2, new BasicStroke( // 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, // 1.0f, new float[] {2.0f, 6.0f}, 0.0f // ) // ); // OPTIONAL CUSTOMISATION COMPLETED. return chart; }
From source file:org.jfree.chart.demo.LegendManiaDemo.java
/** * Creates a sample chart.//from ww w . j av a2 s.c o m * * @param dataset * the dataset. * @return The chart. */ private JFreeChart createChart(final CategoryDataset dataset) { // create the chart... final JFreeChart chart = ChartFactory.createBarChart(CHART_TITLE, // chart title "Activity", // domain axis label "Rate", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? ); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... // set the background color for the chart... chart.setBackgroundPaint(new Color(255, 255, 180)); // get a reference to the plot for further customisation... final CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(BACKGROUND_PAINT); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); // set the range axis to display integers only... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // disable bar outlines... final BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(false); final CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)); final StandardLegend legend = (StandardLegend) chart.getLegend(); legend.setBackgroundPaint(Color.orange); legend.setOutlinePaint(Color.orange); // activate word wrapping when legend is vertical. legend.setPreferredWidth(125); // OPTIONAL CUSTOMISATION COMPLETED. return chart; }
From source file:org.cyberoam.iview.charts.CustomDomainAxis.java
public static JFreeChart getChart(int reportID, ResultSetWrapper rsw, HttpServletRequest request) { ReportBean reportBean = ReportBean.getRecordbyPrimarykey(reportID); JFreeChart chart = null; // DefaultCategoryDataset dataset=null; CustomCategoryDataset datasetCustom = null; try {//ww w . j a v a 2s.c o m ReportColumnBean reportColumnBeanX = null; ReportColumnBean reportColumnBeanY = null; ReportColumnBean reportColumnBeanZ = null; GraphBean graphBean = null; graphBean = GraphBean.getRecordbyPrimarykey(reportBean.getGraphId()); reportColumnBeanX = ReportColumnBean.getRecordByPrimaryKey(reportBean.getReportId(), graphBean.getXColumnId());//getting ReportColumnBean For X Axis String xColumnDBname = reportColumnBeanX.getDbColumnName(); reportColumnBeanY = ReportColumnBean.getRecordByPrimaryKey(reportBean.getReportId(), graphBean.getYColumnId()); String yColumnDBname = reportColumnBeanY.getDbColumnName(); String zColumnDBname = null; datasetCustom = new CustomCategoryDataset(); rsw.beforeFirst(); String data = ""; int count = -1; Boolean showLegend = false; if (graphBean.getZColumnId() == -1) { while (rsw.next()) { data = rsw.getString(xColumnDBname); datasetCustom.addValue(rsw.getLong(yColumnDBname), "", data); } } else { String zData; showLegend = true; reportColumnBeanZ = ReportColumnBean.getRecordByPrimaryKey(reportBean.getReportId(), graphBean.getZColumnId()); zColumnDBname = reportColumnBeanZ.getDbColumnName(); while (rsw.next()) { data = rsw.getString(xColumnDBname); zData = rsw.getString(zColumnDBname); datasetCustom.addValue(rsw.getLong(yColumnDBname), zData, data); count++; } } chart = ChartFactory.createLineChart("", // chart title "", // domain axis label "", // range axis label datasetCustom, // data PlotOrientation.VERTICAL, // orientation showLegend, // include legend true, // tooltips false // urls ); LegendTitle legendTitle = chart.getLegend(); if (legendTitle != null) legendTitle.setItemFont(new Font("Vandara", Font.BOLD, 11)); chart.setBackgroundPaint(Color.white); CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(Color.white); plot.setDomainGridlinePaint(Color.LIGHT_GRAY); plot.setDomainGridlinesVisible(true); plot.setRangeGridlinePaint(Color.LIGHT_GRAY); plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0)); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits()); rangeAxis.setTickLabelFont(new Font("Vandara", Font.CENTER_BASELINE, 10)); rangeAxis.setTickLabelInsets(new RectangleInsets(0, 0, 0, 5)); rangeAxis.setTickLabelsVisible(true); rangeAxis.setTickMarksVisible(false); rangeAxis.setAxisLineVisible(false); CustomDomainAxis catAxis = new CustomDomainAxis(); CustomDomainAxis.counter = 0; CustomDomainAxis.colCount = datasetCustom.getColumnCount(); catAxis.setTickLabelFont(new Font("Arial", Font.CENTER_BASELINE, 10)); catAxis.setTickMarksVisible(false); catAxis.setTickLabelInsets(new RectangleInsets(10, 15, 30, 10)); catAxis.setAxisLineVisible(false); catAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(20 * Math.PI / 180)); plot.setDomainAxis(catAxis); final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setSeriesPaint(0, Color.DARK_GRAY); if (count > 0) { Color[] colors = null; colors = new Color[5]; colors[0] = new Color(24, 112, 176); colors[1] = new Color(168, 192, 232); colors[2] = new Color(248, 120, 8); colors[3] = new Color(248, 184, 120); colors[4] = new Color(152, 216, 136); for (int i = 0; i < count && i < colors.length; i++) renderer.setSeriesPaint(i, colors[i]); } } catch (Exception e) { CyberoamLogger.appLog.debug("LineChart: " + e.getMessage(), e); } return chart; }