BIRT- set the Bar-width of a Bar chart dynamically

Khaled228
edited February 11, 2022 in Analytics #1

Hello everyone,
I'm working on a report using BIRT. In my report I have a Bar chart and I'm trying to adjust it's Bar-width so that all Bars will have a width value that is retrieved from my dataset. I have managed to get the with-value named (steps) stored in a parameter but i don't know where i should use it. I've looked in the Internet and found the method setSeriesThickness() but no idea if it's what i should use.

Please see the following pic to understand i I'm trying to do.

Would appreciate your help

Thanks in advance
Khaled

Comments

  • jfranken
    edited August 12, 2019 #2

    To change the bar width, go to the Format Chart tab in the chart editor. Select Chart Area on the top of the left pane. Click the General Properties button on the bottom middle of the right pane. Edit the Unit Spacing setting in the General Properties popup window. Decreasing the spacing increases the bar width.

    To set the value dynamically, use the following code:

    function beforeGeneration( chart, icsc )
    {
    chart.setUnitSpacing(20);
    }
    

    If you set the value to a small number (i.e. 5) and the bar is still not wide enough, you will need to alter the size of the chart or the axis properties to spread out the bars.

    Warning No formatter is installed for the format ipb
  • Khaled228
    edited August 12, 2019 #3

    Hi Jeff,
    thank you for your support.
    I have tried setting the Bar-width as you mentioned calling a parameter named step which was also used for setting the step-size dynamically on the x-axis, but it shows no effect when previewing the report. The unit spacing in the chart wizard accepts no decimal numbers but between 0-100(assumed i want a fix Bar-width which is 0.115696). So does my script have no effect because the params["step"] includes a decimal number which is not accepted or because i need to alter the size of the chart?. if yes can you please advice me how?

    Please see the following pic to understand what i mean.

    thanks in advance

  • Hi Jeff,
    I actually tried it once again after restarting my PC it turns out that the following script :
    chart.setUnitSpacing(icsc.getExternalContext().getScriptable().getParameterValue("step")); that i've added in the previous picture makes the bars-width wider but not as it should. Practically, it should fill the step size completely.
    Please see the following picture for better imagination.

    hope you can help me with this.
    many thanks
    Khaled

  • Khaled228
    edited August 17, 2019 #5

    Hi jeff,
    hope you can help me with this as well. In fact, The SetUnitSpacing() increases the width of the bars, but this is done independent from the axis-scaling.
    I mean, i want that each bar (where it exists) to fill it's Interval completely like the following

    Thanks in advance

  • jfranken
    edited August 19, 2019 #6

    Hi Khaled,

    See if this helps:

    • Go to the Format Chart tab in the chart editor.
    • Select "X-Axis" on the left.
    • Click the "Gridlines" button.
    • Check the checkbox named "Cross between Categories" and close to save.
    • Next, click the "Label" button.
    • Change Insets to move the labels and close to save. It will take numerous attempts to line them up properly.
    Warning No formatter is installed for the format ipb
  • Hi Jeff,
    thank you for your Help. Actually, your idea works with me, when the data remain the same. This means, the data retrieved from the data source change at the runtime. This means, that your steps will apply correctly when the chart data values are changed. I will illustrate an example in order to understand what i'm trying to do.

    Therefor, i must do this using chart-script. I found something like beforeDrawSeries() and beforeDrawDatapoint. Here i think i can call the values of my x-axis ( where it exists) or the Series and force them to stay in my grouping-Interval.

    I hope i could express the problem and hope you could help me.

    thanks in advance.

  • Thanks for the detailed description. After I posted my previous response, I was wondering if there was a way to make it dynamic but I haven't had a chance to work on it in the interim. I will let you know what I find.

    Warning No formatter is installed for the format ipb
  • Hi jeff,
    thanks alot for your consideration. Just wanna let you know that the bar-width(where it exists) does not have to fill the whole scale-Interval completely. Most importantly that it does not exceed the limits of it's scale-Interval (where it's located) and must be located in between it's Interval.

    take a look at the following pic for better understanding:

    Thanks in advance

  • How about checking "Cross between Categories" in the X-axis properties to permanently shift the axis tick marks to place then between the bars. Then write code to expand the bars to fill the space between the tick marks:

    function beforeGeneration( chart, icsc )
    {
       chart.setUnitSpacing(10);
    }
    

    Also write code to shift the labels:

    function beforeDrawAxisLabel( axis, label, icsc )
    {
      label.getInsets().setTop(40.1);    // or setBottom, setLeft, setRight
    }
    
    Warning No formatter is installed for the format ipb
  • Khaled228
    edited August 24, 2019 #11

    Hi Jeff,
    thank you for your collaboration. well, I have tried your suggestion. it actually works perfectly only when i check the** is Category axis** in the x-axis properties which i don't want to, because that way the visualization of my data will not be correct since the data on the x-axis are grouped in an interval that alters (when using the report for another database).

    Therefore, checking the cross between categories will not take effect as long as the data on the x-axis are grouped. To understand this please see the following pic

    I have also attached an example to see this practically.

    I have really straI hope you could figure out how i can do this so that the bars do not go out of it's Intervals in case there there Values that are located near the ticks.

  • Thanks for the details and the example. I am looking at it. I haven't had much time to test..

    Warning No formatter is installed for the format ipb
  • Hi Jeff,
    I 'm just wondering if you've found something regarding my problem. I've noticed that almost nobody interacts to my post regarding BIRT excepts you which i'm thankful for, but do you know why is that?

    best wishes

    KHALED

  • jfranken
    edited September 3, 2019 #14

    Hi Khaled,

    I've looked into shifting the bars or the tick marks, but I have not yet found a way to move one without moving the other. Regarding your question about why nobody else is responding, I'm sure there are numerous reasons. My guess is that the main reason is time. We are short-handed at the moment. Also, these questions are complicated enough that not everyone will be able to offer suggestions.

    Warning No formatter is installed for the format ipb
  • Hi Jeff,
    Thank you for your reply. Yes you're absolutely right. Therefore I've been looking for an alternative. Please do not hesitate to post your thoughts if you comes up with an idea, because I've started to lose some hair on this thing ´:)

    Thanks in advanced
    Khaled

  • jfranken
    edited September 5, 2019 #16

    Hi Khaled,

    The attached example is a possible workaround. I hid the X-axis, then created a second chart with the same data below it. On the second chart, I set the axis Origin property to "Max" to make it display on top and hid everything else. I changed the size and margin of the chart to position the tick marks to line up between the bars of the upper chart, then moved the labels in code.

    Warning No formatter is installed for the format ipb
  • Hi Jeff, thank you very much i‘ll implement this on the actual report as soon as i‘m home and then let you know if everything works.
  • Hello Jeff, I've looked into your workaround, it seems to be working only when the Interval is previously known. But in my case, the Grouping Interval changes by runtime.

  • Hallo Jeff,
    just wanna ask you if we can consider this problem ( shifting the Bars correspondingly with the dynamic step-Interval so that the each Bar is located in it's corresponding Interval), I know that you've tried a lot . But just want to ask you if you know someone who could help me with this.

    The whole matter is that i'm building a Histogram where where the the Data on the x- axis (Linear) are grouped in a step-size that is not prior known (dynamic). So I'm just looking for a way to take control of the Bars when they are placed on the x-axis.

    Please see the following picture

    Would be really thankful for your help

    Thanks n advance
    Khaled

  • jfranken
    edited October 25, 2019 #20

    Sorry, I did not remember that this was still unsolved. I don't see the version. Could you please remind me what version of the designer you are using. It is listed in the Help > About menu. Also, what is the chart Output Format as shown on the Select Chart Type tab (i.e. HTML5, SVG, PNG, etc)

    Warning No formatter is installed for the format ipb
  • Khaled228
    edited October 25, 2019 #21

    Hi Jeff,
    below is a picture showing info about the version:

    Regarding the Chart Format,it's Bar-chart SVG.

    Just FYI, The data on the x-axis are grouped with function (sum). This Grouping Interval is set dynamically using script in the beforeFactory() and takes it's value form Data Element called Step. This Step is not fixed. Meaning that it might change it's value when i apply different Data source.

    Thanks in Advance
    Khaled