跳過到頁腳內容
.NET幫助

NPlot C#(開發者的工作原理)

本教程專為有興趣探索兩個強大庫的集成的初學者設計:NPlot和IronPDF。 它們共同構成了一個強大的工具包,用於在C#應用中創建和導出圖表。

NPlot是 .NET 框架中的一個多功能圖表庫,非常適合生成各種圖形和圖表。 從簡單的折線圖到複雜的散點圖,NPlot在顯示基於樣本的數據和生成動態圖表方面表現出色,無論你處理的是小數據集還是非常大的數據集。

IronPDF通過將這些圖表轉換為PDF文檔來補充NPlot。 無論你處理的是HTML電子郵件內容還是特定的圖表類別,IronPDF都可以將它們渲染成高質量的PDF。

這一功能特別適用於需要生成報告或分析數據文檔的應用程序。

開始使用NPlot

介紹NPlot在 .NET 框架中的應用

NPlot是一個專門為 .NET 框架設計的動態圖表庫,滿足廣泛的數據可視化需求。 無論你是在開發桌面應用還是基於網絡的解決方案,NPlot都提供了將數據以圖形方式清晰有效地表示的功能。

在你的C#項目中安裝NPlot

要開始在你的C#項目中使用NPlot,你需要安裝它。 以下是輕鬆將NPlot添加到應用程序中的方法:

使用NuGet包管理器:

  1. 在Visual Studio中,轉到 '工具' > 'NuGet包管理器' > '為解決方案管理NuGet包......'。
  2. 搜索 'NPlot' 並安裝到你的項目中。

NPlot C# (開發者如何使用): 圖1

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
$vbLabelText   $csharpLabel

以下是輸出圖像:

NPlot C# (開發者如何使用): 圖2

使用NPlot的高級圖表技術

在掌握基本圖表後,NPlot提供了一系列更複雜的圖表類型,以提升你的數據可視化能力。 這些包括條形圖、散點圖和階躍圖,每種都適合不同類型的數據表示。

利用條形圖和散點圖

條形圖:適合顯示一個或多個類別中的數據值。 每個條形代表一個數據值,其高度表明值的大小。

散點圖:完美適合可視化每個數據點相互獨立的數據集。 它在二維圖上繪製數據點,便於分析模式或趨勢。

實施階梯圖

階梯圖:用於涉及相繼橫坐標值的數據,例如時間序列數據。 它創建了一個階梯式的表示,清晰顯示相繼數據點之間的變化。

將NPlot與IronPDF集成

將NPlot與IronPDF集成可以無縫地將圖表轉換為PDF文檔。 IronPDF是一個功能強大的庫,可將HTML內容和圖表類別渲染成高質量的PDF文件。 這種集成特別適用於需要生成報告或記錄分析數據的應用程序。

開始使用IronPDF


安裝IronPDF庫

通過 NuGet 包管理器安裝

若要使用NuGet包管理器將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 安裝

或者,您可以直接使用其 dll 文件將 IronPDF 合併到您的項目中。從 IronPDF 下載頁面下載包含 DLL 的 ZIP 文件。 解壓縮並將 DLL 包含在您的項目中。

使用NPlot生成動態圖表

NPlot在創建動態且視覺吸引人的C#應用中表現出色。 本節將引導你生成散點圖,這是一種典型的顯示雙變量數據的用例。

散點圖在可視化變量之間的關係方面特別有效。 按照以下步驟創建散點圖:

  1. 初始化繪圖表面:首先創建一個PlotSurface2D實例。
  2. 準備數據:收集你想要繪製的數據值。 散點圖繪製個別點,因此你將需要兩個數值數組:一個用於x坐標,另一個用於y坐標。 你可以在PlotSurface2D上添加任意多的圖表。
  3. 實例化散點圖:使用NPlot的PointPlotScatterPlot類,用你的圖表對象創建圖表。
  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
$vbLabelText   $csharpLabel

使用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
$vbLabelText   $csharpLabel

結論

NPlot C# (開發者如何使用): 圖3

在本教程中,我們探討了C#中兩個強大庫的集成:NPlot用於從數據依賴元素中創建動態、數據驅動的圖表,以及IronPDF用於將這些圖表轉換為PDF文檔。

這種組合為C#開發者提供了一個全面的工具包,允許他們有效地可視化數據,然後無縫地將這些數據轉換為可共享、存档的格式。

開始使用IronPDF的免費試用許可,從$799起。

常見問題解答

什麼是NPlot以及它在C#中的用法?

NPlot是一個多功能的圖表庫,適用於.NET框架,用於在C#中生成各種類型的圖形和圖表。它非常適合於可視化從簡單折線圖到複雜散佈圖的小型和大型數據集。

如何在C#專案中安裝NPlot?

您可以使用Visual Studio中的NuGet套件管理器在C#專案中安裝NPlot。進入“工具”>“NuGet套件管理器”>“管理解決方案的NuGet套件...”,搜索“NPlot”並繼續安裝。

如何使用NPlot在C#中創建圖表?

要使用NPlot創建圖表,先初始化一個PlotSurface2D對象,使用您的數據創建一個LinePlot,然後將其添加到繪圖表面。用標題、標籤進行自定義,然後渲染圖表。

NPlot中有哪些高級圖表技術?

NPlot提供了諸如柱狀圖、散佈圖和步進圖等高級圖表技術,允許您以各種可視化格式有效地表示數據。

如何使用NPlot圖表與IronPDF?

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試用許可證可用,使開發者能夠在承諾購買完整許可證之前探索其功能和能力。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。