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

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

  • 1 year later...
On 2/10/2021 at 8:15 PM, gillyr7 said:

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>

 

 Hi,

You may refer to this link on how to appy stylings to multiple sibling elements all at once:

 

Link to comment
Share on other sites

  • 3 months later...
On 2/17/2021 at 8:53 AM, FaeFaeFae said:

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.

 

Hi, sharing in here a script that I found that allows you to check if an element exists (followed by your code if that element in fact exists) instead of using a timer (setInterval):

 

function waitForElm(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}

 

 

 

 

To use it:

waitForElm('.some-class').then((elm) => {
    console.log('Element is ready');
    console.log(elm.textContent);
});
Link to comment
Share on other sites

  • 1 year later...
On 2/11/2021 at 4:15 AM, gillyr7 said:

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>

 

Any ideas how we would use the same approach to color different segments of a pie chart?

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...
×
×
  • Create New...