简体   繁体   English

如何使BarChart透明

[英]How to make BarChart transparent

I'm new with javafx, and i don't know how to fix my problem with my css code to make my BarChart component transparent through background. 我是javafx的新手,并且我不知道如何使用CSS代码解决问题,以使BarChart组件在后台透明。 Any solution will be appreciated. 任何解决方案将不胜感激。

Here my css code: 这是我的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;
}

Image of output: 输出图像:

The following CSS will make everything in the BarChart invisible/transparent except for the bars themselves. 以下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;
}

Here's an example using the above (assuming the file is named Main.css ): 这是使用上述示例的示例(假设文件名为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;
    }

}

And here's an image of the result: 这是结果的图像:

示例屏幕截图

Now, I did have to modify the CSS file slightly to make the root of the Scene transparent and get the bars to have that linear gradient look you have in your image. 现在,我确实必须稍微修改CSS文件,以使Scene的根透明,并使这些条形具有图像中的线性渐变外观。 All I did was... 我所做的只是...

Modify this: 修改此:

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

And add this: 并添加以下内容:

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

Here you go: -fx-background-color: transparent; 在这里,您可以执行以下操作: -fx-background-color: transparent;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM