本文介紹了JFree圖表自定義標(biāo)簽的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我想在我的餅圖中個(gè)性化我的數(shù)據(jù)標(biāo)簽,因?yàn)樵谖业膱D例中我看到Data=Value,在Label中我看到Data=Value。
如何才能只看到圖例中的名稱數(shù)據(jù)和標(biāo)簽中的值?
我畫(huà)圖表的代碼是這樣的:
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();
}
我的餅圖是這樣的:
推薦答案
如果沒(méi)有createDataset()
的實(shí)現(xiàn),我猜問(wèn)題來(lái)自于傳遞給DefaultPieDataset
的setValue()
方法。相反,可以使用MessageFormat
符號(hào){1}
構(gòu)建自定義StandardPieSectionLabelGenerator
,如here所示。
PiePlot plot = (PiePlot) chart.getPlot();
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0} = {1}");
plot.setLabelGenerator(gen);
然后,您可以從createDataset()
的實(shí)現(xiàn)中刪除不需要的字符,將它們排除在調(diào)用ChartFactory.createPieChart()
中請(qǐng)求的legend
之外。
這篇關(guān)于JFree圖表自定義標(biāo)簽的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,