Page 1 of 1

Same color element of the legend and the graph bubble

Posted: Thu Jun 08, 2023 7:15 pm
by JoséCarlosMartínez
(Apologies for my English)

I use this R code to build a highcharts chart that I include in a report created with rmardown from RStudio.

Code: Select all

dfaus <- dfaus[order(dfaus$Municipio),]
hc <- dfaus %>%
 hchart(
  type = "scatter",
  hcaes(
   x = dfaus$`Esfuerzo Social por Habitante`,
   y = dfaus$`Umbral Pobreza`,
   size = dfaus$`Relevancia Esfuerzo Social` * 100,
   group = dfaus$`Municipio`,
   color = ifelse(dfaus$Municipio == "Murcia", "#C31A00", "#8673A1")
   ),
  color = ifelse(dfaus$Municipio == "Murcia", "#C31A00", "#8673A1"),
  tooltip = list(
   valueDecimals = 2,
   valueSuffix = " %"
  )
 )

But the color of the element of the legend does not correspond to the bubble of the chart.

I have detected that the order of the elements of the legend is affected by the stressed words, for example, "Málaga" is before "Murcia", however, it appears after. The same happens with "Córdoba" and "Coruña (A)". I think that HighCharts when sorting the legend elements does not take into account the latin characters but their unicode values.

Thanks for your help.
Greetings.

Re: Same color element of the legend and the graph bubble

Posted: Fri Jun 09, 2023 11:13 am
by jedrzej.r
Hi!

Thanks for reaching out to us with your question!

It seems that this problem could be related to R wrapper, as i assumed you are using Highcharter. Instead of changing colors in config, you could utilize native Highcharts load event and update the colors of bubble and legend item after the initial load:

Code: Select all

hc_chart(events = list(load = JS("function () {
   this.series.forEach(function (series) {
    series.update({
      color: series.data[0].color
    }, false);
   });
   this.redraw();
  }"))) %>%
Under this URL you can find a full example of adding such function as a string to the chart config: https://stackoverflow.com/questions/622 ... 1#62262591

Let me know if this what you were looking for.
Best regards!

Re: Same color element of the legend and the graph bubble

Posted: Fri Jun 09, 2023 7:44 pm
by JoséCarlosMartínez
Checked and works perfectly.

Thanks for your help.
Greetings.