gpt4 book ai didi

java - jfree 图表自定义标签

转载 作者:行者123 更新时间:2023-11-30 10:45:59 28 4
gpt4 key购买 nike

我想在我的饼图中个性化我的数据标签,因为在我的图例中我看到数据=值,在标签中我看到数据=值。我如何才能只看到图例中的名称数据和标签中的值?我绘制图表的代码是这样的:

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

我的饼图是这样的:

pieCart

最佳答案

缺少 createDataset() 的实现,我猜测问题出在传递给 setValue() 方法的 key您的 DefaultPieDataset。相反,使用 MessageFormat 符号 {1} 构建自定义 StandardPieSectionLabelGenerator,如图所示 here .

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

image

然后,您可以从 createDataset() 的实现中删除不需要的字符,以将它们排除在调用 ChartFactory.createPieChart( )

关于java - jfree 图表自定义标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790731/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com