Skip to content

Commit ff7b443

Browse files
committed
Support discontinuities with NaN
1 parent 146dc14 commit ff7b443

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

AsciiChart.Sharp.Tests/Tests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,31 @@ public void TestPlot(double[] series, Options opts, string expected)
187187
38.46 ┤ ││ │││
188188
37.46 ┤ ╰╯ │││
189189
36.45 ┤ ╰╯╰ "
190+
},
191+
new object[] { new[] { 1, 1, double.NaN, 1, 1 }, null, @" 1.00 ┼─╴╶─ " },
192+
new object[] { new[] { double.NaN, 1 }, null, @" 1.00 ┤╶ " },
193+
new object[]
194+
{
195+
new[] { 0, 0, 1, 1, double.NaN, double.NaN, 3, 3, 4 }, null, @"
196+
4.00 ┤ ╭
197+
3.00 ┤ ╶─╯
198+
2.00 ┤
199+
1.00 ┤ ╭─╴
200+
0.00 ┼─╯ "
201+
},
202+
new object[]
203+
{
204+
new[] { 0.1, 0.2, 0.3, double.NaN, 0.5, 0.6, 0.7, double.NaN, double.NaN, 0.9, 1 }, new Options { Height = 9 }, @"
205+
1.00 ┤ ╭
206+
0.90 ┤ ╶╯
207+
0.80 ┤
208+
0.70 ┤ ╭╴
209+
0.60 ┤ ╭╯
210+
0.50 ┤ ╶╯
211+
0.40 ┤
212+
0.30 ┤ ╭╴
213+
0.20 ┤╭╯
214+
0.10 ┼╯ "
190215
}
191216
};
192217
}

AsciiChart.Sharp/AsciiChart.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static string Plot(IEnumerable<double> series, Options options = null)
1919
options = options ?? new Options();
2020

2121
var seriesList = series.ToList();
22-
var min = seriesList.Min();
22+
var min = seriesList.Where(v => !double.IsNaN(v)).Min();
2323
var max = seriesList.Max();
2424

2525
var range = Math.Abs(max - min);
@@ -37,13 +37,28 @@ public static string Plot(IEnumerable<double> series, Options options = null)
3737
ApplyYAxisLabels(resultArray, yAxisLabels, columnIndexOfFirstDataPoint);
3838

3939
var rowIndex0 = Math.Round(seriesList[0] * ratio, MidpointRounding.AwayFromZero) - min2;
40-
resultArray[(int) (rows - rowIndex0)][columnIndexOfFirstDataPoint - 1] = "┼";
40+
if (!double.IsNaN(rowIndex0))
41+
{
42+
resultArray[(int) (rows - rowIndex0)][columnIndexOfFirstDataPoint - 1] = "┼";
43+
}
4144

4245
for (var x = 0; x < seriesList.Count - 1; x++)
4346
{
4447
var rowIndex1 = Math.Round(seriesList[x + 1] * ratio, MidpointRounding.AwayFromZero) - min2;
48+
if (double.IsNaN(rowIndex0) && double.IsNaN(rowIndex1))
49+
{
50+
continue;
51+
}
4552

46-
if (rowIndex0 == rowIndex1)
53+
if (double.IsNaN(rowIndex0))
54+
{
55+
resultArray[(int) (rows - rowIndex1)][x + columnIndexOfFirstDataPoint] = "╶";
56+
}
57+
else if (double.IsNaN(rowIndex1))
58+
{
59+
resultArray[(int) (rows - rowIndex0)][x + columnIndexOfFirstDataPoint] = "╴";
60+
}
61+
else if (rowIndex0 == rowIndex1)
4762
{
4863
resultArray[(int) (rows - rowIndex0)][x + columnIndexOfFirstDataPoint] = "─";
4964
}
@@ -57,9 +72,9 @@ public static string Plot(IEnumerable<double> series, Options options = null)
5772
{
5873
resultArray[(int) (rows - y)][x + columnIndexOfFirstDataPoint] = "│";
5974
}
60-
61-
rowIndex0 = rowIndex1;
6275
}
76+
77+
rowIndex0 = rowIndex1;
6378
}
6479

6580
return ToString(resultArray);

0 commit comments

Comments
 (0)