javafx.scene.chart.Axis Java Examples

The following examples show how to use javafx.scene.chart.Axis. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From quantumjava with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Chart plotFunction(List<Function<Double, Double>> functions, Number xStart, Number xEnd) {
    int div = 500;
    double x0 = xStart.doubleValue();
    double x1 = xEnd.doubleValue();
    double step = 1./div* (x1-x0);
    Axis<Number> xAxis = new NumberAxis(x0, x1, .1* (x1-x0));
    Axis<Number> yAxis = new NumberAxis();
    ObservableList<XYChart.Series<Number, Number>> series = FXCollections.observableArrayList();
    LineChart<Number,Number> chart = new LineChart(xAxis, yAxis, series);
    chart.setCreateSymbols(false);
    for (Function<Double, Double> f: functions) {
        XYChart.Series<Number, Number> mainSeries = new XYChart.Series();
        series.add(mainSeries);
        ObservableList<XYChart.Data<Number, Number>> data = FXCollections.observableArrayList();
        mainSeries.setData(data);
        for (double x = x0; x < x1; x= x +step) {
            final Number y = f.apply(x);
            data.add(new XYChart.Data<>(x,y));
        }
    }
    return chart;

}
 
Example #2
Source File: XYChartInfo.java    From jfxutils with Apache License 2.0 6 votes vote down vote up
/**
	 * Returns the plot area in the reference's coordinate space.
	 */
	public Rectangle2D getPlotArea() {
		Axis<?> xAxis = chart.getXAxis();
		Axis<?> yAxis = chart.getYAxis();

		double xStart = getXShift( xAxis, referenceNode );
		double yStart = getYShift( yAxis, referenceNode );

		//If the direct method to get the width (which is based on its Node dimensions) is not found to
		//be appropriate, an alternative method is commented.
//		double width = xAxis.getDisplayPosition( xAxis.toRealValue( xAxis.getUpperBound() ) );
		double width = xAxis.getWidth();
//		double height = yAxis.getDisplayPosition( yAxis.toRealValue( yAxis.getLowerBound() ) );
		double height = yAxis.getHeight();

		return new Rectangle2D( xStart, yStart, width, height );
	}
 
Example #3
Source File: XYChartInfo.java    From jfxutils with Apache License 2.0 6 votes vote down vote up
/**
 * Given graphical coordinates in the reference's coordinate system, returns x and y axis value as
 * a point via the {@link Axis#getValueForDisplay(double)} and {@link Axis#toNumericValue(Object)}
 * methods.
 *
 * @param minX lower X value (upper left point)
 * @param minY lower Y value (upper left point)
 * @param maxX upper X value (bottom right point)
 * @param maxY upper Y value (bottom right point)
 */
@SuppressWarnings( "unchecked" )
public Rectangle2D getDataCoordinates( double minX, double minY, double maxX, double maxY ) {
	if ( minX > maxX || minY > maxY ) {
		throw new IllegalArgumentException( "min > max for X and/or Y" );
	}

	Axis xAxis = chart.getXAxis();
	Axis yAxis = chart.getYAxis();

	double xStart = getXShift( xAxis, referenceNode );
	double yStart = getYShift( yAxis, referenceNode );

	double minDataX = xAxis.toNumericValue( xAxis.getValueForDisplay( minX - xStart ) );
	double maxDataX = xAxis.toNumericValue( xAxis.getValueForDisplay( maxX - xStart ) );

	//The "low" Y data value is actually at the maxY graphical location as Y graphical axis gets
	//larger as you go down on the screen.
	double minDataY = yAxis.toNumericValue( yAxis.getValueForDisplay( maxY - yStart ) );
	double maxDataY = yAxis.toNumericValue( yAxis.getValueForDisplay( minY - yStart ) );

	return new Rectangle2D( minDataX,
	                        minDataY,
	                        maxDataX - minDataX,
	                        maxDataY - minDataY );
}
 
Example #4
Source File: AdvCandleStickChartSample.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
/**
 * This is called when the range has been invalidated and we need to update it. If the axis are auto
 * ranging then we compile a list of all data that the given axis has to plot and call invalidateRange() on the
 * axis passing it that data.
 */
@Override
protected void updateAxisRange() {
    // For candle stick chart we need to override this method as we need to let the axis know that they need to be able
    // to cover the whole area occupied by the high to low range not just its center data value
    final Axis<Number> xa = getXAxis();
    final Axis<Number> ya = getYAxis();
    List<Number> xData = null;
    List<Number> yData = null;
    if (xa.isAutoRanging()) {
        xData = new ArrayList<Number>();
    }
    if (ya.isAutoRanging()) {
        yData = new ArrayList<Number>();
    }
    if (xData != null || yData != null) {
        for (Series<Number, Number> series : getData()) {
            for (Data<Number, Number> data : series.getData()) {
                if (xData != null) {
                    xData.add(data.getXValue());
                }
                if (yData != null) {
                    CandleStickExtraValues extras = (CandleStickExtraValues) data.getExtraValue();
                    if (extras != null) {
                        yData.add(extras.getHigh());
                        yData.add(extras.getLow());
                    } else {
                        yData.add(data.getYValue());
                    }
                }
            }
        }
        if (xData != null) {
            xa.invalidateRange(xData);
        }
        if (yData != null) {
            ya.invalidateRange(yData);
        }
    }
}
 
Example #5
Source File: AdvCandleStickChartSample.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new CandleStickChart with the given axis.
 *
 * @param xAxis The x axis to use
 * @param yAxis The y axis to use
 */
public CandleStickChart(Axis<Number> xAxis, Axis<Number> yAxis) {
    super(xAxis, yAxis);
    setAnimated(false);
    xAxis.setAnimated(false);
    yAxis.setAnimated(false);
}
 
Example #6
Source File: AdvCandleStickChartSample.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
/**
 * This is called when the range has been invalidated and we need to update it. If the axis are auto
 * ranging then we compile a list of all data that the given axis has to plot and call invalidateRange() on the
 * axis passing it that data.
 */
@Override
protected void updateAxisRange() {
    // For candle stick chart we need to override this method as we need to let the axis know that they need to be able
    // to cover the whole area occupied by the high to low range not just its center data value
    final Axis<Number> xa = getXAxis();
    final Axis<Number> ya = getYAxis();
    List<Number> xData = null;
    List<Number> yData = null;
    if (xa.isAutoRanging()) {
        xData = new ArrayList<Number>();
    }
    if (ya.isAutoRanging()) {
        yData = new ArrayList<Number>();
    }
    if (xData != null || yData != null) {
        for (Series<Number, Number> series : getData()) {
            for (Data<Number, Number> data : series.getData()) {
                if (xData != null) {
                    xData.add(data.getXValue());
                }
                if (yData != null) {
                    CandleStickExtraValues extras = (CandleStickExtraValues) data.getExtraValue();
                    if (extras != null) {
                        yData.add(extras.getHigh());
                        yData.add(extras.getLow());
                    } else {
                        yData.add(data.getYValue());
                    }
                }
            }
        }
        if (xData != null) {
            xa.invalidateRange(xData);
        }
        if (yData != null) {
            ya.invalidateRange(yData);
        }
    }
}
 
Example #7
Source File: AdvCandleStickChartSample.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new CandleStickChart with the given axis.
 *
 * @param xAxis The x axis to use
 * @param yAxis The y axis to use
 */
public CandleStickChart(Axis<Number> xAxis, Axis<Number> yAxis) {
    super(xAxis, yAxis);
    setAnimated(false);
    xAxis.setAnimated(false);
    yAxis.setAnimated(false);
}
 
Example #8
Source File: VolumeChart.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void updateAxisRange() {
    final Axis<Number> xa = getXAxis();
    final Axis<Number> ya = getYAxis();
    List<Number> xData = null;
    List<Number> yData = null;
    if (xa.isAutoRanging()) {
        xData = new ArrayList<>();
    }
    if (ya.isAutoRanging())
        yData = new ArrayList<>();
    if (xData != null || yData != null) {
        for (XYChart.Series<Number, Number> series : getData()) {
            for (XYChart.Data<Number, Number> data : series.getData()) {
                if (xData != null) {
                    xData.add(data.getXValue());
                }
                if (yData != null)
                    yData.add(data.getYValue());
            }
        }
        if (xData != null) {
            xa.invalidateRange(xData);
        }
        if (yData != null) {
            ya.invalidateRange(yData);
        }
    }
}
 
Example #9
Source File: MultipleAxesLineChart.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
private void bindMouseEvents(final LineChart<?, ?> baseChart, final Double strokeWidth) {
    getChildren().add(detailsWindow);
    detailsWindow.prefHeightProperty().bind(heightProperty());
    detailsWindow.prefWidthProperty().bind(widthProperty());
    detailsWindow.setMouseTransparent(true);

    setOnMouseMoved(null);
    setMouseTransparent(false);

    final Axis<?> xAxis = baseChart.getXAxis();
    final Axis<?> yAxis = baseChart.getYAxis();

    final Line xLine = new Line();
    final Line yLine = new Line();
    yLine.setFill(Color.GRAY);
    xLine.setFill(Color.GRAY);
    yLine.setStrokeWidth(strokeWidth / 2);
    xLine.setStrokeWidth(strokeWidth / 2);
    xLine.setVisible(false);
    yLine.setVisible(false);

    final Node chartBackground = baseChart.lookup(".chart-plot-background");
    for (final Node n : chartBackground.getParent().getChildrenUnmodifiable()) {
        if ((n != chartBackground) && (n != xAxis) && (n != yAxis)) {
            n.setMouseTransparent(true);
        }
    }
}
 
Example #10
Source File: ChartLayoutAnimator.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(long l) {
    if (isAxis) {
        ((Axis<?>) nodeToLayout).requestAxisLayout();
    } else {
        nodeToLayout.requestLayout();
    }
}
 
Example #11
Source File: CandleStickChart.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This is called when the range has been invalidated and we need to update it. If the axis are auto
 * ranging then we compile a list of all data that the given axis has to plot and call invalidateRange() on the
 * axis passing it that data.
 */
@Override
protected void updateAxisRange() {
    // For candle stick chart we need to override this method as we need to let the axis know that they need to be able
    // to cover the whole area occupied by the high to low range not just its center data value
    final Axis<Number> xa = getXAxis();
    final Axis<Number> ya = getYAxis();
    List<Number> xData = null;
    List<Number> yData = null;
    if (xa.isAutoRanging()) {
        xData = new ArrayList<>();
    }
    if (ya.isAutoRanging()) {
        yData = new ArrayList<>();
    }
    if (xData != null || yData != null) {
        for (XYChart.Series<Number, Number> series : getData()) {
            for (XYChart.Data<Number, Number> data : series.getData()) {
                if (xData != null) {
                    xData.add(data.getXValue());
                }
                if (yData != null) {
                    if (data.getExtraValue() instanceof CandleData) {
                        CandleData candleData = (CandleData) data.getExtraValue();
                        yData.add(candleData.high);
                        yData.add(candleData.low);
                    } else {
                        yData.add(data.getYValue());
                    }
                }
            }
        }
        if (xData != null) {
            xa.invalidateRange(xData);
        }
        if (yData != null) {
            ya.invalidateRange(yData);
        }
    }
}
 
Example #12
Source File: XYChartInfo.java    From jfxutils with Apache License 2.0 5 votes vote down vote up
/**
 * Given graphical coordinates in the reference's coordinate system, returns x and y axis value as
 * a point via the {@link Axis#getValueForDisplay(double)} and {@link Axis#toNumericValue(Object)}
 * methods.
 */
@SuppressWarnings( "unchecked" )
public Point2D getDataCoordinates( double x, double y ) {
	Axis xAxis = chart.getXAxis();
	Axis yAxis = chart.getYAxis();

	double xStart = getXShift( xAxis, referenceNode );
	double yStart = getYShift( yAxis, referenceNode );

	return new Point2D(
			xAxis.toNumericValue( xAxis.getValueForDisplay( x - xStart ) ),
	    yAxis.toNumericValue( yAxis.getValueForDisplay( y - yStart ) )
	);
}
 
Example #13
Source File: NumberAxisBuilder.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public Axis<Number> build() {
    final NumberAxis axis = new NumberAxis();
    axis.setAutoRanging(true);
    axis.setForceZeroInRange(false);
    return axis;
}
 
Example #14
Source File: ChartZoomManager.java    From jfxutils with Apache License 2.0 5 votes vote down vote up
private static <T> DoubleProperty toDoubleProperty( final Axis<T> axis, final Property<T> property ) {
	final ChangeListener<Number>[] doubleChangeListenerAry = new ChangeListener[1];
	final ChangeListener<T>[] realValListenerAry = new ChangeListener[1];

	final DoubleProperty result = new SimpleDoubleProperty() {
		/** Retain references so that they're not garbage collected. */
		private final Object[] listeners = new Object[] {
			doubleChangeListenerAry, realValListenerAry
		};
	};

	doubleChangeListenerAry[0] = new ChangeListener<Number>() {
		@Override
		public void changed( ObservableValue<? extends Number> observable, Number oldValue, Number newValue ) {
			property.removeListener( realValListenerAry[0] );
			property.setValue( axis.toRealValue(
					newValue == null ? null : newValue.doubleValue() )
			);
			property.addListener( realValListenerAry[0] );
		}
	};
	result.addListener(doubleChangeListenerAry[0]);

	realValListenerAry[0] = new ChangeListener<T>() {
		@Override
		public void changed( ObservableValue<? extends T> observable, T oldValue, T newValue ) {
			result.removeListener( doubleChangeListenerAry[0] );
			result.setValue( axis.toNumericValue( newValue ) );
			result.addListener( doubleChangeListenerAry[0] );
		}
	};
	property.addListener(realValListenerAry[0]);

	return result;
}
 
Example #15
Source File: VolumeChart.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public VolumeChart(Axis<Number> xAxis, Axis<Number> yAxis, StringConverter<Number> toolTipStringConverter) {
    super(xAxis, yAxis);
    this.toolTipStringConverter = toolTipStringConverter;
}
 
Example #16
Source File: TileBuilder.java    From tilesfx with Apache License 2.0 4 votes vote down vote up
public final B yAxis(final Axis AXIS) {
    properties.put("yAxis", new SimpleObjectProperty(AXIS));
    return (B)this;
}
 
Example #17
Source File: TileBuilder.java    From tilesfx with Apache License 2.0 4 votes vote down vote up
public final B xAxis(final Axis AXIS) {
    properties.put("xAxis", new SimpleObjectProperty(AXIS));
    return (B)this;
}
 
Example #18
Source File: SmoothedChart.java    From tilesfx with Apache License 2.0 4 votes vote down vote up
public SmoothedChart(final Axis<X> xAxis, final Axis<Y> yAxis, final ObservableList<Series<X, Y>> data) {
    super(xAxis, yAxis, data);
    init();
    registerListeners();
}
 
Example #19
Source File: SmoothedChart.java    From tilesfx with Apache License 2.0 4 votes vote down vote up
public SmoothedChart(final Axis<X> xAxis, final Axis<Y> yAxis) {
    super(xAxis, yAxis);
    init();
    registerListeners();
}
 
Example #20
Source File: ChartZoomManager.java    From jfxutils with Apache License 2.0 4 votes vote down vote up
private static <T> DoubleProperty getLowerBoundProperty( Axis<T> axis ) {
	return axis instanceof ValueAxis ?
			((ValueAxis<?>) axis).lowerBoundProperty() :
			toDoubleProperty(axis, ChartZoomManager.<T>getProperty(axis, "lowerBoundProperty") );
}
 
Example #21
Source File: Clustering.java    From java-ml-projects with Apache License 2.0 4 votes vote down vote up
@Override
public void start(Stage stage) throws Exception {
	loadData();
	tree = new J48();
	tree.buildClassifier(data);

	noClassificationChart = buildChart("No Classification (click to add new data)", buildSingleSeries());
	clusteredChart = buildChart("Clustered", buildClusteredSeries());
	realDataChart = buildChart("Real Data (+ Decision Tree classification for new data)", buildLabeledSeries());

	noClassificationChart.setOnMouseClicked(e -> {
		Axis<Number> xAxis = noClassificationChart.getXAxis();
		Axis<Number> yAxis = noClassificationChart.getYAxis();
		Point2D mouseSceneCoords = new Point2D(e.getSceneX(), e.getSceneY());
		double x = xAxis.sceneToLocal(mouseSceneCoords).getX();
		double y = yAxis.sceneToLocal(mouseSceneCoords).getY();
		Number xValue = xAxis.getValueForDisplay(x);
		Number yValue = yAxis.getValueForDisplay(y);
		reloadSeries(xValue, yValue);
	});

	Label lblDecisionTreeTitle = new Label("Decision Tree generated for the Iris dataset:");
	Text txtTree = new Text(tree.toString());
	String graph = tree.graph();
	SwingNode sw = new SwingNode();
	SwingUtilities.invokeLater(() -> {
		TreeVisualizer treeVisualizer = new TreeVisualizer(null, graph, new PlaceNode2());
		treeVisualizer.setPreferredSize(new Dimension(600, 500));
		sw.setContent(treeVisualizer);
	});

	Button btnRestore = new Button("Restore original data");
	Button btnSwapColors = new Button("Swap clustered chart colors");
	StackPane spTree = new StackPane(sw);
	spTree.setPrefWidth(300);
	spTree.setPrefHeight(350);
	VBox vbDecisionTree = new VBox(5, lblDecisionTreeTitle, new Separator(), spTree,
			new HBox(10, btnRestore, btnSwapColors));
	btnRestore.setOnAction(e -> {
		loadData();
		reloadSeries();
	});
	btnSwapColors.setOnAction(e -> swapClusteredChartSeriesColors());
	lblDecisionTreeTitle.setTextFill(Color.DARKRED);
	lblDecisionTreeTitle.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD, FontPosture.ITALIC, 16));
	txtTree.setTranslateX(100);
	txtTree.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD, FontPosture.ITALIC, 14));
	txtTree.setLineSpacing(1);
	txtTree.setTextAlignment(TextAlignment.LEFT);
	vbDecisionTree.setTranslateY(20);
	vbDecisionTree.setTranslateX(20);

	GridPane gpRoot = new GridPane();
	gpRoot.add(realDataChart, 0, 0);
	gpRoot.add(clusteredChart, 1, 0);
	gpRoot.add(noClassificationChart, 0, 1);
	gpRoot.add(vbDecisionTree, 1, 1);

	stage.setScene(new Scene(gpRoot));
	stage.setTitle("Íris dataset clustering and visualization");
	stage.show();
}
 
Example #22
Source File: ChartZoomManager.java    From jfxutils with Apache License 2.0 4 votes vote down vote up
private static <T> DoubleProperty getUpperBoundProperty( Axis<T> axis ) {
	return axis instanceof ValueAxis ?
			((ValueAxis<?>) axis).upperBoundProperty() :
			toDoubleProperty(axis, ChartZoomManager.<T>getProperty(axis, "upperBoundProperty") );
}
 
Example #23
Source File: TimelineChart.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
     * Constructs a new <code>TimelineChart</code> component given a parent
     * panel and axes.
     *
     * @param parent the panel containing this chart.
     * @param xAxis the x axis.
     * @param yAxis the y axis.
     *
     * @see TimelinePanel
     * @see NumberAxis
     */
    public TimelineChart(final TimelinePanel parent, final Axis<Number> xAxis, final NumberAxis yAxis) {
        super(xAxis, yAxis);

        this.parent = parent;
        timeline = this;

        this.xAxis = (NumberAxis) xAxis;
        this.yAxis = yAxis;
//        this.xAxis.setLowerBound(System.currentTimeMillis() - (31536000000l * 5));
//        this.xAxis.setUpperBound(System.currentTimeMillis());
//        setExtents(System.currentTimeMillis() - (31536000000l * 5), System.currentTimeMillis(), true);
        tooltip = createTooltip();

        formatAxes();
        // Create the selection box:
        selection = createSelectionRectange();
        selection.setStroke(Color.SILVER);
        selection.setStrokeWidth(2d);
        final LinearGradient gradient
                = new LinearGradient(0.0, 0.0, 0.0, 0.75, true, CycleMethod.NO_CYCLE, new Stop[]{
            new Stop(0, Color.LIGHTGREY),
            new Stop(1, Color.GREY.darker())
        });
        selection.setFill(gradient);
        selection.setSmooth(true);

        // Round the edges of the rectangle:
        selection.setArcWidth(5.0);
        selection.setArcHeight(5.0);
        getChildren().add(selection);

        // Install event handlers:
        //  Handles zooming for the timeline:
        setOnScroll(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(final ScrollEvent t) {
                final double mouseX = t.getX();
                performZoom(t, mouseX);
                t.consume();
            }
        });
        //  Recognise mouse presses:
        setOnMousePressed(timelineMouseHandler);
        //  Handles scrolling back and forth:

        setOnMouseDragged(timelineMouseHandler);
        //  Handles determination of time under the mouse cursor, and sets tooltips accordingly:

        setOnMouseMoved(timelineMouseHandler);
        //  Handles the change of the mouse cursor for the timeline:

        setOnMouseEntered(timelineMouseHandler);
        // Handles the release of selection events:

        setOnMouseReleased(timelineMouseHandler);
        setOnMouseClicked(timelineMouseHandler);

        // Style the timeline:
        setAnimated(true);
        setLegendVisible(false);
        setVerticalZeroLineVisible(false);
    }
 
Example #24
Source File: LabeledBarChart.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public LabeledBarChart(Axis xAxis, Axis yAxis) {
    super(xAxis, yAxis);
    init();
}
 
Example #25
Source File: LabeledHorizontalBarChart.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public LabeledHorizontalBarChart(Axis xAxis, Axis yAxis) {
    super(xAxis, yAxis);
    init();
}
 
Example #26
Source File: TileBuilder.java    From OEE-Designer with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public final B yAxis(final Axis AXIS) {
       properties.put("yAxis", new SimpleObjectProperty(AXIS));
       return (B)this;
   }
 
Example #27
Source File: TileBuilder.java    From OEE-Designer with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public final B xAxis(final Axis AXIS) {
       properties.put("xAxis", new SimpleObjectProperty(AXIS));
       return (B)this;
   }
 
Example #28
Source File: SmoothedChart.java    From OEE-Designer with MIT License 4 votes vote down vote up
public SmoothedChart(final Axis<X> xAxis, final Axis<Y> yAxis, final ObservableList<Series<X, Y>> data) {
    super(xAxis, yAxis, data);
    init();
    registerListeners();
}
 
Example #29
Source File: SmoothedChart.java    From OEE-Designer with MIT License 4 votes vote down vote up
public SmoothedChart(final Axis<X> xAxis, final Axis<Y> yAxis) {
    super(xAxis, yAxis);
    init();
    registerListeners();
}
 
Example #30
Source File: ChartLayoutAnimator.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
public ChartLayoutAnimator(Parent nodeToLayout) {
    this.nodeToLayout = nodeToLayout;
    isAxis = nodeToLayout instanceof Axis;
}