最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there a way to control JavaFX PieChart size to make them uniform, independent of the text size in the slice labels - Stack Ov

programmeradmin1浏览0评论

I have an application that creates a lot of pie charts. The labels on the pie chart slices can have a text size of 6 or 8 characters up to sometimes 30 or so. The size of the text in the label affects the size of the rendered chart. I would like to be able to display a uniform chart size independent of the label size.

The input to the chart create logic is statistical data contained in class files that are in linked lists, and to copy that code here just would not be practical. But I do have this code here that if you run it will demonstrate what I am seeing in the app.

public class PieChartDemo extends Application {
    Rectangle2D primaryScreenBounds;
    double stgWdth;
    double stgHght;
    HBox AllBox;
    String newln = System.getProperty("line.separator");
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryScreenBounds = Screen.getPrimary().getVisualBounds();
        double totWd  = primaryScreenBounds.getWidth();
        double totHt  = primaryScreenBounds.getHeight();
        stgWdth = (totWd / 100) * 60;
        stgHght = ((totHt / 100) * 75);
        AllBox = new HBox();
        AllBox.setSpacing(0.0);
        buildCharts();
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
        scrollPane.setPannable(true);
        scrollPane.setContent(AllBox);
        //scrollPane.setFitToHeight(true);
        //scrollPane.setFitToWidth(true);
        scrollPane.setHvalue(0.0);
        scrollPane.setPrefViewportHeight((stgHght / 100) * 70);
        scrollPane.setPrefViewportWidth((stgWdth / 100) * 70);
        Scene scene = new Scene(scrollPane);
        //scene.getStylesheets().add(getClass().getResource("LabelColor.css").toString());
        primaryStage.setScene(scene);
        primaryStage.setTitle("Pie Chart Demo");
        //primaryStage.setHeight(stgHght);
        //primaryStage.setWidth(stgWdth);
        //primaryStage.setMaxHeight(scnHght);
        //primaryStage.setMaxWidth(scnWdth);
        primaryStage.show();
    }

    public static void main(String[] args) {

        Application.launch(args);
    }
    private void buildCharts() {
        PieChart pieChart = new PieChart();
        pieChart.setTitle("Pie Chart With long labels Demo" + newln +
                "Legends visible on the left, with no labels visible");
        pieChart.setLegendVisible(true);
        pieChart.setLegendSide(Side.LEFT);
        pieChart.setLabelsVisible(false);
        //pieChart.setLabelLineLength(20);
        String[] longOneLables = {"This is a rather long label for slice One ",
                "This is a rather long label for slice Two",
                "this is a rather long label for slice Three",
                "this is a rather long label for slice Four",
                "this is a rather long label for slice Five",
                "This is a rather long label for slice Six",
                "this is a rather long label for slice Seven",
                "this is a rather long label for slice Eight",
                "this is a rather long label for slice Nine"};
        double dbl = 100.00;
        for (int i = 0; i < longOneLables.length; i++) {
            PieChart.Data slice = new PieChart.Data(longOneLables[i], dbl);
            pieChart.getData().add(slice);
            dbl = dbl + 50;
        }
        pieChart.setPrefWidth((stgWdth / 100) * 50);
        pieChart.setMinWidth((stgWdth / 100) * 50);
        pieChart.setMaxWidth((stgWdth / 100) * 50);
        pieChart.setPrefHeight((stgHght / 100) * 50);
        pieChart.setMinHeight((stgHght / 100) * 50);
        pieChart.setMaxHeight((stgHght / 100) * 50);
        //
        PieChart pieChart2 = new PieChart();
        pieChart2.setTitle("Pie Chart With long labels Demo" + newln +
                "No Legends visible,with labels visible");
        pieChart2.setLegendVisible(false);
        pieChart2.setLabelsVisible(true);
        pieChart.setLabelLineLength(20);
        dbl = 100.00;
        for (int i = 0; i < longOneLables.length; i++) {
            PieChart.Data slice = new PieChart.Data(longOneLables[i], dbl);
            pieChart2.getData().add(slice);
            dbl = dbl + 50;
        }
        pieChart2.setPrefWidth((stgWdth / 100) * 50);
        pieChart2.setMinWidth((stgWdth / 100) * 50);
        pieChart2.setMaxWidth((stgWdth / 100) * 50);
        pieChart2.setPrefHeight((stgHght / 100) * 50);
        pieChart2.setMinHeight((stgHght / 100) * 50);
        pieChart2.setMaxHeight((stgHght / 100) * 50);
        //
        PieChart pieChart3 = new PieChart();
        pieChart3.setTitle("Pie Chart With long labels Demo" + newln +
                "No Legends visible,with labels visible, increased size of chart");
        pieChart3.setLegendVisible(false);
        pieChart3.setLabelsVisible(true);
        pieChart3.setLabelLineLength(20);
        dbl = 100.00;
        for (int i = 0; i < longOneLables.length; i++) {
            PieChart.Data slice = new PieChart.Data(longOneLables[i], dbl);
            pieChart3.getData().add(slice);
            dbl = dbl + 50;
        }
        pieChart3.setPrefWidth((stgWdth / 100) * 65);
        pieChart3.setMinWidth((stgWdth / 100) * 65);
        pieChart3.setMaxWidth((stgWdth / 100) * 65);
        pieChart3.setPrefHeight((stgHght / 100) * 65);
        pieChart3.setMinHeight((stgHght / 100) * 65);
        pieChart3.setMaxHeight((stgHght / 100) * 65);
        //
        PieChart pieChart4 = new PieChart();
        pieChart4.setTitle("Pie Chart With different size labels Demo");
        pieChart4.setLegendVisible(false);
        pieChart4.setLabelsVisible(true);
        pieChart4.setLabelLineLength(20);
        String[] mixed = {"size for slice One",
                "slice Two",
                "Change the size slice Three",
                "change it again Four", "Five","Six", "Seven", "Eight", "Nine"};
        dbl = 100.00;
        for (int i = 0; i < mixed.length; i++) {
            PieChart.Data slice = new PieChart.Data(mixed[i], dbl);
            pieChart4.getData().add(slice);
            dbl = dbl + 50;
        }
        pieChart4.setLabelLineLength(20.0);
        pieChart4.setPrefWidth((stgWdth / 100) * 50);
        pieChart4.setMinWidth((stgWdth / 100) * 50);
        pieChart4.setMaxWidth((stgWdth / 100) * 50);
        pieChart4.setPrefHeight((stgHght / 100) * 50);
        pieChart4.setMinHeight((stgHght / 100) * 50);
        pieChart4.setMaxHeight((stgWdth / 100) * 50);
        //
        AllBox.getChildren().addAll(pieChart, pieChart2,pieChart3,pieChart4);
        //AllBox.setStyle("-fx-background-color: DARKSLATEGRAY;");
        HBox.setHgrow(pieChart, Priority.ALWAYS);
        HBox.setHgrow(pieChart2, Priority.SOMETIMES);
        HBox.setHgrow(pieChart3, Priority.SOMETIMES);
        HBox.setHgrow(pieChart4, Priority.SOMETIMES);
    }
}

I tried putting the hbox into a dummy scene, then running it thru layout(). I then had some code that tried to get the position and size of the node for each slice. My thought was that if the chart size was smaller than the hbox size, there should be an area between the chart and the side of the hbox that I could put text for the label. But could not figure out how to do it. Any help I could get would be greatly appreciated.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论