开发者

Getting value instead of name on pie chart in Java

开发者 https://www.devze.com 2023-02-18 20:40 出处:网络
package charts; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart;
package charts;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class Pie {

    public void DrawChart() {
        DefaultPieDataset pieDataSet = new DefaultPieDataset();

        pieDataSet.setValue("C", new Integer(10));

        pieDataSet.setValue("C++", new Integer(20));

        pieDataSet.setValue("JAVA", new Integer(50));

        pieDataSet.setValue("C#", new Integer(40));

        JFreeChart chart = ChartFactory.createPieChart3D("MY CHART",
            pieDataSet, false, false, true);

        ChartFrame frame = new ChartFrame("Usage Trend", chart);

        frame.setVisible(true);

        frame.setSize(300, 300);

    }

    public static void main(String[] args) {
        Pie pie = new Pie();

        pie.DrawChart();
    }
}

The above code will generate a pie chart with slices as "C, C++, Java and C#" but I want to display the values ins开发者_开发知识库tead of name.

What modifications should I do?


Something like

pieDataSet.setValue("50", new Integer(50));

or

pieDataSet.setValue("Java(50)", new Integer(50));

or

pieDataSet.setValue("Java[50]", new Integer(50));

or

pieDataSet.setValue("Java:50", new Integer(50));

Another method would be to enable ToolTips using

JFreeChart chart = ChartFactory.createPieChart3D("MY CHART", 
                     pieDataSet, false, true, true);

ToolTips shows both the name and the value.


I want value part separately, along key as legend.

You can use StandardPieSectionLabelGenerator.

PiePlot plot = (PiePlot) chart.getPlot();
plot.setLegendLabelGenerator(new
    StandardPieSectionLabelGenerator("{0}: {2}"));
0

精彩评论

暂无评论...
验证码 换一张
取 消