Created
December 12, 2011 04:18
-
-
Save lukencode/1464831 to your computer and use it in GitHub Desktop.
Spark-Graphs for .NET. Clone of https://github.com/holman/spark.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace NSpark | |
{ | |
public static class SparkExtensions | |
{ | |
public static String Spark(this string input) | |
{ | |
var numbers = new List<double>(); | |
foreach (var c in input) | |
{ | |
try | |
{ | |
numbers.Add(Double.Parse(c.ToString())); | |
} | |
catch | |
{ | |
//lol char graph | |
numbers.Add((int)c); | |
} | |
} | |
return numbers.Spark(); | |
} | |
public static String Spark(this IEnumerable<double> input) | |
{ | |
double min = input.Min(); | |
double max = input.Max(); | |
double intervalSize = max - min; | |
StringBuilder sb = new StringBuilder(input.Count()); | |
String sparks = "▁▂▃▄▅▆▇"; | |
foreach (var d in input) | |
{ | |
int sparkIndex = (int)((d - min) / intervalSize * (sparks.Length - 1)); | |
sb.Append(sparks[sparkIndex]); | |
} | |
return sb.ToString(); | |
} | |
public static String Spark(this IEnumerable<int> input) | |
{ | |
return input.Cast<double>().Spark(); | |
} | |
public static String Spark(this IEnumerable<decimal> input) | |
{ | |
return input.Cast<double>().Spark(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment