Example usage for org.jfree.chart JFreeChart getLegend

List of usage examples for org.jfree.chart JFreeChart getLegend

Introduction

In this page you can find the example usage for org.jfree.chart JFreeChart getLegend.

Prototype

public LegendTitle getLegend() 

Source Link

Document

Returns the legend for the chart, if there is one.

Usage

From source file:org.jfree.chart.demo.LegendTitleToImageDemo1.java

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("A", 1.0D);
    defaultpiedataset.setValue("B", 2D);
    defaultpiedataset.setValue("C", 3D);
    JFreeChart jfreechart = ChartFactory.createPieChart("Test", defaultpiedataset, true, false, false);
    LegendTitle legendtitle = jfreechart.getLegend();
    legendtitle.setMargin(0.0D, 0.0D, 1.0D, 1.0D);
    BufferedImage bufferedimage = new BufferedImage(1, 1, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    Size2D size2d = legendtitle.arrange(graphics2d);
    graphics2d.dispose();/*from  w  w  w. ja va2s.  c  o  m*/
    int i = (int) Math.rint(size2d.width);
    int j = (int) Math.rint(size2d.height);
    BufferedImage bufferedimage1 = new BufferedImage(i, j, 2);
    Graphics2D graphics2d1 = bufferedimage1.createGraphics();
    legendtitle.draw(graphics2d1, new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j));
    graphics2d1.dispose();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
            new FileOutputStream(new File("LegendTitleToImageDemo1.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

From source file:org.jfree.chart.demo.LegendTitleToImageDemo2.java

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("England", 1.0D);
    defaultpiedataset.setValue("France", 2D);
    defaultpiedataset.setValue("Germany", 3D);
    defaultpiedataset.setValue("Italy", 4D);
    defaultpiedataset.setValue("Scotland", 5D);
    defaultpiedataset.setValue("Belgium", 6D);
    defaultpiedataset.setValue("Poland", 7D);
    defaultpiedataset.setValue("Spain", 8D);
    defaultpiedataset.setValue("Portugal", 9D);
    defaultpiedataset.setValue("Switzerland", 10D);
    defaultpiedataset.setValue("Austria", 11D);
    defaultpiedataset.setValue("Luxembourg", 12D);
    JFreeChart jfreechart = ChartFactory.createPieChart("Test", defaultpiedataset, true, false, false);
    LegendTitle legendtitle = jfreechart.getLegend();
    legendtitle.setMargin(0.0D, 0.0D, 1.0D, 1.0D);
    BufferedImage bufferedimage = new BufferedImage(1, 1, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    Size2D size2d = legendtitle.arrange(graphics2d, new RectangleConstraint(250D, new Range(0.0D, 10000D)));
    graphics2d.dispose();/*from ww  w  .j av a2 s . c om*/
    int i = (int) Math.rint(size2d.width);
    int j = (int) Math.rint(size2d.height);
    BufferedImage bufferedimage1 = new BufferedImage(i, j, 2);
    Graphics2D graphics2d1 = bufferedimage1.createGraphics();
    legendtitle.draw(graphics2d1, new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j));
    graphics2d1.dispose();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
            new FileOutputStream(new File("LegendTitleToImageDemo2.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

From source file:de.aidger.view.utils.Charts.java

/**
 * Creates a 3D pie chart.//from   w ww.  j a  va  2s.  com
 * 
 * @param title
 *            the diagram title
 * @param dataset
 *            the dataset.
 * @param width
 *            the width of the chart as image
 * @param height
 *            the height of the chart as image
 * 
 * @return the 3D pie chart as image
 */
public static ImageIcon createPieChart3D(String title, PieDataset dataset, int width, int height) {
    JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true, true, false);

    Font titleFont = UIManager.getFont("TitledBorder.font");

    chart.setBackgroundPaint(null);
    chart.getLegend().setBackgroundPaint(null);
    chart.getTitle().setFont(new Font(titleFont.getName(), titleFont.getStyle(), 14));
    chart.getTitle().setPaint(UIManager.getColor("TitledBorder.titleColor"));
    chart.setBorderPaint(null);
    chart.getLegend().setBorder(0, 0, 0, 0);

    PiePlot3D plot = (PiePlot3D) chart.getPlot();
    plot.setStartAngle(290);
    plot.setForegroundAlpha(0.9f);
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setNoDataMessage(_("No data to display."));
    plot.setLabelGenerator(null);
    plot.setInsets(new RectangleInsets(10, 1, 5, 1));
    plot.setBackgroundPaint(null);
    plot.setOutlineVisible(false);
    plot.setDarkerSides(true);

    return new ImageIcon(chart.createBufferedImage(width, height));
}

From source file:gov.nih.nci.caintegrator.plots.kaplanmeier.JFreeChartIKMPlottermpl.java

public static void createLegend(JFreeChart kmPlot, List<KMPlotPointSeriesSet> plotPointSeriesSetCollection) {
    LegendTitle legend = kmPlot.getLegend();
    LegendItemSource[] sources = new LegendItemSource[1];
    KMLegendItemSource legendSrc = new KMLegendItemSource();
    Comparator<KMPlotPointSeriesSet> nameComparator = new Comparator<KMPlotPointSeriesSet>() {
        public int compare(KMPlotPointSeriesSet series1, KMPlotPointSeriesSet series2) {
            return series1.getName().compareToIgnoreCase(series2.getName());
        }/*w  ww .  jav a  2 s .  c o  m*/
    };
    Collections.sort(plotPointSeriesSetCollection, nameComparator);

    LegendItem item;
    for (KMPlotPointSeriesSet plotPointSeries : plotPointSeriesSetCollection) {
        Color color = plotPointSeries.getColor();
        String title = plotPointSeries.getLegendTitle() + " (" + plotPointSeries.getGroupSize() + ")";
        item = new LegendItem(title, null, null, null, new Rectangle2D.Double(2, 2, 10, 10), color);
        legendSrc.addLegendItem(item);
    }
    sources[0] = legendSrc;
    legend.setSources(sources);
}

From source file:org.gbif.portal.web.util.ChartUtils.java

/**
 * Writes out the image using the supplied file name.
 * //w w w . j  a v  a  2  s  .  c  o m
 * @param legend
 * @param fileName
 * @return
 */
public static String writePieChartImageToTempFile(Map<String, Double> legend, String fileName) {

    String filePath = System.getProperty(tmpDirSystemProperty) + File.separator + fileName + defaultExtension;
    File fileToCheck = new File(filePath);
    if (fileToCheck.exists()) {
        return fileName + defaultExtension;
    }

    final DefaultPieDataset data = new DefaultPieDataset();
    Set<String> keys = legend.keySet();
    for (String key : keys) {
        logger.info("Adding key : " + key);
        data.setValue(key, legend.get(key));
    }

    // create a pie chart...
    final boolean withLegend = true;
    final JFreeChart chart = ChartFactory.createPieChart(null, data, withLegend, false, false);

    PiePlot piePlot = (PiePlot) chart.getPlot();
    piePlot.setLabelFont(new Font("Arial", Font.PLAIN, 10));
    piePlot.setLabelBackgroundPaint(Color.WHITE);

    LegendTitle lt = chart.getLegend();
    lt.setBackgroundPaint(Color.WHITE);
    lt.setWidth(300);
    lt.setBorder(0, 0, 0, 0);
    lt.setItemFont(new Font("Arial", Font.PLAIN, 11));

    chart.setPadding(new RectangleInsets(0, 0, 0, 0));
    chart.setBorderVisible(false);
    chart.setBackgroundImageAlpha(0);
    chart.setBackgroundPaint(Color.WHITE);
    chart.setBorderPaint(Color.LIGHT_GRAY);

    final BufferedImage image = new BufferedImage(300, 250, BufferedImage.TYPE_INT_RGB);
    KeypointPNGEncoderAdapter adapter = new KeypointPNGEncoderAdapter();
    adapter.setQuality(1);
    try {
        adapter.encode(image);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    final Graphics2D g2 = image.createGraphics();
    g2.setFont(new Font("Arial", Font.PLAIN, 11));
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 300, 250);

    // draw
    chart.draw(g2, chartArea, null, null);

    try {
        FileOutputStream fOut = new FileOutputStream(fileToCheck);
        ChartUtilities.writeChartAsPNG(fOut, chart, 300, 250);
        return fileToCheck.getName();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

From source file:fr.gouv.diplomatie.applitutoriel.utility.Graphique.java

/**
 * Creer camember3 d.//from ww w .ja va 2s.c  om
 *
 * @param title
 *            the title
 * @param dataset
 *            the dataset
 * @param legend
 *            the legend
 * @param tooltips
 *            the tooltips
 * @param urls
 *            the urls
 * @return the j free chart
 * @throws FontFormatException
 *             the font format exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static JFreeChart creerCamember3D(final String title, final DefaultPieDataset dataset,
        final boolean legend, final boolean tooltips, final boolean urls)
        throws FontFormatException, IOException {

    dataset.sortByValues(SortOrder.DESCENDING);
    final JFreeChart jfreeChart = ChartFactory.createPieChart3D(title, dataset, legend, tooltips, urls);

    jfreeChart.setBackgroundPaint(Color.white);
    jfreeChart.setBorderVisible(true);
    jfreeChart.getLegend().setPosition(RectangleEdge.LEFT);
    final GraphicsEnvironment graph = GraphicsEnvironment.getLocalGraphicsEnvironment();
    final InputStream inputStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("hornet/framework/font/LiberationSans-Bold.ttf");
    final Font font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
    graph.registerFont(font);
    jfreeChart.getLegend().setItemFont(new Font("Liberation Sans", Font.BOLD, 11));
    jfreeChart.getLegend().setHeight(400);
    jfreeChart.getLegend().setBorder(0, 0, 0, 0);
    jfreeChart.setTitle(new TextTitle(title, new Font("Liberation Sans", Font.BOLD, 16)));
    final PiePlot piePlot = (PiePlot) jfreeChart.getPlot();

    final int nbData = dataset.getItemCount();
    int cptColor = 0;
    for (int x = 0; x < nbData; x++) {
        if (cptColor >= listColor.size()) {
            cptColor = 0;
        }
        piePlot.setSectionPaint(dataset.getKey(x), listColor.get(cptColor));

        cptColor++;

    }

    piePlot.setForegroundAlpha(0.5f);
    piePlot.setLabelFont(new Font("Liberation Sans", Font.BOLD, 12));
    piePlot.setLabelOutlineStroke(null);
    piePlot.setLabelLinkStroke(new BasicStroke(0.4f));
    piePlot.setLabelBackgroundPaint(Color.WHITE);
    piePlot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD);
    piePlot.setBackgroundAlpha(0);
    piePlot.setOutlineVisible(false);
    piePlot.setForegroundAlpha(1); // transparence
    piePlot.setInteriorGap(0); // le camembert occupe plus de place
    piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
    piePlot.setStartAngle(70);
    piePlot.setCircular(true); // force pour avoir un cercle et pas un oval
    piePlot.setMaximumLabelWidth(0.20);
    piePlot.setBaseSectionOutlinePaint(Color.BLACK); // bordure du camembert

    return jfreeChart;

}

From source file:ec.ui.view.DistributionView.java

private static JFreeChart createDistributionViewChart() {
    XYPlot plot = new XYPlot();

    XYLineAndShapeRenderer dRenderer = new XYSplineRenderer();
    dRenderer.setBaseShapesVisible(false);
    dRenderer.setAutoPopulateSeriesPaint(false);
    dRenderer.setAutoPopulateSeriesStroke(false);
    dRenderer.setBaseStroke(TsCharts.getStrongStroke(LinesThickness.Thin));
    dRenderer.setDrawSeriesLineAsPath(true); // not sure if useful
    plot.setDataset(DISTRIBUTION_INDEX, Charts.emptyXYDataset());
    plot.setRenderer(DISTRIBUTION_INDEX, dRenderer);

    XYBarRenderer hRenderer = new XYBarRenderer();
    hRenderer.setShadowVisible(false);//from  w  w  w  .ja va2  s . c om
    hRenderer.setDrawBarOutline(true);
    hRenderer.setAutoPopulateSeriesPaint(false);
    hRenderer.setAutoPopulateSeriesOutlinePaint(false);
    hRenderer.setBaseSeriesVisibleInLegend(false);
    plot.setDataset(HISTOGRAM_INDEX, Charts.emptyXYDataset());
    plot.setRenderer(HISTOGRAM_INDEX, hRenderer);

    NumberAxis domainAxis = new NumberAxis();
    domainAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR);
    plot.setDomainAxis(domainAxis);
    plot.setDomainGridlinesVisible(false);

    NumberAxis rangeAxis = new NumberAxis();
    rangeAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR);
    rangeAxis.setTickUnit(new NumberTickUnit(0.05));
    rangeAxis.setNumberFormatOverride(new DecimalFormat("0.###"));
    plot.setRangeAxis(rangeAxis);

    plot.mapDatasetToDomainAxis(0, 0);
    plot.mapDatasetToRangeAxis(0, 0);
    plot.mapDatasetToDomainAxis(1, 0);
    plot.mapDatasetToRangeAxis(1, 0);

    JFreeChart result = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    result.setPadding(TsCharts.CHART_PADDING);
    result.getTitle().setFont(TsCharts.CHART_TITLE_FONT);
    result.getLegend().setFrame(BlockBorder.NONE);
    result.getLegend().setBackgroundPaint(null);
    return result;
}

From source file:cn.InstFS.wkr.NetworkMining.UIs.TimeSeriesChart1.java

public static JFreeChart createChart2(ArrayList<DataItems> _nor_model, ArrayList<DataItems> _abnor_model,
        DataItems nor, DataItems abnor, Map<String, ArrayList<LinePos>> mapAB, String chartname,
        String protocol1, String protocol2) {
    XYDataset xydataset = createNormalDataset(nor, protocol1);
    JFreeChart jfreechart = ChartFactory.createXYLineChart(chartname, "", "", xydataset);
    jfreechart.getLegend().setVisible(false);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
    numberaxis.setAutoRangeIncludesZero(false);
    java.awt.geom.Ellipse2D.Double double1 = new java.awt.geom.Ellipse2D.Double(-4D, -4D, 6D, 6D);

    XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
    // ??/*  w  ww  . j  a va2  s .  c o  m*/
    xylineandshaperenderer.setSeriesLinesVisible(0, true);
    xylineandshaperenderer.setBaseShapesVisible(false);
    xylineandshaperenderer.setSeriesShape(0, double1);
    xylineandshaperenderer.setSeriesPaint(0, Color.blue);
    xylineandshaperenderer.setSeriesFillPaint(0, Color.blue);
    xylineandshaperenderer.setSeriesOutlinePaint(0, Color.blue);
    xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(0.5F));
    // ?
    // xylineandshaperenderer.setBaseItemLabelGenerator(new
    // StandardXYItemLabelGenerator());
    // xylineandshaperenderer.setBaseItemLabelsVisible(true);
    int datasetcount0 = xyplot.getDatasetCount();

    XYDataset xydataset1 = createNormalDataset(abnor, protocol2);
    // xydataset1.
    XYLineAndShapeRenderer xylineandshaperenderer1 = new XYLineAndShapeRenderer();
    int datasetcount = xyplot.getDatasetCount();
    xyplot.setDataset(datasetcount, xydataset1);
    xyplot.setRenderer(datasetcount, xylineandshaperenderer1);
    // ???
    xylineandshaperenderer1.setBaseShapesVisible(false);
    // ??
    xylineandshaperenderer1.setSeriesLinesVisible(0, true);
    xylineandshaperenderer1.setSeriesShape(0, double1);
    // 
    xylineandshaperenderer1.setSeriesPaint(0, Color.green);
    xylineandshaperenderer1.setSeriesFillPaint(0, Color.green);
    xylineandshaperenderer1.setSeriesOutlinePaint(0, Color.green);
    xylineandshaperenderer1.setUseFillPaint(true);

    xylineandshaperenderer1.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
    xylineandshaperenderer1.setSeriesStroke(0, new BasicStroke(0.5F));

    // //?
    /*
     * for (int i = 0; i < _nor_model.size(); i++) { XYDataset xydataset2 =
     * createmodeDataset(_nor_model.get(i), "_nor_model" + i);
     * XYLineAndShapeRenderer xylineandshaperenderer2 = new
     * XYLineAndShapeRenderer(); xyplot.setDataset(i + 2, xydataset2);
     * xyplot.setRenderer(2 + i, xylineandshaperenderer2); // ???
     * xylineandshaperenderer2.setBaseShapesVisible(false); // ??
     * xylineandshaperenderer2.setSeriesLinesVisible(0, true);
     * xylineandshaperenderer2.setSeriesShape(0, double1); // 
     * xylineandshaperenderer2.setSeriesPaint(0, Color.red);
     * xylineandshaperenderer2.setSeriesFillPaint(0, Color.red);
     * xylineandshaperenderer2.setSeriesOutlinePaint(0, Color.red);
     * xylineandshaperenderer2.setUseFillPaint(true);
     * xylineandshaperenderer2 .setBaseItemLabelGenerator(new
     * StandardXYItemLabelGenerator());
     * xylineandshaperenderer2.setSeriesStroke(0, new BasicStroke(2.5F));
     * 
     * } for (int i = 0; i < _abnor_model.size(); i++) { XYDataset
     * xydataset3 = createmodeDataset(_abnor_model.get(i), "_abnor_model" +
     * i); XYLineAndShapeRenderer xylineandshaperenderer3 = new
     * XYLineAndShapeRenderer(); xyplot.setDataset(i + 2 +
     * _nor_model.size(), xydataset3); xyplot.setRenderer(i + 2 +
     * _nor_model.size(), xylineandshaperenderer3); // ???
     * xylineandshaperenderer3.setBaseShapesVisible(false); // ??
     * xylineandshaperenderer3.setSeriesLinesVisible(0, true);
     * xylineandshaperenderer3.setSeriesShape(0, double1); // 
     * xylineandshaperenderer3.setSeriesPaint(0, Color.red);
     * xylineandshaperenderer3.setSeriesFillPaint(0, Color.red);
     * xylineandshaperenderer3.setSeriesOutlinePaint(0, Color.red);
     * xylineandshaperenderer3.setUseFillPaint(true);
     * xylineandshaperenderer3 .setBaseItemLabelGenerator(new
     * StandardXYItemLabelGenerator());
     * xylineandshaperenderer3.setSeriesStroke(0, new BasicStroke(2.5F));
     * 
     * }
     */
    // ??
    // 
    // /////////////////////////////////
    // ?

    XYDataset xydataset4 = createLineDataset(nor, abnor, mapAB, xyplot);

    // ??y=1
    ValueMarker valuemarker = new ValueMarker(1); // 
    valuemarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
    valuemarker.setPaint(Color.black); // ?
    valuemarker.setStroke(new BasicStroke(1.0F)); // 
    // valuemarker.setLabel(""); //?
    valuemarker.setLabelFont(new Font("SansSerif", 0, 11)); // ?
    valuemarker.setLabelPaint(Color.red);
    valuemarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    valuemarker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
    xyplot.addRangeMarker(valuemarker);
    // //

    //jfreechart.getLegend().setVisible(true);
    return jfreechart;
}

From source file:org.toobsframework.pres.chart.ChartUtil.java

public static void configureLegend(IRequest componentRequest, JFreeChart chart, Legend legend, Map params) {
    if (legend != null) {
        if (legend.getBorder() != null) {
            RectangleInsets rect = getRectangle(componentRequest, legend.getBorder(), params);
            chart.getLegend().setBorder(rect.getTop(), rect.getLeft(), rect.getBottom(), rect.getRight());
        }/*from  w w w .j ava  2s . co m*/
        if (legend.getPadding() != null) {
            chart.getLegend().setItemLabelPadding(getRectangle(componentRequest, legend.getPadding(), params));
        }
    }
}

From source file:com.griddynamics.jagger.reporting.chart.ChartHelper.java

private static void formatColorTheme(JFreeChart chart, ColorTheme theme) {
    Plot rawPlot = chart.getPlot();//from  ww w .  j av a2s.c om

    Paint background = Color.BLACK;
    Paint foregroung = Color.WHITE;
    if (theme == ColorTheme.LIGHT) {
        background = Color.WHITE;
        foregroung = Color.BLACK;
    }

    chart.setBackgroundPaint(background);
    chart.getLegend().setBackgroundPaint(background);
    chart.getLegend().setItemPaint(foregroung);

    if (rawPlot instanceof XYPlot) {
        XYPlot plot = (XYPlot) rawPlot;
        plot.getDomainAxis().setLabelPaint(foregroung);
        plot.getRangeAxis().setLabelPaint(foregroung);
        plot.getDomainAxis().setTickLabelPaint(foregroung);
        plot.getDomainAxis().setTickMarkPaint(foregroung);
        plot.getRangeAxis().setTickLabelPaint(foregroung);
        plot.getRangeAxis().setTickMarkPaint(foregroung);
        plot.setBackgroundPaint(background);
        plot.setDomainGridlinePaint(foregroung);
        plot.setRangeGridlinePaint(foregroung);
    }
    if (rawPlot instanceof CategoryPlot) {
        CategoryPlot plot = (CategoryPlot) rawPlot;
        plot.getDomainAxis().setLabelPaint(foregroung);
        plot.getRangeAxis().setLabelPaint(foregroung);
        plot.getDomainAxis().setTickLabelPaint(foregroung);
        plot.getDomainAxis().setTickMarkPaint(foregroung);
        plot.getRangeAxis().setTickLabelPaint(foregroung);
        plot.getRangeAxis().setTickMarkPaint(foregroung);
        plot.setBackgroundPaint(background);
        plot.setDomainGridlinePaint(foregroung);
        plot.setRangeGridlinePaint(foregroung);
    }
}