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

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

Introduction

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

Prototype

public LegendTitle(LegendItemSource source, Arrangement hLayout, Arrangement vLayout) 

Source Link

Document

Creates a new legend title with the specified arrangement.

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 .j  a  va2 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: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;//ww  w  .j a  v a 2 s .  c  o m
    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: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  ww. j  a  va  2s  . c om*/
    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.hmsinc.epicenter.webapp.chart.ChartService.java

/**
 * @param chart//w  w  w .j  a  v a 2  s . c o 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:edu.ucla.stat.SOCR.chart.demo.XYBarChartDemo2.java

protected JFreeChart createLegendChart(JFreeChart origchart) {

    JFreeChart legendChart = new JFreeChart("", null, new HiddenPlot(), false);

    legendChart.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) origchart.getPlot();

    LegendTitle legendTitle = new LegendTitle(plot,
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0),
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0));
    legendChart.addLegend(legendTitle);//  w  w  w.j av  a  2 s  .  c om

    return legendChart;

}

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
 * /*from  ww  w  . j a v  a2s .c  o  m*/
 * @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:edu.ucla.stat.SOCR.chart.SuperBoxAndWhiskerChart.java

protected JFreeChart createLegendChart(JFreeChart origchart) {

    JFreeChart legendChart = new JFreeChart("", null, new HiddenPlot(), false);

    legendChart.setBackgroundPaint(Color.white);
    CategoryPlot plot = origchart.getCategoryPlot();

    LegendTitle legendTitle = new LegendTitle(plot,
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0),
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0));
    legendChart.addLegend(legendTitle);//from w  w w.  j a  v a2  s .c  o  m

    return legendChart;

}

From source file:lucee.runtime.tag.Chart.java

private void setLegend(JFreeChart chart, Plot plot, Font font) {
    if (!showlegend)
        return;/*  w ww.ja v  a 2  s .c  o  m*/

    Color bg = backgroundcolor == null ? databackgroundcolor : backgroundcolor;
    if (font == null)
        font = getFont();

    LegendTitle legend = legendMultiLine
            ? new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement())
            : new LegendTitle(plot);
    legend.setBackgroundPaint(bg);
    legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
    legend.setFrame(new LineBorder());
    legend.setPosition(RectangleEdge.BOTTOM);
    legend.setHorizontalAlignment(HorizontalAlignment.LEFT);

    legend.setWidth(chartwidth - 20);// geht nicht
    legend.setItemFont(font);
    legend.setItemPaint(foregroundcolor);

    //RectangleInsets labelPadding;
    legend.setItemLabelPadding(new RectangleInsets(2, 2, 2, 2));
    legend.setBorder(0, 0, 0, 0);
    legend.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT);
    legend.setLegendItemGraphicPadding(new RectangleInsets(8, 10, 0, 0));
    chart.addLegend(legend);

}