簡體   English   中英

如何使BarChart透明

[英]How to make BarChart transparent

我是javafx的新手,並且我不知道如何使用CSS代碼解決問題,以使BarChart組件在后台透明。 任何解決方案將不勝感激。

這是我的CSS代碼:

.chart{
    -fx-background-color: null; 
    -fx-alternative-column-fill-visible: false;
    -fx-alternative-row-fill-visible: false;
    -fx-content-display:bottom;
}
.axis {
    -fx-tick-mark-visible: false;
    -fx-minor-tick-visible: false;
    -fx-minor-tick-length: 0;
}
.axis-label {
    -fx-text-fill: null;
}

輸出圖像:

以下CSS將使BarChart所有內容BarChart不可見/透明,除了條本身。

.bar-chart {
    -fx-alternative-row-visible: false;
    -fx-alternative-column-visible: false;
    -fx-horizontal-grid-lines-visible: false;
    -fx-vertical-grid-lines-visible:false;
    -fx-horizontal-zero-line-visible: false;
    -fx-vertical-zero-line-visible: false;
    -fx-legend-visible: false;
}

.chart-plot-background {
    -fx-background-color: transparent;
}

.axis {
    -fx-border-color: transparent;
    -fx-tick-mark-visible: false;
    -fx-minor-tick-visible: false;
    -fx-tick-labels-visible: false;
}

這是使用上述示例的示例(假設文件名為Main.css ):

import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        var scene = new Scene(new StackPane(createBarChart()), Color.TRANSPARENT);
        scene.getStylesheets().add("Main.css");

        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(scene);

        primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
            if (event.getCode() == KeyCode.ESCAPE) {
                primaryStage.close();
            }
        });

        var bounds = Screen.getPrimary().getVisualBounds();
        primaryStage.setX(bounds.getMinX());
        primaryStage.setY(bounds.getMinY());
        primaryStage.setWidth(bounds.getWidth());
        primaryStage.setHeight(bounds.getHeight());

        primaryStage.show();
    }

    private BarChart<?, ?> createBarChart() {
        var chart = new BarChart<>(new CategoryAxis(), new NumberAxis(0, 20, 1));

        var random = new Random();

        var series = new Series<String, Number>();
        for (int i = 0; i < 20; i++) {
            var category = Integer.toString(i);
            series.getData().add(new Data<>(category, random.nextInt(15)));
            ((CategoryAxis) chart.getXAxis()).getCategories().add(category);
        }

        chart.getData().add(series);
        return chart;
    }

}

這是結果的圖像:

示例屏幕截圖

現在,我確實必須稍微修改CSS文件,以使Scene的根透明,並使這些條形具有圖像中的線性漸變外觀。 我所做的只是...

修改此:

.root,
.chart-plot-background {
    -fx-background-color: transparent;
}

並添加以下內容:

.chart-bar {
    -fx-background-color: linear-gradient(to bottom, transparent 0%, gray 45%);
}

在這里,您可以執行以下操作: -fx-background-color: transparent;

信用: https : //www.java2s.com/Code/Java/JavaFX/fxbackgroundcolortransparent.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM