Tag Archives: LinqPad

Histograms and LinqPad

As an avid user of stackoverflow I find myself reaching for LinqPad when I want to write and test a snippet of code quickly.  LinqPad is billed as a great way to query databases and data sources with linq, but its is also one of the best C# scratchpads available. The basic version is free and fully functional, but there’s an upsell for autocompletion, smart tags, code outlining and .Net reflector in the Pro edition. The Premium edition includes code snippets and the ability to execute queries across multiple databases in Microsoft SQL Server. You can find out more about LinqPad at http://www.linqpad.net 

Recently LinqPad added support for custom ‘visualizers’ as a way to display queried data, and I thought that it would be a great to be able to add a front-end to the histogram control found in NumSkull.

The custom visualizer support is currently in beta (as of 3/25/2012), so you’ll need to download the beta version until the new version is released. You’ll also need to download and compile NumSkull from https://bitbucket.org/skyguy94/numskull.

Once you have the tools in-hand, start up LinqPad and create a new query. From the Query menu, select query properties (or press F4 from the query window). The Query Properties window should appear. You’ll need to add references to the following assemblies (if you miss one, a detailed error window will appear and it’ll be obvious which is missing).

QueryDialog

Once you’ve configured the references, creating a histogram is similar to programmatically creating a standard control in C#:

var histogram = new LinqPadVisualizers.HistogramVisualizer();
var data = new NumSkull.Histogram(new[] { 0d, 5, 10, 15, 20 });

The first line creates an instance of the histogram visualizer, and the second line creates an instance of the underlying histogram control with five evenly spaced bins. You connect the two by means of a dependency property on the visualizer, but the next step is to add some data to the histogram control.

var deltas = new[] { 195d,20,2,17,3,54,2,18,1065,45,34 };
data.Add(deltas);
LinqPadVisualizers.HistogramVisualizer.SetBins(histogram, data);

The last line uses the dependency property to assign the data to the visualizer. The final step is to tell LinqPad to show the control. Here’s the entire snippet:

var histogram = new LinqPadVisualizers.HistogramVisualizer();
var data = new NumSkull.Histogram(new[] { 0d, 5, 10, 15, 20 });
var deltas = new[] { 195d,20,2,17,3,54,2,18,1065,45,34 };
data.Add(deltas);
LinqPadVisualizers.HistogramVisualizer.SetBins(histogram, data);
PanelManager.DisplayWpfElement(histogram, "My Histogram"); 

The call to PanelManager.DisplayWpfElement is needed to tell LinqPad to display the visualizer. The first argument is the instance of the visualizer and the second argument is its title which will appear in the results window.

This is a sample of typical output (this is from a different, and much larger dataset):

Graph

You can find more information on creating LinqPad visualizers from the LinqPad website.

Enjoy!