JavaFx 2.x: How to draw minor grid lines

拈花ヽ惹草 提交于 2019-12-24 13:35:14

问题


I'm unable to draw minor grid lines, it is possible to do so in JavaFx 2.2? In my case the grid and the graph moves so I cannot use a static image to generate the minor grid (live graph as shown in this example here).

Currently I see this

an example of what I'm looking for.


回答1:


As suggested by @colti and a little bit of module logic I came up with this solution

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class Axis extends Application
{

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

    @Override
    public void start(final Stage stage) throws Exception
    {
        final NumberAxis x = new NumberAxis();
        final NumberAxis y = new NumberAxis();
        x.setAutoRanging(false);
        y.setAutoRanging(false);

        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(x, y);
        lineChart.setPrefSize(900, 900);
        lineChart.setLegendVisible(false);
        lineChart.setCreateSymbols(false);
        lineChart.setAlternativeColumnFillVisible(false);
        lineChart.setAlternativeRowFillVisible(false);

        final XYChart.Series seriesX = new XYChart.Series();

        for (int i = 1; i < 100; i++) {
            if ((i % 2) != 0) {
                seriesX.getData().add(new XYChart.Data(i, 0));
                seriesX.getData().add(new XYChart.Data(i, 100));
            }
            else {
                seriesX.getData().add(new XYChart.Data(i, 100));
                seriesX.getData().add(new XYChart.Data(i, 0));
            }

        }

        final XYChart.Series seriesY = new XYChart.Series();

        for (int i = 1; i < 100; i++) {
            if ((i % 2) != 0) {
                seriesY.getData().add(new XYChart.Data(0, i));
                seriesY.getData().add(new XYChart.Data(100, i));
            }
            else {
                seriesY.getData().add(new XYChart.Data(100, i));
                seriesY.getData().add(new XYChart.Data(0, i));
            }

        }

        lineChart.getData().add(seriesX);
        lineChart.getData().add(seriesY);
        lineChart.getStylesheets().add("site.css");
        final Scene scene = new Scene(lineChart);
        stage.setScene(scene);
        stage.show();
    }
}

CSS

.chart-series-line{    
-fx-stroke-width: 1px;
-fx-stroke:#eedede;
-fx-effect: null;
}
.chart-vertical-grid-lines {
    -fx-stroke: #cccccc;
}
.chart-horizontal-grid-lines {
    -fx-stroke: #cccccc;
}


来源:https://stackoverflow.com/questions/25330897/javafx-2-x-how-to-draw-minor-grid-lines

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