pepstock-org / Charba

J2CL and GWT Charts library based on CHART.JS
https://pepstock-org.github.io/Charba-Wiki/docs
Apache License 2.0
62 stars 6 forks source link

TypeError: Cannot read properties of null (reading 'compareTo_4_g$') #87

Closed Speykious closed 1 year ago

Speykious commented 1 year ago

We are trying to integrate Charba into our GWT project, and we need to use a timeline series chart.

However, when trying to set the timeline series data, the chart refuses to display on screen, instead an error appears in the console about not being able to read from null:

ConsoleLogger.java:33 TypeError: Cannot read properties of null (reading 'compareTo_4_g$')
    at gyl_g$ (Dataset.java:144:1)
    at YIl_g$.ZIl_g$ [as compare_2_g$] (Dataset.java:144:1)
    at Hri_g$ (Arrays.java:1605:1)
    at Lri_g$ (Arrays.java:1673:1)
    at Lri_g$ (Arrays.java:1681:1)
    at Kri_g$ (Arrays.java:1650:1)
    at Osi_g$ (Arrays.java:1297:1)
    at eAo_g$.Ryl_g$ [as setInternalTimeSeriesItems_1_g$] (Dataset.java:1183:1)
    at Mzo_g$ (HasTimeSeriesItems.java:137:1)
    at eAo_g$.tAo_g$ [as setTimeSeriesData_1_g$] (HasTimeSeriesItems.java:134:1)
    # ...

Here's the relevant code I used to test things:

    private TimeSeriesLineChartWidget createTimeSeriesLineChart() {
        TimeSeriesLineChartWidget chart = new TimeSeriesLineChartWidget();

        TimeSeriesLineOptions chartOptions = chart.getOptions();
        chartOptions.setResponsive(true);
        chartOptions.setAspectRatio(3.5);
        chartOptions.setMaintainAspectRatio(true);
        chartOptions.getLegend().setDisplay(true);
        chartOptions.getTitle().setDisplay(true);
        chartOptions.getTitle().setText("test");
        chartOptions.getTooltips().setEnabled(true);
        chartOptions.setAnimationEnabled(false);
        chartOptions.getDecimation().setEnabled(true);
        chartOptions.getDecimation().setAlgorithm(DecimationAlgorithm.MIN_MAX);

        // tooltip interaction options
        Interaction interaction = chartOptions.getInteraction();
        interaction.setMode(InteractionMode.NEAREST);
        interaction.setAxis(InteractionAxis.X);
        interaction.setIntersect(false);

        // axes options
        CartesianTimeSeriesAxis xAxis = chartOptions.getScales().getTimeAxis();
        xAxis.setDisplay(true);
        xAxis.getTitle().setDisplay(true);
        xAxis.getTitle().setText("Time");
        xAxis.getTicks().setSource(TickSource.DATA);
        xAxis.getTicks().getMajor().setEnabled(true);
        xAxis.getTime().setUnit(TimeUnit.SECOND);
        xAxis.getTime().getDisplayFormats().setDisplayFormat(TimeUnit.SECOND, "m’ss”");
        xAxis.getTime().getDisplayFormats().setDisplayFormat(TimeUnit.MINUTE, "H:mm:ss");
        xAxis.getTime().getDisplayFormats().setDisplayFormat(TimeUnit.HOUR, "H:mm:ss");

        CartesianLinearAxis yAxis = chartOptions.getScales().getLinearAxis();
        yAxis.setDisplay(true);
        yAxis.getTitle().setDisplay(true);
        yAxis.getTitle().setText("test");

        TimeSeriesItem[] data = new TimeSeriesItem[10];
        for (int i = 0; i < 10; i++)
            data[i] = new TimeSeriesItem(new Date((long) i), Random.nextDouble());

        // dataset
        TimeSeriesLineDataset dataset = chart.newDataset();
        dataset.setLabel("labeltest");
        dataset.setBorderColor(Color.CHARBA);
        dataset.setBorderWidth(1);
        dataset.setPointRadius(0);
        dataset.setParsing(false);
        dataset.setTimeSeriesData(data); // view crashes here

        chart.getData().setDatasets(dataset);

        return chart;
    }

There seems to be some null issue while setTimeSeriesData is sorting the given data?

stockiNail commented 1 year ago

@Speykious I was able to reproduce the issue. Having a look.

stockiNail commented 1 year ago

@Speykious the exception is generating because you are creating a epoch to 0 (new Date(0)).

The check is here:

https://github.com/pepstock-org/Charba/blob/cbfbc69918dc7cf7465e66d9b69bac8a9b88dc0b/src/org/pepstock/charba/client/commons/NativeObjectContainer.java#L568

I can change that check to avoid this issue. Maybe you can try starting from 1 to see if it's working.

Speykious commented 1 year ago

That indeed fixed it, thank you a lot!

Ultimately in our use-case the dates are going to be closer to today anyway, so this zero shouldn't cause problems for the time being.

stockiNail commented 1 year ago

@Speykious Thank you! I have anyway already fixed! ;) https://github.com/pepstock-org/Charba/commit/fe30521e92ca9a015337deb071a039a70301345d

It will be available in next version!