CategoryPlot from a JTable - JFreeChart

倾然丶 夕夏残阳落幕 提交于 2019-12-11 00:37:07

问题


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

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:

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:

and there is just String, the correspondig histogram is nice:

--edit1

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


回答1:


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.



来源:https://stackoverflow.com/questions/36549593/categoryplot-from-a-jtable-jfreechart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!