gmlion
gmlion

Reputation: 308

Set <chart:guide> value at runtime

I'm trying to access the

<chart:guide>

value property from the screen controller, but I can't find any getter to reach it. The xml block is like this:

<chart:valueAxes>
 <chart:axis position="LEFT"
  stackType="REGULAR"
  title="Graph Title">
  <chart:guides>
   <chart:guide
    value="0"
    inside="true"
    lineAlpha="1"
   />
  </chart:guides>
 </chart:axis>
</chart:valueAxes>

I need to set the value of the guide at runtime. Any suggestion?

Upvotes: 1

Views: 65

Answers (1)

jreznot
jreznot

Reputation: 2773

Let's say we have the following SerialChart:

<chart:serialChart id="serialChart"
                   caption="Serial chart"
                   height="100%"
                   width="100%"
                   categoryField="x">
    <chart:graphs>
        <chart:graph valueField="y"/>
    </chart:graphs>
    <chart:valueAxes>
        <chart:axis position="LEFT">
            <chart:guides>
                <chart:guide value="12"
                             inside="true"
                             lineAlpha="1"/>
            </chart:guides>
        </chart:axis>
    </chart:valueAxes>
    <chart:data>
        <chart:item>
            <chart:property name="x" value="10"/>
            <chart:property name="y" value="12"/>
        </chart:item>
        <chart:item>
            <chart:property name="x" value="11"/>
            <chart:property name="y" value="2"/>
        </chart:item>
        <chart:item>
            <chart:property name="x" value="12"/>
            <chart:property name="y" value="120"/>
        </chart:item>
        <chart:item>
            <chart:property name="x" value="13"/>
            <chart:property name="y" value="16"/>
        </chart:item>
    </chart:data>
</chart:serialChart>

You can simply get ValueAxis and then get Guide object by index or iterate collection and find by id (optional attribute of Guide):

@Inject
private SerialChart serialChart;

ValueAxis valueAxis = serialChart.getValueAxes().get(0);
Guide guide = valueAxis.getGuides().get(0);
guide.setValue(15);

serialChart.repaint();

Note, if we want to change already displayed chart configuration then we have to call repaint() method.

If you use CUBA 6.4 or older, you have to obtain Configuration object first using getConfiguration() method and cast it to appropriate chart type as shown here: https://doc.cuba-platform.com/charts-6.4/cdp_screen_controller.html

Upvotes: 1

Related Questions