JavaFX可以绘制指定的彩色气泡图吗?

时间:2018-02-10 08:41:33

标签: java javafx

我是JavaFX的新用户。我试图绘制如下彩色气泡图:Colored Bubble Chart Example

我编写以下代码,可以控制气泡位置和尺寸

import javafx.application.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.Text;
import javafx.stage.Stage;
public class Scatter 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);
        stage.setScene(scene);
        stage.show();
        Integer i = 0;
        for (XYChart.Data<Number, Number> data : series1.getData()) {
            Text text = new Text(i.toString());
            ((StackPane) data.getNode()).getChildren().add(text);
            i++;
        }
    }
}

如下图所示 JavaFX Code Result

但我没有找到可以改变每个泡泡颜色的地方。有人能帮我吗?提前谢谢!

2 个答案:

答案 0 :(得分:0)

公共类Scatter扩展了Application {

public static void main(String[] args) {
    launch(args);
}
@Override
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();
    XYChart.Series<Number, Number> series2 = new XYChart.Series();
    XYChart.Series<Number, Number> series3 = new XYChart.Series();
    XYChart.Series<Number, Number> series4 = new XYChart.Series();

    series1.getData().add(new XYChart.Data(1, 0.1, 0.02));

    series2.getData().add(new XYChart.Data(2, 0.2, 0.03));
    series3.getData().add(new XYChart.Data(0.5, -0.4, 0.04));
    series4.getData().add(new XYChart.Data(0.75, -0.25, 0.05));
    Set<XYChart.Series> bubbleSeries = new HashSet<>();

    bubbleSeries.add(series1);
    bubbleSeries.add(series2);
    bubbleSeries.add(series3);
    bubbleSeries.add(series4);


    bc.getData().addAll(series1,series2,series3,series4);
    Scene scene = new Scene(bc);
    stage.setScene(scene);
    stage.show();
    Integer i = 0;

    for(XYChart.Series<Number, Number> obj : bubbleSeries){
        for (XYChart.Data<Number, Number> data : obj.getData()) {
            Text text = new Text(i.toString());
            ((StackPane) data.getNode()).getChildren().add(text);
            i++;
        }
    }
}

}

答案 1 :(得分:0)

import javafx.application.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;
}
 */

本书&#34;学习JavaFX 8:用Java 8构建用户体验和接口&#34;解释了如何在第23章和第34章中设置每个系列和每个项目的气泡颜色;使用CSS和#34;设置BubbleChart样式。只需遵循指示,一切都会奏效。