如何在列中显示饼图的图例? [英] How to display legend for Pie Chart in columns?

查看:50
本文介绍了如何在列中显示饼图的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多部分的饼图,此饼图的图例呈现为一行.如何将图例呈现为两列?

I have a PieChart with many sections, legend for this PieChart renders as one row. How to render legend as two columns?

推荐答案

getLegendItem()方法,见此处,提供在您选择的任何Container 中呈现图例项所需的所有信息.GridLayout(0, 2) 会将它们排列成两列任意行.要取消现有图例,请在调用图表工厂时将 legend 设置为 false;这些项目仍然可用,如此处所建议的那样.

The method getLegendItem(), seen here, provides all the information needed to render a legend item in any Container you choose. GridLayout(0, 2) will arrange them in two columns for any number of rows. To suppress the existing legend, set legend to false when you call your chart factory; the items will still be available, as suggested here.

附录:基于 PieChartDemo1,这个片段使用了 getLegendItems().iterator 和这个 颜色图标.

Addendum: Based on PieChartDemo1, this fragment uses the getLegendItems().iterator and a variation of this ColorIcon.

public static JPanel createDemoPanel() {
    JPanel panel = new JPanel();
    JFreeChart chart = createChart(createDataset());
    panel.add(new ChartPanel(chart));
    panel.add(createLegendPanel((PiePlot) chart.getPlot()));
    return panel;
}

private static JPanel createLegendPanel(PiePlot plot) {
    JPanel panel = new JPanel(new GridLayout(0, 2, 5, 5));
    Iterator iterator = plot.getLegendItems().iterator();
    while (iterator.hasNext()) {
        LegendItem item = (LegendItem) iterator.next();
        JLabel label = new JLabel(item.getLabel());
        label.setIcon(new ColorIcon(8, item.getFillPaint()));
        panel.add(label);
    }
    return panel;
}

这篇关于如何在列中显示饼图的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆