2

I have this Line chart which displays some data using JavaFX Task:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class LineChartSample extends Application {

    @Override public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        //creating the chart
        final LineChart<Number,Number> lineChart = 
                new LineChart<Number,Number>(xAxis,yAxis);

        lineChart.setTitle("Stock Monitoring, 2010");
        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));
        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));

        Scene scene  = new Scene(lineChart,800,600);
        lineChart.getData().add(series);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

I'm interested when I have null data when I have to inset line value how I can display label "No data" into the chart?

Is there any example like this?

user1285928
  • 1,328
  • 29
  • 98
  • 147
  • This would make more sense with a bar chart. A line chart draws a line between values. What value should the line terminate at? Just a discontinuous line? – brian Nov 13 '14 at 00:17
  • What is null data? No data at all for the chart (e.g. series.getData() returns an empty list)? or a null value for a month (e.g. series.getData().get(5) is XYChart.Data(5, null))? – jewelsea Nov 13 '14 at 00:28
  • I need to display message for this XYChart.Data(5, null)) – user1285928 Nov 13 '14 at 10:24

1 Answers1

4

Interesting problem, as the XYChart.Data was final, the best I achieved was:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class LineChartSample extends Application {

    static void checkNoData(XYChart.Series<Integer,Double> series) {
        double d1 = 0;
        XYChart.Data<Integer,Double> last = null;
        for (Object data : series.getData()) {
            if (data instanceof XYChart.Data<?,?>) {
                XYChart.Data<Integer,Double> cdata = (XYChart.Data<Integer,Double>)data;
                if (last != null && last.getYValue() == null) { 
                    double mid = (d1 + cdata.getYValue())/2;
                    last.setYValue(mid);
                    Text nodata = new Text("no data");
                    nodata.setTranslateY(nodata.getLayoutBounds().getHeight()/2);
                    last.setNode(nodata);
                }
                if (last != null) d1 = last.getYValue();
                last = cdata;
            }
        }
        if (last != null && last.getYValue() == null) {
            last.setYValue(d1);
            Text nodata = new Text("no data");
            nodata.setTranslateY(nodata.getLayoutBounds().getHeight()/2);
            last.setNode(nodata);

        }
    }
    @Override public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        //creating the chart
        final LineChart<Number,Number> lineChart = 
                new LineChart<Number,Number>(xAxis,yAxis);

        lineChart.setTitle("Stock Monitoring, 2010");
        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        series.getData().add(new XYChart.Data(1, 23.0));
        series.getData().add(new XYChart.Data(2, 14.0));
        series.getData().add(new XYChart.Data(3, 15.0));
        series.getData().add(new XYChart.Data(4, 24.0));
        series.getData().add(new XYChart.Data(5, null));
        series.getData().add(new XYChart.Data(6, 36.0));
        series.getData().add(new XYChart.Data(7, 22.0));
        series.getData().add(new XYChart.Data(8, 45.0));
        series.getData().add(new XYChart.Data(9, 43.0));
        series.getData().add(new XYChart.Data(10, 17.0));
        series.getData().add(new XYChart.Data(11, 29.0));
        series.getData().add(new XYChart.Data(12, 25.0));


        checkNoData(series);

        Scene scene  = new Scene(lineChart,800,600);
        lineChart.getData().add(series);

        GUIHelper.allowImageDrag(lineChart);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

Jens-Peter Haack
  • 1,887
  • 13
  • 18
  • Its still ugly if first or last has no data and also interpolates incorect if two consecutives have no data...:-( – Jens-Peter Haack Nov 13 '14 at 20:33
  • Rather than overlaying a translated text node, you could [set a node on the data](http://stackoverflow.com/questions/14615590/javafx-linechart-hover-values). – jewelsea Nov 13 '14 at 21:33