ChartDirector 7.1 (.NET Edition)

Marks and Zones




This example demonstrates adding marks and zones to the chart, and adding custom legend keys to represent the zones.

A "mark" is a line drawn in the plot area. The purple "Alert" line and the green "Watch" line on the chart are mark lines. It is added to the chart using Axis.addMark.

Mark lines can be horizontal or vertical, depending on which axis the marks are defined on. Marks can be drawn on the front of the plot area (the default), or at the back like grid lines. This is configured using Mark.setDrawOnTop.

A "zone" is a color band on the back of the plot area. In this example, the red, yellow and green areas are zones. They are added to the chart using Axis.addZone. Like marks, zones can be horizontal or vertical.

In this example, LegendBox.addKey is used to add the names of the zones to the legend box as custom entries.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\markzone.cs
using System; using ChartDirector; namespace CSharpChartExplorer { public class markzone : DemoModule { //Name of demo module public string getName() { return "Marks and Zones (1)"; } //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 data for the chart double[] data = {40, 45, 37, 24, 32, 39, 53, 52, 63, 49, 46, 40, 54, 50, 57, 57, 48, 49, 63, 67, 74, 72, 70, 89, 74}; string[] labels = {"0\nJun 4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "0\nJun 5"}; // Create a XYChart object of size 400 x 270 pixels XYChart c = new XYChart(400, 270); // Set the plotarea at (80, 60) and of size 300 x 200 pixels. Turn off the grid lines by // setting their colors to Transparent. c.setPlotArea(80, 28, 300, 200).setGridColor(Chart.Transparent); // Add a title to the y axis ChartDirector.TextBox textbox = c.yAxis().setTitle("Temperature"); // Set the y axis title upright (font angle = 0) textbox.setFontAngle(0); // Put the y axis title on top of the axis textbox.setAlignment(Chart.TopLeft2); // Add green (0x99ff99), yellow (0xffff99) and red (0xff9999) zones to the y axis to // represent the ranges 0 - 50, 50 - 80, and 80 - max. c.yAxis().addZone(0, 50, 0x99ff99); c.yAxis().addZone(50, 80, 0xffff99); c.yAxis().addZone(80, 9999, 0xff9999); // Add a purple (0x800080) mark at y = 70 using a line width of 2. c.yAxis().addMark(70, 0x800080, "Alert = 70").setLineWidth(2); // Add a green (0x008000) mark at y = 40 using a line width of 2. c.yAxis().addMark(40, 0x008000, "Watch = 40").setLineWidth(2); // Add a legend box at (165, 0) (top right of the chart) using 8pt Arial font. and // horizontal layout. LegendBox legend = c.addLegend(165, 0, false, "Arial Bold", 8); // Disable the legend box boundary by setting the colors to Transparent legend.setBackground(Chart.Transparent, Chart.Transparent); // Add 3 custom entries to the legend box to represent the 3 zones legend.addKey("Normal", 0x80ff80); legend.addKey("Warning", 0xffff80); legend.addKey("Critical", 0xff8080); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Display 1 out of 3 labels on the x-axis. Show minor ticks for remaining labels. c.xAxis().setLabelStep(3, 1); // Add a 3D bar layer with the given data BarLayer layer = c.addBarLayer(data, 0xbbbbff); // Set the bar gap to 0 so that the bars are packed tightly layer.setBarGap(0); // Set the border color of the bars same as the fill color, with 1 pixel 3D border // effect. layer.setBorderColor(Chart.SameAsMainColor, 1); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='Temperature at {x}:00 = {value} C'"); } } }

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\markzone.vb
Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class markzone Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Marks and Zones (1)" 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 data for the chart Dim data() As Double = {40, 45, 37, 24, 32, 39, 53, 52, 63, 49, 46, 40, 54, 50, 57, 57, _ 48, 49, 63, 67, 74, 72, 70, 89, 74} Dim labels() As String = {"0<*br*>Jun 4", "1", "2", "3", "4", "5", "6", "7", "8", "9", _ "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", _ "0<*br*>Jun 5"} ' Create a XYChart object of size 400 x 270 pixels Dim c As XYChart = New XYChart(400, 270) ' Set the plotarea at (80, 60) and of size 300 x 200 pixels. Turn off the grid lines by ' setting their colors to Transparent. c.setPlotArea(80, 28, 300, 200).setGridColor(Chart.Transparent) ' Add a title to the y axis Dim textbox As ChartDirector.TextBox = c.yAxis().setTitle("Temperature") ' Set the y axis title upright (font angle = 0) textbox.setFontAngle(0) ' Put the y axis title on top of the axis textbox.setAlignment(Chart.TopLeft2) ' Add green (0x99ff99), yellow (0xffff99) and red (0xff9999) zones to the y axis to ' represent the ranges 0 - 50, 50 - 80, and 80 - max. c.yAxis().addZone(0, 50, &H99ff99) c.yAxis().addZone(50, 80, &Hffff99) c.yAxis().addZone(80, 9999, &Hff9999) ' Add a purple (0x800080) mark at y = 70 using a line width of 2. c.yAxis().addMark(70, &H800080, "Alert = 70").setLineWidth(2) ' Add a green (0x008000) mark at y = 40 using a line width of 2. c.yAxis().addMark(40, &H008000, "Watch = 40").setLineWidth(2) ' Add a legend box at (165, 0) (top right of the chart) using 8pt Arial font. and horizontal ' layout. Dim legend As LegendBox = c.addLegend(165, 0, False, "Arial Bold", 8) ' Disable the legend box boundary by setting the colors to Transparent legend.setBackground(Chart.Transparent, Chart.Transparent) ' Add 3 custom entries to the legend box to represent the 3 zones legend.addKey("Normal", &H80ff80) legend.addKey("Warning", &Hffff80) legend.addKey("Critical", &Hff8080) ' Set the labels on the x axis. c.xAxis().setLabels(labels) ' Display 1 out of 3 labels on the x-axis. Show minor ticks for remaining labels. c.xAxis().setLabelStep(3, 1) ' Add a 3D bar layer with the given data Dim layer As BarLayer = c.addBarLayer(data, &Hbbbbff) ' Set the bar gap to 0 so that the bars are packed tightly layer.setBarGap(0) ' Set the border color of the bars same as the fill color, with 1 pixel 3D border effect. layer.setBorderColor(Chart.SameAsMainColor, 1) ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='Temperature at {x}:00 = {value} C'") End Sub End Class

[WPF - C#] NetWPFCharts\CSharpWPFCharts\markzone.cs
using System; using ChartDirector; namespace CSharpWPFCharts { public class markzone : DemoModule { //Name of demo module public string getName() { return "Marks and Zones (1)"; } //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 data for the chart double[] data = {40, 45, 37, 24, 32, 39, 53, 52, 63, 49, 46, 40, 54, 50, 57, 57, 48, 49, 63, 67, 74, 72, 70, 89, 74}; string[] labels = {"0\nJun 4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "0\nJun 5"}; // Create a XYChart object of size 400 x 270 pixels XYChart c = new XYChart(400, 270); // Set the plotarea at (80, 60) and of size 300 x 200 pixels. Turn off the grid lines by // setting their colors to Transparent. c.setPlotArea(80, 28, 300, 200).setGridColor(Chart.Transparent); // Add a title to the y axis ChartDirector.TextBox textbox = c.yAxis().setTitle("Temperature"); // Set the y axis title upright (font angle = 0) textbox.setFontAngle(0); // Put the y axis title on top of the axis textbox.setAlignment(Chart.TopLeft2); // Add green (0x99ff99), yellow (0xffff99) and red (0xff9999) zones to the y axis to // represent the ranges 0 - 50, 50 - 80, and 80 - max. c.yAxis().addZone(0, 50, 0x99ff99); c.yAxis().addZone(50, 80, 0xffff99); c.yAxis().addZone(80, 9999, 0xff9999); // Add a purple (0x800080) mark at y = 70 using a line width of 2. c.yAxis().addMark(70, 0x800080, "Alert = 70").setLineWidth(2); // Add a green (0x008000) mark at y = 40 using a line width of 2. c.yAxis().addMark(40, 0x008000, "Watch = 40").setLineWidth(2); // Add a legend box at (165, 0) (top right of the chart) using 8pt Arial font. and // horizontal layout. LegendBox legend = c.addLegend(165, 0, false, "Arial Bold", 8); // Disable the legend box boundary by setting the colors to Transparent legend.setBackground(Chart.Transparent, Chart.Transparent); // Add 3 custom entries to the legend box to represent the 3 zones legend.addKey("Normal", 0x80ff80); legend.addKey("Warning", 0xffff80); legend.addKey("Critical", 0xff8080); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Display 1 out of 3 labels on the x-axis. Show minor ticks for remaining labels. c.xAxis().setLabelStep(3, 1); // Add a 3D bar layer with the given data BarLayer layer = c.addBarLayer(data, 0xbbbbff); // Set the bar gap to 0 so that the bars are packed tightly layer.setBarGap(0); // Set the border color of the bars same as the fill color, with 1 pixel 3D border // effect. layer.setBorderColor(Chart.SameAsMainColor, 1); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='Temperature at {x}:00 = {value} C'"); } } }

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\markzone.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) { // The data for the chart double[] data = {40, 45, 37, 24, 32, 39, 53, 52, 63, 49, 46, 40, 54, 50, 57, 57, 48, 49, 63, 67, 74, 72, 70, 89, 74}; string[] labels = {"0\nJun 4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "0\nJun 5"}; // Create a XYChart object of size 400 x 270 pixels XYChart c = new XYChart(400, 270); // Set the plotarea at (80, 60) and of size 300 x 200 pixels. Turn off the grid lines by setting // their colors to Transparent. c.setPlotArea(80, 28, 300, 200).setGridColor(Chart.Transparent); // Add a title to the y axis ChartDirector.TextBox textbox = c.yAxis().setTitle("Temperature"); // Set the y axis title upright (font angle = 0) textbox.setFontAngle(0); // Put the y axis title on top of the axis textbox.setAlignment(Chart.TopLeft2); // Add green (0x99ff99), yellow (0xffff99) and red (0xff9999) zones to the y axis to represent // the ranges 0 - 50, 50 - 80, and 80 - max. c.yAxis().addZone(0, 50, 0x99ff99); c.yAxis().addZone(50, 80, 0xffff99); c.yAxis().addZone(80, 9999, 0xff9999); // Add a purple (0x800080) mark at y = 70 using a line width of 2. c.yAxis().addMark(70, 0x800080, "Alert = 70").setLineWidth(2); // Add a green (0x008000) mark at y = 40 using a line width of 2. c.yAxis().addMark(40, 0x008000, "Watch = 40").setLineWidth(2); // Add a legend box at (165, 0) (top right of the chart) using 8pt Arial font. and horizontal // layout. LegendBox legend = c.addLegend(165, 0, false, "Arial Bold", 8); // Disable the legend box boundary by setting the colors to Transparent legend.setBackground(Chart.Transparent, Chart.Transparent); // Add 3 custom entries to the legend box to represent the 3 zones legend.addKey("Normal", 0x80ff80); legend.addKey("Warning", 0xffff80); legend.addKey("Critical", 0xff8080); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Display 1 out of 3 labels on the x-axis. Show minor ticks for remaining labels. c.xAxis().setLabelStep(3, 1); // Add a 3D bar layer with the given data BarLayer layer = c.addBarLayer(data, 0xbbbbff); // Set the bar gap to 0 so that the bars are packed tightly layer.setBarGap(0); // Set the border color of the bars same as the fill color, with 1 pixel 3D border effect. layer.setBorderColor(Chart.SameAsMainColor, 1); // Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='Temperature at {x}:00 = {value} C'" ); } </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\markzone.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) ' The data for the chart Dim data() As Double = {40, 45, 37, 24, 32, 39, 53, 52, 63, 49, 46, 40, 54, 50, 57, 57, 48, _ 49, 63, 67, 74, 72, 70, 89, 74} Dim labels() As String = {"0<*br*>Jun 4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", _ "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", _ "0<*br*>Jun 5"} ' Create a XYChart object of size 400 x 270 pixels Dim c As XYChart = New XYChart(400, 270) ' Set the plotarea at (80, 60) and of size 300 x 200 pixels. Turn off the grid lines by setting ' their colors to Transparent. c.setPlotArea(80, 28, 300, 200).setGridColor(Chart.Transparent) ' Add a title to the y axis Dim textbox As ChartDirector.TextBox = c.yAxis().setTitle("Temperature") ' Set the y axis title upright (font angle = 0) textbox.setFontAngle(0) ' Put the y axis title on top of the axis textbox.setAlignment(Chart.TopLeft2) ' Add green (0x99ff99), yellow (0xffff99) and red (0xff9999) zones to the y axis to represent ' the ranges 0 - 50, 50 - 80, and 80 - max. c.yAxis().addZone(0, 50, &H99ff99) c.yAxis().addZone(50, 80, &Hffff99) c.yAxis().addZone(80, 9999, &Hff9999) ' Add a purple (0x800080) mark at y = 70 using a line width of 2. c.yAxis().addMark(70, &H800080, "Alert = 70").setLineWidth(2) ' Add a green (0x008000) mark at y = 40 using a line width of 2. c.yAxis().addMark(40, &H008000, "Watch = 40").setLineWidth(2) ' Add a legend box at (165, 0) (top right of the chart) using 8pt Arial font. and horizontal ' layout. Dim legend As LegendBox = c.addLegend(165, 0, False, "Arial Bold", 8) ' Disable the legend box boundary by setting the colors to Transparent legend.setBackground(Chart.Transparent, Chart.Transparent) ' Add 3 custom entries to the legend box to represent the 3 zones legend.addKey("Normal", &H80ff80) legend.addKey("Warning", &Hffff80) legend.addKey("Critical", &Hff8080) ' Set the labels on the x axis. c.xAxis().setLabels(labels) ' Display 1 out of 3 labels on the x-axis. Show minor ticks for remaining labels. c.xAxis().setLabelStep(3, 1) ' Add a 3D bar layer with the given data Dim layer As BarLayer = c.addBarLayer(data, &Hbbbbff) ' Set the bar gap to 0 so that the bars are packed tightly layer.setBarGap(0) ' Set the border color of the bars same as the fill color, with 1 pixel 3D border effect. layer.setBorderColor(Chart.SameAsMainColor, 1) ' Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG) ' Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", _ "title='Temperature at {x}:00 = {value} C'") 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\MarkzoneController.cs
using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class MarkzoneController : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Marks and Zones (1)"; createChart(ViewBag.Viewer = new RazorChartViewer(HttpContext, "chart1")); return View("~/Views/Shared/ChartView.cshtml"); } // // Create chart // private void createChart(RazorChartViewer viewer) { // The data for the chart double[] data = {40, 45, 37, 24, 32, 39, 53, 52, 63, 49, 46, 40, 54, 50, 57, 57, 48, 49, 63, 67, 74, 72, 70, 89, 74}; string[] labels = {"0\nJun 4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "0\nJun 5"}; // Create a XYChart object of size 400 x 270 pixels XYChart c = new XYChart(400, 270); // Set the plotarea at (80, 60) and of size 300 x 200 pixels. Turn off the grid lines by // setting their colors to Transparent. c.setPlotArea(80, 28, 300, 200).setGridColor(Chart.Transparent); // Add a title to the y axis ChartDirector.TextBox textbox = c.yAxis().setTitle("Temperature"); // Set the y axis title upright (font angle = 0) textbox.setFontAngle(0); // Put the y axis title on top of the axis textbox.setAlignment(Chart.TopLeft2); // Add green (0x99ff99), yellow (0xffff99) and red (0xff9999) zones to the y axis to // represent the ranges 0 - 50, 50 - 80, and 80 - max. c.yAxis().addZone(0, 50, 0x99ff99); c.yAxis().addZone(50, 80, 0xffff99); c.yAxis().addZone(80, 9999, 0xff9999); // Add a purple (0x800080) mark at y = 70 using a line width of 2. c.yAxis().addMark(70, 0x800080, "Alert = 70").setLineWidth(2); // Add a green (0x008000) mark at y = 40 using a line width of 2. c.yAxis().addMark(40, 0x008000, "Watch = 40").setLineWidth(2); // Add a legend box at (165, 0) (top right of the chart) using 8pt Arial font. and horizontal // layout. LegendBox legend = c.addLegend(165, 0, false, "Arial Bold", 8); // Disable the legend box boundary by setting the colors to Transparent legend.setBackground(Chart.Transparent, Chart.Transparent); // Add 3 custom entries to the legend box to represent the 3 zones legend.addKey("Normal", 0x80ff80); legend.addKey("Warning", 0xffff80); legend.addKey("Critical", 0xff8080); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Display 1 out of 3 labels on the x-axis. Show minor ticks for remaining labels. c.xAxis().setLabelStep(3, 1); // Add a 3D bar layer with the given data BarLayer layer = c.addBarLayer(data, 0xbbbbff); // Set the bar gap to 0 so that the bars are packed tightly layer.setBarGap(0); // Set the border color of the bars same as the fill color, with 1 pixel 3D border effect. layer.setBorderColor(Chart.SameAsMainColor, 1); // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='Temperature at {x}:00 = {value} C'"); } } }

[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>