To change the X-axis starting value of graph in Jfreechart
Asked Answered
S

2

6

I am calculating histogram of red component of the image and stored it in redhisto[]. The index of the array represent the intensity(0 to 255) and the value represent the number of pixel with that intensity. Then plotting those values with JFreeChart.

My question is:

  1. How to make X-axis value start from 0. Now its starting from negative number.
  2. Can we change the color of the bars in the graph Output Graph obtained code is :

     public class Histogram extends ApplicationFrame {
       public Histogram(final String title) throws IOException {
        super(title);
        IntervalXYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
       }
    
      private IntervalXYDataset createDataset() throws IOException {
       BufferedImage imageA = ImageIO.read(new File("XYZ.bmp"));
       int[] red = new int[imageA.getHeight()*imageA.getWidth()];
       int[] redhisto = new int[256];
       int[] pixel;
       int k= 0;
       for (int y = 0; y < imageA.getHeight(); y++) {
          for (int x = 0; x < imageA.getWidth(); x++) {
            pixel = imageA.getRaster().getPixel(x, y, new int[3]);       
            red[k] = pixel[0];
            k++;
          }
       }        
    
       for(int x=0;x<red.length;x++){
           int y = red[x];
           redhisto[y]++;
        }
    
      final XYSeries series = new XYSeries("No of pixels");
      for(int i=0; i<redhisto.length;i++)
        series.add(i,redhisto[i]);
    
      final XYSeriesCollection dataset = new XYSeriesCollection(series);
      return dataset;
     }
    
     private JFreeChart createChart(IntervalXYDataset dataset) {
      final JFreeChart chart = ChartFactory.createXYBarChart("Color Intensity   Histogram","X",false,"Y",dataset,PlotOrientation.VERTICAL,true,true,false);
      XYPlot plot = (XYPlot) chart.getPlot();
      return chart;    
     }
    
     public static void main(final String[] args) throws IOException {
      final Histogram demo = new Histogram("Image Histogram");
      demo.pack();
      RefineryUtilities.centerFrameOnScreen(demo);
      demo.setVisible(true);
     }
    }
    
Sylvestersylvia answered 12/1, 2014 at 9:0 Comment(0)
R
3

You can change the lower bound of the domain axis and set the series paint as shown below. The default XYBarPainter has a gradient color highlight, so I used a StandardXYBarPainter.

image

XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setLowerBound(0);
XYBarRenderer r = (XYBarRenderer) plot.getRenderer();
r.setBarPainter(new StandardXYBarPainter());
r.setSeriesPaint(0, Color.blue);
Reek answered 12/1, 2014 at 9:53 Comment(0)
K
1
    XYPlot plot = (XYPlot) chart.getPlot();  

    //To change the lower bound of X-axis
    NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
    xAxis.setLowerBound(0);

    //To change the lower bound of Y-axis       
    NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
    yAxis.setLowerBound(0);

    // To change the color
    XYItemRenderer renderer = plot.getRenderer();
    renderer.setSeriesPaint(0, Color.green);
Krystenkrystin answered 12/1, 2014 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.