2017-05-31 204 views
3

我有一個圖表,看起來像這樣:的JavaFX餅圖 - 標籤重疊的問題與缺失標籤

enter image description here

標籤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的,但我想在標籤行做這樣的事情:

enter image description here

我能做些什麼來防止它們重疊和顯示標籤d?

+0

你想他們的時候,他們在同一地點見面做什麼?它們重疊無疑,但是當它們相遇時,您是否想要在標籤A旁邊的右側標記「標籤E」? – Yahya

+0

@Yahya我不需要移動餡餅切片,但寧願讓標籤線改變。我會編輯我的問題,並舉例說明我正在嘗試做什麼。 –

+0

我可以建議的唯一的事情就是確保你的數字加起來爲100.意思是,你正在使用的當前數字找到它們的百分比。 – Sedrick

回答

1

您可以使用JFreeChart獲得所需的效果,它可以與JavaFX一起使用,如here所示。爲PieChartFXDemo1的完整源,看到here,包含在分配:

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

這個完整的例子反映了你的數據集和顏色選擇。

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

我知道我可以使用JFreeChart。在我的問題中的第二個圖像是一個JFreeChart,但我需要做JavaFX。 –

+0

'JFreeChart'渲染_in_ JavaFX; 'org.jfree.chart.fx.ChartViewer' _is_是一個'javafx.scene.control.Control'。 – trashgod

+0

我無法使用JFreeChart作爲授權原因。 –