2

有一个简单的 XY LineChart,我想在不使用 css 的情况下设置网格和线条描边(宽度)、样式(点、虚线等)和颜色为 rgb 以及背景颜色。

这可能吗?如果是这样,怎么做?我找不到任何合适的方法。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class XyChart extends Application {

@Override
public void start(Stage stage) {
   stage.setTitle("Line plot");

   final CategoryAxis xAxis = new CategoryAxis();
   final NumberAxis yAxis = new NumberAxis(1, 21,0.1);

   yAxis.setTickUnit(1);
   yAxis.setPrefWidth(35);
   yAxis.setMinorTickCount(10);

   yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis){
        @Override
    public String toString(Number object){
            String label;
            label = String.format("%7.2f", object.floatValue());
            return label;
    }
});
final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis);

   lineChart.setCreateSymbols(false);
   lineChart.setAlternativeRowFillVisible(false);
   lineChart.setLegendVisible(false);

   XYChart.Series series1 = new XYChart.Series();

    series1.getData().add(new XYChart.Data("Jan", 1));
    series1.getData().add(new XYChart.Data("Feb", 4));
    series1.getData().add(new XYChart.Data("Mar", 2.5));
    series1.getData().add(new XYChart.Data("Apr", 5));
    series1.getData().add(new XYChart.Data("May", 6));
    series1.getData().add(new XYChart.Data("Jun", 8));
    series1.getData().add(new XYChart.Data("Jul", 12));
    series1.getData().add(new XYChart.Data("Aug", 8));
    series1.getData().add(new XYChart.Data("Sep", 11));
    series1.getData().add(new XYChart.Data("Oct", 13));
    series1.getData().add(new XYChart.Data("Nov", 10));
    series1.getData().add(new XYChart.Data("Dec", 20));


    BorderPane pane = new BorderPane();
    pane.setCenter(lineChart);          
    Scene scene = new Scene(pane, 800, 600);
    lineChart.setAnimated(false);
    lineChart.getData().addAll(series1);       

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

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

1 回答 1

2

是的,您的案例在逻辑上比如何在 JavaFX 2.0 折线图中动态更改线型更简单?而且您还要求提供更多功能。标记为重复只是指向类似问题和答案的一种简单方法。

我认为答案是相同的(使用 css 查找)——JavaFX 2.2 中没有lineChart.setStroke.

Eventually some of the stuff which is only accessible via css lookups might be made available via Java API. For instance some of the region background stuff is API in JavaFX 8 so, once you get a reference to it, you could modify it via api - though, even then, I still don't think there is a way to get a reference to something like a chart background without a css lookup or some unthinkably ugly sequence of getChildren().get(idx) calls or hacking into the chart source code. Of those options, I think the css lookup approach is the most preferable most of the time.

请注意,您并没有真正避免使用 css 文件,因为 JavaFX 2.2 附带了一个用于设置图表样式的默认 css文件。另请注意,动态线条样式问题的链接示例解决方案不提供用户样式表 - 所有用户样式都在代码中完成。

于 2013-01-03T12:09:22.317 回答