Highlighting topN in bar chart

mennovh
edited February 11, 2022 in Analytics #1
I have a bar chart showing 10 bars. I would like to give the 3 longest bars (i.e. the 3 bars with the highest value) a different colour then the other bars.<br />
<br />
I have found an example online (<a class='bbc_url' href='http://marklorenz.blogspot.com/2006/02/birt-tips-for-java-developers.html'>http://marklorenz.blogspot.com/2006/02/birt-tips-for-java-developers.html</a>) that shows how to change the colour of a bar if it passes a marker line, but have been struggeling to adapt this to my wishes. <br />
<br />
Example I found:<br />
<br />
function beforeDrawDataPoint(dataPointHints, fill, scriptContext )<br />
{ val = dataPointHints.getOrthogonalValue();<br />
chart = scriptContext.getChartInstance();<br />
marker = chart.getAxes().get(0).getAssociatedAxes().get(0).getMarkerLines().get(0).getValue().getValue(); <br />
if (val > marker) //crosses marker?<br />
fill.set(255, 0, 0); //yes - display in red <br />
elsefill.set(0, 0, 255); //no - display in blue<br />
}<br />
<br />
Which function should I use in a script attached to a chart to test whether a value is in the Top 3 ?<br />
<br />
I have tried things like: if (isTopN(val, 3)....<br />
<br />
but keep getting errors that isTopN is undefined.<br />
<br />
Any help would be greatly appreciated.

Comments

  • mwilliams
    edited December 31, 1969 #2
    Hi Mennovh,

    If it's possible to do with your data. You could sort it by the column that has the values you are charting and put in a loop in the onRender script section of the chart to change the color of the first N bars. I'll try a couple things to see if I can figure out how to highlight the top N without sorting the data.

    Regards,

    Michael
    Warning No formatter is installed for the format ipb
  • mennovh
    edited December 31, 1969 #3
    Michael,

    that would be great.

    Thanks,

    Menno
  • indie
    edited December 31, 1969 #4
    i also need to highlight / conditional <strong class='bbc'>color the top n results in a bar chart</strong>.<br />
    so i tried the solution described about from mark lorenz blog.<br />
    but unfortunatly it's not working with birt 2.2 any more <strong class='bbc'>because the fill object doesn't have a method .set </strong>anymore.<br />
    <br />
    <strong class='bbc'>error msg:</strong><br />
    TypeError: Cannot find function set. at line 20 of chart script: ...<br />
    <br />
    <strong class='bbc'>script is used:</strong><br />
    <pre class='_prettyXprint _lang-auto _linenums:0'>
    /**
    * Called before drawing each datapoint graphical representation or marker.
    * @param dph DataPointHints
    * @param fil Fill
    * @param icsc IChartScriptContext
    */

    function beforeDrawDataPoint(dph, fill, icsc)
    {
    val = dph.getOrthogonalValue();
    chart = icsc.getChartInstance();
    marker = chart.getAxes().get(0).getAssociatedAxes().get(0). getMarkerLines().get(0).getValue().getValue();
    if (val > marker) //crosses marker?
    [B]fill.set(255,0,0)[/B]; //yes - display in red
    else
    fill.set(0,0,255); //no - display in blue
    }
    </pre>
  • mwilliams
    edited December 31, 1969 #5
    Mennovh and Indie,<br />
    <br />
    I just posted a DevShare example on one way to highlight the top N values of a bar chart. Hope it helps. Here is the link:<br />
    <br />
    <a class='bbc_url' href='http://www.birt-exchange.com/modules/wfdownloads/singlefile.php?cid=2&lid=401'>http://www.birt-exchange.com/modules/wfdownloads/singlefile.php?cid=2&lid=401</a><br />
    <br />
    Regards,<br />
    <br />
    Michael Williams
    Warning No formatter is installed for the format ipb
  • indie
    edited December 31, 1969 #6
    I'll try to adapt your solution.
    Thx a lot for making this tutorial report!!!
  • mennovh
    edited December 31, 1969 #7
    Thanks a lot. An answer and an example report brilliant.
  • Ronak
    edited December 31, 1969 #8
    Hi Michael,

    It is good example for Highlighting.

    Thanks
    Ronak