内联转储自定义 LINQPad 图表

Dumping a customized LINQPad chart inline

如果我不在 LINQPad 中自定义图表,可以使用 linqpadChart.DumpInline().

内联转储的方法

是否有类似的内联转储自定义图表的方法,或者像我在本示例中所做的那样创建和转储 Bitmap 的唯一方法?

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();
}

是的,这类似于 DumpInline 在幕后所做的事情,尽管使用的是 WebChart。

这是来自 LINQPadChart 的代码,如果您想将其复制到 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;
    }
}

此方法需要以下命名空间:

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

编辑:更改了方法,使其使用 Windows Forms 图表而不是 Web Forms 图表,因此它也适用于 .NET Core。