JTable中的CategoryPlot-JFreeChart [英] CategoryPlot from a JTable - 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:
-
使用
ChartFactory.createHistogram()
和SimpleHistogramDataset
可能会获得更有吸引力的结果,请参见在此处.
You may get a more appealing result with
ChartFactory.createHistogram()
and aSimpleHistogramDataset
, seen here.
要获取多种颜色,请按照建议此处在自定义XYBarRenderer
中覆盖getItemPaint()
方法. >.
To get diverse colors, override the getItemPaint()
method in a custom XYBarRenderer
, as suggested here.
这篇关于JTable中的CategoryPlot-JFreeChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!