2

I have a double 2-D matrix, which contains negative and postive float values as well as NAs. These values belong to an image data. The values lie in the range -0.4 to +0.4 I want to use the JFreeChart library to create a histogram and see the frequency with a bin width of 0.05. To prepare the dataset matrix as a HistogramDataset, I first converted the matrix to a 1-D double matrix (code below) and then used the createHistogram method available in the chartFactory class to draw the histogram. But, I do not get the results. I can just see a vertical line in the chartPanel area. I looked at these examples, but they do not use a 2-D matrix like data as input.

http://www.java2s.com/Code/Java/Chart/JFreeChartXYSeriesDemo3.htm

Image histogram generated by JFreeChart

The second example was a little similar, but it does not use a 2-D matrix.

This is the code which I have implemented to prepare the dataset and create the histogram.

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;

import java.awt.*;

public class Histogram {

    public JFreeChart createHistogram(double[][] doubleMatrix){

        // Generate a one dimensional array of the size w*h of the double matrix
        double[] data = new double[doubleMatrix.length * doubleMatrix[0].length];
        int count = 0;

        for (int i=0; i<doubleMatrix.length; i++) {
            for (int j = 0; j < doubleMatrix[i].length; j++) {
                data[count] = doubleMatrix[i][j];
                count++;
            }
        }

       // int number = data.length;
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.FREQUENCY);
        dataset.addSeries("Hist",data,50); // Number of bins is 50
        String plotTitle = "";
        String xAxis = "Frequency";
        String yAxis = "Mass Error (Da)";
        PlotOrientation orientation = PlotOrientation.VERTICAL;

        boolean show = false;
        boolean toolTips = false;
        boolean urls = false;
        JFreeChart chart = ChartFactory.createHistogram(plotTitle, xAxis, yAxis,
                dataset, orientation, show, toolTips, urls);

        chart.setBackgroundPaint(Color.white);

        return chart;
    }
} 

The 2-D double matrix I am using can be found here: http://www.filedropper.com/data_4

What I get on using the above code for this dataset is the following histogram (!). Is it the size of the histogram which does not fit into the ChartPanel or the JPanel?

enter image description here

Community
  • 1
  • 1
novicegeek
  • 773
  • 2
  • 9
  • 29
  • Please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that shows your current approach, for [example](http://stackoverflow.com/q/28519355/230513) and [example](http://stackoverflow.com/q/29279550/230513). – trashgod Sep 25 '15 at 00:46
  • I have added my current code implementation :) – novicegeek Sep 25 '15 at 08:33
  • Don't you need `SimpleHistogramBin`? What's in `doubleMatrix `? A little representative data would let others try your code. – trashgod Sep 25 '15 at 10:29
  • I have added the dataset which I am using (http://www.filedropper.com/data_4). The double matrix contains negative and positive float values and zeros (initially the zeros were NA's). – novicegeek Sep 25 '15 at 10:45

1 Answers1

1

I found a solution. i first removed all the NA's from the double matrix by using a ArrayList and then converted this arrayList to a 1D array and then generating the histogram. It worked now. Here is the code:

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;

import java.awt.*;
import java.util.ArrayList;

public class Histogram {

    public JFreeChart createHistogram(double[][] doubleMatrix){

        // Generate a one dimensional array of the size w*h of the double matrix
        ArrayList<Double> dataArrayList = new ArrayList<Double>();

        for (int i=0; i<doubleMatrix.length; i++) {
            for (int j = 0; j < doubleMatrix[i].length; j++) {
                double value =  doubleMatrix[i][j];
                if( Double.isNaN(value))
                    continue;
                else
                    dataArrayList.add(value);
                    System.out.println(value);
            }
        }

        double[] data = new double[dataArrayList.size()];

        for(int p = 0; p < dataArrayList.size();p++)
             data[p] = dataArrayList.get(p);


       // int number = data.length;
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.RELATIVE_FREQUENCY);
        dataset.addSeries("Hist",data,200); // Number of bins is 50
        String plotTitle = "";
        String xAxis = "Frequency";
        String yAxis = "Mass Error (Da)";
        PlotOrientation orientation = PlotOrientation.VERTICAL;

        boolean show = false;
        boolean toolTips = false;
        boolean urls = false;
        JFreeChart chart = ChartFactory.createHistogram(plotTitle, xAxis, yAxis,
                dataset, orientation, show, toolTips, urls);

        chart.setBackgroundPaint(Color.white);

        return chart;
    }
}
novicegeek
  • 773
  • 2
  • 9
  • 29
  • Maybe add some sample data and/or a [*screenshot*](http://meta.stackoverflow.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) to complement the image in the question. – trashgod Sep 25 '15 at 11:26