JavaFX饼图 – 重叠标签和缺少标签

我有一个看起来像这样的图表:

在此处输入图像描述

标签E和A重叠,缺少标签D. 标签F值为0所以我不会感到惊讶它缺失。

以下是标签的值:

ObservableList pieChartData = FXCollections.observableArrayList( new PieChart.Data("A", 0.80), new PieChart.Data("B", 9.44), new PieChart.Data("C", 89.49), new PieChart.Data("D", 0.08), new PieChart.Data("E", 0.18), new PieChart.Data("F", 0.0)); 

我努力了:

 .chart{ -fx-background-color: lightgray; -fx-border-color: black; -fx-legend-visible: true; -fx-legend-side: bottom; -fx-title-side: top; -fx-clockwise: true; -fx-pie-label-visible: true; -fx-label-line-length: 25; -fx-pie-to-label-line-curved: true; //curve label lines? } 

我意识到很多都是默认的和不必要的,但我认为最后一行会弯曲标签线,但事实并非如此。

这个例子是一个JFreechart,但我想标签行做这样的事情:

在此处输入图像描述

我该怎么做才能防止它们重叠并显示标签D?

您可以使用JFreeChart获得所需的效果, JFreeChart与JavaFX一起使用,如此处所示。 PieChartFXDemo1的完整源代码(见此处 )包含在发行版中:

 java -cp .:lib/* org.jfree.chart.fx.demo.PieChartFXDemo1 

此完整示例反映了您的数据集和颜色选择。

图片

 import java.awt.Color; import java.awt.Font; import java.text.DecimalFormat; import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.fx.ChartViewer; import org.jfree.chart.labels.PieSectionLabelGenerator; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; /** * @see http://stackoverflow.com/q/44289920/230513 */ public class PieChartFX extends Application { private static PieDataset createDataset() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("A", 0.8); dataset.setValue("B", 9.4); dataset.setValue("C", 0.1); dataset.setValue("D", 89.5); dataset.setValue("E", 0.2); dataset.setValue("F", 0.0); return dataset; } private static JFreeChart createChart(PieDataset dataset) { JFreeChart chart = ChartFactory.createPieChart( "", dataset, false, true, false); chart.setBackgroundPaint(Color.LIGHT_GRAY); PiePlot plot = (PiePlot) chart.getPlot(); plot.setOutlineVisible(false); plot.setSectionPaint("A", Color.RED); plot.setSectionPaint("B", Color.BLUE); plot.setSectionPaint("C", Color.GREEN); plot.setSectionPaint("D", Color.YELLOW); plot.setSectionPaint("E", Color.CYAN); plot.setLabelFont(new Font(Font.SANS_SERIF, Font.BOLD, 16)); // Custom labels https://stackoverflow.com/a/17507061/230513 PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator( "{0}: {2}", new DecimalFormat("0"), new DecimalFormat("0.0%")); plot.setLabelGenerator(gen); return chart; } @Override public void start(Stage stage) throws Exception { PieDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); ChartViewer viewer = new ChartViewer(chart); stage.setScene(new Scene(viewer)); stage.setTitle("JFreeChart: PieChartFX"); stage.setWidth(600); stage.setHeight(400); stage.show(); } public static void main(String[] args) { launch(args); } }