JavaFx 2.x: XYChart properties

早过忘川 提交于 2019-12-08 00:54:06

问题


Having a simple XY LineChart, I would like to set grid and line stroke (width), style (dotted, dashed and so on) and color as rgb as well as background color without using css.

Is this possibile? And if so, how to? I can't find any suitable method.

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);
}   
}

回答1:


Yes, your case is simpler in logic than How to dynamically change line style in JavaFX 2.0 line chart? and you are also asking for a couple more features. Marking as duplicate was just an easy way to point to a similar question and answer.

I think the answer is the same (use css lookups) - there is no such api in JavaFX 2.2 as 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.

Note that you are not really avoiding a css file, as JavaFX 2.2 ships with a default css file which is used to style charts. Also note that the linked sample solution to the dynamic line style question does not supply a user stylesheet - all user styling is done in code.



来源:https://stackoverflow.com/questions/14137314/javafx-2-x-xychart-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!