I have a curious question about the graph LineChart JavaFX.
I have this graph: dots forming a "jump" on the X axis (as shown by the two red points I scored) and therefore JavaFX draws me the line between these two points. How do I remove that line between each "jump"?
I post the code:
public class ControllerIndividua {
public static void plotIndividuaFull(String path, Stage stage, String name) {
final NumberAxis xAxisIntensity = new NumberAxis(); //Dichiarazione asseX
final NumberAxis yAxisIntensity = new NumberAxis();//Dichiarazione asseY
DetectionS1.countS1();
//Dichiarazione del tipo di grafico
final LineChart<Number, Number> lineChartIntensity = new LineChart<Number, Number>(xAxisIntensity,yAxisIntensity);
ArrayList<Double> extractedData; //Lista dei valori dello dell' intensità
ArrayList<Double> extractedTime; //Lista del tempo
ArrayList<Double> extractedS1; //Lista del tempo
ArrayList<Double> extractedS1Time; //Lista del tempo
//Gestione e settaggio del grafico
lineChartIntensity.getData().clear();
try {
//Popolamento delle liste
extractedTime = IntensityExtractor.pointsTime();
extractedData = IntensityExtractor.pointsIntensity();
extractedS1 = DetectionS1.S1pitch();
extractedS1Time = DetectionS1.pointsS1Time();
XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
XYChart.Series<Number, Number> seriesS1 = new XYChart.Series<Number, Number>(); //Creazione seconda serie
series.setName("Intensità di:\t" + name.toUpperCase());
for (int j = 0; j < extractedS1.size(); j++) {
seriesS1.getData().add(new XYChart.Data<Number, Number>(extractedS1Time.get(j), extractedS1.get(j)));
lineChartIntensity.getStyleClass().add("CSSintensity");
}
//Creazione finestra e stampa del grafico
Scene scene = new Scene(lineChartIntensity, 1000, 600);
lineChartIntensity.getData().addAll(series,seriesS1);
scene.getStylesheets().add("application/application.css");
stage.setScene(scene);
stage.show();
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
Someone also has a little idea on how I could do?
Thank you all in advance.
I was in need of adapting LineChart so that it drew line segments, so each pair of data points created a single line segment. It's pretty straight-forward to do by extending
LineChart
and overriding thelayoutPlotChildren
function, although I had to delete an animationy
-scaling variable as it wasprivate
in the super class, but I'm not animating the graph so it was fine. Anyway, here is the Kotlin version (which you can easily adapt to Java, or just copy the JavaLineChart
layoutPlotChildren
code and adapt). It's easy to adapt to your circumstances, just change the contents of thefor
loop at the very end.This is an old question with an accepted answer but I came across it and was curious. I wanted to know if it was possible to put a gap in a
LineChart
(at least without having to create a custom chart implementation). It turns out that there is. The solution is kind of hacky and brittle. It involves getting thePath
by usingXYChart.Series.getNode()
and manipulating the list ofPathElement
s. The following code gives an example:This code results in the following:
This is possible because the
ObservableList
ofPathElement
s seems to be aMoveTo
followed by a bunch ofLineTo
s. I simply picked aLineTo
and replaced it with aMoveTo
to the same coordinates. I haven't quite figured out which index ofLineTo
matches with whichXYChart.Data
, however, and picked8
for the example randomly.There are a couple of issues with this solution. The first and obvious one is that this relies on the internal implementation of
LineChart
. The second is where the real brittleness comes from though. Any change to the data, either axes' value ranges, the chart's width or height, or pretty much anything that causes the chart to redraw itself will cause thePath
to be recomputed and redrawn. This means if you use this solution you'll have to reapply the modification every time the chart redraws itself.I checked it. It is not possible to change only one part of this line. Because this is one long Path and you can't change one element of Path.
I think the only way is add each "jump" in different series.
I made a GapLineChart that automatically sets a MoveTo like Slaw pointed it whenever it sees a Double.NaN. You can find it here https://gist.github.com/sirolf2009/ae8a7897b57dcf902b4ed747b05641f9. Check the first comment for an example