BarChartコントロールを透明にする方法

イワン:

私はJavaFXの持つ新たなんだ、と私は私のBarChartコントロールコンポーネント透明経由の背景を作るために私のCSSコードで私の問題を解決する方法がわかりません。いずれのソリューションは、理解されるであろう。

私の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バー自身を除いて透明見えません/。

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

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=204568&siteId=1