xdev-software / chartjs-java-model

Provides Java models for Chart.js
Apache License 2.0
7 stars 5 forks source link

Migration from PF 13 to 14 - Handling "no data" #198

Open uvula opened 1 week ago

uvula commented 1 week ago

Checklist

Description

I just have a question. I migrating from Primefaces(PF) 13 to 14. I have some chart and go to XDEV chartjs-java-model. It is more or less easy to migrate because the Chart, Data, *Dataset are similar. But I didn't have investigated the new library very well yet.

My question: I found: "no data" are not handled by the library itself, with PF lib not a problem seen yet. I got the "Chart not drawable" exceptions, e.g because I have no data samples from my source. I handle this now in my code.

But I'd like to have support from chartjs-java-model to generate "empty" charts, so I can handle it easier in my code.

E.g:

chartJson = ChartUtil.buildGraph(LineChart.class, () -> { <code to generate a LineChart, maybe with empty dataset> return model.toJson(); });

public static String buildGraph(Class<? extends HomogeneousChart<?,?,?>> clazz, Supplier supplier) { Objects.requireNonNull(clazz); Objects.requireNonNull(supplier); try { return supplier.get(); } catch (Throwable t) { if ( !( t instanceof IllegalArgumentException)) logger.error(t); return generateEmptyChart(clazz) // somehow generate easy by chartjs-java-model library } }

Does it make sense?

Additional information

No response

AB-xdev commented 1 week ago

Thank you for the issue

I see a few options here:

  1. Check inside your code if the data you fetched is empty and if so display another component instead of the chart that e.g. says "No data found" or something. I think this is the best and user friendliest decision.
  2. Alternatively you can also use toJsonNative to bypass the isDrawable-check, however this might result in unexpected drawings
uvula commented 1 week ago

I'd like to have support from chartjs-java-model library in this case. I want request a empty (drawable) chart from the lib. E.g: during my migration, I thought, I just can create a *Chart, but no data inside and I can draw it. No, also got "not drawable". I started with LineChart (in the case I have "no data"), so I added one data point, fine (empty+drawable). Next I migrated all my PolarChart's, just put one data point. No, here I need 2 dummy data points. RadarChart: I need 3.

It would be nice, it the library can help here. E.g: a "EmptyChart" class with some constructor + argument "label='no data received from source') or however and UI will draw some nice object.

Or I can (my example) request an empty *Chart instance, maybe alter the object (setting color etc).

Together with .isDrawable, I can use if/then/else or try/catch and decide to send the "json" object to the UI. But I can get "empty charts" from library. So far my wish :-)

no-data