jfree chart custom label(JFree图表自定义标签)
                            本文介绍了JFree图表自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我想在我的饼图中个性化我的数据标签,因为在我的图例中我看到Data=Value,在Label中我看到Data=Value。 如何才能只看到图例中的名称数据和标签中的值? 我画图表的代码是这样的:    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();
}
我的饼图是这样的:
推荐答案
如果没有createDataset()的实现,我猜问题来自于传递给DefaultPieDataset的setValue()方法。相反,可以使用MessageFormat符号{1}构建自定义StandardPieSectionLabelGenerator,如here所示。
PiePlot plot = (PiePlot) chart.getPlot();
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0} = {1}");
plot.setLabelGenerator(gen);
然后,您可以从createDataset()的实现中删除不需要的字符,将它们排除在调用ChartFactory.createPieChart()中请求的legend之外。
这篇关于JFree图表自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:JFree图表自定义标签
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - 验证是否调用了所有 getter 方法 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				