JavaFX Pie Chart - Labels and Missing Labels Overlay
I have a diagram that looks like this:
Labels E and A overlap and label D is missing. Label F value is 0, so I'm not surprised it's missing.
Here are the values for the labels:
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));
I tried:
.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?
}
I understand that many of them are defaults and unnecessary, but I thought that the last line would be the curved line of the label, which is not the case.
This example is JFreechart, but I would like the label strings to do something like this:
What can I do to prevent them from matching and displaying the D label?
source to share
You can get the desired effect using the JFreeChart
one that works with JavaFX as shown here . The complete source PieChartFXDemo1
, displayed here here , is included with the distribution:
java -cp .:lib/* org.jfree.chart.fx.demo.PieChartFXDemo1
This complete example reflects the choice of your dataset and colors.
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);
}
}
source to share