How to add a horizontal line to a bar chart?

cjmaloof
cjmaloof Member
edited February 11, 2022 in Analytics #1
Quick question that's been stumping me. I'm displaying some data in a stacked bar chart, where the total height of the stack should be compared to a target value. How can I draw a horizontal line on the chart to represent that target?

Thanks,
Chris

Comments

  • bhanley
    bhanley Member
    edited December 31, 1969 #2
    Hi,

    This is pretty straightforward. On your stacked bar chart (or bar chart for that matter), go to the "Format Chart" tab of the wizard. Click on Y-Series (under the "Chart Area" on the left). Then click on the "Markers" button to add your marker.

    I have attached a report design that does this. It uses the sample data base. A yellow line is added to the stacked bar chart.

    Good Luck!
    Warning No formatter is installed for the format ipb
  • cjmaloof
    cjmaloof Member
    edited December 31, 1969 #3
    Oh, awesome -- thanks!

    I don't suppose there's any way to make the target depend on a report parameter? Even if not, it's still pretty helpful.
  • bhanley
    bhanley Member
    edited December 31, 1969 #4
    I do not see a way to bind the target to a parameter. That does sound like a better way to do it though.<br />
    <br />
    I filed an enhancement request to have it looked at. You can track it here: <br />
    <br />
    <a class='bbc_url' href='http://ted.birt-exchange.com/show_bug.cgi?id=61'>Redirecting to BIRT Exchange</a>
    Warning No formatter is installed for the format ipb
  • cjmaloof
    cjmaloof Member
    edited December 31, 1969 #5
    Bit of a bruising syntax-wrangling session, but I eventually met Rhino and pieced together enough documentation to come up with the following code for the onRender script of the chart:<br />
    <br />
    <pre class='_prettyXprint _lang-auto _linenums:0'>
    importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
    function beforeDrawMarkerLine( axis, markerLine, icsc ) {
    target = icsc.getExternalContext().getScriptable().getParameterValue("total_target");
    markerLine.setValue(NumberDataElementImpl.create(target));
    }</pre>
    <br />
    which changes the line position at runtime.
  • bhanley
    bhanley Member
    edited December 31, 1969 #6
    Very cool. Thanks for sharing the code.
    Warning No formatter is installed for the format ipb
  • Susmitha
    Susmitha Member
    edited December 31, 1969 #7
    Hi,
    is "total_target" in the script is a global variable? I am getting error when I pass dataset variable in the getParameterValue () method. Can you please let me know how can I declare/pass backend table column to the getParameterValue method.

    Thanks,
    Susmitha.
  • cjmaloof
    cjmaloof Member
    edited December 31, 1969 #8
    total_target is a report parameter. Since icsc.getExternalContext().getScriptable() returns an IReportContext ( see <a class='bbc_url' href='http://www.birt-exchange.org/documentation/BIRT_220/EngineJavadoc/enginescript/api/index.html'>Birt Script API</a>) you could use getGlobalVariable() to get a global variable.<br />
    <br />
    I'm not sure about computing a marker line from the dataset -- don't know if the data series values are available when the line is drawn. If they are, I guess you could use the afterDataSetFilled event to compute a value and store it in a global variable for use by beforeDrawMarkerLine?
  • oespinosa
    oespinosa Member
    edited December 31, 1969 #9
    Hi.

    I add two lines marks giving them value parameters as it ago?
  • how to fix the marker (Target Line). I have added two lines to define min and max values on y-axis. But they are not fixed, they are moving constantly as data changes. Thanks in advance. 
  • jfranken
    jfranken E Member
    edited September 29, 2020 #11
    To create a fixed line at y=10, add code like the following in the chart's beforeGeneration method:
    fixedLine = MarkerLineImpl.create(yAxis, NumberDataElementImpl.create(10));

    Warning No formatter is installed for the format ipb