Simple question about dynamic attributes

Hello.

I’m starting the use of Cuba and I’m a little bit lost to get a “how to” for solving my problem.

I need to manage rendering a chart the number of graphs may change. I’ve seen how add dynamically new graphs for a chart. However the chart is bound to a non persistent entity and I need to add a new attribute that will hold the value for a new chart’s graph.

That’s here where I’m stuck. I tried to use something like
pt.setDynamicAttributes(new HashMap<>());
pt.setValue(“newSerie”,value );
but it doesn’t work.

yes, because of course I want to do it dynamically, by code…

Is someone able to provide me with a working example or a link to existing sources dealing with that issue?
Would be highly appreciated.

Thanks a lot - at least for reading me.

Hi,

If I got your requirements right, I would recommend using DataProvider with MapDataItems.

MapDataItem is based on a map, so that you can easily add value fields dynamically, e.g.:

/**
 * Add data for existing categories for the given graph's valueField.
 *
 * @param valueField the name of graph's valueField
 */
private void addGraphData(String valueField) {
    DataProvider dataProvider = serialChart.getDataProvider();
    for (DataItem dataItem : dataProvider.getItems()) {
        Date date = (Date) dataItem.getValue(CATEGORY_FIELD_NAME);
        Integer value = getItemValue(date, valueField);
        if (dataItem instanceof MapDataItem) {
            ((MapDataItem) dataItem).add(valueField, value);
        } else {
            throw new IllegalStateException("'dataItem' isn't MapDataItem, but " + dataItem.getClass());
        }
    }
}
@Subscribe("addGraphBtn")
public void onAddGraphBtnClick(Button.ClickEvent event) {
    String valueField = "...";

    Graph graph = new Graph()
            .setValueField(valueField);

    addGraphData(valueField);

    serialChart.addGraphs(graph);
    serialChart.repaint();
}

In order to illustrate the above, I’ve attached a demo project: dynamic-chart-demo.zip (79.3 KB)

Regards,
Gleb

2 Likes

Hi Gleb

Thanks it makes the job!
I’ll dive in the example’s code and adapt it to my context.
Thanks again.

One more question, in the example you kindly provided, how would I do to add a legend displaying the name of the series (value, value1, etc) ?
<chart:legend autoMargins=“false”
marginRight=“20”
markerType=“CIRCLE”
position=“RIGHT”
valueText="[[???]]"/>
I can’t find what to replace the question marks with.

Thanks in advance

Hi hi.
I’m answering to myself

1/ when creating the new Graph add .setTilte(your_serial_name_variable)
2/ the code for the legend is something like:
<chart:legend autoMargins=“false”
marginRight=“20”
markerType=“CIRCLE”
position=“RIGHT”
valueText="[[title]]"
/>

Easy.

1 Like