2015-08-23 65 views
1

我有這樣 histogramjfreechart的直方圖設置半透明的顏色

這裏紅色柱狀圖在前面,中間和綠色在後面藍色柱狀圖。所以大部分的綠色直方圖都無法看到。懸停鼠標或任何其他選項以查看綠色和藍色直方圖時,是否有任何選項在顏色之間切換?

下面是代碼

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

} 
+1

使用單獨的半透明顏色的一種方法是檢查[這裏](http://stackoverflow.com/a/28519356/230513)。 – trashgod

+0

也嘗試一個'StandardBarPainter',建議[這裏](http://stackoverflow.com/a/7092567/230513)。 – trashgod

+0

@trashgod謝謝。 DefaultDrawingSupplier給了半透明的顏色並修復了問題 – Dilushan

回答

1

下面是溶液和圖形圖像的完整代碼。現在半透明的顏色更清晰地看到每個直方圖。

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

} 
+0

很好的使用了「StandardXYBarPainter」。 – trashgod

相關問題