我有一个看起来像这样的图表:
标签E和A重叠,缺少标签D。标签F的值为0,所以不感到惊讶。
以下是标签的值:
ObservableList<PieChart.Data> 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
获得所需的效果,它与foot_a所示的JavaFX一起使用。发行版中包含here的 PieChartFXDemo1
的完整源代码:
java -cp .:lib/* org.jfree.chart.fx.demo.PieChartFXDemo1
这个完整的示例反映了您的数据集和颜色选择。
here
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);
}
}