2

I have to implement an histogram using JFreeChart API. This histogram has to represent the datas of this JTable:

enter image description here

So I have a JTable with three columns: "thea", "type", "Number of occurrences". My histogram has two targets: the first is to count the number of occurrences of each thea field; the second is to mark with different colors the bars corresponding to JTable records with different types.

To implement my histogram I used a DefaultCategoryDataset:

private DefaultCategoryDataset createDataset(ArrayList<String>fieldsOccs) {

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int i = 0; i<this.fieldsOccs.size() && i<end; i++) {
    String thea = fieldsOccs.get(i).getFieldName();
    String type = fieldsOccs.get(i).getType();
    int occurrences  = fieldsOccs.get(i).getOccurrences();

    dataset.setValue(occurrences, type, thea);
    }   

return dataset;
}

Anf then I create my chart using a createChart method:

private JFreeChart createChart(DefaultCategoryDataset dataset) {

    JFreeChart chart = ChartFactory.createBarChart(
            "",                                             
            "",                                             //X-axis title
            "",                                             //Y-axis title  
            dataset,                                        //dataset
            PlotOrientation.HORIZONTAL,                     //plot orientation
            true,                                           //show legends      
            true,                                           //use tooltips
            false                                           //generate URLs
            );

    return chart;

}

This is what I get: enter image description here

As you can see in the picture it is not nice to see. The values on x axes are not formatted correctly.

How can I solve this rendering problem?

--edit

I have this problem just in case of more types in the JTable. For example if my JTable is: enter image description here

and there is just String, the correspondig histogram is nice: enter image description here

--edit1

What dou you think about StackedBarChart3D? I get this output:

enter image description here

harry-potter
  • 1,981
  • 5
  • 29
  • 55

1 Answers1

3

My histogram has two targets:

  1. You may get a more appealing result with ChartFactory.createHistogram() and a SimpleHistogramDataset, seen here.

  2. To get diverse colors, override the getItemPaint() method in a custom XYBarRenderer, as suggested here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045