Forums BIRT Reports stacked bar chart with value on y-axis and percent as value

This topic contains 2 replies, has 2 voices, and was last updated by  joe_dallesandro 2 years, 9 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #110


    Hello ,

    i have the stacked bar chart
    does anyone know how to display the percent OF THE STAKED BAR instead of the value

    if i tryed to change the serie label value for “percentil value data”
    it will display :

    on the first staked bar
    serieA_2014 / total_serieA
    serieB_2014 / total_serieB

    on the second staked bar
    serieA_2015 / total_serieA
    serieB_2015 / total_serieB

    !!! BUT !!!

    i would like to display

    on the first staked bar
    serieA_for_2014 / total_2014
    serieB_for_2014 / total_2014

    on the second staked bar
    serieA_for_2015 / total_2015
    serieB_for_2015 / total_2015

    AND no i don’t want to use a percent stack, because i don’t wanna lose the value on y-axis

    Any idea ?




    It’s possible, but you’ll need to get the data, say in the afterDataSetFilled event, compute the percentages and then update the data labels accordingly.

    Looks like you are using Open Source BIRT? It would be easier if you’re using commercial BIRT.

    I don’t have an OS BIRT example handy, but maybe later in the week, I’ll post one.




    The afterDataSetFilled event is the right place to add your script to compute the total for each category. However, there’s an added twist: every time the afterDataSetFilled event is called, you get the data set for a series. This means that you have to pivot the data and keep the result in an array saved as a persistent global variable. Once all the series are computed, you can calculate the percentage of each data point in the beforeDrawDataPoint event.

    So, here’s the code. First, initialize a couple of persistent global variables in the beforeFactory method of the report:
    keepTotal = new java.util.ArrayList;
    cnt = new java.lang.Integer(0);
    reportContext.setPersistentGlobalVariable(“g_keepTotal”, keepTotal)
    reportContext.setPersistentGlobalVariable(“g_cnt”, cnt);
    Then, add the following script in the onRender of the chart:
    function afterDataSetFilled( series, dataSet, icsc )

    var keepTotal = icsc.getExternalContext().getScriptable().getPersistentGlobalVariable(“g_keepTotal”);
    var cnt = icsc.getExternalContext().getScriptable().getPersistentGlobalVariable(“g_cnt”);
    var ds = dataSet.getValues();

    if (cnt > 0) {
    for (i = 0; i < ds.length; i++) {
    var tmp = keepTotal.get(i) * 1;
    tmp += ds[i] * 1;
    keepTotal.set(i, tmp);
    } else {
    for (i = 0; i < ds.length; i++) {
    keepTotal.add(new java.lang.Integer(0));

    icsc.getExternalContext().getScriptable().setPersistentGlobalVariable(“g_keepTotal”, keepTotal);
    icsc.getExternalContext().getScriptable().setPersistentGlobalVariable(“g_cnt”, new java.lang.Integer(cnt));

    function beforeDrawDataPoint( dph, fill, icsc )
    var keepTotal = icsc.getExternalContext().getScriptable().getPersistentGlobalVariable(“g_keepTotal”);
    dph.setOrthogonalValue( (dph.getOrthogonalValue() / keepTotal[dph.getIndex()]) * 100 )

    The full sample report is attached.

    Hope this helps,

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.