首页 > 解决方案 > 在瀑布图中加入条形图

问题描述

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

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

预期的瀑布输出

标签: javajfreechart

解决方案


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

输出:

带有连接线的瀑布演示


推荐阅读