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

Cannot use Gauge Chart in GWT 2.11 due to error: "Cannot read properties of null (reading 'getController') #99

Closed divq closed 4 months ago

divq commented 4 months ago

This is how I use Gauge chart in GWT 2.11 (The GWT example code in https://pepstock-org.github.io/Charba-Wiki/docs/charts/ChartGauge/ was using UiBinder, which I've also tried but failed):

GaugeChartWidget.register();

GaugeChartWidget chart = new GaugeChartWidget();

Threshold normal = DefaultThreshold.NORMAL.getThreshold().setValue(60).setColor(HtmlColor.RED);
Threshold warning = DefaultThreshold.WARNING.getThreshold().setValue(80).setColor(HtmlColor.ORANGE);
Threshold critical = DefaultThreshold.CRITICAL.getThreshold().setValue(100).setColor(HtmlColor.GREEN);

GaugeDataset ds = chart.newDataset(100);
ds.setValue(40);
ValueLabel vl = ds.getValueLabel();
vl.setDisplay(true);
DescriptionLabel dl = ds.getDescriptionLabel();
ds.setDisplay(true);
ds.setContent("This is description");

ds.setThreshold(normal, warning, critical);

chart.getData().setDatasets(ds);

RootPanel.get().add(chart);

Stack trace:

Uncaught TypeError: Cannot read properties of null (reading 'getController')
  at execute (BaseMeterController.java:254:1)
  at drawLabel (BaseMeterController.java:189:1)
  at onAfterDraw (BaseMeterController.java:162:1)
  at onDraw (WrapperController.java:296:1)
.......

In BaseMeterController, the exception was thrown where datasetMetaItem.getController().getInnerRadius() was called, since datasetMetaItem was null.

datasetMetaItem was the result of calling chart.getDatasetItem(0), inside which Chart instance = lookForConsistentInstance returned null (the two methods thereof are inside AbstractChart). And lookForConsistentInstance just looks for the Chart chart field inside AbstractChart.

It seems that, creating a GaugeChartWidget this way, the AbstractChart has an uninitialized Chart field.

My environment: GWT 2.11, charba-gwt 6.5

Update: MeterChart has the same error. But LineChart works just fine.

stockiNail commented 4 months ago

@divq thank you for feedback. I have tested your code locally (GWT 2.10, later I'll do on GWT 2.11) but I don't have any issue :(.

Result:

Screenshot from 2024-05-08 13-14-25

Code

Charba.enable();
GaugeChartWidget.register();

GaugeChartWidget chart = new GaugeChartWidget();
chart.getCanvas().getStyle().setWidth("600px");
chart.getCanvas().getStyle().setHeight("400px");

Threshold normal = DefaultThreshold.NORMAL.getThreshold().setValue(60).setColor(HtmlColor.RED);
Threshold warning = DefaultThreshold.WARNING.getThreshold().setValue(80).setColor(HtmlColor.ORANGE);
Threshold critical = DefaultThreshold.CRITICAL.getThreshold().setValue(100).setColor(HtmlColor.GREEN);

GaugeDataset ds = chart.newDataset(100);
ds.setValue(40);
ValueLabel vl = ds.getValueLabel();
vl.setDisplay(true);
DescriptionLabel dl = ds.getDescriptionLabel();
dl.setDisplay(true);
dl.setContent("This is description");

ds.setThresholds(normal, warning, critical);

chart.getData().setDatasets(ds);

RootPanel.get().add(chart);

I don't expect a different behaviour using GWT 2.11.

divq commented 4 months ago

@stockiNail I created an empty GWT 2.11 project and re-tested my code above and it worked. So I doubt if the error is due to interference with my other dependencies (although at the entry point class I didn't use anything other than charba). I'll check that later. Thank you.

stockiNail commented 4 months ago

@divq I'm very glad to hear that now you are not stuck!