stephensmitchell
4/17/2018 - 4:55 PM

Quick and easy helper to render chars in LinqPad

Quick and easy helper to render chars in LinqPad #LINQPad

<Query Kind="Program">
  <Reference>&lt;RuntimeDirectory&gt;\System.Windows.Forms.DataVisualization.dll</Reference>
  <Reference>&lt;RuntimeDirectory&gt;\System.Windows.Forms.dll</Reference>
  <Namespace>System.Windows.Forms.DataVisualization.Charting</Namespace>
  <Namespace>System.Drawing</Namespace>
  <Namespace>System.Windows.Forms</Namespace>
</Query>

void Main()
{
	new LpChart(series: GetSeries(), chartType: SeriesChartType.Column, width: 1250).Dump();
	new LpChart(series: GetSeries(), chartType: SeriesChartType.Line, width: 1250).Dump();
	new LpChart(series: GetSeries(), chartType: SeriesChartType.Bar, width: 500).Dump();
}

private ChartSeries GetSeries()
{
	var items = Enumerable.Range(1, 25).Select(x => string.Format("item-{0}", x));

	var rand = new Random(100);
	var prices = items.Select(x => rand.NextDouble());

	return new ChartSeries { XSeries = items.ToList(), YSeries = prices.ToList() };
}

public class ChartSeries
{
	public IList<string> XSeries { get; set; }
	public IList<double> YSeries { get; set; }
}

public class LpChart
{
	public Chart Chart { get; private set; }
	public ChartArea ChartArea { get; private set; }
	public Series Series { get; private set; }
	public Bitmap Bitmap { get; private set; }

	public LpChart(ChartSeries series, SeriesChartType chartType = SeriesChartType.Column, int width = 1200)
	{
		this.Chart = new Chart();
		this.ChartArea = new ChartArea();
		this.Chart.ChartAreas.Add(this.ChartArea);

		this.Series = new Series();
		this.Series.ChartType = chartType;
		this.Series.Points.DataBindXY(series.XSeries.ToArray(), series.YSeries.ToArray());
		this.Chart.Series.Add(this.Series);

		this.Chart.Width = width;

		this.Bitmap = new Bitmap(width: this.Chart.Width, height: this.Chart.Height);
	}

	public void Dump()
	{
		this.Chart.DrawToBitmap(this.Bitmap, this.Chart.Bounds);
		this.Bitmap.Dump();
	}
}