Jump to content

Different Colors of Columns in Column Chart


Recommended Posts

Hello, I have a working JS solution for my chart that shows my first 4 columns one color (default for my style), my 5th a new color, and my 6th another color. My JS is below. My issue is that while most of the time the chart will show all 6 columns, there are a few instances where less than 6 will show. In that case I need to adjust the colors based on how many there are. Basically, the last two columns should be the different colors than the original columns.

Again, the solution works well if there are 6 columns like usual, but sometimes there might only be 4 columns. In that case, the first 2 columns should be my default color, while the 3rd and 4th should be the different colors. Any idea on how to make the code below conditional on how many columns there are?

 

<style>
rect.highcharts-point.highcharts-color-0:nth-child(5) {
fill: #ffb32b;
}
rect.highcharts-point.highcharts-color-0:nth-child(6) {
fill: #13B5EA;
}
</style>

 

Link to comment
Share on other sites

@FaeFaeFae This is great. Thank you so much.

 

One more related question... There might be a case where it is not the last two columns. Is there a way to add a conditional component to it? Like Below?

If #Columns = 6 then color column 5 and color column 6

if #Columns = 5 then color column 5

Link to comment
Share on other sites

That's a bit more tricky. Please try the solution below:

1. Remove styles from Header.

2. Add the following script to Footer:

<script type="text/javascript">
document.addEventListener('DataPageReady', chartUpdateHandler);

function chartUpdateHandler(event) {
  
    var cleaner = function(interv) {
        clearInterval(interv);
    } 

    let interv = setInterval(() => {
        if (!!Highcharts) {
            const options = Highcharts.charts[0].series[0].data;
            var lastName = options[options.length-1].name;
            var secondLastName = options[options.length-2].name;
            let data = []; 

            options.forEach(option => {
                let localData = {};
                localData.y = option.y;
                localData.name = option.name; 
                    if(options.length >= 6){
                        if(option.name === lastName) {
                        	localData.color = '#13B5EA';
                        }else if(option.name === secondLastName){
                            localData.color = '#ffb32b';
                        }
                    } else {
                        if(option.name === lastName){
                            localData.color = '#13B5EA';
                        }
                    }
                data.push(localData);
            });

            Highcharts.charts[0].update({
                series: {
                data: data
                }    
            });
            cleaner(interv);
        } 
    }, 200);
}
</script>

 

What the script will do is: if there are 6 or more columns, then the last two will be colored; if there are less than 6 columns, then only the last one will be colored.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...