private void updateChart(XYChart<Number, Number> chart, Map<String, List<TimeValuePair<Double>>> seriesTitleToData) { chart.getData().clear(); for (Entry<String, List<TimeValuePair<Double>>> seriesData : seriesTitleToData.entrySet()) { final Series<Number, Number> series = new Series<Number, Number>(); series.setName(seriesData.getKey()); for (TimeValuePair<Double> timeValuePair : seriesData.getValue()) { final Data<Number, Number> data = new Data<Number, Number>(timeValuePair.date.getTime(), timeValuePair.value); data.setNode(new HoveredNode(timeValuePair)); series.getData().add(data); } chart.getData().add(series); } ComponentUtil.setupXAxis((NumberAxis) chart.getXAxis(), chart.getData()); // measurePointsChart.getYAxis().setAutoRanging(false); // ((NumberAxis)measurePointsChart.getYAxis()).setLowerBound(0); // ((NumberAxis)measurePointsChart.getYAxis()).setUpperBound(ymax*1.10); }
chart.getStylesheets().clear(); String stylesheet = stylesheetSupplier.get(); if(stylesheet != null) { Path targetCSS = Paths.get(cssFile.toURI().normalize()); Files.copy(sourceStream, targetCSS, StandardCopyOption.REPLACE_EXISTING); chart.getStylesheets().add(targetCSS.toUri().normalize().toString()); } catch(Exception e) { log.warn("Cannot configure the cloned CSS stylesheet.", e); chart.getStylesheets().add(stylesheet); chart.setTitle(data.getTitle());
/** * 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 ); }
chart.setAnimated(false); series.setName(seriesName); seriesMap.put(seriesName, series); chart.getData().add(series); });
/** * Returns the X axis area in the reference's coordinate space. */ public Rectangle2D getXAxisArea() { return getComponentArea( chart.getXAxis() ); }
/** * Returns the Y axis area in the reference's coordinate space. */ public Rectangle2D getYAxisArea() { return getComponentArea( chart.getYAxis() ); }
public static void addMarker(final XYChart<?, ?> chart, final StackPane chartWrap) { final Line valueMarker = new Line(); final Node chartArea = chart.lookup(".chart-plot-background"); chartArea.setOnMouseMoved(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { Point2D scenePoint = chart.localToScene(event.getSceneX(), event.getSceneY()); Point2D position = chartWrap.sceneToLocal(scenePoint.getX(), scenePoint.getY()); Bounds chartAreaBounds = chartArea.localToScene(chartArea.getBoundsInLocal()); valueMarker.setStartY(0); valueMarker.setEndY(chartWrap.sceneToLocal(chartAreaBounds).getMaxY() - chartWrap.sceneToLocal(chartAreaBounds).getMinY()); valueMarker.setStartX(0); valueMarker.setEndX(0); valueMarker.setTranslateX(position.getX() - chartWrap.getWidth() / 2); double ydelta = chartArea.localToScene(0, 0).getY() - chartWrap.localToScene(0, 0).getY(); valueMarker.setTranslateY(-ydelta * 2); } }); chartWrap.getChildren().add(valueMarker); }
StackPane chartPane = new StackPane(); if ( chart.getParent() != null ) JFXUtil.replaceComponent( chart, chartPane );
@Override public void handle(MouseEvent event) { Point2D scenePoint = chart.localToScene(event.getSceneX(), event.getSceneY()); Point2D position = chartWrap.sceneToLocal(scenePoint.getX(), scenePoint.getY()); Bounds chartAreaBounds = chartArea.localToScene(chartArea.getBoundsInLocal()); valueMarker.setStartY(0); valueMarker.setEndY(chartWrap.sceneToLocal(chartAreaBounds).getMaxY() - chartWrap.sceneToLocal(chartAreaBounds).getMinY()); valueMarker.setStartX(0); valueMarker.setEndX(0); valueMarker.setTranslateX(position.getX() - chartWrap.getWidth() / 2); double ydelta = chartArea.localToScene(0, 0).getY() - chartWrap.localToScene(0, 0).getY(); valueMarker.setTranslateY(-ydelta * 2); } });
public ChartPanManager(XYChart<?, ?> chart ) { handlerManager = new EventHandlerManager( chart ); xAxis = (ValueAxis<?>) chart.getXAxis(); yAxis = (ValueAxis<?>) chart.getYAxis(); chartInfo = new XYChartInfo( chart, chart ); handlerManager.addEventHandler( false, MouseEvent.DRAG_DETECTED, new EventHandler<MouseEvent>() { @Override public void handle( MouseEvent mouseEvent ) { if ( passesFilter( mouseEvent ) ) startDrag( mouseEvent ); } } ); handlerManager.addEventHandler( false, MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() { @Override public void handle( MouseEvent mouseEvent ) { drag( mouseEvent ); } } ); handlerManager.addEventHandler( false, MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() { @Override public void handle( MouseEvent mouseEvent ) { release(); } } ); }
/** * 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 ) ) ); }
@Override public void handle( MouseEvent event ) { if ( event.getClickCount() == 2 && event.getButton() == MouseButton.PRIMARY ) { double x = event.getX(); double y = event.getY(); if ( !chartInfo.getXAxisArea().contains( x, y ) ) chartInfo.getChart().getYAxis().setAutoRanging( true ); if ( !chartInfo.getYAxisArea().contains( x, y ) ) chartInfo.getChart().getXAxis().setAutoRanging( true ); } } };
/** * 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 ); }