gpt4 book ai didi

java - 在瀑布图中连接条形图

转载 作者:行者123 更新时间:2023-11-30 05:24:35 25 4
gpt4 key购买 nike

我正在创建一个瀑布图,如下图所示。

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

瀑布演示输出:

Waterfall Demo

如何使用如下所示的线将一个条连接到另一个条中?我只能通过调用 CategoryAxissetCategoryMargin(double) 来添加条形之间的空间,但没有找到任何执行条形与线条连接的 API。

注意:下图取自使用不同图表框架生成的另一个示例。

Expected Waterfall output

最佳答案

根据上面的 @trashgod 评论,我可以使用 CategoryLineAnnotation 在上述用例的条形之间绘制线条。

更新的代码:

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

输出:

Waterfall Demo with connector lines

关于java - 在瀑布图中连接条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com