ChartDirector 7.1 (.NET Edition)

Contour Color Scale


      

This example demonstrates using various color scales for contour charts.

ChartDirector supports two ways to configure the color scale:

This example includes 4 charts to demonstrate both of the above methods.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\contourcolor.cs
using System; using ChartDirector; namespace CSharpChartExplorer { public class contourcolor : DemoModule { //Name of demo module public string getName() { return "Contour Color Scale"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 4; } //Main code for creating charts public void createChart(WinChartViewer viewer, int chartIndex) { // The x and y coordinates of the grid double[] dataX = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; double[] dataY = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; // Use random numbers for the z values on the XY grid RanSeries r = new RanSeries(99); double[] dataZ = r.get2DSeries(dataX.Length, dataY.Length, -0.9, 0.9); // Create a XYChart object of size 420 x 360 pixels XYChart c = new XYChart(420, 360); // Set the plotarea at (30, 25) and of size 300 x 300 pixels. Use semi-transparent grey // (0xdd000000) horizontal and vertical grid lines c.setPlotArea(30, 25, 300, 300, -1, -1, -1, unchecked((int)0xdd000000), -1); // Set the x-axis and y-axis scale c.xAxis().setLinearScale(-4, 4, 1); c.yAxis().setLinearScale(-4, 4, 1); // Add a contour layer using the given data ContourLayer layer = c.addContourLayer(dataX, dataY, dataZ); // Enable contour label by setting its style to black (000000) 8pt default bold font layer.setContourLabelStyle("bold", 8, 0x000000); // Set the label to display the value in a CDML block with semi-transparent white // (4FFFFFFF) background and 2 pixels left/right margin and 1 pixel top/bottom margin layer.setContourLabelFormat("<*block,bgcolor=4FFFFFFF,margin=2 2 1 1*>{value}"); // Move the grid lines in front of the contour layer c.getPlotArea().moveGridBefore(layer); // Add a color axis (the legend) in which the top left corner is anchored at (350, 25). // Set the length to 400 300 and the labels on the right side. ColorAxis cAxis = layer.setColorAxis(350, 25, Chart.TopLeft, 300, Chart.Right); if (chartIndex == 1) { // Speicify a color gradient as a list of colors, and use it in the color axis. int[] colorGradient = {0x0044cc, 0xffffff, 0x00aa00}; cAxis.setColorGradient(false, colorGradient); } else if (chartIndex == 2) { // Specify the color scale to use in the color axis double[] colorScale = {-1.0, 0x1a9850, -0.75, 0x66bd63, -0.5, 0xa6d96a, -0.25, 0xd9ef8b, 0, 0xfee08b, 0.25, 0xfdae61, 0.5, 0xf46d43, 0.75, 0xd73027, 1}; cAxis.setColorScale(colorScale); } else if (chartIndex == 3) { // Specify the color scale to use in the color axis. Also specify an underflow color // 0x66ccff (blue) for regions that fall below the lower axis limit. double[] colorScale = {0, 0xffff99, 0.2, 0x80cdc1, 0.4, 0x35978f, 0.6, 0x01665e, 0.8, 0x003c30, 1}; cAxis.setColorScale(colorScale, 0x66ccff); } // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='<*cdml*><*font=bold*>X: {x|2}<*br*>Y: {y|2}<*br*>Z: {z|2}'"); } } }

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\contourcolor.vb
Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class contourcolor Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Contour Color Scale" End Function 'Number of charts produced in this demo module Public Function getNoOfCharts() As Integer Implements DemoModule.getNoOfCharts Return 4 End Function 'Main code for creating charts Public Sub createChart(viewer As WinChartViewer, chartIndex As Integer) _ Implements DemoModule.createChart ' The x and y coordinates of the grid Dim dataX() As Double = {-4, -3, -2, -1, 0, 1, 2, 3, 4} Dim dataY() As Double = {-4, -3, -2, -1, 0, 1, 2, 3, 4} ' Use random numbers for the z values on the XY grid Dim r As RanSeries = New RanSeries(99) Dim dataZ() As Double = r.get2DSeries(UBound(dataX) + 1, UBound(dataY) + 1, -0.9, 0.9) ' Create a XYChart object of size 420 x 360 pixels Dim c As XYChart = New XYChart(420, 360) ' Set the plotarea at (30, 25) and of size 300 x 300 pixels. Use semi-transparent grey ' (0xdd000000) horizontal and vertical grid lines c.setPlotArea(30, 25, 300, 300, -1, -1, -1, &Hdd000000, -1) ' Set the x-axis and y-axis scale c.xAxis().setLinearScale(-4, 4, 1) c.yAxis().setLinearScale(-4, 4, 1) ' Add a contour layer using the given data Dim layer As ContourLayer = c.addContourLayer(dataX, dataY, dataZ) ' Enable contour label by setting its style to black (000000) 8pt default bold font layer.setContourLabelStyle("bold", 8, &H000000) ' Set the label to display the value in a CDML block with semi-transparent white (4FFFFFFF) ' background and 2 pixels left/right margin and 1 pixel top/bottom margin layer.setContourLabelFormat("<*block,bgcolor=4FFFFFFF,margin=2 2 1 1*>{value}") ' Move the grid lines in front of the contour layer c.getPlotArea().moveGridBefore(layer) ' Add a color axis (the legend) in which the top left corner is anchored at (350, 25). Set ' the length to 400 300 and the labels on the right side. Dim cAxis As ColorAxis = layer.setColorAxis(350, 25, Chart.TopLeft, 300, Chart.Right) If chartIndex = 1 Then ' Speicify a color gradient as a list of colors, and use it in the color axis. Dim colorGradient() As Integer = {&H0044cc, &Hffffff, &H00aa00} cAxis.setColorGradient(False, colorGradient) ElseIf chartIndex = 2 Then ' Specify the color scale to use in the color axis Dim colorScale() As Double = {-1.0, &H1a9850, -0.75, &H66bd63, -0.5, &Ha6d96a, -0.25, _ &Hd9ef8b, 0, &Hfee08b, 0.25, &Hfdae61, 0.5, &Hf46d43, 0.75, &Hd73027, 1} cAxis.setColorScale(colorScale) ElseIf chartIndex = 3 Then ' Specify the color scale to use in the color axis. Also specify an underflow color ' 0x66ccff (blue) for regions that fall below the lower axis limit. Dim colorScale() As Double = {0, &Hffff99, 0.2, &H80cdc1, 0.4, &H35978f, 0.6, _ &H01665e, 0.8, &H003c30, 1} cAxis.setColorScale(colorScale, &H66ccff) End If ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='<*cdml*><*font=bold*>X: {x|2}<*br*>Y: {y|2}<*br*>Z: {z|2}'") End Sub End Class

[WPF - C#] NetWPFCharts\CSharpWPFCharts\contourcolor.cs
using System; using ChartDirector; namespace CSharpWPFCharts { public class contourcolor : DemoModule { //Name of demo module public string getName() { return "Contour Color Scale"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 4; } //Main code for creating charts public void createChart(WPFChartViewer viewer, int chartIndex) { // The x and y coordinates of the grid double[] dataX = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; double[] dataY = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; // Use random numbers for the z values on the XY grid RanSeries r = new RanSeries(99); double[] dataZ = r.get2DSeries(dataX.Length, dataY.Length, -0.9, 0.9); // Create a XYChart object of size 420 x 360 pixels XYChart c = new XYChart(420, 360); // Set the plotarea at (30, 25) and of size 300 x 300 pixels. Use semi-transparent grey // (0xdd000000) horizontal and vertical grid lines c.setPlotArea(30, 25, 300, 300, -1, -1, -1, unchecked((int)0xdd000000), -1); // Set the x-axis and y-axis scale c.xAxis().setLinearScale(-4, 4, 1); c.yAxis().setLinearScale(-4, 4, 1); // Add a contour layer using the given data ContourLayer layer = c.addContourLayer(dataX, dataY, dataZ); // Enable contour label by setting its style to black (000000) 8pt default bold font layer.setContourLabelStyle("bold", 8, 0x000000); // Set the label to display the value in a CDML block with semi-transparent white // (4FFFFFFF) background and 2 pixels left/right margin and 1 pixel top/bottom margin layer.setContourLabelFormat("<*block,bgcolor=4FFFFFFF,margin=2 2 1 1*>{value}"); // Move the grid lines in front of the contour layer c.getPlotArea().moveGridBefore(layer); // Add a color axis (the legend) in which the top left corner is anchored at (350, 25). // Set the length to 400 300 and the labels on the right side. ColorAxis cAxis = layer.setColorAxis(350, 25, Chart.TopLeft, 300, Chart.Right); if (chartIndex == 1) { // Speicify a color gradient as a list of colors, and use it in the color axis. int[] colorGradient = {0x0044cc, 0xffffff, 0x00aa00}; cAxis.setColorGradient(false, colorGradient); } else if (chartIndex == 2) { // Specify the color scale to use in the color axis double[] colorScale = {-1.0, 0x1a9850, -0.75, 0x66bd63, -0.5, 0xa6d96a, -0.25, 0xd9ef8b, 0, 0xfee08b, 0.25, 0xfdae61, 0.5, 0xf46d43, 0.75, 0xd73027, 1}; cAxis.setColorScale(colorScale); } else if (chartIndex == 3) { // Specify the color scale to use in the color axis. Also specify an underflow color // 0x66ccff (blue) for regions that fall below the lower axis limit. double[] colorScale = {0, 0xffff99, 0.2, 0x80cdc1, 0.4, 0x35978f, 0.6, 0x01665e, 0.8, 0x003c30, 1}; cAxis.setColorScale(colorScale, 0x66ccff); } // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='<*cdml*><*font=bold*>X: {x|2}<*br*>Y: {y|2}<*br*>Z: {z|2}'"); } } }

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\contourcolor.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"> // // Create chart // protected void createChart(WebChartViewer viewer, int chartIndex) { // The x and y coordinates of the grid double[] dataX = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; double[] dataY = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; // Use random numbers for the z values on the XY grid RanSeries r = new RanSeries(99); double[] dataZ = r.get2DSeries(dataX.Length, dataY.Length, -0.9, 0.9); // Create a XYChart object of size 420 x 360 pixels XYChart c = new XYChart(420, 360); // Set the plotarea at (30, 25) and of size 300 x 300 pixels. Use semi-transparent grey // (0xdd000000) horizontal and vertical grid lines c.setPlotArea(30, 25, 300, 300, -1, -1, -1, unchecked((int)0xdd000000), -1); // Set the x-axis and y-axis scale c.xAxis().setLinearScale(-4, 4, 1); c.yAxis().setLinearScale(-4, 4, 1); // Add a contour layer using the given data ContourLayer layer = c.addContourLayer(dataX, dataY, dataZ); // Enable contour label by setting its style to black (000000) 8pt default bold font layer.setContourLabelStyle("bold", 8, 0x000000); // Set the label to display the value in a CDML block with semi-transparent white (4FFFFFFF) // background and 2 pixels left/right margin and 1 pixel top/bottom margin layer.setContourLabelFormat("<*block,bgcolor=4FFFFFFF,margin=2 2 1 1*>{value}"); // Move the grid lines in front of the contour layer c.getPlotArea().moveGridBefore(layer); // Add a color axis (the legend) in which the top left corner is anchored at (350, 25). Set the // length to 400 300 and the labels on the right side. ColorAxis cAxis = layer.setColorAxis(350, 25, Chart.TopLeft, 300, Chart.Right); if (chartIndex == 1) { // Speicify a color gradient as a list of colors, and use it in the color axis. int[] colorGradient = {0x0044cc, 0xffffff, 0x00aa00}; cAxis.setColorGradient(false, colorGradient); } else if (chartIndex == 2) { // Specify the color scale to use in the color axis double[] colorScale = {-1.0, 0x1a9850, -0.75, 0x66bd63, -0.5, 0xa6d96a, -0.25, 0xd9ef8b, 0, 0xfee08b, 0.25, 0xfdae61, 0.5, 0xf46d43, 0.75, 0xd73027, 1}; cAxis.setColorScale(colorScale); } else if (chartIndex == 3) { // Specify the color scale to use in the color axis. Also specify an underflow color // 0x66ccff (blue) for regions that fall below the lower axis limit. double[] colorScale = {0, 0xffff99, 0.2, 0x80cdc1, 0.4, 0x35978f, 0.6, 0x01665e, 0.8, 0x003c30, 1}; cAxis.setColorScale(colorScale, 0x66ccff); } // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='<*cdml*><*b*>X: {x|2}<*br*>Y: {y|2}<*br*>Z: {z|2}'"); // Output Javascript chart model to support contour chart tooltips viewer.ChartModel = c.getJsChartModel(); } // // Page Load event handler // protected void Page_Load(object sender, EventArgs e) { createChart(WebChartViewer0, 0); createChart(WebChartViewer1, 1); createChart(WebChartViewer2, 2); createChart(WebChartViewer3, 3); } </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer0" runat="server" /> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> <chart:WebChartViewer id="WebChartViewer2" runat="server" /> <chart:WebChartViewer id="WebChartViewer3" runat="server" /> </body> </html>

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\contourcolor.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"> ' ' Create chart ' Protected Sub createChart(viewer As WebChartViewer, chartIndex As Integer) ' The x and y coordinates of the grid Dim dataX() As Double = {-4, -3, -2, -1, 0, 1, 2, 3, 4} Dim dataY() As Double = {-4, -3, -2, -1, 0, 1, 2, 3, 4} ' Use random numbers for the z values on the XY grid Dim r As RanSeries = New RanSeries(99) Dim dataZ() As Double = r.get2DSeries(UBound(dataX) + 1, UBound(dataY) + 1, -0.9, 0.9) ' Create a XYChart object of size 420 x 360 pixels Dim c As XYChart = New XYChart(420, 360) ' Set the plotarea at (30, 25) and of size 300 x 300 pixels. Use semi-transparent grey ' (0xdd000000) horizontal and vertical grid lines c.setPlotArea(30, 25, 300, 300, -1, -1, -1, &Hdd000000, -1) ' Set the x-axis and y-axis scale c.xAxis().setLinearScale(-4, 4, 1) c.yAxis().setLinearScale(-4, 4, 1) ' Add a contour layer using the given data Dim layer As ContourLayer = c.addContourLayer(dataX, dataY, dataZ) ' Enable contour label by setting its style to black (000000) 8pt default bold font layer.setContourLabelStyle("bold", 8, &H000000) ' Set the label to display the value in a CDML block with semi-transparent white (4FFFFFFF) ' background and 2 pixels left/right margin and 1 pixel top/bottom margin layer.setContourLabelFormat("<*block,bgcolor=4FFFFFFF,margin=2 2 1 1*>{value}") ' Move the grid lines in front of the contour layer c.getPlotArea().moveGridBefore(layer) ' Add a color axis (the legend) in which the top left corner is anchored at (350, 25). Set the ' length to 400 300 and the labels on the right side. Dim cAxis As ColorAxis = layer.setColorAxis(350, 25, Chart.TopLeft, 300, Chart.Right) If chartIndex = 1 Then ' Speicify a color gradient as a list of colors, and use it in the color axis. Dim colorGradient() As Integer = {&H0044cc, &Hffffff, &H00aa00} cAxis.setColorGradient(False, colorGradient) ElseIf chartIndex = 2 Then ' Specify the color scale to use in the color axis Dim colorScale() As Double = {-1.0, &H1a9850, -0.75, &H66bd63, -0.5, &Ha6d96a, -0.25, _ &Hd9ef8b, 0, &Hfee08b, 0.25, &Hfdae61, 0.5, &Hf46d43, 0.75, &Hd73027, 1} cAxis.setColorScale(colorScale) ElseIf chartIndex = 3 Then ' Specify the color scale to use in the color axis. Also specify an underflow color 0x66ccff ' (blue) for regions that fall below the lower axis limit. Dim colorScale() As Double = {0, &Hffff99, 0.2, &H80cdc1, 0.4, &H35978f, 0.6, &H01665e, _ 0.8, &H003c30, 1} cAxis.setColorScale(colorScale, &H66ccff) End If ' Output the chart viewer.Image = c.makeWebImage(Chart.SVG) ' Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", _ "title='<*cdml*><*b*>X: {x|2}<*br*>Y: {y|2}<*br*>Z: {z|2}'") ' Output Javascript chart model to support contour chart tooltips viewer.ChartModel = c.getJsChartModel() End Sub ' ' Page Load event handler ' Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) createChart(WebChartViewer0, 0) createChart(WebChartViewer1, 1) createChart(WebChartViewer2, 2) createChart(WebChartViewer3, 3) End Sub </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer0" runat="server" /> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> <chart:WebChartViewer id="WebChartViewer2" runat="server" /> <chart:WebChartViewer id="WebChartViewer3" runat="server" /> </body> </html>

[ASP.NET MVC - Controller] NetMvcCharts\Controllers\ContourcolorController.cs
using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class ContourcolorController : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Contour Color Scale"; // This example contains 4 charts. ViewBag.Viewer = new RazorChartViewer[4]; for (int i = 0; i < ViewBag.Viewer.Length; ++i) createChart(ViewBag.Viewer[i] = new RazorChartViewer(HttpContext, "chart" + i), i); return View("~/Views/Shared/ChartView.cshtml"); } // // Create chart // private void createChart(RazorChartViewer viewer, int chartIndex) { // The x and y coordinates of the grid double[] dataX = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; double[] dataY = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; // Use random numbers for the z values on the XY grid RanSeries r = new RanSeries(99); double[] dataZ = r.get2DSeries(dataX.Length, dataY.Length, -0.9, 0.9); // Create a XYChart object of size 420 x 360 pixels XYChart c = new XYChart(420, 360); // Set the plotarea at (30, 25) and of size 300 x 300 pixels. Use semi-transparent grey // (0xdd000000) horizontal and vertical grid lines c.setPlotArea(30, 25, 300, 300, -1, -1, -1, unchecked((int)0xdd000000), -1); // Set the x-axis and y-axis scale c.xAxis().setLinearScale(-4, 4, 1); c.yAxis().setLinearScale(-4, 4, 1); // Add a contour layer using the given data ContourLayer layer = c.addContourLayer(dataX, dataY, dataZ); // Enable contour label by setting its style to black (000000) 8pt default bold font layer.setContourLabelStyle("bold", 8, 0x000000); // Set the label to display the value in a CDML block with semi-transparent white (4FFFFFFF) // background and 2 pixels left/right margin and 1 pixel top/bottom margin layer.setContourLabelFormat("<*block,bgcolor=4FFFFFFF,margin=2 2 1 1*>{value}"); // Move the grid lines in front of the contour layer c.getPlotArea().moveGridBefore(layer); // Add a color axis (the legend) in which the top left corner is anchored at (350, 25). Set // the length to 400 300 and the labels on the right side. ColorAxis cAxis = layer.setColorAxis(350, 25, Chart.TopLeft, 300, Chart.Right); if (chartIndex == 1) { // Speicify a color gradient as a list of colors, and use it in the color axis. int[] colorGradient = {0x0044cc, 0xffffff, 0x00aa00}; cAxis.setColorGradient(false, colorGradient); } else if (chartIndex == 2) { // Specify the color scale to use in the color axis double[] colorScale = {-1.0, 0x1a9850, -0.75, 0x66bd63, -0.5, 0xa6d96a, -0.25, 0xd9ef8b, 0, 0xfee08b, 0.25, 0xfdae61, 0.5, 0xf46d43, 0.75, 0xd73027, 1}; cAxis.setColorScale(colorScale); } else if (chartIndex == 3) { // Specify the color scale to use in the color axis. Also specify an underflow color // 0x66ccff (blue) for regions that fall below the lower axis limit. double[] colorScale = {0, 0xffff99, 0.2, 0x80cdc1, 0.4, 0x35978f, 0.6, 0x01665e, 0.8, 0x003c30, 1}; cAxis.setColorScale(colorScale, 0x66ccff); } // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='<*cdml*><*b*>X: {x|2}<*br*>Y: {y|2}<*br*>Z: {z|2}'"); // Output Javascript chart model to support contour chart tooltips viewer.ChartModel = c.getJsChartModel(); } } }

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