1π
β
-
Iterate each element in
BarDataExample.SimpleBar
array, and assign each elementβsBackgroundColor
andBorderColor
values according todataItem.Value.Value
. -
Select the values of
BackgroundColor
andBorderColor
from theBarDataExample.SimpleBar
and assign them to the config forBackgroundColor
andBorderColor
ofBarDataset
instance respectively.
foreach (var dataItem in BarDataExample.SimpleBar)
{
if (dataItem.Value.HasValue)
{
if (dataItem.Value.Value >= (decimal)0m
&& dataItem.Value.Value < (decimal)1.25m)
{
dataItem.BackgroundColor = "#008000";
dataItem.BorderColor = "#008000";
}
else if (dataItem.Value.Value >= (decimal)1.25m
&& dataItem.Value.Value < (decimal)2.5m)
{
dataItem.BackgroundColor = "#FFA500";
dataItem.BorderColor = "#FFA500";
}
else if (dataItem.Value.Value >= (decimal)2.5
&& dataItem.Value.Value < (decimal)3.75m)
{
dataItem.BackgroundColor = "#ff0000";
dataItem.BorderColor = "#ff0000";
}
else if (dataItem.Value.Value >= (decimal)3.75m
&& dataItem.Value.Value < (decimal)5m)
{
dataItem.BackgroundColor = "#000000";
dataItem.BorderColor = "#000000";
}
else
{
dataItem.BackgroundColor = "#D3D3D3";
dataItem.BorderColor = "#D3D3D3";
}
}
}
_config1.Data.Datasets.Add(new BarDataset()
{
Label = "Value",
Data = BarDataExample.SimpleBar.Select(l => l.Value).ToList(),
BackgroundColor = BarDataExample.SimpleBar.Select(l => l.BackgroundColor).ToList(),
BorderColor = BarDataExample.SimpleBar.Select(l => l.BorderColor).ToList(),
BorderWidth = 1
});
Demo
Source:stackexchange.com