Skip to content

Commit

Permalink
fix(Legend): fix Legend with None
Browse files Browse the repository at this point in the history
  • Loading branch information
MingboPeng committed Apr 5, 2024
1 parent 20e2326 commit f208541
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
13 changes: 5 additions & 8 deletions src/LadybugDisplaySchema/ManualAdded/Model/LegendParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,7 @@ public List<Color> CalColor(IEnumerable<double> values)
var colorDomins = this.ColorDomains(colors.Count);
Color noneColor = null;
var hasNoneColor = this.HasNoneColor(out noneColor);
var hasNoneInOrdDic = hasNoneColor && ordinalDictionary.ContainsValue(VisualizationData.NoneKey);
if (hasNoneInOrdDic)
{ // remove None from the dictionary, in order to calculate the color based on min/max correctly
max -= 1;
}


return CalColor(values, ordinalDictionary, min, max, colorDomins, colors, noneColor);
}

Expand All @@ -296,10 +291,12 @@ public static Color CalColor(double value, Dictionary<double, string> ordinalDic
var colorStart = colors.First();
var colorEnd = colors.Last();

if (value <= min)
return colorStart;
// check the max first
if (value >= max)
return colorEnd;
if (value <= min)
return colorStart;


var range_p = max.Equals(min) ? 0 : max - min;
var factor = range_p == 0 ? 0 : (value - min) / range_p;
Expand Down
68 changes: 52 additions & 16 deletions src/LadybugDisplaySchema/ManualAdded/Model/VisualizationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public VisualizationData(List<double> results, LegendParameters legend = default
/// <param name="categorizedLegend">This is currently only used in the GlobalRenderer for ColorByProperties</param>
public VisualizationData(List<string> results, LegendParameters legend, bool categorizedLegend = false) : this()
{

if (!categorizedLegend && legend != null) legend.OrdinalDictionary = null;

this.LegendParameters = legend;
var isNumber = IsNumberList(results, out var nums, out var numBounds); // checks for None case

Expand All @@ -55,11 +58,9 @@ public VisualizationData(List<string> results, LegendParameters legend, bool cat
{
// check the raw numbers with double.NaN
var valueMin = nums.Min();
var valueMax = nums.Max();

// check None case which is set to double.NaN
hasNone = double.IsNaN(valueMin);
allNones = double.IsNaN(valueMax);

// only true numbers can have max and min
if (numBounds.HasValue)
Expand All @@ -81,7 +82,6 @@ public VisualizationData(List<string> results, LegendParameters legend, bool cat
// check nones
var mappers = keyMapper.Values.ToList();
hasNone = mappers.Any(_ => _ == NoneKey);
allNones = mappers.All(_ => _ == NoneKey);

// update the legend
newLegend = UpdateLegendWithTextValues(legend, keyMapper);
Expand All @@ -96,7 +96,7 @@ public VisualizationData(List<string> results, LegendParameters legend, bool cat
newLegend = newLegend.AddUserData("_unit", this.Unit);

// set none color to legend
if (hasNone && !allNones)
if (hasNone)
{
newLegend = newLegend.SetNoneColor(newLegend.GetNoneColorWithDefault());
}
Expand Down Expand Up @@ -176,9 +176,9 @@ public static LegendParameters UpdateLegendWithValues(List<double> values, Legen
valueMax = numBounds.Value.max;
}

// update the legend with the real number values except NaNs
if (hasNone && !allNones)
values = values.Where(_ => !double.IsNaN(_)).ToList();
//// update the legend with the real number values except NaNs
//if (hasNone && !allNones)
// values = values.Where(_ => !double.IsNaN(_)).ToList();

var distinctCounts = values.Distinct().Count();
var steps = distinctCounts > 11 ? 11 : distinctCounts;
Expand Down Expand Up @@ -233,9 +233,29 @@ public static LegendParameters UpdateLegendWithValues(List<double> values, Legen

public static LegendParameters UpdateLegendWithTextValues(LegendParameters legend, Dictionary<double, string> keyMapper)
{
var min = keyMapper.First().Key;
var max = keyMapper.Last().Key;
var keys = keyMapper.Keys;
var min = keys.Min();
var max = keys.Max();
var steps = keyMapper.Count;

// has none
if (double.IsNaN(min))
{
min = 0;
//steps = steps > 1 ? steps - 1 : 0;
}

// all none
if (double.IsNaN(max))
{
min = 0;
max = 0;
steps = 1;
}

// the min of steps has to be 1
steps = steps == 0 ? 1 : steps;

var legendPar = legend ?? new LegendParameters(min, max, steps);
legendPar = legendPar.DuplicateLegendParameters();

Expand All @@ -258,17 +278,33 @@ public static List<double> CategorizeValues(List<string> results, out Dictionary
// sort keys
var comparer = new StringComparer();
gps = gps.OrderBy(_ => _.Key, comparer).ToList();
var steps = gps.Count;
for (int i = 0; i < steps; i++)

var catIndex = 0;
var hasNone = false;
foreach (var gp in gps)
{
var gp = gps[i];
var key = _noneKeys.Contains(gp.Key) ? NoneKey : gp.Key;
keyMapper.Add(i, key);

var isNone = _noneKeys.Contains(gp.Key);
if (isNone)
{
hasNone = true;
continue;
}

var key = gp.Key;
var value = catIndex;
keyMapper.Add(value, key);
foreach (var item in gp)
{
values[item.i] = i;
values[item.i] = value;
}

catIndex++;
}

// add none key at the end
if (hasNone)
{
keyMapper.Add(double.NaN, NoneKey);
}
mapper = keyMapper;
return values;
Expand Down

0 comments on commit f208541

Please sign in to comment.