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?