瀑布图中的连接条

问题描述 投票:1回答:1

我正在创建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

java jfreechart
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.