This discussion is archived
0 Replies Latest reply: Jan 24, 2013 9:46 AM by 935521 RSS

XY LineChart : line on chart above Y axis

935521 Newbie
Currently Being Moderated
This code plots a LineChart with a black line on the chart: when scrolling left/right (left mouse click and drag) the black line is above the Y left and right axis, while yellow XYLine goes below.

How to modify this code so that the black line stays below the Y axis?

Thanks!
import javafx.application.Application; 
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
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.scene.shape.Line;
import javafx.stage.Stage;

public class GridMoveAndLine extends Application {

BorderPane pane;
Line       newLine;
XYChart.Series series1 = new XYChart.Series(); 

SimpleDoubleProperty rectinitX = new SimpleDoubleProperty(); 
SimpleDoubleProperty rectX = new SimpleDoubleProperty(); 
SimpleDoubleProperty rectY = new SimpleDoubleProperty(); 

double oldXLowerBound = -1;
double oldXUpperBound = -1;
double oldYLowerBound = -1;
double oldYUpperBound = -1;

@Override 
public void start(Stage stage) { 

final NumberAxis xAxis = new NumberAxis(1, 12, 1); 
final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); 

xAxis.setAnimated(false);
yAxis.setAnimated(false);

yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { 

    @Override 
    public String toString(Number object) { 
        return String.format("%7.5f", object); 
    } 
}); 

final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis); 

lineChart.setCreateSymbols(false); 
lineChart.setAlternativeRowFillVisible(false); 
lineChart.setAnimated(false); 
lineChart.setLegendVisible(false);

series1.getData().add(new XYChart.Data(1, 0.53185)); 
series1.getData().add(new XYChart.Data(2, 0.532235)); 
series1.getData().add(new XYChart.Data(3, 0.53234)); 
series1.getData().add(new XYChart.Data(4, 0.538765)); 
series1.getData().add(new XYChart.Data(5, 0.53442)); 
series1.getData().add(new XYChart.Data(6, 0.534658)); 
series1.getData().add(new XYChart.Data(7, 0.53023)); 
series1.getData().add(new XYChart.Data(8, 0.53001)); 
series1.getData().add(new XYChart.Data(9, 0.53589)); 
series1.getData().add(new XYChart.Data(10, 0.53476)); 

pane = new BorderPane(); 
pane.setCenter(lineChart); 
Scene scene = new Scene(pane, 800, 600); 
lineChart.getData().addAll(series1); 

stage.setScene(scene);         

scene.setOnMouseClicked(mouseHandler); 
scene.setOnMouseDragged(mouseHandler); 
scene.setOnMouseEntered(mouseHandler); 
scene.setOnMouseExited(mouseHandler); 
scene.setOnMouseMoved(mouseHandler); 
scene.setOnMousePressed(mouseHandler); 
scene.setOnMouseReleased(mouseHandler); 
stage.show();

newLine = new Line(260, 35, 390, 280);
pane.getChildren().add(newLine);

oldXLowerBound = xAxis.getLowerBound();
oldXUpperBound = xAxis.getUpperBound();

DoubleBinding LineXaxisBinding = new DoubleBinding() {
    {                                
        super.bind(xAxis.lowerBoundProperty(), xAxis.upperBoundProperty(), xAxis.widthProperty());
    }
    @Override
    protected double computeValue() {
        double chartWidthX = xAxis.getWidth();
        double axisSpanX = xAxis.getUpperBound() - xAxis.getLowerBound();
        double displacementX = (chartWidthX / axisSpanX) * ( oldXLowerBound-xAxis.getLowerBound()  );

        return (0 + displacementX);
        
    }
};

newLine.translateXProperty().bind(LineXaxisBinding);
} 

EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { 

@Override 
public void handle(MouseEvent mouseEvent) { 

    if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {             
        rectinitX.set(mouseEvent.getX()); 
    } 
    else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED || mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { 
        LineChart<Number, Number> lineChart = (LineChart<Number, Number>) pane.getCenter(); 
        NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); 

        double Tgap = xAxis.getWidth()/(xAxis.getUpperBound() - xAxis.getLowerBound()); 
        double newXlower=xAxis.getLowerBound(), newXupper=xAxis.getUpperBound();             
        double Delta=0.3;
        
        if(mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED){
        if(rectinitX.get() < mouseEvent.getX()){    
            newXlower=xAxis.getLowerBound()-Delta;
            newXupper=xAxis.getUpperBound()-Delta;
        }
    else if(rectinitX.get() > mouseEvent.getX()){    
            newXlower=xAxis.getLowerBound()+Delta;
            newXupper=xAxis.getUpperBound()+Delta;
        }    
        xAxis.setLowerBound( newXlower ); 
        xAxis.setUpperBound( newXupper );                        
        }                                        
        rectinitX.set(mouseEvent.getX());                                 
     } 
  } 
}; 
    public static void main(String[] args) {
        launch(args);
        
    }
}
Edited by: 932518 on Jan 23, 2013 9:15 AM

Edited by: 932518 on Jan 24, 2013 6:46 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points