如果我不在 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
的代码,如果你想把它复制到我的扩展
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.Windows.Forms
- System.Windows.Forms.DataVisualization.Chartting
编辑:更改了方法,使其使用 Windows 窗体图表而不是 Web 窗体图表,以便它也可以在 .NET Core 中工作。