bubble chart with color setting in JAVAFX


importimport javafx javafx..applicationapplicati .Application;
import javafx.scene.Scene;
import javafx.scene.chart.BubbleChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class ColorBubble extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    public void start(Stage stage) throws Exception {
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BubbleChart bc = new BubbleChart(xAxis, yAxis);
        XYChart.Series<Number, Number> series1 = new XYChart.Series();
        series1.getData().add(new XYChart.Data(1, 0.1, 0.02));
        series1.getData().add(new XYChart.Data(2, 0.2, 0.03));
        series1.getData().add(new XYChart.Data(0.5, -0.4, 0.04));
        series1.getData().add(new XYChart.Data(0.75, -0.25, 0.05));
        bc.getData().addAll(series1);
        Scene scene = new Scene(bc);
        //// CSS Setting Start ////
        String css = ColorBubble.class.getResource("/bubble.css").toExternalForm();
        scene.getStylesheets().clear();
        scene.getStylesheets().add(css.toString());
        //// CSS Setting End   ////
        stage.setScene(scene);
        stage.show();
        Integer i = 0;
        for (XYChart.Data<Number, Number> data : series1.getData()) {
            Text text = new Text(i.toString());
            text.setFont(Font.font(10));
            ((StackPane) data.getNode()).getChildren().add(text);
            i++;
        }
    }
}
// CSS file content
/*
.chart-bubble.series0.data0.default-color0 {
    -fx-bubble-fill: yellow;
}

.chart-bubble.series0.data1.default-color0 {
    -fx-bubble-fill: pink;
}

.chart-bubble.series0.data2.default-color0 {
    -fx-bubble-fill: red;
}

.chart-bubble.series0.data3.default-color0 {
    -fx-bubble-fill: blue;
}
 */

猜你喜欢

转载自blog.csdn.net/alphachx/article/details/82925176