Photo
- - - - -

Charting In Linqpad


  • Please log in to reply
2 replies to this topic

#1 brianbec

brianbec

    Active Member

  • Members
  • PipPip
  • 12 posts

Posted 31 December 2010 - 10:04 AM

Here's a really tiny example of charting via LinqPad via the Forms.DataVisualization DLL. It's got a lot of stuff; here's a quickstart. EDIT: for official dox, see code.msdn.microsoft.com/mschart

CODE

<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Drawing.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Windows.Forms.dll</Reference>
<Reference>&lt;ProgramFiles&gt;\Microsoft Cloud Programmability\Reactive Extensions\v1.0.2787.0\Net4\System.CoreEx.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Windows.Forms.DataVisualization.dll</Reference>
<Namespace>System.Drawing</Namespace>
<Namespace>System.Windows.Forms</Namespace>
<Namespace>System.Windows.Forms.DataVisualization.Charting</Namespace>
</Query>

// Almost the smallest meaningful example of Charting
void Main()
{
// Chart must have a chart area, but it's not externally referenced later
var chartArea1 = new ChartArea();
var chart1 = new Chart();
chart1.ChartAreas.Add(chartArea1);

var series1 = new Series();

// The following goes beyond the minimal, but just a little. You can delete these two lines.
// Fun to set the series ChartType; default is column chart
series1.ChartType = SeriesChartType.Pie;
series1.CustomProperties = "LabelsRadialLineSize=1, PieDrawingStyle=Concave, LabelStyle=outside";

var r = new Random(Guid.NewGuid().GetHashCode());
var ys = Enumerable.Range(0, 5).Select (e => r.NextDouble()).Dump("Doubles");
var xs = Enumerable.Range(0, 5).Select (e => GetRandomString(3).ToUpper()).Dump("Strings");
series1.Points.DataBindXY(xs.ToArray(), ys.ToArray());
chart1.Series.Add(series1);

var frm = new Form();
// Seems 300 x 300 is the default chart-area size and chart size, so set the form to hold it
frm.ClientSize = new Size(width: 300, height: 300);
frm.Controls.Add(chart1);

Application.Run(frm);
}

static string GetRandomString(int length)
{
var sb = new StringBuilder();
do
sb.Append(Path.GetRandomFileName().Replace(".", "").Substring(0, length < 11 ? length : 11));
while ((length -= 11) > 0);
return sb.ToString();
}

Edited by brianbec, 31 December 2010 - 10:10 AM.


#2 JoeAlbahari

JoeAlbahari

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 529 posts
  • Gender:Male
  • Location:Perth, Australia

Posted 01 January 2011 - 05:41 PM

This is great.

You can enhance it further in the latest version of LINQPad which lets you dump bitmaps. So rather than creating a form and showing it, you can do the following:

CODE
var b = new Bitmap (chart1.Width, chart1.Height);
chart1.DrawToBitmap (b, chart1.Bounds);
b.Dump();


#3 brianbec

brianbec

    Active Member

  • Members
  • PipPip
  • 12 posts

Posted 01 January 2011 - 05:54 PM

Superb! More results for less code & complications! Just when you think it can't get any better smile.gif

I'm already hooked on the new Reflector integration.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

"