Jump to content


Photo
- - - - -

More Charting Samples


  • Please log in to reply
No replies to this topic

#1 brianbec

brianbec

    Active Member

  • Members
  • PipPip
  • 12 posts

Posted 02 January 2011 - 09:19 PM

for your amusement (EDIT: by the way, there is a version of the static-dynamic joke in here smile.gif)

CODE
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Drawing.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Windows.Forms.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>

// Small but meaningful samples of charting
void Main()
{
PieSample();
LineSample();
SplineSample();
}

public static void SplineSample()
{
// This is oddly fascinating . . .
var data = RandomArrayPair(10);
var p = new ChartCapsule(SeriesChartType.Spline);

p.ChartArea.AxisX.Minimum = 0D;
p.ChartArea.AxisX.Maximum = 1D;

p.ChartArea.AxisY.Minimum = 0D;
p.ChartArea.AxisY.Maximum = 1D;

p.Series.Color = Color.Red;
p.Series.MarkerColor = Color.BlueViolet;
p.Series.MarkerStyle = MarkerStyle.Circle;
p.Series.MarkerSize = 8;
p.Series.Points.DataBindXY(data.Xs, data.Ys);
p.Show();
}

public static void LineSample()
{
var data = RandomArrayPair();
var p = new ChartCapsule(SeriesChartType.Line);

// If you don't explicitly set the maximum and minimum of the AxisX,
// the chart class will use the actual max and min of the data,
// which doesn't look great.
p.ChartArea.AxisX.Minimum = 0D;
p.ChartArea.AxisX.Maximum = 1D;

// The AxisY auto-ranging seems a little smarter, but it may only be
// because we've got enough numbers in our 30-element sample to force
// AxisY to 'intelligently' pick 0 and 1. I think it can't hurt to
// set them explicitly, so let's do that:
p.ChartArea.AxisY.Minimum = 0D;
p.ChartArea.AxisY.Maximum = 1D;

// The following produces a messy squiggle all over the unit-square:
p.Series.Points.DataBindXY(data.Xs, data.Ys);
p.Show();

// But, if we sort the pairs, as follows, we get a nice line chart.
var qs = SortedPairs(data.Xs, data.Ys);
p.Series.Points.Clear();
p.Series.Points.DataBindXY(qs.Xs, qs.Ys);
p.Show();
}

public static dynamic RandomArrayPair(int size = 30)
{
// Make a couple of equal-length sequences of random data in
// the interval [0, 1) (closed on left, open on right)
var r = new Random(Guid.NewGuid().GetHashCode());
var xs = Enumerable.Range(0, size).Select(e => r.NextDouble());
var ys = Enumerable.Range(0, size).Select(e => r.NextDouble());
return new {Xs = xs.ToArray(), Ys = ys.ToArray()};
}

public static dynamic SortedPairs<T>(IEnumerable<T> xs, IEnumerable<T> ys)
{
var ps = xs.Zip(ys, (x, y) => new {X = x, Y = y}).OrderBy (e => e.X);
return new {Xs = ps.Select(e => e.X).ToArray(),
Ys = ps.Select(e => e.Y).ToArray()};
}

public static void PieSample()
{
var p = new ChartCapsule(SeriesChartType.Pie);
p.Series.CustomProperties = "LabelsRadialLineSize=1, PieDrawingStyle=Concave, LabelStyle=outside";

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

p.Show();
}

public class ChartCapsule
{
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 ChartCapsule(SeriesChartType chartType = SeriesChartType.Bar)
{
this.Chart = new Chart();

this.ChartArea = new ChartArea();
this.Chart.ChartAreas.Add(this.ChartArea);

this.Series = new Series();
this.Series.ChartType = chartType;
this.Chart.Series.Add(this.Series);

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

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

static IEnumerable<string> CharRange(Char c, int length)
{
return (from e in Enumerable.Range(Convert.ToInt32©, length)
select Char.ConvertFromUtf32(e));
}

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, 02 January 2011 - 09:21 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users