JavaFX饼图 - 重叠标签&缺少标签 [英] JavaFX Pie Chart - Overlapping Labels & Missing Labels

查看:271
本文介绍了JavaFX饼图 - 重叠标签&缺少标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图表如下所示:

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

Labels E and A are overlapping and Label D is missing. Label F value is 0 so I am not surprised it is missing.

以下是标签的值:

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?
      }

我意识到很多都是违约和不必要但我想最后一行会弯曲标签线但不会。

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.

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

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

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

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

推荐答案

您可以使用 JFreeChart ,适用于JavaFX,如图所示此处 <$ c $的完整来源c> PieChartFXDemo1 ,见这里,包含在发行版中:

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.

< img src =https://i.stack.imgur.com/3e0iR.pngalt =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);
    }
}

这篇关于JavaFX饼图 - 重叠标签&amp;缺少标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆