Skip to content

Instantly share code, notes, and snippets.

@lukencode
Created December 12, 2011 04:18
Show Gist options
  • Save lukencode/1464831 to your computer and use it in GitHub Desktop.
Save lukencode/1464831 to your computer and use it in GitHub Desktop.
Spark-Graphs for .NET. Clone of https://github.com/holman/spark.
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