How to display legend for Pie Chart in columns?

末鹿安然 提交于 2019-12-17 02:33:12

问题


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


回答1:


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.

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



回答2:


Have a look at this thread: Link

Seems like something you are looking for. If not, please post some more information or screenshots of what you have and what you need.



来源:https://stackoverflow.com/questions/13307500/how-to-display-legend-for-pie-chart-in-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!