Search in sources :

Example 6 with PaintScaleLegend

use of org.jfree.chart.title.PaintScaleLegend in project mzmine2 by mzmine.

the class ChartExportUtil method writeChartToImage.

/**
 * This method is used to save all image formats. it uses the specific methods for each file
 * format
 *
 * @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);
}
Also used : PaintScaleLegend(org.jfree.chart.title.PaintScaleLegend) Paint(java.awt.Paint) Dimension(java.awt.Dimension) File(java.io.File) Paint(java.awt.Paint)

Example 7 with PaintScaleLegend

use of org.jfree.chart.title.PaintScaleLegend in project mzmine2 by mzmine.

the class VanKrevelenDiagramTask method create3DVanKrevelenDiagram.

/**
 * create 3D Van Krevelen Diagram chart
 */
private JFreeChart create3DVanKrevelenDiagram() {
    logger.info("Creating new 3D chart instance");
    appliedSteps++;
    // load data set
    VanKrevelenDiagramXYZDataset dataset3D = new VanKrevelenDiagramXYZDataset(zAxisLabel, filteredRows);
    // copy and sort z-Values for min and max of the paint scale
    double[] copyZValues = new double[dataset3D.getItemCount(0)];
    for (int i = 0; i < dataset3D.getItemCount(0); i++) {
        copyZValues[i] = dataset3D.getZValue(0, i);
    }
    Arrays.sort(copyZValues);
    // get index in accordance to percentile windows
    int minScaleIndex = 0;
    int maxScaleIndex = copyZValues.length - 1;
    double min = 0;
    double max = 0;
    if (zAxisScaleType.equals("percentile")) {
        minScaleIndex = (int) Math.floor(copyZValues.length * (zScaleRange.lowerEndpoint() / 100));
        maxScaleIndex = copyZValues.length - (int) (Math.ceil(copyZValues.length * ((100 - zScaleRange.upperEndpoint()) / 100)));
        if (zScaleRange.upperEndpoint() == 100) {
            maxScaleIndex = copyZValues.length - 1;
        }
        if (zScaleRange.lowerEndpoint() == 0) {
            minScaleIndex = 0;
        }
        min = copyZValues[minScaleIndex];
        max = copyZValues[maxScaleIndex];
    }
    if (zAxisScaleType.equals("custom")) {
        min = zScaleRange.lowerEndpoint();
        max = zScaleRange.upperEndpoint();
    }
    // create paint scale for third dimension
    Paint[] contourColors = XYBlockPixelSizePaintScales.getPaintColors(zAxisScaleType, zScaleRange, paintScaleStyle);
    LookupPaintScale scale = null;
    scale = new LookupPaintScale(copyZValues[0], copyZValues[copyZValues.length - 1], new Color(0, 0, 0));
    double[] scaleValues = new double[contourColors.length];
    double delta = (max - min) / (contourColors.length - 1);
    double value = min;
    for (int i = 0; i < contourColors.length; i++) {
        scale.add(value, contourColors[i]);
        scaleValues[i] = value;
        value = value + delta;
    }
    // create chart
    chart = ChartFactory.createScatterPlot(title, "O/C", "H/C", dataset3D, PlotOrientation.VERTICAL, true, true, false);
    // set renderer
    XYBlockPixelSizeRenderer renderer = new XYBlockPixelSizeRenderer();
    XYPlot plot = chart.getXYPlot();
    appliedSteps++;
    renderer.setPaintScale(scale);
    double maxX = plot.getDomainAxis().getRange().getUpperBound();
    double maxY = plot.getRangeAxis().getRange().getUpperBound();
    renderer.setBlockWidth(0.001);
    renderer.setBlockHeight(renderer.getBlockWidth() / (maxX / maxY));
    // set tooltip generator
    ScatterPlotToolTipGenerator tooltipGenerator = new ScatterPlotToolTipGenerator("O/C", "H/C", zAxisLabel, filteredRows);
    renderer.setSeriesToolTipGenerator(0, tooltipGenerator);
    renderer.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -45), true);
    // set item label generator
    NameItemLabelGenerator generator = new NameItemLabelGenerator(filteredRows);
    renderer.setDefaultItemLabelGenerator(generator);
    renderer.setDefaultItemLabelsVisible(false);
    renderer.setDefaultItemLabelFont(legendFont);
    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));
    plot.setOutlinePaint(Color.black);
    plot.setBackgroundPaint(Color.white);
    plot.setDomainCrosshairPaint(Color.GRAY);
    plot.setRangeCrosshairPaint(Color.GRAY);
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    // Legend
    NumberAxis scaleAxis = new NumberAxis(zAxisLabel);
    scaleAxis.setRange(min, max);
    scaleAxis.setAxisLinePaint(Color.white);
    scaleAxis.setTickMarkPaint(Color.white);
    PaintScaleLegend legend = new PaintScaleLegend(scale, scaleAxis);
    legend.setStripOutlineVisible(false);
    legend.setAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    legend.setAxisOffset(5.0);
    legend.setMargin(new RectangleInsets(5, 5, 5, 5));
    legend.setFrame(new BlockBorder(Color.white));
    legend.setPadding(new RectangleInsets(10, 10, 10, 10));
    legend.setStripWidth(10);
    legend.setPosition(RectangleEdge.LEFT);
    legend.getAxis().setLabelFont(legendFont);
    legend.getAxis().setTickLabelFont(legendFont);
    chart.addSubtitle(legend);
    appliedSteps++;
    return chart;
}
Also used : XYBlockPixelSizeRenderer(net.sf.mzmine.chartbasics.chartutils.XYBlockPixelSizeRenderer) PaintScaleLegend(org.jfree.chart.title.PaintScaleLegend) NumberAxis(org.jfree.chart.axis.NumberAxis) Color(java.awt.Color) BlockBorder(org.jfree.chart.block.BlockBorder) Paint(java.awt.Paint) Paint(java.awt.Paint) XYPlot(org.jfree.chart.plot.XYPlot) RectangleInsets(org.jfree.chart.ui.RectangleInsets) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) LookupPaintScale(org.jfree.chart.renderer.LookupPaintScale) ScatterPlotToolTipGenerator(net.sf.mzmine.chartbasics.chartutils.ScatterPlotToolTipGenerator) NameItemLabelGenerator(net.sf.mzmine.chartbasics.chartutils.NameItemLabelGenerator)

Aggregations

PaintScaleLegend (org.jfree.chart.title.PaintScaleLegend)7 Paint (java.awt.Paint)4 XYPlot (org.jfree.chart.plot.XYPlot)4 XYBlockPixelSizeRenderer (net.sf.mzmine.chartbasics.chartutils.XYBlockPixelSizeRenderer)3 NumberAxis (org.jfree.chart.axis.NumberAxis)3 Color (java.awt.Color)2 NameItemLabelGenerator (net.sf.mzmine.chartbasics.chartutils.NameItemLabelGenerator)2 ScatterPlotToolTipGenerator (net.sf.mzmine.chartbasics.chartutils.ScatterPlotToolTipGenerator)2 BlockBorder (org.jfree.chart.block.BlockBorder)2 ItemLabelPosition (org.jfree.chart.labels.ItemLabelPosition)2 LookupPaintScale (org.jfree.chart.renderer.LookupPaintScale)2 RectangleInsets (org.jfree.chart.ui.RectangleInsets)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Dimension (java.awt.Dimension)1 BufferedImage (java.awt.image.BufferedImage)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Iterator (java.util.Iterator)1