setup JavaFX NumberAxis XAxis - Java JavaFX

Java examples for JavaFX:Chart

Description

setup JavaFX NumberAxis XAxis

Demo Code

/*//from  ww w.  ja  v  a2  s  .c  o  m
 * Copyright 2002-2014 SCOOP Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Date;

import javafx.collections.ObservableList;

import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;

import javafx.util.StringConverter;

public class Main {
    public static void setupXAxis(NumberAxis numberAxis,
            ObservableList<XYChart.Series<Number, Number>> seriesList) {
        long min = Long.MAX_VALUE;
        long max = 0;

        for (XYChart.Series<Number, ?> series : seriesList) {
            for (Data<Number, ?> data : series.getData()) {
                min = Math.min(data.getXValue().longValue(), min);
                max = Math.max(data.getXValue().longValue(), max);
            }
        }
        setupXAxis(numberAxis, min, max);
    }

    public static void setupXAxis(NumberAxis numberAxis, long min, long max) {
        numberAxis.setAutoRanging(false);
        numberAxis.setTickUnit((max - min) / 20);
        numberAxis.setLowerBound(min);
        numberAxis.setUpperBound(max);
        numberAxis.setTickLabelFormatter(new StringConverter<Number>() {
            private final SimpleDateFormat format = new SimpleDateFormat(
                    "dd.MM.yyyy\nHH:mm:ss,SSS");

            @Override
            public String toString(Number object) {
                return format.format(new Date(object.longValue()));
            }

            @Override
            public Number fromString(String string) {
                return null;
            }
        });
    }
}

Related Tutorials