java - 在瀑布图中加入条形图
问题描述
我正在创建一个Waterfall Chart
,如下图所示。
public class WaterfallDemo extends ApplicationFrame {
public WaterfallDemo(String title) {
super(title);
JFreeChart chart = createWaterfallChart();
ChartPanel chartPanel = new ChartPanel(chart);
setContentPane(chartPanel);
}
public static JFreeChart createWaterfallChart() {
JFreeChart chart = ChartFactory.createWaterfallChart("","","",createDataset(),PlotOrientation.VERTICAL,LEGEND_ON,TOOLTIP_ON,false);
//Customize chart legend for waterfall chart only
chart.removeLegend();
LegendTitle legend = new LegendTitle(new LegendItemSource1());
chart.addLegend(legend);
//update chart styles, legend and properties
updateProperties(chart, isHorizontalBar, isStackedBar);
return chart;
}
public static void main(final String[] args) {
WaterfallDemo waterfallDemo = new WaterfallDemo("Waterfall Chart Demo");
waterfallDemo.pack();
waterfallDemo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
RefineryUtilities.centerFrameOnScreen(waterfallDemo);
waterfallDemo.setVisible(true);
}
}
class LegendItemSource1 implements LegendItemSource {
public LegendItemCollection getLegendItems() {
LegendItemCollection legendList = new LegendItemCollection();
LegendItem item1 = new LegendItem("Increase");
LegendItem item2 = new LegendItem("Decrease");
LegendItem item3 = new LegendItem("Total");
legendList.add(item1);
legendList.add(item2);
legendList.add(item3);
item1.setFillPaint(ChartDefaults.SERIES_COLOR[1]);
item2.setFillPaint(ChartDefaults.SERIES_COLOR[3]);
item3.setFillPaint(ChartDefaults.SERIES_COLOR[0]);
return legendList;
}
}
瀑布演示输出:
如何使用如下所示的线将一个条连接到另一个条?我只能通过调用setCategoryMargin(double)
来在条之间添加空间,CategoryAxis
但没有找到任何执行条与线连接的 API。
注意:下图取自另一个使用不同图表框架生成的示例。
解决方案
CategoryLineAnnotation
通过使用上面的@trashgod 评论,我能够为上面的用例在条之间画线。
更新代码:
public class WaterfallDemo extends ApplicationFrame {
public WaterfallDemo(String title) {
super(title);
JFreeChart chart = createWaterfallChart();
ChartPanel chartPanel = new ChartPanel(chart);
setContentPane(chartPanel);
}
public static JFreeChart createWaterfallChart() {
JFreeChart chart = ChartFactory.createWaterfallChart("","","",createDataset(),PlotOrientation.VERTICAL,LEGEND_ON,TOOLTIP_ON,false);
//Customize chart legend for waterfall chart only
chart.removeLegend();
LegendTitle legend = new LegendTitle(new LegendItemSource1());
chart.addLegend(legend);
//update chart styles, legend and properties
updateProperties(chart, isHorizontalBar, isStackedBar);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.addAnnotation(new CategoryLineAnnotation("GROUP A", 42.0,
"GROUP B", 42.0, Color.red, new BasicStroke(1.0f)));
plot.addAnnotation(new CategoryLineAnnotation("GROUP B", 32.0,
"GROUP C", 32.0, Color.red, new BasicStroke(1.0f)));
plot.addAnnotation(new CategoryLineAnnotation("GROUP C", 62.0,
"GROUP D", 62.0, Color.red, new BasicStroke(1.0f)));
plot.addAnnotation(new CategoryLineAnnotation("GROUP D", 40.0,
"GROUP E", 40.0, Color.red, new BasicStroke(1.0f)));
plot.addAnnotation(new CategoryLineAnnotation("GROUP E", 50.0,
"GROUP F", 50.0, Color.red, new BasicStroke(1.0f)));
return chart;
}
public static void main(final String[] args) {
WaterfallDemo waterfallDemo = new WaterfallDemo("Waterfall Chart Demo");
waterfallDemo.pack();
waterfallDemo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
RefineryUtilities.centerFrameOnScreen(waterfallDemo);
waterfallDemo.setVisible(true);
}
}
class LegendItemSource1 implements LegendItemSource {
public LegendItemCollection getLegendItems() {
LegendItemCollection legendList = new LegendItemCollection();
LegendItem item1 = new LegendItem("Increase");
LegendItem item2 = new LegendItem("Decrease");
LegendItem item3 = new LegendItem("Total");
legendList.add(item1);
legendList.add(item2);
legendList.add(item3);
item1.setFillPaint(ChartDefaults.SERIES_COLOR[1]);
item2.setFillPaint(ChartDefaults.SERIES_COLOR[3]);
item3.setFillPaint(ChartDefaults.SERIES_COLOR[0]);
return legendList;
}
}
输出:
推荐阅读
- modelica - 如何在 Dymola 中设置默认求解器?
- php - Probelm 将 Laravel 连接到 filrebase
- php - 将 DES-EDE3-CBC 从 PHP 移植到 Ruby 会产生不同的结果
- apache-spark - 将 sql 代码转换为 Pyspark 代码的问题
- ios - lineWidth SwiftUI 不正确
- flutter - 当我在颤动中添加容器小部件时显示空白屏幕
- javascript - 带参数的 Reacjs setInterval 函数
- r - 如何将大型列表对象(igraph.vs 类)转换为 R 中的数据框
- encryption - HTTPS 使用非对称或对称加密吗?
- r - dcast 和重新排序数据帧的数据