简体   繁体   English

jfree 图表自定义标签

[英]jfree chart custom label

i would like to personilize my label of data in my Pie Chart, beacause in my legend i see data=value, in label i see data=value.我想在我的饼图中个性化我的数据标签,因为在我的图例中我看到数据=值,在标签中我看到数据=值。 how i do to see only name data in legend and value in label?我如何只看到图例中的名称数据和标签中的值? my code for paint a chart is this:我绘制图表的代码是这样的:

    public JfreeChart(String applicationTitle, String chartTitle)throws SQLException {
    super(applicationTitle);
    JFreeChart barChart = null;
    dataset=createDataset(conn,barChart);


    barChart = ChartFactory.createPieChart(chartTitle,dataset, true, true, false);
    ChartPanel chartPanel = new ChartPanel(barChart);
    PiePlot plot = new PiePlot(dataset);
    plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
    plot.setNoDataMessage("No data available");
    plot.setCircular(true);
    plot.setLabelGap(0.02);

    chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
    setContentPane(chartPanel);
    try {
        ChartUtilities.saveChartAsPNG(new File("directory"), barChart, 600,450);
        writeChartToPDF(barChart, 600, 450,"C:\\Users\\axs0552\\Desktop\\grafico.pdf");
    } catch (IOException ex) {
        System.out.println(ex.getLocalizedMessage());
    }
}

public static void writeChartToPDF(JFreeChart chart, int width, int height,
        String fileName) {
    PdfWriter writer = null;

    Document document = new Document();

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(
                fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height,new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();
}

and my pie chart is this:我的饼图是这样的:

馅饼车

Absent your implementation of createDataset() , I'm guessing that the problem comes from the key passed to the setValue() method of your DefaultPieDataset .没有你的createDataset() ,我猜问题来自传递给你的DefaultPieDatasetsetValue()方法的key Instead, use the MessageFormat symbol {1} to construct a custom StandardPieSectionLabelGenerator , as shown here .相反,使用MessageFormat符号{1}来构建定制StandardPieSectionLabelGenerator ,如图这里

PiePlot plot = (PiePlot) chart.getPlot();
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0} = {1}");
plot.setLabelGenerator(gen);

图片

Then you can elide the unwanted characters from your implementation of createDataset() to keep them out of the legend requested in your invocation of ChartFactory.createPieChart() .然后,您可以从createDataset()实现中删除不需要的字符,以将它们排除在调用ChartFactory.createPieChart()请求的legend ChartFactory.createPieChart()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM