Changing a Series Color in a Line Chart with Script

RichT
edited February 11, 2022 in Analytics #1
Hello,<br />
<br />
I'm having trouble changing the line color of a series in a line chart. <br />
<br />
I have successfully changed the colors of the markers (within beforeDrawDataPoint) and the legend (within beforeDrawLegendItem) but I'm having difficulties with the line color. The only way I've managed to change the line color is within beforeGeneration but, as a side effect, the outline of the markers are also changing from black to the color I'm assigning to the line.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>function beforeDrawLegendItem( lerh, bounds, icsc )
{
var seriesValue = lerh.getLabel().getCaption().getValue();
var fill = lerh.getFill();

if( seriesValue == "141" )
fill.set( 250, 178, 23, 255 );
}

function beforeDrawDataPoint( dph, fill, icsc )
{
if( dph.getSeriesDisplayValue() == '141' )
fill.set( 250, 178, 23, 255 );
}

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

var xAxis = chart.getAxes().get(0);
var yAxis = xAxis.getAssociatedAxes().get(0);
var xSeriesDef = xAxis.getSeriesDefinitions().get(0);
var ySeriesDef = yAxis.getSeriesDefinitions().get(0);
var ySeries = ySeriesDef.getRunTimeSeries();
var numberofrunseries = ySeries.size();

//loop thru all of the series
for (var i = 0; i < numberofrunseries; i++) {
//my condition
if ( ySeries.get(i).getSeriesIdentifier() == '141' ) {
ySeries.get(i).setPaletteLineColor(false);
ySeries.get(i).getLineAttributes().setColor( ColorDefinitionImpl.create( 250, 178, 23 ) );
i = numberofrunseries;
}
}
}</pre>
<br />
I spent some time researching this topic but haven't seen a solution that will help me out. <br />
<br />
<a class='bbc_url' href='http://www.birt-exchange.org/org/forum/index.php/topic/20136-change-chart-series-color-via-script-in-birt-2-3-1/page__s__4a21e3d43fa52b95e8e0e8ae9929310a'>Change chart series color via script in birt 2.3.1</a><br />
<a class='bbc_url' href='http://www.birt-exchange.org/org/forum/index.php/topic/22208-how-to-set-marker-color-different-from-line-color-in-chart/page__s__bcef1fddd7d83b95f5e5f3d48a6e38cd'>How to set marker color different from line color in chart?</a><br />
<a class='bbc_url' href='http://www.birt-exchange.org/org/forum/index.php/topic/14709-change-line-color-via-script-in-a-chart/page__s__52eca4c59bfd383d72646997320fb965'>Change line color via script in a chart </a><br />
<br />
I keep wandering back to the beforeDrawSeries but I can't seem to find anything in there that deal with the line attributes.<br />
<br />
I'm looking for help to either change the marker outline back to black OR just change the line of the series without affecting the marker outline. Any suggestions are appreciated. I've added an example report for reference.<br />
<br />
BIRT version 2.6.2<br />
<br />
Thanks in advance,<br />
Rich

Comments

  • mwilliams
    edited December 31, 1969 #2
    Try this:<br />
    <br />
    <pre class='_prettyXprint _lang-auto _linenums:0'>
    function beforeDrawMarker( marker, dph, icsc )
    {
    marker.getOutline().setVisible(false);
    }
    </pre>
    Warning No formatter is installed for the format ipb
  • mwilliams
    edited December 31, 1969 #3
    Or, you could just go to the Value (Y) Series section in the chart editor, select the Markers button at the bottom, select the drop down arrow, and disable "outline".
    Warning No formatter is installed for the format ipb
  • RichT
    edited December 31, 1969 #4
    Thanks - I appreciate the reply.<br />
    <br />
    I actually want the outline. My problem seems to be that when I set the line color in the beforeGeneration it is changing the marker outline color as well. I tried adding the code below but it did not change the outline color.<br />
    <br />
    <pre class='_prettyXprint _lang-auto _linenums:0'>function beforeDrawMarker( marker, dph, icsc )
    {
    importPackage( Packages.org.eclipse.birt.chart.model.attribute.impl );
    marker.getOutline().setColor( ColorDefinitionImpl.create( 0, 0, 0 ) );
    }</pre>
    <br />
    Also - I realized that in the example attached to the original post, I had the 'Use Series Palette as Line Color' unchecked by mistake for the Value (Y) Series. I meant to have that setting selected.<br />
    <br />
    Any other thoughts are appreciated.<br />
    Rich
  • mwilliams
    edited December 31, 1969 #5
    Oh, sorry. I misread your first post. Let me take a closer look at this. I'll post back what I find.
    Warning No formatter is installed for the format ipb
  • mwilliams
    edited December 31, 1969 #6
    You're wanting to assign a specific color to a specific series? Or could you just put your colors you're wanting into the palette and just let BIRT assign the colors? Even if I grab the marker in the beforeGeneration and try to change the outline color, it doesn't work. I've asked a couple colleagues as well. I'll let you know if we get anything.
    Warning No formatter is installed for the format ipb
  • RichT
    edited December 31, 1969 #7
    yea - specific color for specific series. The visibility is the only thing that I seem to be set/unset. no luck with thickness, style, or color.

    Thanks again for taking a look at this - let me know if you find anything.

    Rich
  • mwilliams
    edited December 31, 1969 #8
    Well, I found a way to change just the outline color. The problem is, it doesn't work if you're not using the series palette color. It changes the entire line color if you don't use the series palette, so it doesn't help.
    Warning No formatter is installed for the format ipb
  • RichT
    edited December 31, 1969 #9
    Is changing the series palette colors in script easy? I could monitor my dataset in the onFetch and, because I know the order of the data coming in, I could redefine the palette to match the order of the data. Would that work?
  • mwilliams
    edited December 31, 1969 #10
    Nevermind. Found the error. There was a place where a condition wasn't wrapped and when I put the extra line in there, it didn't consider it for the condition. Take a look.
    Warning No formatter is installed for the format ipb
  • RichT
    edited December 31, 1969 #11
    PERFECT!!!

    Much appreciated - I can stop pulling out my hair now
  • kosta
    edited August 5, 2013 #12
    <p>Hello BIRT community,</p><p> </p><p>I see that this solution is for the line chart. What if we have bar chart. Are the methods for setting the color the same like for the line chart?</p><p> </p><p>Thanks in advance.</p><p> </p><p>I found a solution. it is a combination of code posted here and on other topic:</p><p><a data-ipb='nomediaparse' href='http://www.birt-exchange.org/forum/index.php?/topic/22210-how-to-get-series-palette-from-bar-chart/'>http://www.birt-exchange.org/forum/index.php?/topic/22210-how-to-get-series-palette-from-bar-chart/</a></p><p> </p><p>This worked for me:</p><pre class="_prettyXprint _lang-js _linenums:1">sd = chart.getAxes().get(0).getAssociatedAxes().get(0).getSeriesDefinitions().get(0);sd.getSeriesPalette().getEntries().clear();sd.getSeriesPalette().getEntries().add(ColorDefinitionImpl.GREEN());// optional - orange with gradientsd.getSeriesPalette().getEntries().add(GradientImpl.create( ColorDefinitionImpl.create( 250, 178, 23 ), ColorDefinitionImpl.create( 250, 178, 23 ), -35, false ));</pre>
    Warning No formatter is installed for the format ipb