gillyr7 Posted February 11, 2021 Report Share Posted February 11, 2021 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> Quote Link to comment Share on other sites More sharing options...
FaeFaeFae Posted February 11, 2021 Report Share Posted February 11, 2021 Hi! Try using :last-child and :nth-last-child(2) instead of :nth-child <style> rect.highcharts-point.highcharts-color-0:nth-last-child(2) { fill: #ffb32b; } rect.highcharts-point.highcharts-color-0:last-child { fill: #13B5EA; } </style> Quote Link to comment Share on other sites More sharing options...
gillyr7 Posted February 12, 2021 Author Report Share Posted February 12, 2021 @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 Quote Link to comment Share on other sites More sharing options...
FaeFaeFae Posted February 17, 2021 Report Share Posted February 17, 2021 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. Quote Link to comment Share on other sites More sharing options...
futurist Posted October 3, 2022 Report Share Posted October 3, 2022 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: Quote Link to comment Share on other sites More sharing options...
futurist Posted January 8, 2023 Report Share Posted January 8, 2023 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); }); Quote Link to comment Share on other sites More sharing options...
oliverk Posted September 20 Report Share Posted September 20 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? Quote Link to comment Share on other sites More sharing options...
PotatoMato Posted September 23 Report Share Posted September 23 Hi @oliverk, you can use this one for pie charts: <style> path.highcharts-point:nth-last-child(2) { fill: red; /* Change color for the second-to-last slice */ } path.highcharts-point:last-child { fill: blue; /* Change color for the last slice */ } </style> -Potato oliverk 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.