5

我是 Java 新手,也是 JavaFX 新手。我正在尝试构建一个使用 JavaFx LineChart 显示实时数据的动态图。

我在 oracle 文档http://docs.oracle.com/javafx/2/charts/css-styles.htm#CIHGIAGE中注意到可以使用 CSS 设置图表样式。

是否可以以编程方式更改系列的颜色?

public Node addLineChart(){

    //final NumberAxis yAxis = new NumberAxis(-80,60,20);
    final NumberAxis yAxis;
    final CategoryAxis xAxis;

    if(firingOrderSelectionIndex >= 0){
        String selectedFiringOrder = firingOrders[cylselectionIndex][firingOrderSelectionIndex];
        String[] currentFiringOrder = selectedFiringOrder.split("-");

        xAxis = new CategoryAxis(FXCollections.observableArrayList(currentFiringOrder));            
        xAxis.setAutoRanging(false);

        if(selectionIndex > 1){
          yAxis = new NumberAxis(-60,40,20);
        }else{
          yAxis = new NumberAxis(-75,50,15);
        }             
    }else{
        xAxis = new CategoryAxis();
        yAxis = new NumberAxis(0,0,0);          
    }   

    xAxis.setLabel("Order");
    xAxis.setStyle("-fx-font-size: 14;-fx-font-weight: bolder;");
    //xAxis.setAutoRanging(false); //If you are manually setting values in the constructor you can do this.
    //otherwise if this is done, the graph will crash. So, while using constructor with no values leave this config as default.

    //xAxis.setLowerBound(1);        
    //xAxis.setUpperBound(4);
    //xAxis.setTickUnit(1);

    yAxis.setMinorTickVisible(false);

    final LineChart<String,Number> lineChart = new LineChart<String,Number>(xAxis,yAxis);
    lineChart.setTitle("Graph");
    lineChart.setLegendVisible(false);
    lineChart.setStyle(".default-color0.chart-series-line { -fx-stroke: #f0e68c; }");

    //series.setName("Live");
    series.getData().add(new XYChart.Data("1", 5));
    series.getData().add(new XYChart.Data("2", 5));

    lineChart.getData().add(series);    
    xAxis.setVisible(false);

    return lineChart;
}
4

1 回答 1

0
chart.setStyle(".default-color0.chart-series-line { -fx-stroke: #e9967a; }");
于 2012-12-20T03:11:06.523 回答