Jeremy Cook
Jeremy Cook

Reputation: 22063

Dumping a customized LINQPad chart inline

If I don't customize a chart in LINQPad there is a method for dumping inline using linqpadChart.DumpInline().

Is there an similar method for dumping a customized chart inline, or is creating and dumping a Bitmap like I've done in this example the only way?

string[] xSeries = { "John", "Mary", "Sue" };
int[] ySeries = { 100, 120, 140 };

var winChart = xSeries.Chart().AddYSeries (ySeries, Util.SeriesType.Pie).ToWindowsChart();

// Make tweaks/customizations:
var area = winChart.ChartAreas.First();
area.Area3DStyle.Enable3D = true;
area.Area3DStyle.Inclination = 50;
winChart.Series.First().Points[2].SetCustomProperty ("Exploded", "true");

// Draw it to a bitmap and then dump it:
using (var bitmap = new Bitmap(500, 500))
{
    winChart.DrawToBitmap(bitmap, new Rectangle(Point.Empty, bitmap.Size));
    bitmap.Dump();
}

Upvotes: 0

Views: 457

Answers (1)

Joe Albahari
Joe Albahari

Reputation: 30934

Yes, this is similar to what DumpInline does behind the scenes, albeit with a WebChart.

Here's the code from LINQPadChart, if you'd like to copy it to My Extensions

public static class MyExtensions
{
    public static Bitmap ToBitmap (this Chart chart, int width = 0, int height = 0)
    {
        if (width <= 0) width = Screen.PrimaryScreen.WorkingArea.Width / 2;
        if (height <= 0) height = width / 2;

        var img = new Bitmap (width, height);
        using (Graphics g = Graphics.FromImage (img))
            chart.Printing.PrintPaint (g, new Rectangle (0, 0, width, height));
        return img;
    }
}

This method requires the following namespaces:

  • System.Drawing
  • System.Windows.Forms
  • System.Windows.Forms.DataVisualization.Charting

Edit: changed the method so that it uses the Windows Forms chart rather than the Web Forms chart, so that it also works in .NET Core.

Upvotes: 1

Related Questions