使用 ScottPlot 在 .NET WinForms 中快速實(shí)現(xiàn)大型數(shù)據(jù)集的交互式顯示!
前言
在.NET應(yīng)用開發(fā)中數(shù)據(jù)集的交互式顯示是一個(gè)非常常見的功能,如需要?jiǎng)?chuàng)建折線圖、柱狀圖、餅圖、散點(diǎn)圖等不同類型的圖表將數(shù)據(jù)呈現(xiàn)出來,幫助人們更好地理解數(shù)據(jù)、發(fā)現(xiàn)規(guī)律,并支持決策和溝通。本文我們將一起來學(xué)習(xí)一下如何使用ScottPlot庫在.NET WinForms中快速實(shí)現(xiàn)大型數(shù)據(jù)集的交互式顯示。
ScottPlot類庫介紹
ScottPlot是一個(gè)免費(fèi)、開源(采用MIT許可證)的強(qiáng)大.NET交互式繪圖庫,能夠輕松地實(shí)現(xiàn)大型數(shù)據(jù)集的交互式顯示。使用幾行代碼即可快速創(chuàng)建折線圖、柱狀圖、餅圖、散點(diǎn)圖等不同類型的圖表。

ScottPlot類庫支持平臺(tái)和框架
Console Application、WinForms、WPF、Avalonia、Blazor、WinUI等多個(gè)平臺(tái)和框架。
ScottPlot類庫源代碼

新建WinForms項(xiàng)目
新建一個(gè)名為ScottPlotWinFormsExercise的項(xiàng)目。




安裝ScottPlot.WinForms包
搜索ScottPlot.WinForms包安裝:

折線圖實(shí)現(xiàn)
創(chuàng)建名為:LineChart窗體。


將FormsPlot (ScottPlot.WinForms)從工具箱拖到窗體中:

輸入以下代碼:
public partial class LineChart : Form
{
public LineChart()
{
double[] dataX = GetRandomNum(20).Distinct().OrderByDescending(x => x).ToArray();
double[] dataY = GetRandomNum(19).Distinct().OrderByDescending(x => x).ToArray();
formsPlot1.Plot.Add.Scatter(dataX, dataY);
formsPlot1.Refresh();
}
public double[] GetRandomNum(int length)
{
double[] getDate = new double[length];
Random random = new Random(); //創(chuàng)建一個(gè)Random實(shí)例
for (int i = 0; i < length; i++)
{
getDate[i] = random.Next(1, 100); //使用同一個(gè)Random實(shí)例生成隨機(jī)數(shù)
}
return getDate;
}
}運(yùn)行效果展示:

柱狀圖實(shí)現(xiàn)
創(chuàng)建名為:BarChart窗體。

將FormsPlot (ScottPlot.WinForms)從工具箱拖到窗體中:

輸入以下代碼:
public partial class BarChart : Form
{
public BarChart()
{
double[] values = { 5, 10, 7, 13, 22, 18, 33, 16 };
formsPlot1.Plot.Add.Bars(values);
formsPlot1.Refresh();
}
}運(yùn)行效果展示:

餅圖實(shí)現(xiàn)
創(chuàng)建名為:PieChart窗體。

將FormsPlot (ScottPlot.WinForms)從工具箱拖到窗體中:

輸入以下代碼:
public partial class PieChart : Form
{
public PieChart()
{
double[] values = { 3, 2, 8, 4, 8, 10 };
formsPlot1.Plot.Add.Pie(values);
formsPlot1.Refresh();
}
}運(yùn)行效果展示:

散點(diǎn)圖實(shí)現(xiàn)
創(chuàng)建名為:ScatterChart窗體。

將FormsPlot (ScottPlot.WinForms)從工具箱拖到窗體中:

輸入以下代碼:
public partial class ScatterChart : Form
{
public ScatterChart()
{
//從原始數(shù)據(jù)開始
double[] xs = Generate.Consecutive(100);
double[] ys = Generate.NoisyExponential(100);
//對(duì)數(shù)據(jù)進(jìn)行對(duì)數(shù)縮放,并處理負(fù)值
double[] logYs = ys.Select(Math.Log10).ToArray();
//將對(duì)數(shù)縮放的數(shù)據(jù)添加到繪圖中
var sp = formsPlot1.Plot.Add.Scatter(xs, logYs);
sp.LineWidth = 0;
//創(chuàng)建一個(gè)次要刻度生成器,用于放置對(duì)數(shù)分布的次要刻度
ScottPlot.TickGenerators.LogMinorTickGenerator minorTickGen = new();
//創(chuàng)建一個(gè)數(shù)值刻度生成器,使用自定義的次要刻度生成器
ScottPlot.TickGenerators.NumericAutomatic tickGen = new();
tickGen.MinorTickGenerator = minorTickGen;
//創(chuàng)建一個(gè)自定義刻度格式化程序,用于設(shè)置每個(gè)刻度的標(biāo)簽文本
static string LogTickLabelFormatter(double y) => $"{Math.Pow(10, y):N0}";
//告訴我們的主要刻度生成器僅顯示整數(shù)的主要刻度
tickGen.IntegerTicksOnly = true;
//告訴我們的自定義刻度生成器使用新的標(biāo)簽格式化程序
tickGen.LabelFormatter = LogTickLabelFormatter;
//告訴左軸使用我們的自定義刻度生成器
formsPlot1.Plot.Axes.Left.TickGenerator = tickGen;
//顯示次要刻度的網(wǎng)格線
var grid = formsPlot1.Plot.GetDefaultGrid();
grid.MajorLineStyle.Color = Colors.Black.WithOpacity(.15);
grid.MinorLineStyle.Color = Colors.Black.WithOpacity(.05);
grid.MinorLineStyle.Width = 1;
formsPlot1.Refresh();
}
}運(yùn)行效果展示:

項(xiàng)目演示入口

private void Btn_ScatterChart_Click(object sender, EventArgs e)
{
ScatterChart formScatterChart = new ScatterChart();
// 顯示目標(biāo)窗體
formScatterChart.Show();
}
private void Btn_PieChart_Click(object sender, EventArgs e)
{
PieChart formPieChart = new PieChart();
// 顯示目標(biāo)窗體
formPieChart.Show();
}
private void Btn_BarChart_Click(object sender, EventArgs e)
{
BarChart formbarChart = new BarChart();
// 顯示目標(biāo)窗體
formbarChart.Show();
}
private void Btn_LineChart_Click(object sender, EventArgs e)
{
LineChart formLineChart = new LineChart();
// 顯示目標(biāo)窗體
formLineChart.Show();
}






























