ChartDirector 6.2 (.NET Edition)

Parametric Curve Fitting

This example demonstrates parametric curve fitting.

In addition to linear regression, ChartDirector also supports polynomial, exponential and logarithmic regression. To create these curves, a TrendLayer object is created using XYChart.addTrendLayer, and the regressive type is set using TrendLayer.setRegressionType.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\paramcurve.cs
 ```using System; using ChartDirector; namespace CSharpChartExplorer { public class paramcurve : DemoModule { //Name of demo module public string getName() { return "Parametric Curve Fitting"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 1; } //Main code for creating chart. //Note: the argument chartIndex is unused because this demo only has 1 chart. public void createChart(WinChartViewer viewer, int chartIndex) { // The XY data of the first data series double[] dataX0 = {10, 35, 17, 4, 22, 29, 45, 52, 63, 39}; double[] dataY0 = {2.0, 3.2, 2.7, 1.2, 2.8, 2.9, 3.1, 3.0, 2.3, 3.3}; // The XY data of the second data series double[] dataX1 = {30, 35, 17, 4, 22, 59, 43, 52, 63, 39}; double[] dataY1 = {1.0, 1.3, 0.7, 0.6, 0.8, 3.0, 1.8, 2.3, 3.4, 1.5}; // The XY data of the third data series double[] dataX2 = {28, 35, 15, 10, 22, 60, 46, 64, 39}; double[] dataY2 = {2.0, 2.2, 1.2, 0.4, 1.8, 2.7, 2.4, 2.8, 2.4}; // Create a XYChart object of size 540 x 480 pixels XYChart c = new XYChart(540, 480); // Set the plotarea at (70, 65) and of size 400 x 350 pixels, with white background and // a light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with // light grey color (0xc0c0c0) c.setPlotArea(70, 65, 400, 350, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1); // Add a legend box with the top center point anchored at (270, 30). Use horizontal // layout. Use 10pt Arial Bold Italic font. Set the background and border color to // Transparent. LegendBox legendBox = c.addLegend(270, 30, false, "Arial Bold Italic", 10); legendBox.setAlignment(Chart.TopCenter); legendBox.setBackground(Chart.Transparent, Chart.Transparent); // Add a title to the chart using 18 point Times Bold Itatic font. c.addTitle("Parametric Curve Fitting", "Times New Roman Bold Italic", 18); // Add titles to the axes using 12pt Arial Bold Italic font c.yAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); c.xAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); // Set the axes line width to 3 pixels c.yAxis().setWidth(3); c.xAxis().setWidth(3); // Add a scatter layer using (dataX0, dataY0) c.addScatterLayer(dataX0, dataY0, "Polynomial", Chart.GlassSphere2Shape, 11, 0xff0000); // Add a degree 2 polynomial trend line layer for (dataX0, dataY0) TrendLayer trend0 = c.addTrendLayer2(dataX0, dataY0, 0xff0000); trend0.setLineWidth(3); trend0.setRegressionType(Chart.PolynomialRegression(2)); trend0.setHTMLImageMap("{disable}"); // Add a scatter layer for (dataX1, dataY1) c.addScatterLayer(dataX1, dataY1, "Exponential", Chart.GlassSphere2Shape, 11, 0x00aa00); // Add an exponential trend line layer for (dataX1, dataY1) TrendLayer trend1 = c.addTrendLayer2(dataX1, dataY1, 0x00aa00); trend1.setLineWidth(3); trend1.setRegressionType(Chart.ExponentialRegression); trend1.setHTMLImageMap("{disable}"); // Add a scatter layer using (dataX2, dataY2) c.addScatterLayer(dataX2, dataY2, "Logarithmic", Chart.GlassSphere2Shape, 11, 0x0000ff); // Add a logarithmic trend line layer for (dataX2, dataY2) TrendLayer trend2 = c.addTrendLayer2(dataX2, dataY2, 0x0000ff); trend2.setLineWidth(3); trend2.setRegressionType(Chart.LogarithmicRegression); trend2.setHTMLImageMap("{disable}"); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='[{dataSetName}] ({x}, {value})'"); } } }```

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\paramcurve.vb
 ```Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class paramcurve Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Parametric Curve Fitting" End Function 'Number of charts produced in this demo module Public Function getNoOfCharts() As Integer Implements DemoModule.getNoOfCharts Return 1 End Function 'Main code for creating chart. 'Note: the argument chartIndex is unused because this demo only has 1 chart. Public Sub createChart(viewer As WinChartViewer, chartIndex As Integer) _ Implements DemoModule.createChart ' The XY data of the first data series Dim dataX0() As Double = {10, 35, 17, 4, 22, 29, 45, 52, 63, 39} Dim dataY0() As Double = {2.0, 3.2, 2.7, 1.2, 2.8, 2.9, 3.1, 3.0, 2.3, 3.3} ' The XY data of the second data series Dim dataX1() As Double = {30, 35, 17, 4, 22, 59, 43, 52, 63, 39} Dim dataY1() As Double = {1.0, 1.3, 0.7, 0.6, 0.8, 3.0, 1.8, 2.3, 3.4, 1.5} ' The XY data of the third data series Dim dataX2() As Double = {28, 35, 15, 10, 22, 60, 46, 64, 39} Dim dataY2() As Double = {2.0, 2.2, 1.2, 0.4, 1.8, 2.7, 2.4, 2.8, 2.4} ' Create a XYChart object of size 540 x 480 pixels Dim c As XYChart = New XYChart(540, 480) ' Set the plotarea at (70, 65) and of size 400 x 350 pixels, with white background and a ' light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light ' grey color (0xc0c0c0) c.setPlotArea(70, 65, 400, 350, &Hffffff, -1, &Hc0c0c0, &Hc0c0c0, -1) ' Add a legend box with the top center point anchored at (270, 30). Use horizontal layout. ' Use 10pt Arial Bold Italic font. Set the background and border color to Transparent. Dim legendBox As LegendBox = c.addLegend(270, 30, False, "Arial Bold Italic", 10) legendBox.setAlignment(Chart.TopCenter) legendBox.setBackground(Chart.Transparent, Chart.Transparent) ' Add a title to the chart using 18 point Times Bold Itatic font. c.addTitle("Parametric Curve Fitting", "Times New Roman Bold Italic", 18) ' Add titles to the axes using 12pt Arial Bold Italic font c.yAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12) c.xAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12) ' Set the axes line width to 3 pixels c.yAxis().setWidth(3) c.xAxis().setWidth(3) ' Add a scatter layer using (dataX0, dataY0) c.addScatterLayer(dataX0, dataY0, "Polynomial", Chart.GlassSphere2Shape, 11, &Hff0000) ' Add a degree 2 polynomial trend line layer for (dataX0, dataY0) Dim trend0 As TrendLayer = c.addTrendLayer2(dataX0, dataY0, &Hff0000) trend0.setLineWidth(3) trend0.setRegressionType(Chart.PolynomialRegression(2)) trend0.setHTMLImageMap("{disable}") ' Add a scatter layer for (dataX1, dataY1) c.addScatterLayer(dataX1, dataY1, "Exponential", Chart.GlassSphere2Shape, 11, &H00aa00) ' Add an exponential trend line layer for (dataX1, dataY1) Dim trend1 As TrendLayer = c.addTrendLayer2(dataX1, dataY1, &H00aa00) trend1.setLineWidth(3) trend1.setRegressionType(Chart.ExponentialRegression) trend1.setHTMLImageMap("{disable}") ' Add a scatter layer using (dataX2, dataY2) c.addScatterLayer(dataX2, dataY2, "Logarithmic", Chart.GlassSphere2Shape, 11, &H0000ff) ' Add a logarithmic trend line layer for (dataX2, dataY2) Dim trend2 As TrendLayer = c.addTrendLayer2(dataX2, dataY2, &H0000ff) trend2.setLineWidth(3) trend2.setRegressionType(Chart.LogarithmicRegression) trend2.setHTMLImageMap("{disable}") ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='[{dataSetName}] ({x}, {value})'") End Sub End Class```

[WPF - C#] NetWPFCharts\paramcurve.cs
 ```using System; using ChartDirector; namespace CSharpWPFDemo { public class paramcurve : DemoModule { //Name of demo module public string getName() { return "Parametric Curve Fitting"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 1; } //Main code for creating chart. //Note: the argument chartIndex is unused because this demo only has 1 chart. public void createChart(WPFChartViewer viewer, int chartIndex) { // The XY data of the first data series double[] dataX0 = {10, 35, 17, 4, 22, 29, 45, 52, 63, 39}; double[] dataY0 = {2.0, 3.2, 2.7, 1.2, 2.8, 2.9, 3.1, 3.0, 2.3, 3.3}; // The XY data of the second data series double[] dataX1 = {30, 35, 17, 4, 22, 59, 43, 52, 63, 39}; double[] dataY1 = {1.0, 1.3, 0.7, 0.6, 0.8, 3.0, 1.8, 2.3, 3.4, 1.5}; // The XY data of the third data series double[] dataX2 = {28, 35, 15, 10, 22, 60, 46, 64, 39}; double[] dataY2 = {2.0, 2.2, 1.2, 0.4, 1.8, 2.7, 2.4, 2.8, 2.4}; // Create a XYChart object of size 540 x 480 pixels XYChart c = new XYChart(540, 480); // Set the plotarea at (70, 65) and of size 400 x 350 pixels, with white background and // a light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with // light grey color (0xc0c0c0) c.setPlotArea(70, 65, 400, 350, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1); // Add a legend box with the top center point anchored at (270, 30). Use horizontal // layout. Use 10pt Arial Bold Italic font. Set the background and border color to // Transparent. LegendBox legendBox = c.addLegend(270, 30, false, "Arial Bold Italic", 10); legendBox.setAlignment(Chart.TopCenter); legendBox.setBackground(Chart.Transparent, Chart.Transparent); // Add a title to the chart using 18 point Times Bold Itatic font. c.addTitle("Parametric Curve Fitting", "Times New Roman Bold Italic", 18); // Add titles to the axes using 12pt Arial Bold Italic font c.yAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); c.xAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); // Set the axes line width to 3 pixels c.yAxis().setWidth(3); c.xAxis().setWidth(3); // Add a scatter layer using (dataX0, dataY0) c.addScatterLayer(dataX0, dataY0, "Polynomial", Chart.GlassSphere2Shape, 11, 0xff0000); // Add a degree 2 polynomial trend line layer for (dataX0, dataY0) TrendLayer trend0 = c.addTrendLayer2(dataX0, dataY0, 0xff0000); trend0.setLineWidth(3); trend0.setRegressionType(Chart.PolynomialRegression(2)); trend0.setHTMLImageMap("{disable}"); // Add a scatter layer for (dataX1, dataY1) c.addScatterLayer(dataX1, dataY1, "Exponential", Chart.GlassSphere2Shape, 11, 0x00aa00); // Add an exponential trend line layer for (dataX1, dataY1) TrendLayer trend1 = c.addTrendLayer2(dataX1, dataY1, 0x00aa00); trend1.setLineWidth(3); trend1.setRegressionType(Chart.ExponentialRegression); trend1.setHTMLImageMap("{disable}"); // Add a scatter layer using (dataX2, dataY2) c.addScatterLayer(dataX2, dataY2, "Logarithmic", Chart.GlassSphere2Shape, 11, 0x0000ff); // Add a logarithmic trend line layer for (dataX2, dataY2) TrendLayer trend2 = c.addTrendLayer2(dataX2, dataY2, 0x0000ff); trend2.setLineWidth(3); trend2.setRegressionType(Chart.LogarithmicRegression); trend2.setHTMLImageMap("{disable}"); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='[{dataSetName}] ({x}, {value})'"); } } }```

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\paramcurve.aspx
(Click here on how to convert this code to code-behind style.)
 ```<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="ChartDirector" %> <%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir" %> ```

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\paramcurve.aspx
(Click here on how to convert this code to code-behind style.)
 ```<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="ChartDirector" %> <%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir" %> ```

[ASP.NET MVC - Controller] NetMvcCharts\Controllers\ParamcurveController.cs
 ```using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class ParamcurveController : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Parametric Curve Fitting"; createChart(ViewBag.Viewer = new RazorChartViewer(HttpContext, "chart1")); return View("~/Views/Shared/ChartView.cshtml"); } // // Create chart // private void createChart(RazorChartViewer viewer) { // The XY data of the first data series double[] dataX0 = {10, 35, 17, 4, 22, 29, 45, 52, 63, 39}; double[] dataY0 = {2.0, 3.2, 2.7, 1.2, 2.8, 2.9, 3.1, 3.0, 2.3, 3.3}; // The XY data of the second data series double[] dataX1 = {30, 35, 17, 4, 22, 59, 43, 52, 63, 39}; double[] dataY1 = {1.0, 1.3, 0.7, 0.6, 0.8, 3.0, 1.8, 2.3, 3.4, 1.5}; // The XY data of the third data series double[] dataX2 = {28, 35, 15, 10, 22, 60, 46, 64, 39}; double[] dataY2 = {2.0, 2.2, 1.2, 0.4, 1.8, 2.7, 2.4, 2.8, 2.4}; // Create a XYChart object of size 540 x 480 pixels XYChart c = new XYChart(540, 480); // Set the plotarea at (70, 65) and of size 400 x 350 pixels, with white background and a // light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light // grey color (0xc0c0c0) c.setPlotArea(70, 65, 400, 350, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1); // Add a legend box with the top center point anchored at (270, 30). Use horizontal layout. // Use 10pt Arial Bold Italic font. Set the background and border color to Transparent. LegendBox legendBox = c.addLegend(270, 30, false, "Arial Bold Italic", 10); legendBox.setAlignment(Chart.TopCenter); legendBox.setBackground(Chart.Transparent, Chart.Transparent); // Add a title to the chart using 18 point Times Bold Itatic font. c.addTitle("Parametric Curve Fitting", "Times New Roman Bold Italic", 18); // Add titles to the axes using 12pt Arial Bold Italic font c.yAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); c.xAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); // Set the axes line width to 3 pixels c.yAxis().setWidth(3); c.xAxis().setWidth(3); // Add a scatter layer using (dataX0, dataY0) c.addScatterLayer(dataX0, dataY0, "Polynomial", Chart.GlassSphere2Shape, 11, 0xff0000); // Add a degree 2 polynomial trend line layer for (dataX0, dataY0) TrendLayer trend0 = c.addTrendLayer2(dataX0, dataY0, 0xff0000); trend0.setLineWidth(3); trend0.setRegressionType(Chart.PolynomialRegression(2)); trend0.setHTMLImageMap("{disable}"); // Add a scatter layer for (dataX1, dataY1) c.addScatterLayer(dataX1, dataY1, "Exponential", Chart.GlassSphere2Shape, 11, 0x00aa00); // Add an exponential trend line layer for (dataX1, dataY1) TrendLayer trend1 = c.addTrendLayer2(dataX1, dataY1, 0x00aa00); trend1.setLineWidth(3); trend1.setRegressionType(Chart.ExponentialRegression); trend1.setHTMLImageMap("{disable}"); // Add a scatter layer using (dataX2, dataY2) c.addScatterLayer(dataX2, dataY2, "Logarithmic", Chart.GlassSphere2Shape, 11, 0x0000ff); // Add a logarithmic trend line layer for (dataX2, dataY2) TrendLayer trend2 = c.addTrendLayer2(dataX2, dataY2, 0x0000ff); trend2.setLineWidth(3); trend2.setRegressionType(Chart.LogarithmicRegression); trend2.setHTMLImageMap("{disable}"); // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='[{dataSetName}] ({x}, {value})'"); } } }```

[ASP.NET MVC - View] NetMvcCharts\Views\Shared\ChartView.cshtml
 ```@{ Layout = null; } @ViewBag.Title
@ViewBag.Title

@{ if (ViewBag.Viewer is Array) { // Display multiple charts for (int i = 0; i < ViewBag.Viewer.Length; ++i) { @:@Html.Raw(ViewBag.Viewer[i].RenderHTML()) } } else { // Display one chart only @:@Html.Raw(ViewBag.Viewer.RenderHTML()) } }
```