JFreeChart 2 底部域轴

4
我目前使用JFreeChart遇到了问题。我需要两个相关的域轴,例如: | | | | | | _________ _________ __________(第一个轴) 8:00 - 9:00 9:01 - 10:00 10:01 - 11:00 | ______ ______(第二个轴在第一个轴下面) Phase 1 Phase 2
也就是说,Phase 1实际上对应于8:00到8:45(在第一个轴上),而Phase 2对应于8:46到9:20。
以下是我用来解决问题的代码:
            private ChartPanel createChart(CategoryDataset dataset, CategoryDataset phaseDataset){
    JFreeChart chart = ChartFactory.createBarChart(
        "",
        "",
        "Numbers", 
dataset, 
PlotOrientation.Vertical, 
true, 
true, 
false);

CategoryPlot plot = chart.getCategoryPlot();

NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setStandTickUnits(NumberAxis.createIntegerTickUnits());

CategoryAxis domainAxis = plot.getDomainAxis();
// some paint settings to be done to domainAxis

CategoryAxis axis2 = new CategoryAxis()

plot.setDomainAxis(1, axis2);
plot.setDataset(1, phaseDataset);
plot.mapDatasetToRangeAxis(1,1);
plot.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);

ChartPanel panel = new ChartPanel(chart);

return panel;
}

我的第二个轴(用于相位)的刻度标签似乎在顶部而不是底部。

我该如何实现这一点?抱歉画得很烂,无法附加图像...

另一个问题 - 我如何设置CategoryAxis的范围?我希望所有类别都出现在我的图表中,但如果该类别中没有数据,则该类别将不会出现?

回到设计基础知识?我是否为此使用了正确的域轴?

@EDIT 我注意到我添加的第二个轴无法显示刻度,第二组数据集实际上出现在第一个轴上...

1个回答

4

我也遇到了这个问题,经过漫长而繁琐的API搜索,我终于找到了解决方案。下面是一个代码片段,来自我必须构建的一个图表,其中有3个叠放在一起的x轴:

xyPlot.setDomainAxis(0, xAxis1);
xyPlot.setDomainAxis(1, xAxis2);
xyPlot.setDomainAxis(2, xAxis3);
xyPlot.setRangeAxis(0, yAxis);

xyPlot.mapDatasetToDomainAxis(0, 0);
xyPlot.mapDatasetToRangeAxis(0, 0);

xyPlot.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
xyPlot.setDomainAxisLocation(2, AxisLocation.BOTTOM_OR_LEFT);

虽然我的回答有点晚,但希望这能为其他人节省找到解决方法的繁琐努力!


1
哇塞。是的,JFreeChart非常强大,但除了基本的API文档之外,它的文档非常不足。我见过很多错误的实现方式;感谢您带来正确的方法! - SilverbackNet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接