Pie Chart: Color by Categories

bcs0629
bcs0629 Member
edited February 11, 2022 in Analytics #1
Hi, I'm using a multiple pie charts and the slices are colored based on what the value of the category series is. This is working as intended, but the question I have is can I specify the color based on the value of the category.
For instance, my current slices are 'Agree', 'Disagree' 'Strongly Agree'. Depending on the question, one of these responses may not be in the chart. So a blue slice in Chart A could mean 'Agree', but a blue slice in Chart B could mean 'Disagree', since the slices only look for the category. Is there a way I can specify the coloring to say, if response='Agree' then blue slice, if response='Disagree' then red slice? With multiple charts on a page, users would more than likely get confused seeing the different colors representing the same values.

Thanks,
Bryan

Comments

  • RichT
    RichT Member
    edited December 31, 1969 #2
    Hi,<br />
    <br />
    Yes. Add this to each of your charts (replace the 'R', 'G', 'B' with the RGB color values)<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 == "Agree" )
    fill.set( R, G, B, 255 );
    if( seriesValue == "Disagree" )
    fill.set( R, G, B, 255 );
    if( seriesValue == "Strongly Disagree" )
    fill.set( R, G, B, 255 );
    }

    function beforeDrawDataPoint( dph, fill, icsc )
    {
    if( dph.getBaseDisplayValue() == 'Agree' ){
    fill.set( R, G, B, 255 );
    if( dph.getBaseDisplayValue() == "Disagree" )
    fill.set( R, G, B, 255 );
    if( dph.getBaseDisplayValue() == "Strongly Disagree" )
    fill.set( R, G, B, 255 );
    }</pre>
    <br />
    Hope that helps,<br />
    Rich
  • bcs0629
    bcs0629 Member
    edited December 31, 1969 #3
    Thanks for the reply Rich, I haven't used the scripting in BIRT much, so I just want to clarify where I'm putting the script. The only place I've really seen scripting is the Script tab in the main view, so does it go there? I currently have the two functions set up on the onRender, which was the only option for the chart. Do I need to replace seriesValue with row['value'], or is it reading seriesValue directly from the pie chart from where I have it set?

    Thanks again,
    Bryan
  • mwilliams
    mwilliams Member
    edited December 31, 1969 #4
    Yep, you'd just put Rich's script in your chart script by selecting the chart in your layout, then selecting the script tab. The onRender is the only method in the chart. "seriesValue" is being read from the legend label and then the color changed, based on that. You should just need to put this script in the chart's onRender and run it, as long as the category values (Agree, Diagree, Strongly Disagree) are in your chart exactly as Rich put them in the code!
    Warning No formatter is installed for the format ipb
  • bcs0629
    bcs0629 Member
    edited December 31, 1969 #5
    Thanks Rich and Mike, I've got it working now. I had gradients originally for my charts, which was preventing it from changing color based on those values. I'm all good now, thanks again.

    Bryan
  • @RichT By any chance can we use hexa codes for colors instead of RGB?

  • Hi Nayak,

    You've responded to a pretty old thread, so you likely won't get a response from the people involved in this discussion.

    To answer your question, no you can't use hex codes, but hex codes directly translate to RGB numeric values.

    For example, indigo is #2E0854. To convert that to a syntax for the fill.set() command, you'd use parseInt("2E",16) for R, parseInt("08",16) for G, and parseInt("54",16) for B. The 16 is to inform the function that you're converting from base 16

    Warning No formatter is installed for the format ipb