NPlot C#(開發者的工作原理)
本教程專為初學者設計,旨在探索兩個強大程式庫的整合:NPlot和IronPDF。 它們一起形成了一個強大的工具包,用於在C#應用程式中建立和匯出圖表。
NPlot是一個在.NET框架中的多功能製圖程式庫,適合生成各種圖形和圖表。 從簡單的線圖到複雜的散佈圖,NPlot在顯示基於樣本的資料和生成動態圖表方面都表現出色,不論您是在處理小型資料集還是非常大型的資料集。
IronPDF通過將這些圖表轉換為PDF文件來補充NPlot。 無論您正在處理HTML電子郵件內容還是特定的圖類,IronPDF都能將它們呈現為高質量的PDF。
此功能特別適用於需要生成報告或記錄已分析資料的應用程式。
開始使用NPlot
.NET框架中的NPlot簡介
NPlot是一個動態製圖程式庫,專為.NET框架設計,滿足廣泛的資料可視化需求。 無論您是在開發桌面應用程式還是基於網頁的解決方案,NPlot都提供了直觀、清晰且有效的資料圖形表示功能。
在您的C#專案中安裝NPlot
要在您的C#專案中開始使用NPlot,您需要先安裝它。 以下是如何輕鬆將NPlot新增到您的應用程式中:
使用NuGet套件管理器:
- 在Visual Studio中,前往"工具">"NuGet套件管理器">"管理方案的NuGet套件..."。
- 搜尋'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提供了一系列更複雜的圖表型別,以增強您的資料可視化能力。 這些包括長條圖、散佈圖和階梯圖,各自適合於不同型別的資料表示。
利用長條圖和散佈圖
長條圖: 完美適合顯示來自一個或多個類別中的資料值。 每個長條代表一個資料值,其高度表示值的大小。
散佈圖: 非常適合可視化每個資料點獨立的資料集。 它在二維圖上繪製資料點,允許分析模式或趨勢。
實現階梯圖
階梯圖: 用於涉及連續橫坐標值的資料,例如時間序列資料。 它建立了階梯狀表示,清楚地顯示了連續資料點之間的變化。
將NPlot與IronPDF整合
將NPlot與IronPDF整合允許將圖表無縫轉換為PDF文件。 IronPDF是一個強大的程式庫,能夠將HTML內容和圖類渲染為高質量的PDF文件。 這種整合對於需要生成報告或記錄已分析資料的應用程式特別有用。
開始使用IronPDF
安裝IronPDF程式庫
使用NuGet Package Manager安裝
要在您的NPlot C#專案中使用NuGet套件管理器整合IronPDF,請遵循這些步驟:
- 打開 Visual Studio 並在方案總管中右鍵點擊您的專案。
- 從上下文選單中選擇"管理 NuGet 套件…"。
- 前往瀏覽選項卡並搜尋 IronPDF。
- 從搜索結果中選擇IronPDF程式庫,然後點擊安裝按鈕。
- 接受任何授權協議提示。
如果您想通過套件管理器控制台將IronPDF包含在您的專案中,請在套件管理器控制台中執行以下命令:
Install-Package IronPdf
這將提取並安裝 IronPDF 到您的專案中。
使用NuGet網站安裝
有關IronPDF的詳細概覽,包括其功能、相容性及其他下載選項,請存取NuGet網站上的IronPDF頁面: https://www.nuget.org/packages/IronPdf。
通過DLL安裝
或者,您可以直接將 IronPDF 整合到您的專案中,使用其 dll 文件。從 IronPDF 下載頁面下載包含 DLL 的 ZIP 文件。 解壓縮它,並將DLL包含到您的專案中。
使用NPlot生成動態圖表
NPlot在建立C#應用程式中的動態和視覺上引人注目的圖表方面表現出色。 本節將指導您生成一個散佈圖,一個典型的用例是顯示具有兩個變數的資料。
散佈圖在可視化變數之間的關係方面特別有效。 請按照以下步驟建立散佈圖:
- 初始化圖表畫布: 首先建立一個
PlotSurface2D實例。 - 準備資料: 收集您希望繪製的資料值。 散佈圖繪製單個點,因此您需要兩個值的陣列:一個用於x坐標,另一個用於y坐標。 您可以在
PlotSurface2D中新增任意多的圖表。 - 實例化散佈圖: 使用NPlot的
ScatterPlot類通過您的圖表物件建立您的圖表。 - 自定義圖表: 使用各種自定義選項,如設置點樣式、顏色和軸屬性,使圖表資訊豐富且引人注目。
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:
- 將圖表渲染為圖像: 首先,將您的NPlot圖表轉換為圖像格式。 這可以通過將
PlotSurface2D畫到位圖上完成。 - 使用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#開發者提供了全面的工具包,允許他們有效地可視化資料,然後無縫地將這些資料轉化為可共享和存檔的格式。
從$999開始使用IronPDF的免費試用授權。
常見問題
什麼是NPlot以及它在C#中的用途?
NPlot是.NET框架中的一個多功能繪圖程式庫,用於在C#中生成各種圖形和繪圖。它非常適合用簡單的線圖到複雜的散點圖來視覺化大小資料集。
如何在C#專案中安裝NPlot?
您可以使用Visual Studio中的NuGet包管理器在C#專案中安裝NPlot。導航至“工具”>“NuGet包管理器”>“管理解決方案的NuGet包...”,搜索“NPlot”並繼續安裝。
如何使用NPlot在C#中建立圖表?
要使用NPlot建立圖表,首先初始化一個PlotSurface2D物件,然後用您的資料建立一個LinePlot,並將其新增到繪圖表面。客製化標題、標籤,然後渲染圖表。
NPlot中有哪些高級繪圖技術?
NPlot提供了高級繪圖技術,如條形圖、散點圖和步驟圖,允許您有效地以各種視覺化格式表示資料。
如何將IronPDF與NPlot圖表一起使用?
IronPDF可以將NPlot建立的圖表轉換為高質量的PDF文件,這對於生成報告或以可共享格式記錄資料分析非常有用。
如何將IronPDF新增到我的C#專案中?
要將IronPDF新增到您的專案中,請使用Visual Studio中的NuGet包管理器安裝IronPDF,或從IronPDF網站下載DLL並手動將其包含在專案中。
如何使用IronPDF將NPlot圖表轉換為PDF?
首先,將您的NPlot圖表渲染為圖像。然後,使用IronPDF的API建立PDF文件並插入圖表圖像。此過程使您能夠輕鬆地將圖像文件轉換為PDF格式。
使用NPlot和IronPDF的優點是什麼?
使用NPlot與IronPDF可以讓開發者建立詳細的動態圖表,然後將其轉換為PDF進行報告和文件化,有效地結合資料視覺化和存檔能力。
IronPDF是否有供開發者使用的試用版本?
是的,IronPDF提供免費試用授權,使開發者能夠在承諾購買完整授權之前探索其功能和能力。




