Dynamic Chart Markers

flosmi
edited February 11, 2022 in Analytics #1
Hi,

i want to generate a chart with markers on the x-axis and the x-axis is of type text.

i try it with this script:

function beforeGeneration(chart, icsc){
importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
importPackage(Packages.org.eclipse.birt.chart.model.attribute);
importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl);

var chart = icsc.getChartInstance();
var yAxis = chart.getAxes().get(0).getAssociatedAxes().get(0);

var EoD = icsc.getExternalContext().getScriptable().getParameterValue("EoD");


top_ml = MarkerLineImpl.create(yAxis, NumberDataElementImpl.create(EoD));
top_ml.getLabel().getCaption().setValue("EoD " + EoD);
top_ml.getLineAttributes().getColor().set(0,200,0);
top_ml.getLineAttributes().setStyle(LineStyle.SOLID_LITERAL);
top_ml.getLineAttributes().setThickness(2);

}

But how can i change the axis? and does this also work for text axis?

thx

Comments

  • mwilliams
    edited December 31, 1969 #2
    Can you explain a little more about what you're trying to do? What are the axis values? What is the value of the parameter? One of the axis values? I'm not sure if it can be done, but if you answer the questions above, or just recreate the issue with the sample database, I'll take a look. :)
    Warning No formatter is installed for the format ipb
  • flosmi
    edited December 31, 1969 #3
    hi,

    i want to create a dynamic marker on the x-axis:

    function beforeGeneration(chart, icsc){
    importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
    importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
    importPackage(Packages.org.eclipse.birt.chart.model.attribute);
    importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl);

    var chart = icsc.getChartInstance();
    var xAxis = chart.getAxes().get(0);

    var EoD = icsc.getExternalContext().getScriptable().getParameterValue("EoD");


    top_ml = MarkerLineImpl.create(xAxis, NumberDataElementImpl.create(EoD));
    top_ml.getLabel().getCaption().setValue("EoD " + EoD);
    top_ml.getLineAttributes().getColor().set(0,200,0);
    top_ml.getLineAttributes().setStyle(LineStyle.SOLID_LITERAL);
    top_ml.getLineAttributes().setThickness(2);
    }


    the x-axis is generated out of "to_char(mydate, 'YYYY-WW')"

    the paramter has data type string and is a text box.

    thx for your help.
  • flosmi
    edited December 31, 1969 #4
    hmmm<br />
    <br />
    now i changed my statement to get a date for the x-axis, he sorts the line chart correct. <br />
    But when i want to add a dynamic marker via:<br />
    intcountformaker = 0;<br />
    <br />
    function afterDataSetFilled( series, dataSet, icsc )<br />
    {<br />
    importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl); <br />
    importPackage(Packages.com.ibm.icu.util);<br />
    //x-axis Series<br />
    if (series.eClass().getName().equals("Series")) <br />
    { <br />
    var list = dataSet.getValues();<br />
    <br />
    for ( i=0; i<list.length; i=i+1)<br />
    {<br />
    var cdt = icsc.getExternalContext().getScriptable().getParameterValue("EoD");<br />
    <br />
    var yr = list.fieldDifference( cdt, Calendar.YEAR );<br />
    var mn = list.fieldDifference( cdt, Calendar.MONTH );<br />
    var dy = list.fieldDifference( cdt, Calendar.DATE );<br />
    if( yr == 0 && mn == 0 && dy == 0){<br />
    intcountformaker = i;<br />
    }<br />
    }<br />
    }<br />
    }<br />
    <br />
    function beforeGeneration( chart, icsc )<br />
    {<br />
    importPackage(Packages.org.eclipse.birt.chart.model.component.impl);<br />
    importPackage(Packages.org.eclipse.birt.chart.model.data.impl);<br />
    importPackage(Packages.org.eclipse.birt.chart.model.attribute);<br />
    importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl);<br />
    var xAxis =chart.getAxes().get(0);<br />
    <br />
    target_ml = MarkerLineImpl.create(xAxis, NumberDataElementImpl.create(intcountformaker));<br />
    //target_ml.getLabel().getCaption().setValue("EoD");<br />
    target_ml.getLineAttributes().getColor().set(32,145,245);<br />
    }<br />
    he changed the dates of the x-axis to my EoD parameter - i have no idea why... :(
  • flosmi
    edited December 31, 1969 #5
    now i try this:
    function beforeGeneration( chart, icsc )
    {
    importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
    importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
    importPackage(Packages.org.eclipse.birt.chart.model.attribute);
    importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl);
    var xAxis =chart.getAxes().get(0);
    var EoD = icsc.getExternalContext().getScriptable().getParameterValue("EoD");
    target_ml = MarkerLineImpl.create(xAxis, NumberDataElementImpl.create(EoD));
    target_ml.getLabel().getCaption().setValue("EoD");
    target_ml.getLineAttributes().getColor().set(32,145,245);
    }


    but also failed :(

    any ideas how i cant create a dynamic marker on the x-axis for datetimes?

    thx
  • mwilliams
    edited December 31, 1969 #6
    What is your BIRT version? I'll try to make an example in your version.
    Warning No formatter is installed for the format ipb
  • flosmi
    edited May 23, 2012 #7
    at the moment version 2.6.2.

    thx for your help...
  • mwilliams
    edited December 31, 1969 #8
    Take a look at this report. I am not using the x-axis as a category axis in this example. It is an actual dateTime axis. All I did was add a marker line in the chart wizard, then since it was my only marker line, I only needed a very simple script to change the value to the entered parameter value. Let me know if this is not what you're wanting. If you're using a category axis, it'll be different.
    Warning No formatter is installed for the format ipb
  • flosmi
    edited December 31, 1969 #9
    hi,

    thx for your help.


    unfortunaly it is a category axis.
  • flosmi
    edited May 25, 2012 #10
    importPackage (Packages.org.eclipse.birt.chart.model.data.impl);

    function beforeDrawMarkerLine( axis, markerLine, icsc )
    {
    if (markerLine.getLabel().getCaption().getValue() == "EoD") {
    if (icsc.getExternalContext().getScriptable().getParameterValue("EoD") != null) {
    ml = icsc.getExternalContext().getScriptable().getParameterValue("EoD");
    y = ml.getYear() + 1900;
    m = ml.getMonth();
    d = ml.getDate();
    markerLine.setValue(DateTimeDataElementImpl.create(new Date(y,m,d)));
    }
    else {
    markerLine.getLineAttributes().setVisible(false);
    markerLine.getLabel().setVisible(false);
    }
    }

    if (markerLine.getLabel().getCaption().getValue() == "Frozen"){
    if (icsc.getExternalContext().getScriptable().getParameterValue("Frozen") != null) {
    ml = icsc.getExternalContext().getScriptable().getParameterValue("Frozen");
    y = ml.getYear() + 1900;
    m = ml.getMonth();
    d = ml.getDate();
    markerLine.setValue(DateTimeDataElementImpl.create(new Date(y,m,d)));
    }
    else {
    markerLine.getLineAttributes().setVisible(false);
    markerLine.getLabel().setVisible(false);
    }
    }
    }


    But what do you mean with: category axis, it'll be different.
  • mwilliams
    edited December 31, 1969 #11
    Well, when it's a category axis, the marker value accepts an integer. From what I saw, when I was creating the example above, the value is for which tick on the x-axis will get the marker line, so you won't be able to pass a date for this, you'll have to figure out based on the date entered by the user how many ticks over you want the marker line to be. That's just what I assumed based on what I saw before switching to a non-category axis. I could be very wrong on that. I'll edit the above example to use a category axis and see how it goes.
    Warning No formatter is installed for the format ipb
  • mwilliams
    edited December 31, 1969 #12
    Ok. Take a look at this one. The first chart is the same as the one from the example above. The second chart is with a category axis. Same explanation as above, I added the marker in the wizard and just change the value in script. I elected to do all of my script in the chart, but I'm sure you could figure out the position for the marker outside and pass it in. Maybe easier than what I had to do, in the end. :)
    Warning No formatter is installed for the format ipb
  • flosmi
    edited December 31, 1969 #13
    thanks a lot for your help - it works great :)
  • flosmi
    edited June 1, 2012 #14
    one more question :)

    Is it possible via birt to create a dynamic xAxis? (scalable)

    i try something like this only as example for the yAxis (which is a number):

    function beforeGeneration( chart, icsc )
    {
    min = NumberDataElementImpl.create(100.0);
    yAxis1 = chart.primaryBaseAxes[0];
    yScale1 = yAxis1.getScale();
    yScale1.setMin(min);
    }

    but it didnt work :(
  • mwilliams
    edited December 31, 1969 #15
    Warning No formatter is installed for the format ipb
  • Can the example u have given created using BIRT version 2.6 work with 4.4.2 because when i am trying to do same getting below error

    Cannot convert Invalid Date to java.lang.Long at line 34 of chart script:''