Example usage for org.jfree.chart.title LegendTitle setPosition

List of usage examples for org.jfree.chart.title LegendTitle setPosition

Introduction

In this page you can find the example usage for org.jfree.chart.title LegendTitle setPosition.

Prototype

public void setPosition(RectangleEdge position) 

Source Link

Document

Sets the position for the title and sends a TitleChangeEvent to all registered listeners.

Usage

From source file:edu.cudenver.bios.chartsvc.representation.LegendImageRepresentation.java

/**
 * Called internally by Restlet library to write the image as the HTTP
 * response./*from   w w w. ja va 2  s  . c o m*/
 * @param out output stream
 */
@Override
public void write(OutputStream out) throws IOException {
    // build the legend from the plot, and write it to a jpeg image
    if (plot != null) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        //legend.setMargin(new RectangleInsets(2.0, 2.0, 2.0, 2.0));
        legend.setBackgroundPaint(Color.white);
        legend.setPosition(RectangleEdge.BOTTOM);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        Rectangle2D.Double legendArea = new Rectangle2D.Double(0, 0, width, height);
        g.clip(legendArea);
        legend.arrange(g, new RectangleConstraint(width, height));
        legend.draw(g, legendArea);
        g.dispose();
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    }
}

From source file:org.gumtree.vis.awt.PlotFactory.java

public static JFreeChart createXYErrorChart(IXYErrorDataset dataset) {

    JFreeChart chart;// www  .  j a  v  a  2  s  .c  o  m
    String title = null;
    String xTitle = null;
    String yTitle = null;
    if (dataset != null) {
        title = "";
        if (dataset.getTitle() != null) {
            title = dataset.getTitle();
        }
        xTitle = "";
        if (dataset.getXTitle() != null) {
            xTitle += dataset.getXTitle();
        }
        if (dataset.getXUnits() != null) {
            xTitle += " (" + dataset.getXUnits() + ")";
        }
        yTitle = "";
        if (dataset.getYTitle() != null) {
            yTitle += dataset.getYTitle();
        }
        if (dataset.getYUnits() != null) {
            yTitle += " (" + dataset.getYUnits() + ")";
        }
    } else {
        dataset = new XYErrorDataset();
    }
    chart = createXYLineChart(title, xTitle, yTitle, dataset, PlotOrientation.VERTICAL, true, false, true);
    chart.setBackgroundPaint(Color.WHITE);
    final LegendTitle legend = (LegendTitle) chart.getLegend();
    RectangleEdge legendPosition = RectangleEdge.BOTTOM;
    try {
        String legendProperty = "RectangleEdge." + System.getProperty("kuranda1D.legendPosition");
        if (RectangleEdge.BOTTOM.toString().equals(legendProperty))
            legendPosition = RectangleEdge.BOTTOM;
        else if (RectangleEdge.RIGHT.toString().equals(legendProperty))
            legendPosition = RectangleEdge.RIGHT;
        else if (RectangleEdge.LEFT.toString().equals(legendProperty))
            legendPosition = RectangleEdge.LEFT;
        else if (RectangleEdge.TOP.toString().equals(legendProperty))
            legendPosition = RectangleEdge.TOP;
    } catch (Exception e) {
        // TODO: handle exception
    }
    legend.setPosition(legendPosition);
    chart.setBorderVisible(true);
    //      ChartUtilities.applyCurrentTheme(chart);
    //      chartTheme.apply(chart);

    XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.WHITE);
    plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
    plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    //      plot.setRangeZeroBaselineVisible(false);
    //      plot.setDomainZeroBaselineVisible(false);
    ValueAxis rangeAxis = plot.getRangeAxis();
    if (rangeAxis instanceof NumberAxis) {
        ((NumberAxis) rangeAxis).setAutoRangeStickyZero(false);
        ((NumberAxis) rangeAxis).setAutoRangeIncludesZero(false);
    }
    ValueAxis domainAxis = plot.getDomainAxis();
    if (domainAxis instanceof NumberAxis) {
        ((NumberAxis) domainAxis).setAutoRangeStickyZero(false);
        ((NumberAxis) domainAxis).setAutoRangeIncludesZero(false);
    }
    plot.setDomainPannable(true);
    plot.setRangePannable(true);

    plot.setDomainGridlinesVisible(true);
    //        plot.setDomainCrosshairLockedOnData(true);
    //        plot.setDomainCrosshairVisible(true);
    plot.setRangeGridlinesVisible(true);
    //        plot.setRangeCrosshairLockedOnData(true);
    //        plot.setRangeCrosshairVisible(true);

    //      xAxis = plot.getDomainAxis();
    //        yAxis = plot.getRangeAxis();

    plot.setDataset(dataset);
    XYItemRenderer renderer = chart.getXYPlot().getRenderer();
    if (renderer instanceof XYErrorRenderer) {
        //         ((XYLineAndShapeRenderer) renderer).setBaseShapesVisible(true);
        ((XYErrorRenderer) renderer).setBaseShapesFilled(true);
        ((XYErrorRenderer) renderer).setDrawXError(false);
        ((XYErrorRenderer) renderer).setDrawYError(true);
    }

    chart.fireChartChanged();
    return chart;
}

From source file:daylightchart.daylightchart.chart.DaylightChart.java

private void createLegend(final Options options, final Font font) {
    removeLegend();/*from w ww . j  a  v  a 2 s  . c  o  m*/

    if (options.isShowChartLegend()) {
        final LegendItemSource legendItemSource = new DaylightChartLegendItemSource(options);
        final LegendTitle legendTitle = new LegendTitle(legendItemSource);
        legendTitle.setItemFont(font);
        legendTitle.setPosition(RectangleEdge.BOTTOM);
        addLegend(legendTitle);
    }
}

From source file:com.rapidminer.gui.viewer.ROCChartPlotter.java

private JFreeChart createChart(XYDataset dataset) {
    // create the chart...
    JFreeChart chart = ChartFactory.createXYLineChart(null, // chart title
            null, // x axis label
            null, // y axis label
            dataset, // data
            PlotOrientation.VERTICAL, true, // include legend
            true, // tooltips
            false // urls
    );/*from  ww  w .ja  v  a2s .  com*/

    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.WHITE);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    plot.setRangeGridlinePaint(Color.LIGHT_GRAY);

    ValueAxis valueAxis = plot.getRangeAxis();
    valueAxis.setLabelFont(PlotterAdapter.LABEL_FONT_BOLD);
    valueAxis.setTickLabelFont(PlotterAdapter.LABEL_FONT);

    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setLabelFont(PlotterAdapter.LABEL_FONT_BOLD);
    domainAxis.setTickLabelFont(PlotterAdapter.LABEL_FONT);

    DeviationRenderer renderer = new DeviationRenderer(true, false);
    Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    if (dataset.getSeriesCount() == 1) {
        renderer.setSeriesStroke(0, stroke);
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesFillPaint(0, Color.RED);
    } else if (dataset.getSeriesCount() == 2) {
        renderer.setSeriesStroke(0, stroke);
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesFillPaint(0, Color.RED);

        renderer.setSeriesStroke(1, stroke);
        renderer.setSeriesPaint(1, Color.BLUE);
        renderer.setSeriesFillPaint(1, Color.BLUE);
    } else {
        for (int i = 0; i < dataset.getSeriesCount(); i++) {
            renderer.setSeriesStroke(i, stroke);
            Color color = colorProvider.getPointColor((double) i / (double) (dataset.getSeriesCount() - 1));
            renderer.setSeriesPaint(i, color);
            renderer.setSeriesFillPaint(i, color);
        }
    }
    renderer.setAlpha(0.12f);
    plot.setRenderer(renderer);

    // legend settings
    LegendTitle legend = chart.getLegend();
    if (legend != null) {
        legend.setPosition(RectangleEdge.TOP);
        legend.setFrame(BlockBorder.NONE);
        legend.setHorizontalAlignment(HorizontalAlignment.LEFT);
        legend.setItemFont(PlotterAdapter.LABEL_FONT);
    }
    return chart;
}

From source file:com.itemanalysis.jmetrik.swing.GraphPanel.java

public void setLineChart(String title, String subtitle, String xlabel, String ylabel) {
    XYSeriesCollection dataset = new XYSeriesCollection();

    chart = ChartFactory.createXYLineChart(title, // chart title
            xlabel, // x axis label
            ylabel, // y axis label
            dataset, // data
            chartOrientation, showLegend, // include legend
            true, // tooltips
            false // urls
    );/*from   ww  w . jav  a 2  s.c o  m*/

    if (showLegend) {
        LegendTitle chartTitle = chart.getLegend();
        chartTitle.setPosition(legendPosition);
    }

    if (subtitle != null && !"".equals(subtitle)) {
        TextTitle subtitle1 = new TextTitle(subtitle);
        chart.addSubtitle(subtitle1);
    }

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.WHITE);
    plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
    plot.setDomainGridlinePaint(Color.LIGHT_GRAY);

    //can use this code to modify charts for book. DO NOT USE FOR NORMAL FUNCTIONING
    //        NumberAxis yaxis = (NumberAxis)plot.getRangeAxis();
    //        yaxis.setTickUnit(new NumberTickUnit(.1));

    ChartPanel panel = new ChartPanel(chart);
    panel.getPopupMenu().addSeparator();
    this.addJpgMenuItem(this, panel.getPopupMenu());
    //        this.addEPSMenuItem(this, panel.getPopupMenu());//remove this line for public release versions
    panel.setPreferredSize(new Dimension(width, height));

    chart.setPadding(new RectangleInsets(20.0, 5.0, 20.0, 5.0));
    this.setBackground(Color.WHITE);
    this.add(panel);

}

From source file:de.laures.cewolf.taglib.AbstractChartDefinition.java

/**
 * This method triggers the dataset and chart production. It is only
 * from outside if there is no cached image available in the the
 * image cache./*from  ww w . j ava2 s  .  com*/
 */
public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException {
    if (chart == null) {
        chart = produceChart();
        chart.setAntiAlias(antialias);
        if (background != null) {
            Image image = ImageHelper.loadImage(background);
            chart.setBackgroundImage(image);
            chart.setBackgroundImageAlpha(backgroundImageAlpha);
        }
        if (paint != null) {
            chart.setBackgroundPaint(paint);
        }
        if (showLegend) {

            LegendTitle legend = this.getLegend();
            switch (legendAnchor) {
            case ANCHOR_NORTH:
                legend.setPosition(RectangleEdge.TOP);
                break;
            case ANCHOR_WEST:
                legend.setPosition(RectangleEdge.RIGHT);
                break;
            case ANCHOR_EAST:
                legend.setPosition(RectangleEdge.LEFT);
                break;
            default:
                legend.setPosition(RectangleEdge.BOTTOM);
            }
        } else {
            this.removeLegend();
        }
        // postProcessing
        for (int i = 0; i < postProcessors.size(); i++) {
            ChartPostProcessor pp = (ChartPostProcessor) postProcessors.get(i);
            try {
                pp.processChart(chart, (Map) postProcessorsParams.get(i));
            } catch (Throwable t) {
                log.error(t);
                throw new PostProcessingException(t.getClass().getName() + " raised by post processor '" + pp
                        + "'.\nPost processing of this post processor " + "has been ignored.");
            }
        }
    }
    return chart;
}

From source file:edu.cudenver.bios.chartsvc.resource.ScatterPlotResource.java

/**
 * Create a JFreeChart object from the chart specification.  JFreechart provides 
 * functionality to render 2D scatter plots as jpeg images
 * // w ww .jav a  2s  .  c om
 * @param chart chart specification object
 * @return JFreeChart object
 * @throws ResourceException
 */
private JFreeChart buildScatterPlot(Chart chart) throws ResourceException {
    ArrayList<LineStyle> lineStyleList = chart.getLineStyleList();

    float dashedLength = 1.0f;
    float spaceLength = 1.0f;
    float thickness = 1.0f;

    // the first series is treated as the x values
    if (chart.getSeries() == null || chart.getSeries().size() <= 0)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No data series specified");

    // create the jfree chart series
    XYSeriesCollection chartData = new XYSeriesCollection();
    // use a spline renderer to make the connecting lines smooth
    XYSplineRenderer rend = new XYSplineRenderer();

    int seriesIdx = 0;

    for (Series series : chart.getSeries()) {
        XYSeries xySeries = new XYSeries(series.getLabel());

        List<Double> xList = series.getXCoordinates();
        List<Double> yList = series.getYCoordinates();
        if (xList != null && yList != null && xList.size() == yList.size()) {
            for (int i = 0; i < xList.size(); i++) {
                xySeries.add(xList.get(i), yList.get(i));
            }
        }

        if (seriesIdx >= 0 && seriesIdx < lineStyleList.size()) {
            LineStyle lineStyle = lineStyleList.get(seriesIdx);
            dashedLength = (float) lineStyle.getDashLength();
            spaceLength = (float) lineStyle.getSpaceLength();
            thickness = (float) lineStyle.getWidth();
        } else {
            dashedLength = 1.0f;
            spaceLength = 1.0f;
            thickness = 1.0f;
        }

        // set the line style
        rend.setSeriesPaint(seriesIdx, Color.BLACK);

        if (seriesIdx >= 0) {
            /*rend.setSeriesStroke(seriesIdx, 
                  new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {(float) seriesIdx, (float) 2*seriesIdx}, 0.0f));*/
            rend.setSeriesStroke(seriesIdx, new BasicStroke(thickness, BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_ROUND, 1.0f, new float[] { dashedLength, spaceLength }, 0.0f));
        }
        // add the series to the data set
        chartData.addSeries(xySeries);
        seriesIdx++;
    }

    // turn off shapes displayed at each data point to make a smooth curve
    rend.setBaseShapesVisible(false);

    // Create the line chart
    NumberAxis xAxis = new NumberAxis();
    xAxis.setAutoRangeIncludesZero(false);
    if (chart.getXAxis() != null) {
        Axis xAxisSpec = chart.getXAxis();
        xAxis.setLabel(xAxisSpec.getLabel());
        if (!Double.isNaN(xAxisSpec.getRangeMin()) && !Double.isNaN(xAxisSpec.getRangeMax())) {
            xAxis.setRange(xAxisSpec.getRangeMin(), xAxisSpec.getRangeMax());
        }
    }
    NumberAxis yAxis = new NumberAxis();
    if (chart.getYAxis() != null) {
        Axis yAxisSpec = chart.getYAxis();
        yAxis.setLabel(chart.getYAxis().getLabel());
        if (!Double.isNaN(yAxisSpec.getRangeMin()) && !Double.isNaN(yAxisSpec.getRangeMax())) {
            xAxis.setRange(yAxisSpec.getRangeMin(), yAxisSpec.getRangeMax());
        }
    }
    XYPlot plot = new XYPlot((XYDataset) chartData, xAxis, yAxis, rend);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);

    JFreeChart renderedChart = new JFreeChart(chart.getTitle(), JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    renderedChart.setBackgroundPaint(Color.WHITE);
    if (chart.hasLegend()) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        legend.setPosition(RectangleEdge.BOTTOM);
        renderedChart.addLegend(legend);
    }

    return renderedChart;
}

From source file:org.emftrace.quarc.ui.views.RatioView.java

/**
 * create a SpiderChart /*from   ww  w.  j a  v a  2s .  c o m*/
 * @param dataset the dataset for the chart
 * @param weighted include weights?
 * @return the created SpiderChart
 */
private JFreeChart createSpiderChart(DefaultCategoryDataset dataset, boolean weighted) {

    SpiderWebPlot plot = new SpiderWebPlot(dataset);
    plot.setMaxValue(200.0f);

    plot.setStartAngle(54);
    plot.setInteriorGap(0.40);
    plot.setToolTipGenerator(new CategoryToolTipGenerator() {

        @Override
        public String generateToolTip(CategoryDataset dataset, int section, int index) {
            Float ratingValue = (Float) dataset.getValue(section, index);
            if (ratingValue == null)
                ratingValue = 0.0f;
            else
                ratingValue -= 100.0f;
            return String.valueOf("(" + dataset.getRowKey(section) + "," + dataset.getColumnKey(index) + ") = "
                    + String.format("%.2f", ratingValue));
        }

    });
    plot.setNoDataMessage("No data to display");
    String titleStr = "ratings of selected elements";
    if (weighted)
        titleStr = "weighted " + titleStr;
    JFreeChart chart = new JFreeChart(titleStr, TextTitle.DEFAULT_FONT, plot, false);
    LegendTitle legend = new LegendTitle(plot);
    legend.setPosition(RectangleEdge.BOTTOM);
    chart.addSubtitle(legend);
    ChartUtilities.applyCurrentTheme(chart);
    return chart;

}

From source file:com.ikon.servlet.admin.StatsGraphServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String action = WebUtils.getString(request, "action", "graph");
    String type = WebUtils.getString(request, "t");
    JFreeChart chart = null;/*  w ww  .j  ava2s.  c om*/
    updateSessionManager(request);

    try {
        if ("refresh".equals(action)) {
            new RepositoryInfo().runAs(null);
            ServletContext sc = getServletContext();
            sc.getRequestDispatcher("/admin/stats.jsp").forward(request, response);
        } else {
            response.setContentType("image/png");
            OutputStream out = response.getOutputStream();

            if (DOCUMENTS.equals(type) || DOCUMENTS_SIZE.equals(type) || FOLDERS.equals(type)) {
                chart = repoStats(type);
            } else if (DISK.equals(type)) {
                chart = diskStats();
            } else if (JVM_MEMORY.equals(type)) {
                chart = jvmMemStats();
            } else if (OS_MEMORY.equals(type)) {
                chart = osMemStats();
            }

            if (chart != null) {
                // Customize title font
                chart.getTitle().setFont(new Font("Tahoma", Font.BOLD, 16));

                // Match body {   background-color:#F6F6EE; }
                chart.setBackgroundPaint(new Color(246, 246, 238));

                // Customize no data
                PiePlot plot = (PiePlot) chart.getPlot();
                plot.setNoDataMessage("No data to display");

                // Customize labels
                plot.setLabelGenerator(null);

                // Customize legend
                LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
                legend.setPosition(RectangleEdge.BOTTOM);
                legend.setFrame(BlockBorder.NONE);
                legend.setItemFont(new Font("Tahoma", Font.PLAIN, 12));
                chart.removeLegend();
                chart.addLegend(legend);

                if (DISK.equals(type) || JVM_MEMORY.equals(type) || OS_MEMORY.equals(type)) {
                    ChartUtilities.writeChartAsPNG(out, chart, 225, 225);
                } else {
                    ChartUtilities.writeChartAsPNG(out, chart, 250, 250);
                }
            }

            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.iteratec.iteraplan.presentation.dialog.GraphicalReporting.Line.JFreeChartLineGraphicCreator.java

/**
 * Creates the chart and applies some styling.
 * @return the JFreeChart wrapped in an iteratec API symbol.
 *//* w  ww  .  ja va2  s  .c  o m*/
private JFreeChartSymbol createChart() {
    try {
        ChartFactory cf = new ChartFactory();
        cf.setTitle(title);
        cf.setxAxis(xAxis);
        cf.setyAxis(yAxis);
        JFreeChart chart = cf.createXYStepChart(dataset);
        LegendTitle legend = chart.getLegend();
        legend.setPosition(RectangleEdge.RIGHT);
        legend.setVisible(!emptyDataset); // don't show a legend, if the dataset is empty
        XYPlot xyplot = (XYPlot) chart.getPlot();
        xyplot.setBackgroundAlpha(0.4f);
        JFreeChartSymbol jfcs = new JFreeChartSymbol(chart);
        jfcs.setWidth(xResolution);
        jfcs.setHeight(yResolution);
        return jfcs;
    } catch (Exception e) {
        throw new IteraplanBusinessException(e);
    }
}