Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color of individual bars in BarDataSet based on value #144

Closed
larschristensen20 opened this issue Aug 26, 2020 · 4 comments
Closed

Color of individual bars in BarDataSet based on value #144

larschristensen20 opened this issue Aug 26, 2020 · 4 comments
Labels
question Further information is requested

Comments

@larschristensen20
Copy link

In Chartjs i can do something like:

myBarChart.datasets[0].bars[0].fillColor = "#2ECC71";
myBarChart.datasets[0].bars[0].highlightFill = "#58D68D";
myBarChart.datasets[0].bars[1].fillColor = "#3498DB";
myBarChart.datasets[0].bars[1].highlightFill = "#5DADE2";

To set the color of individual bars of a bar chart.

I have a bar chart that shows both "real" data and "fake (calculated)" data, and I would like to give users a way to distinguish between each type, thus my need to set a bar one color if "real" and another if "fake". Is it possible to do this in Blazor?

I found that you can supply an array of background colors, however this does not fix my issue.

@larschristensen20 larschristensen20 added the question Further information is requested label Aug 26, 2020
@Joelius300
Copy link
Contributor

Thanks for the issue. This is not implemented the way you mention it but you can always fall back to interop if necessary.

I found that you can supply an array of background colors, however this does not fix my issue.

Could you elaborate? As far as I understand, that would be exactly what you want to use for your scenario.

@larschristensen20
Copy link
Author

Could you elaborate? As far as I understand, that would be exactly what you want to use for your scenario.

I'm sorry I should have been a bit more precise.

My chart features several data sets defined by the level of detail each set provides, where use the AddRange() method to display my data.

So my chart shows a yearly overview (level 1), monthly (level 2), daily (level 3) and hourly (level 4), where a higher level of detail is shown each time a user clicks a bar.

At the fourth level I would like to dynamically set the color of each bar based on a value depicting whether or not the data shown in the bar is "real" or "fake".

However, using the array of background colors appears to me to be static, where each bar in the set would simply be assigned the color based on the array index once created. I haven't been able to update the bar color afterwards. I might have been to quick to assess whether or not this would in fact solve my problem.

I guess I could determine the data count in the current set I'm working on, and create an array of colors, one for each bar, with the correct color, and then produce a "new BarDataSet" supplying the mentioned color array. Any comments or suggestions on that?

Hopefully everything makes sense.

@Joelius300
Copy link
Contributor

Joelius300 commented Aug 28, 2020

I haven't been able to update the bar color afterwards.

Could you show us the code you tried? You should just be able to set a new array with different values, call Update and it should be changed. I have very little time currently and cannot test but as far as I remember one of our samples actually does this.

EDIT:
Although otherwise quite busted, our horizontal bar chart sample demonstrates this.

@larschristensen20
Copy link
Author

Although otherwise quite busted, our horizontal bar chart sample demonstrates this.

Sorry for the late response, I haven't had time to check it out before today.
I ended up doing what you did in the said sample.

// Run through the sorted data and find entries that are "calculated in the system".
for (int i = 0; i < sortedData.Count(); i++) {
  if (sortedData[i].Reading == null) // calculated
 { 
    listColors.Add("#eb3434");
  } else 
  {
    listColors.Add("#0820fc");
  }
}
barSet.BackgroundColor = listColors.ToArray();

_config.Data.Datasets.Add(barSet);

My confusion stemmed from having this code handed over from a colleague, and I was simply too fast with opening an issue, instead of looking at the samples!

Thank you for the assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants