JFreeChart的StackedXYAreaChart

时间:2017-12-01 09:36:42

标签: java javafx charts jfreechart jfreechart-fx

在下面的示例中, Eden 是索引为0的系列,幸存者的索引为1 >在索引2

Stacked Area Chart with 3 series

考试, Old 是图表中最顶级的系列,而 Eden 位于底部。

我想要实现的是 Eden 是叠加图表中最顶级的系列, Survivor 中间的那个和 Old 在底部。不过, Eden 应该是图例中的第一个条目, Old 应该是最后一个。

我目前使用以下类创建我的图表:

public class JFreeChartFactory {
    private static String fontName = "Palatino";

    private static Color[] chartSeriesColors = {
            new Color(0.0f, 1.0f, 0.0f, 0.9f),
            new Color(0.0f, 0.0f, 1.0f, 0.9f),
            new Color(1.0f, 0.0f, 0.0f, 0.9f),
            };

    public static JFreeChart createStackedXYAreaChart(String title, String subTitle, String xAxis, String yAxis, TableXYDataset dataset) {

        JFreeChart chart = ChartFactory.createStackedXYAreaChart(
                title,
                xAxis,
                yAxis,
                dataset);

        chart.getTitle().setFont(new Font(fontName, Font.BOLD, 18));

        if (subTitle != null) {
            chart.addSubtitle(new TextTitle(subTitle, new Font(fontName, Font.PLAIN, 14)));
        }

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        plot.getDomainAxis().setLowerMargin(0.0);
        plot.getDomainAxis().setLabelFont(new Font(fontName, Font.BOLD, 14));
        plot.getDomainAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12));
        plot.getRangeAxis().setLabelFont(new Font(fontName, Font.BOLD, 14));
        plot.getRangeAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12));
        chart.getLegend().setItemFont(new Font(fontName, Font.PLAIN, 14));
        chart.getLegend().setFrame(BlockBorder.NONE);
        chart.getLegend().setHorizontalAlignment(HorizontalAlignment.CENTER);

        for (int i = 0; i < plot.getRendererCount(); i++) {
            XYItemRenderer renderer = plot.getRenderer(i);
            switch (renderer.getClass().getSimpleName()) {
                case "StackedXYAreaRenderer2":
                    StackedXYAreaRenderer2 stackedRenderer = (StackedXYAreaRenderer2) renderer;
                    for (int cId = 0; cId < chartSeriesColors.length; cId++) {
                        stackedRenderer.setSeriesPaint(cId, chartSeriesColors[cId]);
                    }
                    break;
            }
        }

        return chart;
    }
}

0 个答案:

没有答案