Dilushan
Dilushan

Reputation: 143

Jfreechart histogram set translucent colours

I have a histogram like this histogram

Here Red histogram in the front, blue in the middle and green at the behind. So the most of the green histogram not able to see. Is there any option to switch between colors when hovering mouse or any other option to see the green and blue histogram.

Here is the code

public class HistogramFrame extends JFrame {

    static double[] red;
    static double[] green;
    static double[] blue;

    public HistogramFrame(String title, double[] red, double[] green, double[] blue) {
        super(title);
        HistogramFrame.red = red;
        HistogramFrame.green = green;
        HistogramFrame.blue = blue;

        JFreeChart jfreechart = ChartFactory.createHistogram("Histograms combination red,blue,green", null, null, createDataset(), PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        xyplot.setForegroundAlpha(0.85F);
        XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
        xybarrenderer.setDrawBarOutline(false);
        JPanel jpanel = new ChartPanel(jfreechart);
        jpanel.setPreferredSize(new Dimension(1000, 600));
        setContentPane(jpanel);
    }

    private static IntervalXYDataset createDataset() {
        HistogramDataset histogramdataset = new HistogramDataset();
        histogramdataset.addSeries("Red histogram", red, 256);
        histogramdataset.addSeries("Blue histogram", blue, 256);
        histogramdataset.addSeries("Green histogram", green, 256);

        return histogramdataset;
    }

}

Upvotes: 1

Views: 1131

Answers (1)

Dilushan
Dilushan

Reputation: 143

Here is the full code for the solution and graph image. Now translucent colors make more clear to see each histograms.

Translucent Histogram

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.xy.IntervalXYDataset;

public class HistogramFrame extends JFrame {

    static double[] red;
    static double[] green;
    static double[] blue;

    public HistogramFrame(String title, double[]     red, double[] green, double[] blue) {
        super(title);
        HistogramFrame.red = red;
        HistogramFrame.green = green;
        HistogramFrame.blue = blue;

        JFreeChart jfreechart = ChartFactory.createHistogram("Histograms combination red,blue,green", null, null, createDataset(), PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        xyplot.setForegroundAlpha(0.85F);
        XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
        xybarrenderer.setBarPainter(new StandardXYBarPainter());
        //xybarrenderer.setDrawBarOutline(false);
        Paint[] paintArray = {              //code related to translucent colors begin here
            new Color(0x80ff0000, true),
            new Color(0x8000ff00, true),
            new Color(0x800000ff, true)
        };

        xyplot.setDrawingSupplier(new DefaultDrawingSupplier(
            paintArray,
            DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));

        JPanel jpanel = new ChartPanel(jfreechart);
        jpanel.setPreferredSize(new Dimension(1000, 600));
        setContentPane(jpanel);
    }

    private static IntervalXYDataset createDataset() {
        HistogramDataset histogramdataset = new HistogramDataset();
        histogramdataset.addSeries("Red histogram", red, 256);
        histogramdataset.addSeries("Blue histogram", blue, 256);
        histogramdataset.addSeries("Green histogram", green, 256);

        return histogramdataset;
    }

}

Upvotes: 1

Related Questions