Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

java - JavaFX Pie Chart - Overlapping Labels & Missing Labels

I have a chart that looks like this:

enter image description here

Labels E and A are overlapping and Label D is missing. Label F value is 0 so I am not surprised it is 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 have 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 realize a lot of those are defaults and unnecessary but I thought the last line would curve the label line and it does not.

This example is a JFreechart but I would like the label lines to do something like this:

enter image description here

What can I do to prevent them from overlapping and display label D?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can get the desired effect using JFreeChart, which works with JavaFX as shown here. The complete source for PieChartFXDemo1, seen here, is included with the distribution:

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

This complete example reflects your dataset and color choices.

image

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...