ChartDirector 7.1 (.NET Edition)

Area Line Chart




This example demonstrates an area chart with a line layer overlap on top. It also demonstrates using date/time x coordinates for the data points (as opposed to using x-axis labels), and also various methods to control the chart appearance.

In ChartDirector, x coordinates for the data points are optional. If no x coordinates are provided for the data points, the x coordinates will be assumed to be the array indexes (0, 1, 2, 3 ...), and the data points would be evenly spaced in the x direction. Axis labels specified using Axis.setLabels or Axis.setLabels2 are also positioned using the array indexes as the x coordinates, so the labels will match with the data points.

In some applications, it may be necessary or more convenient to provide x coordinates for the data points. For example, if the data points are unevenly spaced, or if there are multiple data series with different data point spacing in the x direction, x coordinates are needed. In this case, Layer.setXData or Layer.setXData2 can be used to provide the x coordinates. By default, ChartDirector will then automatically determine the x-axis scale and labels, just like how the y-axis is handled. The axis scale and labels can also be specified by using various Axis methods, such as Axis.setLinearScale, Axis.setLogScale and Axis.setDateScale (please refer to Axis for the full list).

Axis.setLabels or Axis.setLabels2 should not be used if x coordinates are provided, as they use array indexes as the x coordinates, which is unlikely to be consistent with the provided x coordinates.

In this example, x coordinates are used to demonstrate the automatic x-axis scale and labels.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\arealine.cs
using System; using ChartDirector; namespace CSharpChartExplorer { public class arealine : DemoModule { //Name of demo module public string getName() { return "Area Line Chart"; } //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) { // In this example, we simply use random data for the 2 data series. RanSeries r = new RanSeries(127); double[] data0 = r.getSeries(180, 70, -5, 5); double[] data1 = r.getSeries(180, 150, -15, 15); DateTime[] timeStamps = r.getDateSeries(180, new DateTime(2014, 3, 1), 86400); // Create a XYChart object of size 640 x 420 pixels XYChart c = new XYChart(640, 420); // Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, 0x333333); // Add a title box using grey (0x555555) 20pt Arial Bold font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial Bold", 20, 0x555555); // Set the plotarea at (70, 70) and of size 540 x 320 pixels, with transparent // background and border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 540, 320, -1, -1, Chart.Transparent, 0xcccccc); // Add a legend box with horizontal layout above the plot area at (70, 32). Use 12pt // Arial Bold dark grey (0x555555) font, transparent background and border, and line // style legend icon. LegendBox b = c.addLegend(70, 32, false, "Arial Bold", 12); b.setFontColor(0x555555); b.setBackground(Chart.Transparent, Chart.Transparent); b.setLineStyleKey(); // Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12); c.yAxis().setLabelStyle("Arial", 12); // Set the x and y axis stems to transparent, and the x-axis tick color to grey // (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, 0xaaaaaa); c.yAxis().setColors(Chart.Transparent); // Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0); // For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y // axis. c.xAxis().setTickDensity(80); c.yAxis().setTickDensity(40); // Use "mm/yyyy" as the x-axis label format for the first plotted month of a year, and // "mm" for other months c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", Chart.StartOfMonthFilter(), "{value|mm}"); // Add a title to the y axis using dark grey (0x555555) 12pt Arial Bold font c.yAxis().setTitle("Energy (kWh)", "Arial Bold", 14, 0x555555); // Add a line layer with 2-pixel line width LineLayer layer0 = c.addLineLayer(data0, 0xcc0000, "Power Usage"); layer0.setXData(timeStamps); layer0.setLineWidth(2); // Add an area layer using semi-transparent blue (0x7f0044cc) as the fill color AreaLayer layer1 = c.addAreaLayer(data1, 0x7f0044cc, "Effective Load"); layer1.setXData(timeStamps); layer1.setBorderColor(Chart.SameAsMainColor); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='[{x|mm dd, yyyy}] {value} kWh'"); } } }

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\arealine.vb
Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class arealine Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Area Line Chart" 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 ' In this example, we simply use random data for the 2 data series. Dim r As RanSeries = New RanSeries(127) Dim data0() As Double = r.getSeries(180, 70, -5, 5) Dim data1() As Double = r.getSeries(180, 150, -15, 15) Dim timeStamps() As Date = r.getDateSeries(180, DateSerial(2014, 3, 1), 86400) ' Create a XYChart object of size 640 x 420 pixels Dim c As XYChart = New XYChart(640, 420) ' Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, &H333333) ' Add a title box using grey (0x555555) 20pt Arial Bold font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial Bold", 20, &H555555) ' Set the plotarea at (70, 70) and of size 540 x 320 pixels, with transparent background and ' border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 540, 320, -1, -1, Chart.Transparent, &Hcccccc) ' Add a legend box with horizontal layout above the plot area at (70, 32). Use 12pt Arial ' Bold dark grey (0x555555) font, transparent background and border, and line style legend ' icon. Dim b As LegendBox = c.addLegend(70, 32, False, "Arial Bold", 12) b.setFontColor(&H555555) b.setBackground(Chart.Transparent, Chart.Transparent) b.setLineStyleKey() ' Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12) c.yAxis().setLabelStyle("Arial", 12) ' Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, &Haaaaaa) c.yAxis().setColors(Chart.Transparent) ' Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0) ' For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis. c.xAxis().setTickDensity(80) c.yAxis().setTickDensity(40) ' Use "mm/yyyy" as the x-axis label format for the first plotted month of a year, and "mm" ' for other months c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", _ Chart.StartOfMonthFilter(), "{value|mm}") ' Add a title to the y axis using dark grey (0x555555) 12pt Arial Bold font c.yAxis().setTitle("Energy (kWh)", "Arial Bold", 14, &H555555) ' Add a line layer with 2-pixel line width Dim layer0 As LineLayer = c.addLineLayer(data0, &Hcc0000, "Power Usage") layer0.setXData(timeStamps) layer0.setLineWidth(2) ' Add an area layer using semi-transparent blue (0x7f0044cc) as the fill color Dim layer1 As AreaLayer = c.addAreaLayer(data1, &H7f0044cc, "Effective Load") layer1.setXData(timeStamps) layer1.setBorderColor(Chart.SameAsMainColor) ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='[{x|mm dd, yyyy}] {value} kWh'") End Sub End Class

[WPF - C#] NetWPFCharts\CSharpWPFCharts\arealine.cs
using System; using ChartDirector; namespace CSharpWPFCharts { public class arealine : DemoModule { //Name of demo module public string getName() { return "Area Line Chart"; } //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) { // In this example, we simply use random data for the 2 data series. RanSeries r = new RanSeries(127); double[] data0 = r.getSeries(180, 70, -5, 5); double[] data1 = r.getSeries(180, 150, -15, 15); DateTime[] timeStamps = r.getDateSeries(180, new DateTime(2014, 3, 1), 86400); // Create a XYChart object of size 640 x 420 pixels XYChart c = new XYChart(640, 420); // Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, 0x333333); // Add a title box using grey (0x555555) 20pt Arial Bold font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial Bold", 20, 0x555555); // Set the plotarea at (70, 70) and of size 540 x 320 pixels, with transparent // background and border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 540, 320, -1, -1, Chart.Transparent, 0xcccccc); // Add a legend box with horizontal layout above the plot area at (70, 32). Use 12pt // Arial Bold dark grey (0x555555) font, transparent background and border, and line // style legend icon. LegendBox b = c.addLegend(70, 32, false, "Arial Bold", 12); b.setFontColor(0x555555); b.setBackground(Chart.Transparent, Chart.Transparent); b.setLineStyleKey(); // Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12); c.yAxis().setLabelStyle("Arial", 12); // Set the x and y axis stems to transparent, and the x-axis tick color to grey // (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, 0xaaaaaa); c.yAxis().setColors(Chart.Transparent); // Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0); // For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y // axis. c.xAxis().setTickDensity(80); c.yAxis().setTickDensity(40); // Use "mm/yyyy" as the x-axis label format for the first plotted month of a year, and // "mm" for other months c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", Chart.StartOfMonthFilter(), "{value|mm}"); // Add a title to the y axis using dark grey (0x555555) 12pt Arial Bold font c.yAxis().setTitle("Energy (kWh)", "Arial Bold", 14, 0x555555); // Add a line layer with 2-pixel line width LineLayer layer0 = c.addLineLayer(data0, 0xcc0000, "Power Usage"); layer0.setXData(timeStamps); layer0.setLineWidth(2); // Add an area layer using semi-transparent blue (0x7f0044cc) as the fill color AreaLayer layer1 = c.addAreaLayer(data1, 0x7f0044cc, "Effective Load"); layer1.setXData(timeStamps); layer1.setBorderColor(Chart.SameAsMainColor); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='[{x|mm dd, yyyy}] {value} kWh'"); } } }

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\arealine.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" %> <!DOCTYPE html> <script runat="server"> // // Page Load event handler // protected void Page_Load(object sender, EventArgs e) { // In this example, we simply use random data for the 2 data series. RanSeries r = new RanSeries(127); double[] data0 = r.getSeries(180, 70, -5, 5); double[] data1 = r.getSeries(180, 150, -15, 15); DateTime[] timeStamps = r.getDateSeries(180, new DateTime(2014, 3, 1), 86400); // Create a XYChart object of size 640 x 420 pixels XYChart c = new XYChart(640, 420); // Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, 0x333333); // Add a title box using grey (0x555555) 20pt Arial Bold font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial Bold", 20, 0x555555); // Set the plotarea at (70, 70) and of size 540 x 320 pixels, with transparent background and // border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 540, 320, -1, -1, Chart.Transparent, 0xcccccc); // Add a legend box with horizontal layout above the plot area at (70, 32). Use 12pt Arial Bold // dark grey (0x555555) font, transparent background and border, and line style legend icon. LegendBox b = c.addLegend(70, 32, false, "Arial Bold", 12); b.setFontColor(0x555555); b.setBackground(Chart.Transparent, Chart.Transparent); b.setLineStyleKey(); // Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12); c.yAxis().setLabelStyle("Arial", 12); // Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, 0xaaaaaa); c.yAxis().setColors(Chart.Transparent); // Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0); // For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis. c.xAxis().setTickDensity(80); c.yAxis().setTickDensity(40); // Use "mm/yyyy" as the x-axis label format for the first plotted month of a year, and "mm" for // other months c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", Chart.StartOfMonthFilter(), "{value|mm}"); // Add a title to the y axis using dark grey (0x555555) 12pt Arial Bold font c.yAxis().setTitle("Energy (kWh)", "Arial Bold", 14, 0x555555); // Add a line layer with 2-pixel line width LineLayer layer0 = c.addLineLayer(data0, 0xcc0000, "Power Usage"); layer0.setXData(timeStamps); layer0.setLineWidth(2); // Add an area layer using semi-transparent blue (0x7f0044cc) as the fill color AreaLayer layer1 = c.addAreaLayer(data1, 0x7f0044cc, "Effective Load"); layer1.setXData(timeStamps); layer1.setBorderColor(Chart.SameAsMainColor); // Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='[{x|mm dd, yyyy}] {value} kWh'"); } </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> </body> </html>

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\arealine.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" %> <!DOCTYPE html> <script runat="server"> ' ' Page Load event handler ' Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) ' In this example, we simply use random data for the 2 data series. Dim r As RanSeries = New RanSeries(127) Dim data0() As Double = r.getSeries(180, 70, -5, 5) Dim data1() As Double = r.getSeries(180, 150, -15, 15) Dim timeStamps() As Date = r.getDateSeries(180, DateSerial(2014, 3, 1), 86400) ' Create a XYChart object of size 640 x 420 pixels Dim c As XYChart = New XYChart(640, 420) ' Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, &H333333) ' Add a title box using grey (0x555555) 20pt Arial Bold font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial Bold", 20, &H555555) ' Set the plotarea at (70, 70) and of size 540 x 320 pixels, with transparent background and ' border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 540, 320, -1, -1, Chart.Transparent, &Hcccccc) ' Add a legend box with horizontal layout above the plot area at (70, 32). Use 12pt Arial Bold ' dark grey (0x555555) font, transparent background and border, and line style legend icon. Dim b As LegendBox = c.addLegend(70, 32, False, "Arial Bold", 12) b.setFontColor(&H555555) b.setBackground(Chart.Transparent, Chart.Transparent) b.setLineStyleKey() ' Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12) c.yAxis().setLabelStyle("Arial", 12) ' Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, &Haaaaaa) c.yAxis().setColors(Chart.Transparent) ' Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0) ' For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis. c.xAxis().setTickDensity(80) c.yAxis().setTickDensity(40) ' Use "mm/yyyy" as the x-axis label format for the first plotted month of a year, and "mm" for ' other months c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", _ Chart.StartOfMonthFilter(), "{value|mm}") ' Add a title to the y axis using dark grey (0x555555) 12pt Arial Bold font c.yAxis().setTitle("Energy (kWh)", "Arial Bold", 14, &H555555) ' Add a line layer with 2-pixel line width Dim layer0 As LineLayer = c.addLineLayer(data0, &Hcc0000, "Power Usage") layer0.setXData(timeStamps) layer0.setLineWidth(2) ' Add an area layer using semi-transparent blue (0x7f0044cc) as the fill color Dim layer1 As AreaLayer = c.addAreaLayer(data1, &H7f0044cc, "Effective Load") layer1.setXData(timeStamps) layer1.setBorderColor(Chart.SameAsMainColor) ' Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG) ' Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='[{x|mm dd, yyyy}] {value} kWh'") End Sub </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> </body> </html>

[ASP.NET MVC - Controller] NetMvcCharts\Controllers\ArealineController.cs
using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class ArealineController : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Area Line Chart"; createChart(ViewBag.Viewer = new RazorChartViewer(HttpContext, "chart1")); return View("~/Views/Shared/ChartView.cshtml"); } // // Create chart // private void createChart(RazorChartViewer viewer) { // In this example, we simply use random data for the 2 data series. RanSeries r = new RanSeries(127); double[] data0 = r.getSeries(180, 70, -5, 5); double[] data1 = r.getSeries(180, 150, -15, 15); DateTime[] timeStamps = r.getDateSeries(180, new DateTime(2014, 3, 1), 86400); // Create a XYChart object of size 640 x 420 pixels XYChart c = new XYChart(640, 420); // Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, 0x333333); // Add a title box using grey (0x555555) 20pt Arial Bold font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial Bold", 20, 0x555555); // Set the plotarea at (70, 70) and of size 540 x 320 pixels, with transparent background and // border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 540, 320, -1, -1, Chart.Transparent, 0xcccccc); // Add a legend box with horizontal layout above the plot area at (70, 32). Use 12pt Arial // Bold dark grey (0x555555) font, transparent background and border, and line style legend // icon. LegendBox b = c.addLegend(70, 32, false, "Arial Bold", 12); b.setFontColor(0x555555); b.setBackground(Chart.Transparent, Chart.Transparent); b.setLineStyleKey(); // Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12); c.yAxis().setLabelStyle("Arial", 12); // Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, 0xaaaaaa); c.yAxis().setColors(Chart.Transparent); // Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0); // For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis. c.xAxis().setTickDensity(80); c.yAxis().setTickDensity(40); // Use "mm/yyyy" as the x-axis label format for the first plotted month of a year, and "mm" // for other months c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", Chart.StartOfMonthFilter(), "{value|mm}"); // Add a title to the y axis using dark grey (0x555555) 12pt Arial Bold font c.yAxis().setTitle("Energy (kWh)", "Arial Bold", 14, 0x555555); // Add a line layer with 2-pixel line width LineLayer layer0 = c.addLineLayer(data0, 0xcc0000, "Power Usage"); layer0.setXData(timeStamps); layer0.setLineWidth(2); // Add an area layer using semi-transparent blue (0x7f0044cc) as the fill color AreaLayer layer1 = c.addAreaLayer(data1, 0x7f0044cc, "Effective Load"); layer1.setXData(timeStamps); layer1.setBorderColor(Chart.SameAsMainColor); // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='[{x|mm dd, yyyy}] {value} kWh'") ; } } }

[ASP.NET MVC - View] NetMvcCharts\Views\Shared\ChartView.cshtml
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> <style> @ViewBag.Style </style> @Scripts.Render("~/Scripts/cdjcv.js") </head> <body style="margin:5px 0px 0px 5px"> <div style="font:bold 18pt verdana;"> @ViewBag.Title </div> <hr style="border:solid 1px #000080; background:#000080" /> <div> @{ 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()) } } </div> </body> </html>