Javafx 更改图表颜色

Javafx 更改图表颜色,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我测试了此css代码以更改图表颜色,但在运行代码时得到了NPE: public class MainApp extends Application { @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); //defining the axes final NumberAxis xAxis = new NumberAxis();

我测试了此css代码以更改图表颜色,但在运行代码时得到了NPE:

public class MainApp extends Application {

    @Override public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        //creating the chart
        final LineChart<Number,Number> lineChart = new LineChart<>(xAxis,yAxis);


        // Look up first series fill
        Node node = lineChart.lookup(".default-color0.chart-series-area-fill");
        // Set the first series fill to translucent pale green
        node.setStyle("-fx-fill: linear-gradient(#f2f2f2, #d4d4d4);"
            + "  -fx-background-insets: 0 0 -1 0, 0, 1, 2;"
            + "  -fx-background-radius: 3px, 3px, 2px, 1px;");

        Node nodew = lineChart.lookup(".chart-series-area-line");
        // Set the first series fill to translucent pale green
        nodew.setStyle("-fx-stroke: #989898; -fx-stroke-width: 1px; ");



        lineChart.setTitle("Stock Monitoring, 2010");
        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));
        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));

        Scene scene  = new Scene(lineChart,800,600);
        lineChart.getData().add(series);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
你能告诉我怎么解决这个问题吗?此css代码用于AreaChart。现在我想在线形图中使用它

附言

我还尝试了示例中的css:


但我还是得到了NPE。

你必须在舞台展示后查找节点


public类MainApp扩展应用程序
{
@覆盖公共无效开始(阶段)
{
阶段。设置标题(“折线图样本”);
//定义轴
最终数字axis xAxis=新数字axis();
最终数字axis yAxis=新数字axis();
xAxis.setLabel(“月数”);
//创建图表
最终线形图线形图=新线形图(xAxis,yAxis);
线形图.setTitle(“库存监控,2010年”);
//定义系列
XYChart.Series系列=新的XYChart.Series();
series.setName(“我的投资组合”);
//用数据填充序列
series.getData().add(新的XYChart.Data(1,23));
series.getData().add(新的XYChart.Data(2,14));
series.getData().add(新的XYChart.Data(3,15));
series.getData().add(新的XYChart.Data(4,24));
series.getData().add(新的XYChart.Data(5,34));
series.getData().add(新的XYChart.Data(6,36));
series.getData().add(新的XYChart.Data(7,22));
series.getData().add(新的XYChart.Data(8,45));
series.getData().add(新的XYChart.Data(9,43));
series.getData().add(新的XYChart.Data(10,17));
series.getData().add(新的XYChart.Data(11,29));
series.getData().add(新的XYChart.Data(12,25));
场景=新场景(线形图,800600);
lineChart.getData().add(系列);
舞台场景;
stage.show();
//查找第一个系列填充
Node Node=lineChart.lookup(“.default-color0.chart系列区域填充”);
//将第一个系列填充设置为半透明淡绿色
node.setStyle(“-fx填充:线性渐变(#f2f2f2,#d4d4);”
+-fx背景插图:0-10,0,1,2
+“-fx背景半径:3px,3px,2px,1px;”;
Node nodew=lineChart.lookup(“.chart-series-area-line”);
//将第一个系列填充设置为半透明淡绿色
nodew.setStyle(“-fx笔划:#989898;-fx笔划宽度:1px;”);
}
公共静态void main(字符串[]args)
{
发射(args);
}
}
  • 请确保参考的,以查看它支持哪些样式类
  • 在对节点应用css样式之前,通常不提供查找。这意味着,至少它必须被渲染到屏幕上,但有时它似乎需要另外一两帧。因此,如果要使用查找,必须在调用
    stage.show()之后调用它,有时您需要采取进一步的步骤来稍后调用它
  • 更好的方法是使用外部样式表。请参阅:还有一个特定的页面
  • 更新(其他想法):

    在Java8中(可能在JavaFX2.2中:我没有caspian.css的副本),所有与数据相关的颜色都是根据
    CHART\u COLOR\u 1
    CHART\u COLOR\u 8
    定义的。这提供了一个很好的一行设置颜色,至少:

    lineChart.setStyle("CHART_COLOR_1: #e9967a;");
    

    折线图是否有
    图表系列区域填充
    ?你想在这里做什么“填充”?当然,面积填充是特定于
    面积图的。(你也有其他问题,但现在还不清楚你想在这里做什么。)用新代码更新帖子。
    
    Node node = lineChart.lookup(".default-color0.chart-series-line");
            // Set the first series fill to translucent pale green
            node.setStyle("-fx-stroke: #e9967a;");
    
    public class MainApp extends Application 
    {
    
        @Override public void start(Stage stage) 
        {
             stage.setTitle("Line Chart Sample");
    
             //defining the axes
             final NumberAxis xAxis = new NumberAxis();
             final NumberAxis yAxis = new NumberAxis();
             xAxis.setLabel("Number of Month");
    
             //creating the chart
             final LineChart<Number,Number> lineChart = new LineChart<>(xAxis,yAxis);
             lineChart.setTitle("Stock Monitoring, 2010");
    
             //defining a series
             XYChart.Series series = new XYChart.Series();
             series.setName("My portfolio");
    
             //populating the series with data
             series.getData().add(new XYChart.Data(1, 23));
             series.getData().add(new XYChart.Data(2, 14));
             series.getData().add(new XYChart.Data(3, 15));
             series.getData().add(new XYChart.Data(4, 24));
             series.getData().add(new XYChart.Data(5, 34));
             series.getData().add(new XYChart.Data(6, 36));
             series.getData().add(new XYChart.Data(7, 22));
             series.getData().add(new XYChart.Data(8, 45));
             series.getData().add(new XYChart.Data(9, 43));
             series.getData().add(new XYChart.Data(10, 17));
             series.getData().add(new XYChart.Data(11, 29));
             series.getData().add(new XYChart.Data(12, 25));
    
             Scene scene  = new Scene(lineChart,800,600);
             lineChart.getData().add(series);
    
             stage.setScene(scene);
             stage.show();
    
             // Look up first series fill
             Node node = lineChart.lookup(".default-color0.chart-series-area-fill");
             // Set the first series fill to translucent pale green
             node.setStyle("-fx-fill: linear-gradient(#f2f2f2, #d4d4d4);"
                 + "  -fx-background-insets: 0 0 -1 0, 0, 1, 2;"
                 + "  -fx-background-radius: 3px, 3px, 2px, 1px;");
    
             Node nodew = lineChart.lookup(".chart-series-area-line");
             // Set the first series fill to translucent pale green
             nodew.setStyle("-fx-stroke: #989898; -fx-stroke-width: 1px; ");
        }
    
        public static void main(String[] args) 
        {
             launch(args);
        }
    
    }
    
    lineChart.setStyle("CHART_COLOR_1: #e9967a;");