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:hudson.plugins.testlink.TestLinkGraph.java

/**
 * Creates TestLink trend graph./* w  w  w .j a  va  2s  .co  m*/
 * 
 * @return the JFreeChart graph object.
 */
protected JFreeChart createGraph() {
    final JFreeChart chart = ChartFactory.createLineChart(null, null, yLabel, categoryDataset,
            PlotOrientation.VERTICAL, true, true, false);

    final LegendTitle legend = chart.getLegend();
    legend.setPosition(RectangleEdge.RIGHT);

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setForegroundAlpha(0.8f);
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.darkGray);

    final CategoryAxis domainAxis = new ShiftedCategoryAxis(xLabel);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    domainAxis.setCategoryMargin(0.0);
    plot.setDomainAxis(domainAxis);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRange(true);
    rangeAxis.setAutoRangeMinimumSize(5);
    rangeAxis.setLowerBound(0);

    final CategoryItemRenderer renderer = plot.getRenderer();

    renderer.setSeriesStroke(0, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(0, new Color(35, 20, 89));

    renderer.setSeriesStroke(1, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(1, new Color(0, 145, 0));

    renderer.setSeriesStroke(2, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(2, new Color(207, 69, 21));

    renderer.setSeriesStroke(3, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(3, Color.orange);

    plot.setInsets(new RectangleInsets(5.0, 0, 0, 5.0));

    return chart;
}

From source file:hudson.plugins.robot.graph.RobotGraph.java

/**
 * Creates a Robot trend graph//  w w w  . j  ava 2s .  c  o m
 * @return the JFreeChart graph object
 */
protected JFreeChart createGraph() {

    final JFreeChart chart = ChartFactory.createStackedAreaChart(null, null, yLabel, categoryDataset,
            PlotOrientation.VERTICAL, true, true, false);

    final LegendTitle legend = chart.getLegend();
    legend.setPosition(RectangleEdge.RIGHT);

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setForegroundAlpha(0.7f);
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.darkGray);

    final CategoryAxis domainAxis = new ShiftedCategoryAxis(xLabel);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    domainAxis.setCategoryMargin(0.0);
    plot.setDomainAxis(domainAxis);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    if (binaryData) {
        rangeAxis.setUpperBound(1);
    } else if (upperBound != 0) {
        rangeAxis.setUpperBound(upperBound);
    } else {
        rangeAxis.setAutoRange(true);
    }
    rangeAxis.setLowerBound(lowerBound);
    Font font = new Font("Dialog", Font.PLAIN, fontSize);
    plot.getDomainAxis().setLabelFont(font);
    plot.getDomainAxis().setTickLabelFont(font);
    plot.getRangeAxis().setLabelFont(font);
    plot.getRangeAxis().setTickLabelFont(font);
    legend.setItemFont(font);
    final CategoryItemRenderer renderer = plot.getRenderer();

    for (int i = 0; i < colors.length; i++) {
        renderer.setSeriesPaint(i, colors[i]);
    }

    plot.setInsets(new RectangleInsets(15.0, 0, 0, 5.0));

    return chart;
}

From source file:com.googlecode.jchav.chart.MinMeanMaxChart.java

/**
 * Construct a new chart to display the given data.
 *
 * @param title The page id, used for the title of the chart.
 * @param data the data to plot./*from  ww w .j a  va 2  s.co m*/
 */
public MinMeanMaxChart(final String title, final SortedSet<Measurement> data) {
    // The renderer that does all the real work here:
    final MinMaxCategoryRenderer minMaxRenderer = new MinMaxCategoryRenderer();
    minMaxRenderer.setObjectIcon(new FilledCircle());

    // Munge the data into form JFreeChart can use:
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    for (Measurement m : data) {
        // This ordering gives max=red, min=green, mean=blue
        dataset.addValue(m.getMaximumTime(), "max", m.getBuildId().getBuildName());
        dataset.addValue(m.getAverageTime(), "mean", m.getBuildId().getBuildName());
        dataset.addValue(m.getMinimumTime(), "min", m.getBuildId().getBuildName());
    }

    // Create the plot area:
    final CategoryPlot plot = new CategoryPlot();
    plot.setDataset(dataset);
    plot.setRenderer(minMaxRenderer);
    plot.setDomainAxis(new CategoryAxis("Build")); // TO DO: i18n
    plot.setRangeAxis(new NumberAxis("RT"));

    // Build labels running diagonally under the bars of the chart.
    plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);

    plot.setOrientation(PlotOrientation.VERTICAL);
    plot.setRangeGridlinesVisible(true);
    plot.setDomainGridlinesVisible(false);

    // the legend here would be the "min", "max", "mean" strings used when created int category data set.
    boolean showLegend = true;

    // Render the chart:
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, showLegend);
    chart.setTitle(title);
    chart.setBackgroundPaint(Color.WHITE);
    chart.setBorderVisible(false);

    if (showLegend) {
        chart.getLegend().setBorder(BlockBorder.NONE);
    }

    this.setChart(chart);

}

From source file:unalcol.termites.boxplots.ECALMessages.java

private LegendTitle modifyLegend(JFreeChart chart, CategoryPlot plot) {
    LegendTitle legend = chart.getLegend();
    chart.removeLegend();/*from w ww  .j av  a2 s  . co  m*/
    LegendItemSource[] items = legend.getSources();
    for (LegendItemSource item : items) {
        //LegendItemCollection collection = items[i].getLegendItems();
        LegendItemCollection collection = plot.getLegendItems();

        for (Iterator iter = collection.iterator(); iter.hasNext();) {
            LegendItem element = (LegendItem) iter.next();
            System.out.println(" ## #" + element.getLabel());
        }
    }

    return legend;
}

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

public static JFreeChart createXYChart(String title, XYDataset data, String xLabel, String yLabel,
        int pointRadius, int lineThickness, ColorTheme theme) {
    JFreeChart chart = ChartFactory.createXYLineChart(title, xLabel, yLabel, data, PlotOrientation.VERTICAL,
            true, false, false);/*  w  w w . j a va2s . co  m*/

    formatColorTheme(chart, theme);

    XYPlot plot = (XYPlot) chart.getPlot();
    Shape icon = new Ellipse2D.Double(-pointRadius, -pointRadius, pointRadius * 2, pointRadius * 2);

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    plot.setRenderer(renderer);

    Color[] colors = generateJetSpectrum(data.getSeriesCount());
    for (int i = 0; i < data.getSeriesCount(); i++) {
        plot.getRenderer().setSeriesStroke(i, new BasicStroke(lineThickness));
        plot.getRenderer().setSeriesShape(i, icon);
        ((XYLineAndShapeRenderer) plot.getRenderer()).setSeriesShapesVisible(i, true);
        ((XYLineAndShapeRenderer) plot.getRenderer()).setSeriesShapesFilled(i, true);
        plot.getRenderer().setSeriesPaint(i, colors[i]);
    }

    LegendTitle legend = chart.getLegend();
    Font legendFont = legend.getItemFont();
    float legendFontSize = legendFont.getSize();
    Font newLegendFont = legendFont.deriveFont(legendFontSize * 0.6f);
    legend.setItemFont(newLegendFont);

    ValueAxis domainAxis = ((XYPlot) chart.getPlot()).getDomainAxis();
    Font domainAxisLabelFont = domainAxis.getLabelFont();
    float domainAxisLabelFontSize = domainAxisLabelFont.getSize();
    domainAxis.setLabelFont(domainAxisLabelFont.deriveFont(domainAxisLabelFontSize * 0.6f));

    Font domainAxisTickLabelFont = domainAxis.getTickLabelFont();
    float domainAxisTickLabelFontSize = domainAxisTickLabelFont.getSize();
    domainAxis.setTickLabelFont(domainAxisTickLabelFont.deriveFont(domainAxisTickLabelFontSize * 0.6f));

    ValueAxis rangeAxis = ((XYPlot) chart.getPlot()).getRangeAxis();
    Font rangeAxisLabelFont = rangeAxis.getLabelFont();
    float rangeAxisLabelFontSize = rangeAxisLabelFont.getSize();
    rangeAxis.setLabelFont(rangeAxisLabelFont.deriveFont(rangeAxisLabelFontSize * 0.6f));

    Font rangeAxisTickLabelFont = rangeAxis.getTickLabelFont();
    float rangeAxisTickLabelFontSize = rangeAxisTickLabelFont.getSize();
    rangeAxis.setTickLabelFont(rangeAxisTickLabelFont.deriveFont(rangeAxisTickLabelFontSize * 0.6f));

    return chart;
}

From source file:dinamica.ChartOutput.java

public void print(GenericTransaction t) throws Throwable {

    //get chart parameters
    Recordset chartinfo = t.getRecordset("chartinfo");

    //get chart data
    String id = chartinfo.getString("data");
    Recordset data = (Recordset) getSession().getAttribute(id);
    if (data == null)
        throw new Throwable(
                "Invalid Recordset ID:" + id + " - The session does not contain an attribute with this ID.");

    //general chart params
    Integer width = (Integer) chartinfo.getValue("width");
    Integer height = (Integer) chartinfo.getValue("height");

    //load chart plugin
    String plugin = (String) chartinfo.getValue("chart-plugin");
    AbstractChartPlugin obj = (AbstractChartPlugin) Thread.currentThread().getContextClassLoader()
            .loadClass(plugin).newInstance();

    JFreeChart chart = obj.getChart(chartinfo, data);

    //set gradient
    chart.setBackgroundPaint(getGradient());

    //set border and legend params
    chart.setBorderPaint(Color.LIGHT_GRAY);
    chart.setBorderVisible(true);/*from  ww w.ja  v  a 2  s  .c  o  m*/
    if (chart.getLegend() != null) {
        chart.getLegend().setBorder(0.2, 0.2, 0.2, 0.2);
        chart.getLegend().setPadding(5, 5, 5, 5);
        chart.getLegend().setMargin(4, 5, 4, 4);
    }

    //render chart in memory
    BufferedImage img = chart.createBufferedImage(width.intValue(), height.intValue());
    ByteArrayOutputStream b = new ByteArrayOutputStream(32768);

    //encode as PNG
    ImageIO.write(img, "png", b);

    //send bitmap via servlet output
    byte image[] = b.toByteArray();
    getResponse().setContentType("image/png");
    getResponse().setContentLength(image.length);
    OutputStream out = getResponse().getOutputStream();
    out.write(image);
    out.close();

    //save image bytes in session attribute if requested
    if (chartinfo.containsField("session")) {
        String session = chartinfo.getString("session");
        if (session != null && session.equals("true"))
            getSession().setAttribute(chartinfo.getString("image-id"), image);
    }

}

From source file:jmemorize.gui.swing.panels.CardCounterPanel.java

private void setupPieLegend(JFreeChart chart) {
    LegendTitle legend = chart.getLegend();
    Font legendFont = legend.getItemFont();
    legend.setItemFont(legendFont.deriveFont(Font.BOLD, 20));
}

From source file:fr.amap.commons.javafx.chart.ChartViewer.java

public static JFreeChart createBasicChart(String title, XYSeriesCollection dataset, String xAxisLabel,
        String yAxisLabel) {//from w w  w  .  j  a v  a  2s.c  o m

    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);
    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);
        renderer.setSeriesShape(0, shape);
    }

    return chart;
}

From source file:com.xpn.xwiki.plugin.charts.ChartCustomizer.java

public static void customizeChart(JFreeChart jfchart, ChartParams params) {
    // title/*from  w w w  . j a va 2  s .  com*/
    if (params.get(ChartParams.TITLE_PREFIX + ChartParams.TITLE_SUFFIX) != null) {
        TextTitle title = new TextTitle(params.getString(ChartParams.TITLE_PREFIX + ChartParams.TITLE_SUFFIX));

        customizeTitle(title, params, ChartParams.TITLE_PREFIX);

        jfchart.setTitle(title);
    }

    // subtitle
    if (params.get(ChartParams.SUBTITLE_PREFIX + ChartParams.TITLE_SUFFIX) != null) {
        TextTitle subtitle = new TextTitle(
                params.getString(ChartParams.SUBTITLE_PREFIX + ChartParams.TITLE_SUFFIX));

        customizeTitle(subtitle, params, ChartParams.SUBTITLE_PREFIX);

        jfchart.addSubtitle(subtitle);
    }

    // legend
    LegendTitle legend = jfchart.getLegend();

    customizeLegend(legend, params);

    // anti-alias
    if (params.get(ChartParams.ANTI_ALIAS) != null) {
        jfchart.setAntiAlias(params.getBoolean(ChartParams.ANTI_ALIAS).booleanValue());
    }
    // background color
    if (params.get(ChartParams.BACKGROUND_COLOR) != null) {
        jfchart.setBackgroundPaint(params.getColor(ChartParams.BACKGROUND_COLOR));
    }

    // border
    if (params.get(ChartParams.BORDER_VISIBLE) != null
            && params.getBoolean(ChartParams.BORDER_VISIBLE).booleanValue()) {
        jfchart.setBorderVisible(true);
        if (params.get(ChartParams.BORDER_COLOR) != null) {
            jfchart.setBorderPaint(params.getColor(ChartParams.BORDER_COLOR));
        }
        if (params.get(ChartParams.BORDER_STROKE) != null) {
            jfchart.setBorderStroke(params.getStroke(ChartParams.BORDER_STROKE));
        }
    }
}

From source file:gov.nih.nci.caintegrator.ui.graphing.chart.plot.KaplanMeierPlot.java

private void createChart(XYDataset dataset) {
    //Create the chart, dropping in the data set
    JFreeChart chart = ChartFactory.createXYLineChart("", "Days in Study", "Probability of Survival", dataset,
            PlotOrientation.VERTICAL, false, //legend
            true, //tooltips
            false//urls
    );//from   www.  ja va2 s  .  c  om
    LegendTitle legend = chart.getLegend();
    XYPlot plot = (XYPlot) chart.getPlot();
    /********************************************************
     * IMPORTANT:
     * Ideally I would create the actual Renderer settings for
     * the at the time I start to march through the 
     * KaplanMeierPlotPointSeriesSets, adding them to the actual
     * Data Set that is going to be going into the Chart plotter.
     * But you have no idea how they are going to be sitting in
     * the Plot dataset so there is no guarantee that setting the
     * renderer based on a supposed index will actually work. In fact
     * it didn't work when I wrote this.
     * 
     */
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    for (int i = 0; i < finalDataCollection.getSeriesCount(); i++) {
        KaplanMeierPlotPointSeries kmSeries = (KaplanMeierPlotPointSeries) finalDataCollection.getSeries(i);
        if (kmSeries.getType() == SeriesType.CENSOR) {
            renderer.setSeriesLinesVisible(i, false);
            renderer.setSeriesShapesVisible(i, true);
        } else if (kmSeries.getType() == SeriesType.PROBABILITY) {
            renderer.setSeriesLinesVisible(i, true);
            renderer.setSeriesShapesVisible(i, false);

        } else {
            //don't show this set as it is not a known type
            renderer.setSeriesLinesVisible(i, false);
            renderer.setSeriesShapesVisible(i, false);
        }
        renderer.setSeriesPaint(i, getKMSetColor(kmSeries.getKey(), kmSeries.getType()), true);
    }

    renderer.setToolTipGenerator(new StandardXYToolTipGenerator());
    renderer.setDefaultEntityRadius(6);
    plot.setRenderer(renderer);
    //change the auto tick unit selection to integer units only...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
    // OPTIONAL CUSTOMISATION COMPLETED.
    rangeAxis.setAutoRange(true);
    rangeAxis.setRange(0.0, 1.0);
    kmChart = chart;
}