Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This tutorial is designed for beginners who are keen to explore the integration of two powerful libraries: NPlot and IronPDF. Together, they form a robust toolkit for creating and exporting charts in C# applications.
NPlot is a versatile charting library in the .NET framework, ideal for generating a wide range of graphs and plots. From simple line plots to complex scatter charts, NPlot excels in displaying sample-based data and generating dynamic charts, whether you're working with small datasets or very large datasets.
IronPDF complements NPlot by enabling the conversion of these charts into PDF documents. Whether you're dealing with HTML email content or specific plot classes, IronPDF can render them into high-quality PDFs.
This functionality is particularly useful for applications that require report generation or documentation of analyzed data.
NPlot is a dynamic charting library designed for the .NET Framework, catering to a broad range of data visualization needs. Whether you're working on desktop applications or web-based solutions, NPlot offers the functionality to represent data graphically, clearly, and effectively.
To begin using NPlot in your C# project, you need to install it. Here's how you can easily add NPlot to your application:
Using NuGet Package Manager:
Once NPlot is installed, you can start creating charts. NPlot's ease of use makes it ideal for beginners, allowing for the creation of plots with just a few lines of code.
Let's create a simple line plot as our first chart:
Setting Up the Plot Surface: Create a PlotSurface2D
object. This acts as the canvas for your plot. Set a few display properties to customize its appearance, such as the background color and title.
Adding Data to the Plot: Use NPlot's LinePlot
class to create a line plot. Add data values belonging to one or more categories. These data points will be plotted on the graph.
Displaying the Chart: Add the line plot to the plot surface. Render the plot surface in a form or a user control for display.
using System;
using NPlot;
// Create a new bitmap plot surface
var plotSurface = new NPlot.Bitmap.PlotSurface2D(800, 600);
// Create a line plot
var linePlot = new LinePlot();
linePlot.AbscissaData = new double [] { 1, 2, 3, 4, 5 };
linePlot.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;
// Create a new bitmap plot surface
var plotSurface = new NPlot.Bitmap.PlotSurface2D(800, 600);
// Create a line plot
var linePlot = new LinePlot();
linePlot.AbscissaData = new double [] { 1, 2, 3, 4, 5 };
linePlot.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
' Create a new bitmap plot surface
Private plotSurface = New NPlot.Bitmap.PlotSurface2D(800, 600)
' Create a line plot
Private linePlot = New LinePlot()
linePlot.AbscissaData = New Double () { 1, 2, 3, 4, 5 }
linePlot.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")
Here is the output plot image:
After mastering basic plots, NPlot offers a range of more complex chart types to enhance your data visualization capabilities. These include bar plots, scatter charts, and step plots, each suited for different kinds of data representation.
Bar Plot: Ideal for displaying data values in one or more categories. Each bar represents a data value, with its height indicating the value's magnitude.
Scatter Plot: Perfect for visualizing data sets where each data point is independent. It plots data points on a two-dimensional graph, allowing for the analysis of patterns or trends.
Step Plot: Used for data that involves successive abscissa values, such as time-series data. It creates a staircase-like representation, clearly showing changes between successive data points.
The integration of NPlot with IronPDF allows for the seamless conversion of charts into PDF documents. IronPDF is a powerful library that enables the rendering of HTML content and plot classes into high-quality PDF files. This integration is particularly useful for applications that require generating reports or documenting analyzed data.
Install-Package IronPdf
Install-Package IronPdf
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLTo Integrate IronPDF into your NPlot C# project using the NuGet Package manager, follow these steps:
If you want to include IronPDF in your project via Package manager console, then execute the following command in Package Manager Console:
Install-Package IronPdf
It’ll fetch and install IronPDF into your project.
For a detailed overview of IronPDF, including its features, compatibility, and additional download options, visit the IronPDF page on the NuGet website at https://www.nuget.org/packages/IronPdf.
Alternatively, you can incorporate IronPDF directly into your project using its dll file. Download the ZIP file containing the DLL from this link. Unzip it, and include the DLL in your project.
NPlot excels in creating dynamic and visually appealing charts within C# applications. This section will guide you through generating a scatter chart, a typical use case for displaying data with two variables.
Scatter charts are particularly effective in visualizing the relationship between the variables. Follow the following steps to create a scatter plot:
PlotSurface2D
instance.PlotSurface2D
.PointPlot
or ScatterPlot
class to create your chart with your plot objects.var plotSurface = new NPlot.Windows.PlotSurface2D();
var scatterPlot = new PointPlot();
scatterPlot.AbscissaData = new double [] { /* x-coordinates */ };
scatterPlot.OrdinateData = new double [] { /* y-coordinates */ };
plotSurface.Add(scatterPlot);
// Additional code to render the plotSurface
var plotSurface = new NPlot.Windows.PlotSurface2D();
var scatterPlot = new PointPlot();
scatterPlot.AbscissaData = new double [] { /* x-coordinates */ };
scatterPlot.OrdinateData = new double [] { /* y-coordinates */ };
plotSurface.Add(scatterPlot);
// Additional code to render the plotSurface
Dim plotSurface = New NPlot.Windows.PlotSurface2D()
Dim scatterPlot = New PointPlot()
scatterPlot.AbscissaData = New Double () {}
scatterPlot.OrdinateData = New Double () {}
plotSurface.Add(scatterPlot)
' Additional code to render the plotSurface
Once you have created a chart with NPlot, you can use IronPDF to convert this chart into a PDF document. This process involves rendering the chart as an image and then using IronPDF to embed this image in a PDF. You can follow the following steps to convert charts to PDF:
PlotSurface2D
onto a bitmap.// Assuming 'chartImage' is the Bitmap of your NPlot chart
var imageFiles = new string [] { chartImagePath };
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("Chart.pdf");
// Assuming 'chartImage' is the Bitmap of your NPlot chart
var imageFiles = new string [] { chartImagePath };
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("Chart.pdf");
' Assuming 'chartImage' is the Bitmap of your NPlot chart
Dim imageFiles = New String () { chartImagePath }
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("Chart.pdf")
Throughout this tutorial, we've explored the integration of two powerful libraries in C#: NPlot for creating dynamic, data-driven charts from data-dependent elements, and IronPDF for converting these charts into PDF documents.
This combination provides a comprehensive toolkit for C# developers, allowing them to visualize data effectively and then seamlessly transition that data into a shareable, archival format.
Start with IronPDF's free trial, available from $749.
9 .NET API products for your office documents