NPlot C#(開發者的工作原理)
本教學專為熱衷於探索整合兩個強大程式庫的初學者所設計:NPlot 和 IronPDF。 它們共同組成了一個強大的工具包,用於在 C# 應用程式中建立和匯出圖表。
NPlot 是 .NET Framework 中的多用途圖表庫,是產生各種圖表和圖形的理想工具。 從簡單的線圖到複雜的散點圖,無論您是處理小型資料集或超大型資料集,NPlot 都能出色地顯示基於樣本的資料並產生動態圖表。
IronPDF 與 NPlot 相輔相成,可將這些圖表轉換成 PDF 文件。 無論您處理的是 HTML 電子郵件內容或特定的繪圖類,IronPDF 都能將它們渲染成高品質的 PDF。
此功能對於需要產生報告或記錄分析資料的應用程式特別有用。
開始使用 NPlot
.NET Framework 中的 NPlot 入門
NPlot 是專為 .NET Framework 設計的動態圖表庫,可滿足廣泛的資料可視化需求。 無論您是在開發桌面應用程式或網頁型解決方案,NPlot 都能提供圖形化、清晰且有效地表達資料的功能。
在您的 C# 專案中安裝 NPlot。
要開始在 C# 專案中使用 NPlot,您需要先安裝它。 以下是如何輕鬆將 NPlot 加入您的應用程式:
使用 NuGet 套件管理員:
1.在 Visual Studio 中,前往"工具">"NuGet 套件管理員">"管理解決方案的 NuGet 套件..."。 2.搜尋"NPlot"並安裝到您的專案中。

使用 NPlot 的第一步
安裝 NPlot 後,您就可以開始建立圖表。 NPlot 的易用性使其非常適合初學者,只需幾行程式碼即可建立圖形。
建立基本圖表
讓我們建立一個簡單的線圖作為第一張圖表:
設定圖面:建立一個 PlotSurface2D 物件。 這可作為您繪圖的畫布。 設定一些顯示屬性以自訂其外觀,例如背景顏色和標題。
將資料加入圖形:使用 NPlot 的 LinePlot 類建立線圖形。 新增屬於一個或多個類別的資料值。 這些資料點將會繪製在圖表上。
顯示圖表:將線圖新增至繪圖面。 將繪圖面渲染成表格或使用者控制項以供顯示。
using System;
using NPlot;
class Program
{
static void Main()
{
// Create a new bitmap plot surface
var plotSurface = new NPlot.Bitmap.PlotSurface2D(800, 600);
// Create a line plot
var linePlot = new LinePlot
{
AbscissaData = new double[] { 1, 2, 3, 4, 5 },
OrdinateData = new double[] { 1, 4, 9, 16, 25 }
};
// Add the line plot to the plot surface
plotSurface.Add(linePlot);
// Customize the plot (e.g., titles, labels)
plotSurface.Title = "Sample Plot";
plotSurface.XAxis1.Label = "X-Axis";
plotSurface.YAxis1.Label = "Y-Axis";
// Refresh the plot to render it
plotSurface.Refresh();
// Save the plot as a PNG image
plotSurface.Bitmap.Save("c://plot.png", System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine("Plot saved as plot.png");
}
}using System;
using NPlot;
class Program
{
static void Main()
{
// Create a new bitmap plot surface
var plotSurface = new NPlot.Bitmap.PlotSurface2D(800, 600);
// Create a line plot
var linePlot = new LinePlot
{
AbscissaData = new double[] { 1, 2, 3, 4, 5 },
OrdinateData = new double[] { 1, 4, 9, 16, 25 }
};
// Add the line plot to the plot surface
plotSurface.Add(linePlot);
// Customize the plot (e.g., titles, labels)
plotSurface.Title = "Sample Plot";
plotSurface.XAxis1.Label = "X-Axis";
plotSurface.YAxis1.Label = "Y-Axis";
// Refresh the plot to render it
plotSurface.Refresh();
// Save the plot as a PNG image
plotSurface.Bitmap.Save("c://plot.png", System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine("Plot saved as plot.png");
}
}Imports System
Imports NPlot
Friend Class Program
Shared Sub Main()
' Create a new bitmap plot surface
Dim plotSurface = New NPlot.Bitmap.PlotSurface2D(800, 600)
' Create a line plot
Dim linePlot As New LinePlot With {
.AbscissaData = New Double() { 1, 2, 3, 4, 5 },
.OrdinateData = New Double() { 1, 4, 9, 16, 25 }
}
' Add the line plot to the plot surface
plotSurface.Add(linePlot)
' Customize the plot (e.g., titles, labels)
plotSurface.Title = "Sample Plot"
plotSurface.XAxis1.Label = "X-Axis"
plotSurface.YAxis1.Label = "Y-Axis"
' Refresh the plot to render it
plotSurface.Refresh()
' Save the plot as a PNG image
plotSurface.Bitmap.Save("c://plot.png", System.Drawing.Imaging.ImageFormat.Png)
Console.WriteLine("Plot saved as plot.png")
End Sub
End Class以下是輸出的繪圖影像:

使用 NPlot 的進階製圖技術
在掌握基本圖形之後,NPlot 提供一系列更複雜的圖表類型,以增強您的資料可視化能力。 這些工具包括柱狀圖、散點圖和階梯圖,各自適合不同類型的資料表示。
使用條狀圖和散點圖
Bar Plot:用於顯示一個或多個類別中的資料值的理想工具。 每個橫條代表一個資料值,其高度表示該值的大小。
Scatter Plot(散點圖):非常適合將每個資料點都是獨立的資料集視覺化。 它可將資料點描繪在二維圖形上,以便分析模式或趨勢。
實作階梯圖
階梯圖:用於涉及連續缺角值的資料,例如時間序列資料。 它創造了類似階梯的表示方式,清楚地顯示出連續資料點之間的變化。
將 NPlot 與 IronPDF 整合。
NPlot 與 IronPDF 的整合可將圖表無縫轉換成 PDF 文件。 IronPDF 是一個功能強大的函式庫,可將 HTML 內容和圖形類渲染成高品質的 PDF 檔案。 這種整合對需要產生報告或記錄分析資料的應用程式特別有用。
開始使用 IronPdf
安裝 IronPdf Library
使用 NuGet 套件管理員安裝
若要使用 NuGet Package Manager 將 IronPDF 整合至您的 NPlot C# 專案,請遵循下列步驟:
1.開啟 Visual Studio,在解決方案總管中,用滑鼠右鍵按一下專案。 2.從上下文功能表中選擇"管理 NuGet 套件..."。 3.前往瀏覽標籤,搜尋 IronPdf。 4.從搜尋結果中選擇 IronPDF 函式庫,然後按一下安裝按鈕。 5.接受任何許可協議提示。
如果您想透過套件管理員控制台將 IronPDF 包含在專案中,請在套件管理員控制台執行下列指令:
Install-Package IronPdf
它將擷取 IronPDF 並安裝到您的專案中。
使用 NuGet 網站安裝
如需 IronPDF 的詳細概述,包括其功能、相容性和其他下載選項,請造訪 NuGet 網站上的 IronPDF 頁面,網址為 https://www.nuget.org/packages/IronPdf。
透過 DLL 安裝
另外,您也可以使用 IronPDF 的 dll 檔案,直接將 IronPDF 納入您的專案中。從 IronPDF 下載頁面下載包含 DLL 的 ZIP 檔案。 解壓縮,並將 DLL 包含在您的專案中。
使用 NPlot 生成動態圖表
NPlot 擅長於在 C# 應用程式中建立動態且具視覺吸引力的圖表。 本節將引導您產生一個散點圖,這是顯示具有兩個變數的資料的典型用例。
散狀圖對於可視化變數之間的關係特別有效。 按照以下步驟建立散點圖:
1.啟動 Plot Surface:首先建立一個 PlotSurface2D 實例。 2.準備資料:收集您想要繪製的資料值。 散狀圖繪製的是個別的點,因此您需要兩個數值陣列:一個是 x 座標,另一個是 y 座標。 您可以在 PlotSurface2D 中加入任意多的圖形。 3.建立散點圖:使用 NPlot 的 PointPlot 或 ScatterPlot 類與您的圖形物件建立您的圖表。 4.自訂圖表:套用各種自訂功能,例如設定點的樣式、顏色和軸的屬性,讓圖表內容豐富且吸引人。
using NPlot;
class Program
{
static void Main()
{
var plotSurface = new NPlot.Windows.PlotSurface2D();
// Prepare data for the scatter plot
var scatterPlot = new PointPlot
{
AbscissaData = new double[] { /* x-coordinates */ },
OrdinateData = new double[] { /* y-coordinates */ }
};
// Add the scatter plot to the plot surface
plotSurface.Add(scatterPlot);
// Customize the chart and render the plotSurface
plotSurface.Refresh();
}
}using NPlot;
class Program
{
static void Main()
{
var plotSurface = new NPlot.Windows.PlotSurface2D();
// Prepare data for the scatter plot
var scatterPlot = new PointPlot
{
AbscissaData = new double[] { /* x-coordinates */ },
OrdinateData = new double[] { /* y-coordinates */ }
};
// Add the scatter plot to the plot surface
plotSurface.Add(scatterPlot);
// Customize the chart and render the plotSurface
plotSurface.Refresh();
}
}Imports NPlot
Friend Class Program
Shared Sub Main()
Dim plotSurface = New NPlot.Windows.PlotSurface2D()
' Prepare data for the scatter plot
Dim scatterPlot = New PointPlot With {
.AbscissaData = New Double() { },
.OrdinateData = New Double() { }
}
' Add the scatter plot to the plot surface
plotSurface.Add(scatterPlot)
' Customize the chart and render the plotSurface
plotSurface.Refresh()
End Sub
End Class使用 IronPDF 將圖表轉換成 PDF。
使用 NPlot 建立圖表後,您可以使用 IronPDF 將此圖表轉換成 PDF 文件。 這個過程包括將圖表渲染成圖像,然後用 IronPDF 將此圖像嵌入 PDF 中。 您可以按照以下步驟將圖表轉換為 PDF:
1.將圖表渲染為影像:首先,將您的 NPlot 圖表轉換成圖片格式。 您可以將 PlotSurface2D 繪製到位圖上。 2.使用 IronPDF 創建 PDF:使用 IronPDF 的 API 建立新的 PDF 文件並插入圖表影像。
using IronPdf;
class Program
{
static void Main()
{
// Assuming 'chartImagePath' is the path to the Bitmap image of your NPlot chart
var imageFiles = new string[] { "chartImagePath" };
// Convert image files to PDF and save the output
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("Chart.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Assuming 'chartImagePath' is the path to the Bitmap image of your NPlot chart
var imageFiles = new string[] { "chartImagePath" };
// Convert image files to PDF and save the output
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("Chart.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Assuming 'chartImagePath' is the path to the Bitmap image of your NPlot chart
Dim imageFiles = New String() { "chartImagePath" }
' Convert image files to PDF and save the output
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("Chart.pdf")
End Sub
End Class結論

在本教程中,我們探討了 C# 中兩個功能強大的函式庫的整合:NPlot 用於從依賴資料的元素中建立動態、資料驅動的圖表,而 IronPDF 用於將這些圖表轉換成 PDF 文件。
這種組合為 C# 開發人員提供了全面的工具包,讓他們能夠有效地將資料視覺化,然後將資料無縫轉換成可分享的存檔格式。
從 IronPdf 的 免費試用授權開始,可從 $799 取得。
常見問題解答
什麼是 NPlot,在 C# 中如何使用?
NPlot 是 .NET Framework 中的多功能圖表庫,用於在 C# 中產生各種圖表和圖形。從簡單的折線圖到複雜的散點圖,它都是小型和大型資料集視覺化的理想選擇。
如何在 C# 專案中安裝 NPlot?
您可以使用 Visual Studio 中的 NuGet Package Manager 在您的 C# 專案中安裝 NPlot。前往「工具」>「NuGet Package Manager」>「Manage NuGet Packages for Solution...」,搜尋「NPlot」,然後進行安裝。
如何在 C# 中使用 NPlot 建立圖表?
若要使用 NPlot 建立圖表,請初始化 PlotSurface2D 物件,使用您的資料建立 LinePlot 並將其新增至繪圖面。使用標題、標籤自訂它,然後繪製圖表。
NPlot 有哪些進階的圖表繪製技術?
NPlot 提供先進的圖表繪製技術,例如柱狀圖、散點圖和階梯圖,讓您能以各種可視化格式有效地表達資料。
IronPDF 如何與 NPlot 圖表搭配使用?
IronPDF 可以將使用 NPlot 製作的圖表轉換成高品質的 PDF 文件,因此對於以可分享的格式產生報告或記錄資料分析非常有用。
如何將 IronPDF 加入我的 C# 專案?
若要在專案中加入 IronPDF,請使用 Visual Studio 中的 NuGet Package Manager 安裝 IronPDF,或從 IronPDF 網站下載 DLL 並將其手動包含在專案中。
如何使用 IronPDF 將 NPlot 圖表轉換為 PDF?
首先,將您的 NPlot 圖表繪製成圖像。然後,使用 IronPDF 的 API 建立 PDF 文件並插入圖表影像。此流程可讓您輕鬆地將圖像檔轉換為 PDF 格式。
同時使用 NPlot 和 IronPDF 有什麼好處?
將 NPlot 與 IronPDF 搭配使用,可讓開發人員建立詳盡的動態圖表,然後將圖表轉換成 PDF 檔案,用於報告和文件,有效結合資料可視化與歸檔功能。
IronPDF 是否有提供給開發人員的試用版?
是的,我們提供 IronPDF 的免費試用授權,讓開發人員在承諾購買完整授權之前,能夠探索其特色與功能。







