Example usage for org.jfree.chart JFreeChart getBackgroundPaint

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

Introduction

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

Prototype

public Paint getBackgroundPaint() 

Source Link

Document

Returns the paint used for the chart background.

Usage

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

public static void writeChartToJPEG(JFreeChart chart, ChartRenderingInfo info, int width, int height,
        File fileName, int resolution) throws IOException {
    // Background color
    Paint saved = chart.getBackgroundPaint();
    if (((Color) saved).getAlpha() == 0) {
        chart.setBackgroundPaint(Color.WHITE);
        chart.setBackgroundImageAlpha(255);
        if (chart.getLegend() != null)
            chart.getLegend().setBackgroundPaint(Color.WHITE);
        // legends and stuff
        for (int i = 0; i < chart.getSubtitleCount(); i++)
            if (PaintScaleLegend.class.isAssignableFrom(chart.getSubtitle(i).getClass()))
                ((PaintScaleLegend) chart.getSubtitle(i)).setBackgroundPaint(Color.WHITE);

        // apply bg
        chart.getPlot().setBackgroundPaint(Color.WHITE);
    }/*from  w w w .ja v  a2 s .c o  m*/
    //
    if (resolution == 72)
        writeChartToJPEG(chart, width, height, fileName);
    else {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
        try {
            BufferedImage image = paintScaledChartToBufferedImage(chart, info, out, width, height, resolution,
                    BufferedImage.TYPE_INT_RGB);
            EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, 1.f);
        } finally {
            out.close();
        }
    }
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * This method is used to save all image formats. it uses the specific methods for each file
 * format//w ww  .java  2s. c o m
 * 
 * @param chart
 * @param sett
 * @param chartRenderingInfo
 */
private static void writeChartToImage(JFreeChart chart, GraphicsExportParameters sett, ChartRenderingInfo info)
        throws Exception {
    // Background color
    Paint saved = chart.getBackgroundPaint();
    chart.setBackgroundPaint(sett.getColorWithAlpha());
    chart.setBackgroundImageAlpha((float) sett.getTransparency());
    if (chart.getLegend() != null)
        chart.getLegend().setBackgroundPaint(sett.getColorWithAlpha());
    // legends and stuff
    for (int i = 0; i < chart.getSubtitleCount(); i++)
        if (PaintScaleLegend.class.isAssignableFrom(chart.getSubtitle(i).getClass()))
            ((PaintScaleLegend) chart.getSubtitle(i)).setBackgroundPaint(sett.getColorWithAlpha());

    // apply bg
    chart.getPlot().setBackgroundPaint(sett.getColorWithAlpha());

    // create folder
    File f = sett.getFullpath();
    if (!f.exists()) {
        if (f.getParentFile() != null)
            f.getParentFile().mkdirs();
        // f.createNewFile();
    }

    Dimension size = sett.getPixelSize();
    // Format
    switch (sett.getFormat()) {
    case "PDF":
        writeChartToPDF(chart, size.width, size.height, f);
        break;
    case "PNG":
        writeChartToPNG(chart, info, size.width, size.height, f, (int) sett.getDPI());
        break;
    case "JPG":
        writeChartToJPEG(chart, info, size.width, size.height, f, (int) sett.getDPI());
        break;
    case "EPS":
        writeChartToEPS(chart, size.width, size.height, f);
        break;
    case "SVG":
        writeChartToSVG(chart, size.width, size.height, f);
        break;
    case "EMF":
        writeChartToEMF(chart, size.width, size.height, f);
        break;
    }
    //
    chart.setBackgroundPaint(saved);
    chart.setBackgroundImageAlpha(255);
    if (chart.getLegend() != null)
        chart.getLegend().setBackgroundPaint(saved);
    // legends and stuff
    for (int i = 0; i < chart.getSubtitleCount(); i++)
        if (PaintScaleLegend.class.isAssignableFrom(chart.getSubtitle(i).getClass()))
            ((PaintScaleLegend) chart.getSubtitle(i)).setBackgroundPaint(saved);

    // apply bg
    chart.getPlot().setBackgroundPaint(saved);
}

From source file:daylightchart.options.chart.ChartOptions.java

/**
 * {@inheritDoc}/*  ww  w.j  a  v a2  s  .c  om*/
 *
 * @see BaseChartOptions#copyFromChart(org.jfree.chart.JFreeChart)
 */
@Override
public void copyFromChart(final JFreeChart chart) {
    antiAlias = chart.getAntiAlias();
    backgroundPaint = chart.getBackgroundPaint();
    //
    plotOptions.copyFromChart(chart);
    titleOptions.copyFromChart(chart);
}

From source file:org.apache.qpid.disttest.charting.chartbuilder.BaseChartBuilderTest.java

public void testBuildChart() {
    BaseChartBuilder chartBuilder = new ChartBuilder(_seriesBuilder, _strokeAndPaintApplier, _datasetHolder) {
        @Override//from w w  w.  ja  v  a  2 s .  c o  m
        protected JFreeChart createChartImpl(String title, String xAxisTitle, String yAxisTitle,
                Dataset dataset, PlotOrientation plotOrientation, boolean showLegend, boolean showToolTips,
                boolean showUrls) {
            assertEquals(CHART_TITLE, title);
            assertEquals(X_TITLE, xAxisTitle);
            assertEquals(Y_TITLE, yAxisTitle);

            return _jFreeChart;
        }
    };

    JFreeChart chart = chartBuilder.buildChart(_chartingDefinition);

    assertEquals(BaseChartBuilder.BLUE_GRADIENT, chart.getBackgroundPaint());
    assertEquals("The *second* subtitle of the generated chart should have the text from the chart definition",
            CHART_SUB_TITLE, ((TextTitle) chart.getSubtitle(1)).getText());
    verify(_seriesPainter).applySeriesAppearance(_jFreeChart, _seriesDefinitions, _strokeAndPaintApplier);
}

From source file:org.jfree.experimental.chart.swt.editor.SWTOtherEditor.java

/**
 * Creates a new instance.// ww  w  . j  ava  2s .c  o  m
 * 
 * @param parent  the parent.
 * @param style  the style.
 * @param chart  the chart.
 */
public SWTOtherEditor(Composite parent, int style, JFreeChart chart) {
    super(parent, style);
    FillLayout layout = new FillLayout();
    layout.marginHeight = layout.marginWidth = 4;
    setLayout(layout);

    Group general = new Group(this, SWT.NONE);
    general.setLayout(new GridLayout(3, false));
    general.setText(localizationResources.getString("General"));

    // row 1: antialiasing
    this.antialias = new Button(general, SWT.CHECK);
    this.antialias.setText(localizationResources.getString("Draw_anti-aliased"));
    this.antialias.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
    this.antialias.setSelection(chart.getAntiAlias());

    //row 2: background paint for the chart
    new Label(general, SWT.NONE).setText(localizationResources.getString("Background_paint"));
    this.backgroundPaintCanvas = new SWTPaintCanvas(general, SWT.NONE,
            SWTUtils.toSwtColor(getDisplay(), chart.getBackgroundPaint()));
    GridData bgGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
    bgGridData.heightHint = 20;
    this.backgroundPaintCanvas.setLayoutData(bgGridData);
    Button selectBgPaint = new Button(general, SWT.PUSH);
    selectBgPaint.setText(localizationResources.getString("Select..."));
    selectBgPaint.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
    selectBgPaint.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            ColorDialog dlg = new ColorDialog(getShell());
            dlg.setText(localizationResources.getString("Background_paint"));
            dlg.setRGB(SWTOtherEditor.this.backgroundPaintCanvas.getColor().getRGB());
            RGB rgb = dlg.open();
            if (rgb != null) {
                SWTOtherEditor.this.backgroundPaintCanvas.setColor(new Color(getDisplay(), rgb));
            }
        }
    });
}

From source file:com.rcp.wbw.demo.editor.SWTOtherEditor.java

/**
 * Creates a new instance.//  w ww.  j av a2 s. c om
 * 
 * @param parent
 *            the parent.
 * @param style
 *            the style.
 * @param chart
 *            the chart.
 */
public SWTOtherEditor(Composite parent, int style, JFreeChart chart) {
    super(parent, style);
    FillLayout layout = new FillLayout();
    layout.marginHeight = layout.marginWidth = 4;
    setLayout(layout);

    Group general = new Group(this, SWT.NONE);
    general.setLayout(new GridLayout(3, false));
    general.setText(localizationResources.getString("General"));

    // row 1: antialiasing
    this.antialias = new Button(general, SWT.CHECK);
    this.antialias.setText(localizationResources.getString("Draw_anti-aliased"));
    this.antialias.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
    this.antialias.setSelection(chart.getAntiAlias());

    // row 2: background paint for the chart
    new Label(general, SWT.NONE).setText(localizationResources.getString("Background_paint"));
    this.backgroundPaintCanvas = new SWTPaintCanvas(general, SWT.NONE,
            SWTUtils.toSwtColor(getDisplay(), chart.getBackgroundPaint()));
    GridData bgGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
    bgGridData.heightHint = 20;
    this.backgroundPaintCanvas.setLayoutData(bgGridData);
    Button selectBgPaint = new Button(general, SWT.PUSH);
    selectBgPaint.setText(localizationResources.getString("Select..."));
    selectBgPaint.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
    selectBgPaint.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            ColorDialog dlg = new ColorDialog(getShell());
            dlg.setText(localizationResources.getString("Background_paint"));
            dlg.setRGB(SWTOtherEditor.this.backgroundPaintCanvas.getColor().getRGB());
            RGB rgb = dlg.open();
            if (rgb != null) {
                SWTOtherEditor.this.backgroundPaintCanvas.setColor(new Color(getDisplay(), rgb));
            }
        }
    });
}

From source file:com.bdaum.zoom.report.internal.wizards.ReportComponent.java

public void saveChartProperties(Report report) {
    JFreeChart chart = chartComposite.getChart();
    if (chart != null) {
        Map<String, Object> properties = new HashMap<>();
        TextTitle title = chart.getTitle();
        if (title != null) {
            properties.put(TITLE, title.getText());
            properties.put(TITLEFONT, title.getFont());
            properties.put(TITLECOLOR, title.getPaint());
        }//  w  w  w  .  j a  va2  s  .  com
        Plot plot = chart.getPlot();
        properties.put(BGCOLOR, plot.getBackgroundPaint());
        properties.put(OUTLINEPAINT, plot.getOutlinePaint());
        properties.put(OUTLINESTROKE, plot.getOutlineStroke());
        Axis domainAxis = null;
        Axis rangeAxis = null;
        if (plot instanceof CategoryPlot) {
            CategoryPlot p = (CategoryPlot) plot;
            domainAxis = p.getDomainAxis();
            rangeAxis = p.getRangeAxis();
            properties.put(ORIENTATION, p.getOrientation());
        } else if (plot instanceof XYPlot) {
            XYPlot p = (XYPlot) plot;
            domainAxis = p.getDomainAxis();
            rangeAxis = p.getRangeAxis();
            properties.put(ORIENTATION, p.getOrientation());
        }
        if (domainAxis != null)
            saveAxisProperties(domainAxis, "x", properties); //$NON-NLS-1$
        if (rangeAxis != null)
            saveAxisProperties(rangeAxis, "y", properties); //$NON-NLS-1$
        properties.put(ANTIALIAS, chart.getAntiAlias());
        properties.put(CANVASPAINT, chart.getBackgroundPaint());
        report.setProperties(properties);
    }
}

From source file:maltcms.ui.fileHandles.csv.CSV2JFCLoader.java

@Override
public void run() {
    CSV2TableLoader tl = new CSV2TableLoader(this.ph, this.is);

    DefaultTableModel dtm;/* www. ja va 2  s.  c o m*/
    try {
        dtm = tl.call();
        if (this.mode == CHART.XY) {
            XYSeriesCollection cd = new XYSeriesCollection();
            for (int j = 0; j < dtm.getColumnCount(); j++) {
                XYSeries xys = new XYSeries(dtm.getColumnName(j));
                for (int i = 0; i < dtm.getRowCount(); i++) {
                    Object o = dtm.getValueAt(i, j);
                    try {
                        double d = Double.parseDouble(o.toString());
                        xys.add(i, d);
                        Logger.getLogger(getClass().getName()).log(Level.INFO, "Adding {0} {1} {2}",
                                new Object[] { i, d, dtm.getColumnName(j) });
                    } catch (Exception e) {
                    }
                }
                cd.addSeries(xys);
            }
            XYLineAndShapeRenderer d = new XYLineAndShapeRenderer(true, false);
            XYPlot xyp = new XYPlot(cd, new NumberAxis("category"), new NumberAxis("value"), d);

            JFreeChart jfc = new JFreeChart(this.title, xyp);
            jtc.setChart(jfc);
            Logger.getLogger(getClass().getName()).info("creating chart done");
        } else if (this.mode == CHART.MATRIX) {
            DefaultXYZDataset cd = new DefaultXYZDataset();
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Name of column 0: {0}",
                    dtm.getColumnName(0));
            if (dtm.getColumnName(0).isEmpty()) {
                Logger.getLogger(getClass().getName()).info("Removing column 0");
                dtm = removeColumn(dtm, 0);
            }
            if (dtm.getColumnName(dtm.getColumnCount() - 1).equalsIgnoreCase("filename")) {
                dtm = removeColumn(dtm, dtm.getColumnCount() - 1);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dtm.getRowCount(); i++) {
                for (int j = 0; j < dtm.getColumnCount(); j++) {
                    sb.append(dtm.getValueAt(i, j) + " ");
                }
                sb.append("\n");
            }
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Table before sorting: {0}", sb.toString());
            //                dtm = sort(dtm);
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < dtm.getRowCount(); i++) {
                for (int j = 0; j < dtm.getColumnCount(); j++) {
                    sb2.append(dtm.getValueAt(i, j) + " ");
                }
                sb2.append("\n");
            }
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Table after sorting: {0}", sb2.toString());
            int rows = dtm.getRowCount();
            int columns = dtm.getColumnCount();
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Storing {0} * {1} elements, {2} total!",
                    new Object[] { columns, rows, rows * columns });
            double[][] data = new double[3][(columns * rows)];
            ArrayDouble.D1 dt = new ArrayDouble.D1((columns) * rows);
            double min = Double.POSITIVE_INFINITY;
            double max = Double.NEGATIVE_INFINITY;
            EvalTools.eqI(rows, columns, this);
            int k = 0;
            for (int i = 0; i < dtm.getRowCount(); i++) {
                for (int j = 0; j < dtm.getColumnCount(); j++) {

                    Object o = dtm.getValueAt(i, j);
                    try {
                        double d = Double.parseDouble(o.toString());
                        if (d < min) {
                            min = d;
                        }
                        if (d > max) {
                            max = d;
                        }
                        data[0][k] = (double) i;
                        data[1][k] = (double) j;
                        data[2][k] = d;
                        dt.set(k, d);
                        k++;
                        //System.out.println("Adding "+i+" "+d+" "+dtm.getColumnName(j));
                    } catch (Exception e) {
                    }
                }
                //cd.addSeries(xys);
            }
            cd.addSeries(this.title, data);
            XYBlockRenderer xyb = new XYBlockRenderer();
            GradientPaintScale ps = new GradientPaintScale(ImageTools.createSampleTable(256), min, max,
                    ImageTools
                            .rampToColorArray(new ColorRampReader().readColorRamp("res/colorRamps/bcgyr.csv")));

            xyb.setPaintScale(ps);
            final String[] colnames = new String[dtm.getColumnCount()];
            for (int i = 0; i < colnames.length; i++) {
                colnames[i] = dtm.getColumnName(i);
            }
            NumberAxis na1 = new SymbolAxis("category", colnames);
            na1.setVerticalTickLabels(false);
            NumberAxis na2 = new SymbolAxis("category", colnames);
            na1.setVerticalTickLabels(true);
            XYPlot xyp = new XYPlot(cd, na1, na2, xyb);
            xyb.setSeriesToolTipGenerator(0, new XYToolTipGenerator() {

                @Override
                public String generateToolTip(XYDataset xyd, int i, int i1) {
                    return "[" + colnames[xyd.getX(i, i1).intValue()] + ":"
                            + colnames[xyd.getY(i, i1).intValue()] + "] = "
                            + ((XYZDataset) xyd).getZValue(i, i1) + "";
                }
            });

            JFreeChart jfc = new JFreeChart(this.title, xyp);
            NumberAxis values = new NumberAxis("value");
            values.setAutoRange(false);
            values.setRangeWithMargins(min, max);
            PaintScaleLegend psl = new PaintScaleLegend(ps, values);
            psl.setBackgroundPaint(jfc.getBackgroundPaint());
            jfc.addSubtitle(psl);
            psl.setStripWidth(50);
            psl.setPadding(20, 20, 20, 20);
            psl.setHeight(200);
            psl.setPosition(RectangleEdge.RIGHT);
            jtc.setChart(jfc);
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }

    ph.finish();
}