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

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

Introduction

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

Prototype

public void setWidth(double width) 

Source Link

Document

Sets the natural width of the block, if this is known in advance.

Usage

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

/**
 * Writes out the image using the supplied file name.
 * // w ww. jav  a 2  s.com
 * @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:org.jrecruiter.web.actions.admin.ShowStatisticsAction.java

private static JFreeChart createChart(final CategoryDataset categorydataset, final String chartTitle) {
    final JFreeChart chart = ChartFactory.createBarChart(chartTitle, "Jobs", "Number of Hits", categorydataset,
            PlotOrientation.VERTICAL, true, true, false);

    final CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    categoryplot.setNoDataMessage("NO DATA!");

    chart.setBackgroundPaint(new Color(245, 245, 255));
    chart.setBorderPaint(new Color(204, 204, 204));
    chart.setBorderStroke(new BasicStroke(1f));

    final LegendTitle legend = chart.getLegend();
    legend.setWidth(1000);
    legend.setPosition(RectangleEdge.BOTTOM);

    final BlockBorder border = new BlockBorder(new Color(204, 204, 204));
    legend.setBorder(border);/*from  ww  w.  jav  a2 s.c  om*/

    final CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
    categoryPlot.setBackgroundPaint(Color.WHITE);
    categoryPlot.setRangeGridlinePaint(new Color(204, 204, 204));
    categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    final NumberAxis numberaxis = (NumberAxis) categoryPlot.getRangeAxis();

    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    final BarRenderer renderer = (BarRenderer) categoryPlot.getRenderer();
    renderer.setDrawBarOutline(true);

    final ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.CENTER,
            TextAnchor.CENTER);
    renderer.setPositiveItemLabelPosition(itemlabelposition);

    renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());

    renderer.setItemLabelsVisible(true);
    return chart;
}

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

private void setLegend(JFreeChart chart, Plot plot, Font font) {
    if (!showlegend)
        return;/* w w  w.java 2s . com*/

    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);

}