JTable中的CategoryPlot-JFreeChart [英] CategoryPlot from a JTable - JFreeChart

查看:109
本文介绍了JTable中的CategoryPlot-JFreeChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用JFreeChart API实现直方图.此直方图必须代表此JTable的数据:

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

所以我有一个包含三列的JTable:"thea","type",出现次数".我的直方图有两个目标:第一个是计算每个thea字段的出现次数;第二个是计算每个thea字段的出现次数.第二种是用不同的颜色标记与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.

要实现直方图,我使用了DefaultCategoryDataset:

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;
}

然后我使用createChart方法创建图表:

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;

}

这是我得到的:

正如您在图片中看到的那样,很难看到. x轴上的值格式不正确.

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?

-编辑

仅当JTable中有更多类型时,我才遇到这个问题.例如,如果我的JTable是:

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

只有String,对应的直方图很好:

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

-edit1

您对StackedBarChart3D有何看法?我得到以下输出:

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

推荐答案

我的直方图有两个目标:

My histogram has two targets:

  1. 使用ChartFactory.createHistogram()SimpleHistogramDataset可能会获得更有吸引力的结果,请参见在此处.

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

要获取多种颜色,请按照建议此处在自定义XYBarRenderer中覆盖getItemPaint()方法. >.

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

这篇关于JTable中的CategoryPlot-JFreeChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆