瀑布图中的连接条

时间:2019-11-18 16:04:10

标签: java jfreechart

我正在创建一个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;
  }
}

瀑布演示输出:

Waterfall Demo

如何使用一条线将一根钢筋连接到另一根钢筋,如下所示?我只能通过调用setCategoryMargin(double)中的CategoryAxis在小节之间添加空间,但找不到任何执行将小节与行连接的API。

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

Expected Waterfall output

1 个答案:

答案 0 :(得分:1)

根据上面的@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