Skip to content

Commit

Permalink
fix(Legend): support both None and N/A
Browse files Browse the repository at this point in the history
  • Loading branch information
MingboPeng committed Apr 6, 2024
1 parent 53921a3 commit 22520fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static bool IsNone(double value, Dictionary<double,string> ordinalDictio

// check the key mapper to see if the value matches any of key mapper which is None
if (ordinalDictionary!= null && ordinalDictionary.TryGetValue(value, out var name))
return name == VisualizationData.NoneKey;
return name == VisualizationData.NAKey;

return false;
}
Expand Down
41 changes: 27 additions & 14 deletions src/LadybugDisplaySchema/ManualAdded/Model/VisualizationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace LadybugDisplaySchema

public partial class VisualizationData
{
private static readonly string[] _noneKeys = new string[] { "None", "N/A", "NaN", string.Empty, null };
public static readonly string NoneKey = "N/A";
private static readonly string[] _naKeys = new string[] { "N/A", "NaN", string.Empty, null };
public static readonly string NAKey = "N/A";
public static readonly string NoneKey = "None";

/// <summary>
/// Possible max value while removing nones values. Max will be double.NaN if no numerical value found.
Expand Down Expand Up @@ -50,8 +51,6 @@ public VisualizationData(List<string> results, LegendParameters legend, bool cat
var isNumber = IsNumberList(results, out var nums, out var numBounds); // checks for None case

var hasNone = false;
var allNones = false;


LegendParameters newLegend;
if (isNumber && !categorizedLegend)
Expand Down Expand Up @@ -81,7 +80,7 @@ public VisualizationData(List<string> results, LegendParameters legend, bool cat

// check nones
var mappers = keyMapper.Values.ToList();
hasNone = mappers.Any(_ => _ == NoneKey);
hasNone = mappers.Any(_ => _ == NAKey);

// update the legend
newLegend = UpdateLegendWithTextValues(legend, keyMapper);
Expand Down Expand Up @@ -121,7 +120,7 @@ public LegendParameters ValidLegendParameters
/// <returns></returns>
public static bool IsNumberList(List<string> results, out List<double> values, out (double min, double max)? numBounds)
{
var gp = results.GroupBy(_ => _noneKeys.Contains(_));
var gp = results.GroupBy(_ => _naKeys.Contains(_));
var hasNone = (gp.FirstOrDefault(_ => _.Key)?.Any()).GetValueOrDefault();
// check if all the rest values are numbers. If an empty list found, consider it as true to the rest value is number.
var allRestNums = (gp.FirstOrDefault(_ => !_.Key)?.All(r => double.TryParse(r, out _))).GetValueOrDefault(true);
Expand Down Expand Up @@ -275,18 +274,32 @@ public static List<double> CategorizeValues(List<string> results, out Dictionary
var keyMapper = new Dictionary<double, string>();
var gps = results.Select((_, i) => new { _, i }).GroupBy(_ => _._).ToList();

// deal with None case first
var noneGp = gps.FirstOrDefault(_=>_.Key == NoneKey);
var noNoneGps = gps.Where(_ => _.Key != NoneKey);

var catIndex = 0;
if (noneGp!= null)
{
keyMapper.Add(catIndex, NoneKey);
foreach (var item in noneGp)
{
values[item.i] = catIndex;
}
catIndex++;
}

// sort keys
var comparer = new StringComparer();
gps = gps.OrderBy(_ => _.Key, comparer).ToList();
gps = noNoneGps.OrderBy(_ => _.Key, comparer).ToList();

var catIndex = 0;
var hasNone = false;
var hasNA = false;
foreach (var gp in gps)
{
var isNone = _noneKeys.Contains(gp.Key);
if (isNone)
var isNA = _naKeys.Contains(gp.Key);
if (isNA)
{
hasNone = true;
hasNA = true;
continue;
}

Expand All @@ -302,9 +315,9 @@ public static List<double> CategorizeValues(List<string> results, out Dictionary
}

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

0 comments on commit 22520fd

Please sign in to comment.