9
這個可編譯的最小代碼片段示例如何使用JFreeChart作爲繪圖API進行調整,以便顯示絕對值和百分比? 我無法從互聯網上的任何代碼片段或JFreechart手冊本身中提取此信息。代碼片斷會生成一個只顯示百分比的餅圖。在我的情況下絕對值也很重要,所以我需要在百分比下正確顯示它們。jfreechart自定義餅圖以顯示絕對值和百分比
下面是代碼:(注意它缺乏進口)
public class MyMinimalPieChartExample {
public static void main(String[] args) {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("some data 1",99);
dataset.setValue("some data 2", 77);
//third adaption
JFreeChart someChart = ChartFactory.createPieChart(
"some chart header", dataset,
true, true, false);
PiePlot illegalLegalRestPiePlot4 = (PiePlot) someChart.getPlot();
illegalLegalRestPiePlot4.setSectionPaint("some data 1", new Color(0, 255, 0));
illegalLegalRestPiePlot4.setSectionPaint("some data 2",
new Color(255, 0, 0));
PiePlot plot4 = (PiePlot) someChart.getPlot();
plot4.setExplodePercent("some data 1", 0.4);
plot4.setSimpleLabels(true);
PieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(
"{0} = {2}", new DecimalFormat("0"), new DecimalFormat("0.00%"));
plot4.setLabelGenerator(generator);
try {
ChartUtilities.saveChartAsJPEG(new File("C:/myMinimalPieChartExample.jpeg"),
someChart, 1200, 1000);
} catch (Exception e) {
System.err.println("couldn't write chart");
}
}
}
另見本相關的【答案】(http://stackoverflow.com/a/3551238/230513)。 – trashgod