java - 如何使用 JFreeCharts 将图例绘制为表格?

标签 java user-interface plot charts jfreechart

我可以像这样绘制这个图表:

enter image description here 但我想像这样绘制它,这意味着位于图表底部并作为表格(至少看起来像一个)。

enter image description here

这是我的代码:

package charts;

import java.awt.*;
import java.io.*;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleEdge;

public class PieChart {
    public static void main( String[ ] args ) throws Exception
    {

        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("A", new Double( 36.95 ));
        dataset.setValue("B", new Double( 31.51 ));
        dataset.setValue("C", new Double( 12.44 ));
        dataset.setValue("D", new Double( 6.41 ));
        dataset.setValue("E", new Double( 2.76 ));
        dataset.setValue("F", new Double( 2.29 ));
        dataset.setValue("G", new Double( 1.71 ));
        dataset.setValue("H", new Double(1.21));
        dataset.setValue("I", new Double(4.71));

        JFreeChart chart = ChartFactory.createPieChart(
                "", // chart title
                dataset, // dataset
                true, // legends
                true, // tooltips
                false); // urls

        // remove legend border
        chart.getLegend().setFrame(BlockBorder.NONE);

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setSimpleLabels(true);

        PieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{2}");
        plot.setLabelGenerator(generator);

        // background
        plot.setBackgroundPaint(Color.WHITE); //
        plot.setOutlineVisible(false); // remove image border

        // label
        plot.setLabelFont(new Font("Courier New", Font.BOLD, 16));
        plot.setLabelPaint(Color.WHITE);

        Color transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
        plot.setLabelBackgroundPaint(transparent); //background
        plot.setLabelOutlinePaint(transparent); //border
        plot.setLabelShadowPaint(transparent); //shadow

        // legend
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}: {1}"));

        // sections
        // use gradients and white borders for the section colours
        plot.setSectionPaint("A",  new Color(126, 208, 150));
        plot.setSectionPaint("B", new Color(41, 157, 135));
        plot.setSectionPaint("C", new Color(25, 144, 212));
        plot.setSectionPaint("D", new Color(95, 85, 25));
        plot.setSectionPaint("E", new Color(22, 90, 63));
        plot.setSectionPaint("F", new Color(134, 125, 25));
        plot.setSectionPaint("G", new Color(226, 200, 14));
        plot.setSectionPaint("H", new Color(241, 172, 18));
        plot.setSectionPaint("I", new Color(245, 200, 51));
        plot.setBaseSectionOutlinePaint(Color.WHITE);
        plot.setSectionOutlinesVisible(true);

        // chart title at the bottom
        TextTitle legendText = new TextTitle("THIS IS JUST A TEST");
        legendText.setPosition(RectangleEdge.BOTTOM);
        chart.addSubtitle(legendText);

        LegendTitle legend = chart.getLegend();
        legend.setPosition(RectangleEdge.RIGHT);

        int width = 640; /* Width of the image */
        int height = 480; /* Height of the image */
        File pieChart = new File( "PieChart.png" );
        ChartUtilities.saveChartAsPNG(pieChart, chart, width, height);
    }
}

最佳答案

以下是自定义图例布局的方法:

// remove default legend
chart.removeLegend();

// create and add legend
LegendTitle legend = new LegendTitle(plot, new GridArrangement(dataset.getItemCount(), 1), new GridArrangement(1, 1));
legend.setPosition(RectangleEdge.BOTTOM);
chart.addLegend(legend);

// add text title after the legend

但是,完全按照您的需要自定义图例项布局要困难得多。您需要重写一堆方法,例如 LegendTitle 中的 createLegendItemBlock() ,并手动摆弄 JFreechart 的 BlockArrangement API。

或者,您可以完全绕过 JFreechart API,并使用 Swing 组件生成您自己的图例面板。退房this example .

关于java - 如何使用 JFreeCharts 将图例绘制为表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31706647/

相关文章:

java - 提高核心 Java 生产力的前 3 个库是什么?

java - 缺少双阵列

java - 重绘导致组件溢出

r - 带有填充点符号和图例的绘图函数

Python matplotlib 值错误 : The truth value of an array with more than one element is ambiguous

java - 是否可以在现有实例上调用构造函数?

java - 是什么导致我的数组列表中出现此 ArrayOutOfBoundsException?

python - 通过将参数传递给定义来缩短 GUI 代码

Java-实现mvc

plot - 如何在 SGPLOT 图例中组合散点和系列线?