would like to seek a little help and see if what I am trying to do is even doable. I am plotting a histogram as per source code snippet below
Code: Select all
public GARunStatisticsHistogramPanel(GAMultiRunStatisticsObject MSO) {
//create the histogram data
this.MSO = MSO;
dataset = new HistogramDataset();
dataset.addSeries("Generation Solution Found", formatHistogramData(MSO.getGenSolutionFoundVector()), 100,1,101); //(series ID, data, number of bins (granularity), lowest, highest)
init();
}
private void init(){
setHistogramStatistics();
JFreeChart chart = createChart(dataset);
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
panel.setPreferredSize(new java.awt.Dimension(1200, 800));
setLayout(new BorderLayout()); //set layout and add to current content pane
add(panel, BorderLayout.CENTER);
add(getIndicatorPanel(), BorderLayout.SOUTH);
}
public double[] formatHistogramData(Vector<Integer> data) {
double histData[] = new double[data.size()];
for (int z=0; z<data.size(); z++){ // count number of occurrences of each histogram bin value
histData[z] = data.get(z).doubleValue(); //need to subtract 1 to account for missing zero bin
}
return histData;
}// end createCategoryData function
protected void setHistogramStatistics(){
int modeIndex = 0;
int modeValue = 0;
for (int i=0; i<dataset.getItemCount(0); i++){
int nextValue = new Double(dataset.getYValue(0, i)).intValue();
if (modeValue < nextValue){
modeValue = nextValue;
modeIndex = (i+1);
}
}
histogramModeIndex = new Integer(modeIndex);
histogramModeValue = new Integer(modeValue);
}
/**
* Creates a chart.
* @param dataset a dataset.
* @return The chart.
*/
private static JFreeChart createChart(IntervalXYDataset dataset) {
JFreeChart chart = ChartFactory.createHistogram(
title, //title
"Generation Solution Found", //X-Axis Label
"Occurence Count", //X-Axis Label
dataset, //the data
PlotOrientation.VERTICAL, //plot orientation
false, //legend?
true, //tooltips?
false); //URL
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);
plot.setRangeGridlineStroke(new BasicStroke(1.0f));
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //slant axis labels by 45 degrees
XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(true); //draw bar outlines
renderer.setOutlineStroke(new BasicStroke(0.5f)); //these are deprecated by they work. not sure what to use in their place
renderer.setOutlinePaint(Color.black);
renderer.setBarPainter(new StandardXYBarPainter()); //make bars flat paint
renderer.setShadowVisible(false); //turn off shadowing
return chart;
}
public Integer getHistogramModeValue(){
return histogramModeValue;
}
public Integer getHistogramModeIndex(){
return histogramModeIndex;
}
/**
* This method initializes the indicator panel which holds the
* Label and text field statistics indicators and selectors
* @return javax.swing.JPanel containing the GUI features for this panel
*/
private JPanel getIndicatorPanel() {
if (indicatorPanel == null) {
histBinffCallCountLabel = new JLabel(" FF Call Count Bin Avg");
indicatorPanelSpacer1 = new JLabel("");
indicatorPanelSpacer1.setPreferredSize(new java.awt.Dimension(40,20));
indicatorPanelSpacer2 = new JLabel("");
indicatorPanelSpacer2.setPreferredSize(new java.awt.Dimension(30,20));
occurenceCountLabel = new JLabel(" Occurence Count");
histBinNumberlabel = new JLabel("Bin Number");
indicatorPanel = new JPanel();
indicatorPanel.setLayout(new FlowLayout());
indicatorPanel.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.black,2));
indicatorPanel.setBackground(java.awt.Color.lightGray);
indicatorPanel.add(histBinNumberlabel, null);
indicatorPanel.add(getBinNumber(), null);
indicatorPanel.add(occurenceCountLabel, null);
indicatorPanel.add(getOccurenceCountValue(), null);
indicatorPanel.add(histBinffCallCountLabel, null);
indicatorPanel.add(getHistBinFFCallCountField(), null);
indicatorPanel.add(indicatorPanelSpacer2, null);
indicatorPanel.add(getPlotButton(), null);
indicatorPanel.add(indicatorPanelSpacer1, null);
indicatorPanel.add(globalGARunStatsPanel(), null);
}
return indicatorPanel;
}
my histogram has 100 bins starting from bin 1 to 101. this lines up the tickmarks and labels perfectly to the plot's gridlines at the left edge of each histogram bin. I would prefer to have the tickmarks and labels be centered on each histogram bin instead. is there a way to do this?