Chart Programmer's User Guide: C# Numerical Library
Chart Programmer's User Guide: C# Numerical Library
V E R S I O N 6 . 5
THIS RELEASE IS DEDICATED TO
DR. JOHN F. BROPHY
1953‐2008
OUR MENTOR AND FRIEND
WHOSE UNSELFISH EFFORTS WILL HAVE
A LASTING IMPACT ON THE IMSL PRODUCT LINE.
WE WERE BLESSED TO HAVE HAD JOHN AS A MEMBER OF OUR TEAM FOR 25 YEARS.
THE VISUAL NUMERICS PRODUCT DEVELOPMENT TEAM
®
IMSL Numerical Library V.6.
Chart Programmer’s User Guide
Worldwide Offices
USA • UK • France • Germany • Japan
For contact information, please visit
www.vni.com/contact/worldwideoffices.php
Index 175
Appendices:
Describes IMSL C# and web servlets ........ Web Server Application
Describes the ways to save
bitmap images.................... Writing a Chart as a Bitmap Image File
Describes the picture-in-picture effects. .............. Picture-in-Picture
Chart 2D
The IMSL C# Numerical Library Chart Programmer User’s Guide is an overview and discussion
of the IMSL C# Numerical Library charting package. It is intended to be used in conjunction with
the online Reference Manual.
This guide is both an introductory tutorial on the charting package and a “cookbook” for creating
common chart types. The Reference Manual contains the complete details of the classes.
NOTE: Charts in the printed version of this manual appear in black and white. If you are reading
this manual in print and wish to see the charts in color, please open the Acrobat Reader file:
/manual/WordDocuments/chartpg.pdf.
If you do not have Acrobat Reader installed on your machine, you can download it for free at:
http://get.adobe.com/reader/.
Or you may also view the Guide in color from our Web site at:
http://www.vni.com/products/imsl/documentation/index.php#imslcnumlib65 .
Overview
The IMSL C# Numerical Library chart package is designed to allow the creation of highly
customizable charts using any .NET language. An IMSL C# Numerical Library chart is created by
assembling ChartNodes into a tree. This chart tree is then rendered to the screen or printer.
The following class is a simple example of the use of the IMSL C# Numerical Library chart
package. The chart is displayed in a Windows.Forms.Form. The code to create the chart is all in
the constructor. The IMSL C# Numerical Library class FrameChart extends the .NET class Form
and creates a Chart object.
using Imsl.Chart2D;
public Intro1() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double[] y = new double[] {4, 2, 3, 9};
new Data(axis, y);
}
The general pattern of the ChartNode class, and classes derived from it, is to have constructors
whose first argument is the parent ChartNode of the ChartNode being created.
The root node of the tree is always a Chart object. It is usually constructed within FrameChart
or PanelChart, which handles the repainting of the chart within Windows Forms. If a Chart
object is explicitly created, and used within a Windows form, then its Paint(Graphics) method
must be called from the container's Paint(Graphics) method.
Chart nodes can contain attributes, such as FillColor and LineWidth. Attributes are inherited via
the chart tree. For example, when a Data node is being painted, its LineWidth attribute
determines the thickness of the line. If this attribute is set in the data node being drawn, that is the
value used. If it is not set, then its parent node (an AxisXY node in the above example) is queried.
If it is not set there, then its parent is queried. This continues until the root node is reached after
which a default value is used. Note that this inheritance is not the same as C# class inheritance.
Attributes are set using axis.LineWidth = value or SetViewport(double[]) and retrieved
using axis.LineWidth or GetViewport().
Method calls and property references can be chained together. For example, the following sets the
thickness of the major tick marks on the x-axis:
axis.AxisX.MajorTick.LineWidth = 2.0;
where axis is an AxisXY object.
A customized version of the above chart can be obtained by changing its constructor as in the
following:
using Imsl.Chart2D;
public Intro2() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double[] y = new double[] {4, 2, 3, 9};
new Data(axis, y);
chart.Background.FillColor = System.Drawing.Color.Lime;
axis.AxisX.MajorTick.LineWidth = 2.0;
(Download Code)
using Imsl.Chart2D;
public SamplePanel()
{
panelChart = new PanelChart();
panelChart.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(panelChart);
Chart Types
This chapter describes these IMSL C# Numerical Library charting types:
• “Scatter Plot” on page 12
• “Line Plot” on page 15
• “Area Plot” on page 18
• “Function Plot” on page 21
• “Log and SemiLog Plot” on page 24
• “Error Bar Plot” on page 27
• “High-Low-Close Plot” on page 32
• “Candlestick Chart” on page 34
• “Pie Chart” on page 37
• “Box Plot” on page 40
• “Bar Chart” on page 42
• “Contour Chart” on page 48
• “Heatmap” on page 50
• “Treemap” on page 52
• “Histogram” on page 54
• “Polar Plot” on page 56
• “Dendrogram Chart” on page 59
Scatter Plot
This section describes the construction of scatter plots. The markers can be formatted using the
Marker Attributes.
It is also possible to mix lines and markers (see “Mixed Line and Marker Plot” on page 17).
(Download Code)
using Imsl.Chart2D;
using System.Drawing;
public SampleSimpleScatter() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
using Imsl.Chart2D;
using System.Drawing;
using Imsl.Stat;
public SampleScatter() {
Random r = new Random(123457);
// Data set 1
double[] y1 = new double[20];
for (int k = 0; k < y1.Length; k++) y1[k] = r.NextDouble();
Data data1 = new Data(axis, y1);
data1.MarkerType = Data.MARKER_TYPE_FILLED_SQUARE;
data1.MarkerColor = Color.Green;
// Data set 2
double[] y2 = new double[20];
for (int k = 0; k < y2.Length; k++) y2[k] = r.NextDouble();
Data data2 = new Data(axis, y2);
data2.MarkerType = Data.MARKER_TYPE_PLUS;
data2.MarkerColor = Color.Blue;
}
Line Plot
A line plot consists of points by lines. The lines can be formatted using the “Line Attributes” on
page 108.
using Imsl.Chart2D;
using System.Drawing;
public SampleSimpleLine() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
(Download Code)
using Imsl.Chart2D;
using System.Drawing;
public SampleLineMarker() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
Area Plot
Area plots are similar to line plots, but with the area between the line and a reference line filled in.
An area plot is created if the DATA_TYPE_FILL bit is on in the value of the DataType attribute.
The default reference line is y = 0. The location of the reference line can be changed from 0 by
setting the Reference property. The Fill Area Attributes determine how the area is filled.
using Imsl.Chart2D;
using System.Drawing;
public SampleArea() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
using Imsl.Chart2D;
using System.Drawing;
public SampleAreaPaint() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
chart.Legend.IsVisible = true;
Attribute Reference
The attribute Reference defines the reference line. If its value is a, then the reference line is y = a.
Its default value is 0.
Function Plot
A function plot shows the value of a function f(x) over an interval [a,b]. The function must be
defined as an implementation of the ChartFunction interface. A Data node constructor creates
a line chart from the function. The look of the function is controlled by the line attributes.
The ChartFunction interface requires that the function name be "F", that the function has a
single double argument and that it returns a double.
Example
This example plots the sinc function on the interval [-10,10]. The sinc function is defined to be
sin(πx)/πx. In this example, Sinc is a class that implements ChartFunction. This is required by
the function Data constructor. In the code, the case x = 0 is handled specially to avoid returning
NaN.
using Imsl.Chart2D;
using System;
using System.Drawing;
public SampleFunction() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
Histogram Example
For another example of a ChartFunction, see Histogram" on page 54.
Spline Chart
This example shows raw data points, as markers, and their fit to a shape preserving spline. The
spline is computed using CsShape found in the IMSL C# Numerical Library Math namespace
(which extends Spline). The ChartSpline class wraps the Spline into a ChartFunction.
This example also enables the Legend.
(Download Code)
using Imsl.Chart2D;
using Imsl.Math;
using System.Drawing;
public SampleSpline() {
Chart chart = this.Chart;
SemiLog Plot
In this example, data is plotted as lines and markers on a semilog axis.
To set up the y-axis as a logarithmic axis:
• the Transform attribute is set to TRANSFORM_LOG.
• the Density on page 124 attribute is set to 9. Density is the number of minor
ticks between major ticks. It is 9 because base 10 is used and the major tick
marks are not counted.
The TextFormat, used by AxisLabel, is changed to use scientific notation (see “Text
Attributes”). The default decimal format would result in large numbers written with many zeros.
using Imsl.Chart2D;
using System.Drawing;
public SampleSemiLog() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
axis.AxisY.Transform = Axis.TRANSFORM_LOG;
axis.AxisY.Density = 9;
axis.AxisY.TextFormat = "0.E0";
(Download Code)
using Imsl.Chart2D;
using System.Drawing;
public SampleLogLog() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
axis.Transform = Axis.TRANSFORM_LOG;
axis.Density = 9;
axis.TextFormat = "0.E0";
using Imsl.Chart2D;
using System.Drawing;
using Imsl.Stat;
public SampleErrorBar() {
Random r = new Random(123457);
(Download Code)
using Imsl.Chart2D;
using System.Drawing;
using Imsl.Stat;
public SampleHorizontalErrorBar() {
Random r = new Random(123457);
using Imsl.Chart2D;
using System.Drawing;
using Imsl.Stat;
public SampleMixedErrorBar() {
Random r = new Random(123457);
High-Low-Close Plot
High-Low-Close plots are used to show stock prices. They are created using the HighLowClose
class.
The markers can be formatted using the attribute MarkerColor.
In a high-low-close plot the vertical line represent`s the high and low values. The close value is
represented by a “tick” to the right. The open value, if present, is represented by a tick to the left.
The HighLowClose.SetDateAxis method will configure the x-axis for dates. This turns off
autoscaling of the axis.
Example
In this example, random security prices are computed in the CreateData method. The time axis
is prepared by calling HighLowClose.SetDateAxis.
using Imsl.Chart2D;
using System;
using System.Drawing;
public SampleHighLowClose() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
int n = 30;
CreateData(n);
Candlestick Chart
A Candlestick chart is used to show stock price. Each candlestick shows the stock’s high, low,
opening and closing prices.
Example
In this example, random security prices are computed in the CreateData method. The time axis
is prepared by calling SetDateAxis.
The up days are colored green and the down days are colored red.
using Imsl.Chart2D;
using System;
using System.Drawing;
public SampleCandlestick() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
int n = 30;
CreateData(n);
Pie Chart
A pie chart is a graphical way to organize data. This section describes the construction of a pie
chart.
The LabelType attribute is set in the pie node. The pie node itself does not use this attribute, but
from there it is inherited by all of the PieSlice nodes.
The Title attribute is set in each PieSlice node. This is the slice label. It is used to label the slice
only if the slice’s LabelType attribute is LABEL_TYPE_TITLE.
The FillColor attribute is also set in each slice. This determines the color of the slice. Since the
default value of FillColor is black, it is generally recommended that FillColor be set in each
slice.
using Imsl.Chart2D;
using System.Drawing;
public SamplePieChart()
{
Chart chart = this.Chart;
chart.ChartTitle.SetTitle("Pie Chart");
slice[0].FillColor = Color.Green;
slice[0].SetTitle("Green");
slice[0].Explode = 0.1;
slice[1].FillColor = Color.Red;
slice[1].SetTitle("Red");
slice[1].FillOutlineColor = Color.Yellow;
slice[1].Explode = 0.1;
slice[2].FillColor = Color.Blue;
slice[2].SetTitle("Blue");
slice[2].Explode = 0.1;
slice[3].FillColor = Color.Yellow;
slice[3].SetTitle("Yellow");
slice[3].Explode = 0.15;
}
±1.58 IRQ / n
where IRQ is the interquartile range and n is the number of observations. Outside and far outside
values may be displayed as symbols. Outside values are outside the inner fence. Far out values are
outside the outer fence.
The BoxPlot has several child nodes. Any of these nodes can be disabled by setting their
IsVisible property to false.
• The Bodies node has the main body of the box plot elements. Its fill attributes
determine the drawing of (notched) rectangle (see “Fill Area Attributes” on page
111). Its line attributes determine the drawing of the median line. The width of the
box is controlled by the MarkerSize attribute (see Attribute MarkerSize on page
110).
• The Whiskers node draws the lines to the upper and lower quartile. Its drawing is
affected by the marker attributes.
• The FarMarkers node holds the far markers. Its drawing is affected by the marker
attributes.
• The OutsideMarkers node holds the outside markers. Its drawing is affected by the
marker attributes.
Example
In this example, the Fisher iris data set is read from a file and a Box plot is created from data. The
data is in a file called FisherIris.csv in the same directory as this class.
The y-axis labels are taken from the column names.
The boxes are colored green; the markers are all filled circles. The outside markers are blue and
the far outside markers would be red, if there were any.
using Imsl.Chart2D;
using System.Drawing;
using System.IO;
public SampleBoxPlot() {
ReadData("FisherIris.csv");
string line;
string[] tokens;
int lineCount = 0;
StreamReader sr = new StreamReader(fileName);
line = sr.ReadLine();
labels = line.Split(',');
for (int i = 0; i < nObs; i++) {
line = sr.ReadLine();
tokens = line.Split(',');
for (int j = 0; j < nColumns; j++) {
irisData[j][lineCount] =
double.Parse(tokens[j].Trim());
}
lineCount++;
}
}
}
Bar Chart
The class Bar is used to create bar charts and histograms. This page describes the construction of
labeled bar charts. For a discussion of histograms, see “Histogram” on page 54.
using Imsl.Chart2D;
using System.Drawing;
public SampleBar() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
Attribute BarWidth
The BarWidth attribute sets the width of the groups of bars at each index. Its default value is 0.5.
If the number of groups is increased, the width of each individual bar is reduced proportionately.
See Histogram on page 54 for an example of the use of the BarWidth attribute.
using Imsl.Chart2D;
using System.Drawing;
public SampleBarGroup() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
In the above grouped bar chart example, the Bar constructor creates a collection of chart nodes.
For each group, it creates a BarSet node as its direct child. Each BarSet node has BarItem
nodes as children, one for each bar in the set.
using Imsl.Chart2D;
using System.Drawing;
public SampleBarGroupStack() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
// y is a 2 by 3 by 4 array
double[][][] y = new double[2][][] {
new double[3][] {
new double[] {4, 2, 3, 9},
new double[] {8, 4, 2, 3},
new double[] {1, 5, 3, 8}},
new double[3][] {
new double[] {6, 7, 5, 2},
new double[] {4, 1, 7, 2},
new double[] {8, 5, 6, 1}}};
Bar bar = new Bar(axis, y);
bar.BarType = Bar.BAR_TYPE_VERTICAL;
bar.SetLabels(new string[] {"A","B","C","D"});
// group 0 - shades of red
bar.GetBarSet(0,0).FillColor = Color.Red;
bar.GetBarSet(1,0).FillColor = Color.DarkRed;
// group 1 - shades of blue
Legend
The Legend for a bar chart is turned on by setting the Legend’s IsVisible property to true
and defining the Title attributes for the legend entries. The legend entries are the BarSet
objects. The following example is the stacked, grouped bar example with the legend enabled.
using Imsl.Chart2D;
using System.Drawing;
public SampleBarLegend() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
chart.Legend.IsVisible = true;
// y is a 2 by 3 by 4 array
double[][][] y = new double[2][][] {
new double[3][] {
new double[] {4, 2, 3, 9},
new double[] {8, 4, 2, 3},
new double[] {1, 5, 3, 8}},
new double[3][] {
new double[] {6, 7, 5, 2},
new double[] {4, 1, 7, 2},
new double[] {8, 5, 6, 1}}};
Bar bar = new Bar(axis, y);
bar.BarType = Bar.BAR_TYPE_VERTICAL;
bar.SetLabels(new string[] {"A","B","C","D"});
// group 0 - shades of red
bar.GetBarSet(0,0).SetTitle("Red");
bar.GetBarSet(0,0).FillColor = Color.Red;
bar.GetBarSet(1,0).SetTitle("Dark Red");
bar.GetBarSet(1,0).FillColor = Color.DarkRed;
// group 1 - shades of blue
bar.GetBarSet(0,1).SetTitle("Blue");
bar.GetBarSet(0,1).FillColor = Color.Blue;
bar.GetBarSet(1,1).SetTitle("Light Blue");
bar.GetBarSet(1,1).FillColor = Color.LightBlue;
// group 2 - shades of gray
bar.GetBarSet(0,2).SetTitle("Gray");
bar.GetBarSet(0,2).FillColor = Color.Gray;
bar.GetBarSet(1,2).SetTitle("Light Gray");
bar.GetBarSet(1,2).FillColor = Color.LightGray;
}
Contour Chart
A Contour chart shows level curves of a two-dimensional function.
(Download Code)
using Imsl.Chart2D;
using System;
using System.Drawing;
public SampleContour() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
contour.GetContourLevel(0).LineColor = Color.DarkBlue;
contour.GetContourLevel(1).LineColor = Color.Orange;
contour.GetContourLevel(2).LineColor = Color.DarkGreen;
contour.GetContourLevel(3).LineColor = Color.DarkRed;
}
Heatmap
A Heatmap divides a rectangle into subrectangles. The color of each subrectangle is determined
by the value of a data array and a colormap.
If the data array is m by n then there are m divisions along the x-axis and n divisions along the y-
axis.
A Colormap is a mapping from [0,1] to color values. The blue-red colormap, used in the example
below, maps 0 to red and 1 to dark blue and interpolates between these endpoints values. The
heatmap maps the minimum data value to the color corresponding to 0 and the highest data value
to the color corresponding to 1.
The Heatmap class has a special legend for colormaps. It displays the color values as a gradient
labeled with corresponding data values.
(Download Code)
using Imsl.Chart2D;
public SampleHeatmap() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
Treemap
A Treemap divides a rectangle into subrectangles. The size of each rectangle is determined by an
array of data values. The color of each subrectangle is determined by the value of a second data
array and a Colormap.
The primary data array is a sorted set of values to map to the subrectangle areas. The placement
algorithm is adapted from Bruls, Mark and Huizing, Kees and Wijk, Jarke J. van (2000) Squarified
Treemaps. In Proceedings of the Joint Eurographics and IEEE TCVG Symposium on
Visualization.
By default, the algorithm fills either rows first or columns first determined by the aspect ratio of
the resulting subrectangles. The user may force drawing orientation by using the Orientation
property with Treemap.OrientationMethod.ColumnFirst or
Treemap.OrientationMethod.RowFirst.
A Colormap is a mapping from [0,1] to color values. The Treemap maps the minimum value of
the second data array to the color corresponding to 0 and the highest value to the color
corresponding to 1.
The TreemapLegend is enabled by setting its IsVisible property to true.
Example
In this example an array of country data is plotted as a Treemap. The area is proportional to the
country’s land area while the shading maps to the population. The “green-white-linear” Colormap
is used. The TreemapLegend is enabled by setting its IsVisible property to true.
using Imsl.Chart2D;
using System.Drawing;
public SampleTreemap() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double[] areas = {
6592735, 3855081, 3718691, 3705386,
3286470, 2967893, 1269338, 1068296};
double[] population = {
142.893540, 33.098932, 298.444215, 1313.973713,
188.078227, 20.264082, 1095.351995, 39.930091};
string[] names = {
Histogram
A histogram is a bar chart in which the height of the bars is proportional to the frequencies. A
histogram generally uses the same axis style as a scatter plot (i.e. the bars are numbered not
labeled.)
In IMSL C# Numerical Library, histograms are drawn using the Bar class, but its SetLabels
method is not used.
Example
In this example normally distributed random numbers are generated and placed into 20 uniformly
sized bins in the interval [-3,3]. Points outside of this interval are ignored. The bin counts are
scaled by the number of samples and the bin width. The scaled bin counts are charted using Bar
chart. The exact normal distribution is implemented as a ChartFunction and plotted.
The Legend is displayed by setting the legend node’s IsVisible property to true and defining
the bar chart’s Title attribute. The Legend is positioned on the chart by setting its Viewport
attribute.
using Imsl.Chart2D;
using System;
using System.Drawing;
using Imsl.Stat;
public SampleHistogram() {
int nSamples = 1000;
int nBins = 20;
chart.ChartTitle.SetTitle("Normal Distribution");
chart.Legend.IsVisible = true;
chart.Legend.SetViewport(0.7, 1.0, 0.2, 0.3);
chart.Legend.FillOutlineType = Chart.FILL_TYPE_NONE;
Polar Plot
The class Polar is used to plot (r,theta) data, where r is the distance from the origin and theta is
the angle from the positive x-axis. Data node (x, y) values are treated as if they were (r,theta)
values.
When a Polar node is created, a subtree of other nodes is created. These automatically created
nodes are shown in the following diagram.
Example
The function r = 0.5 + cos(q), for 0 <= q <= p is plotted.
using Imsl.Chart2D;
using System.Drawing;
using System;
public SamplePolar() {
Chart chart = this.Chart;
Polar axis = new Polar(chart);
Dendrogram Chart
A Dendrogram chart is a graphical way to display results from hierarchical cluster analysis. This
section describes the construction of a dendrogram chart.
Example
The data for this example is grouped into clusters using the Dissimilarities and
ClusterHierarchical classes. A Dendrogram node is then created as a child of an axis node.
The Dendrogram constructor requires input values from the ClusterHierarchical object.
The SetLables and SetLineColors methods are used to customize the look of the chart.
Labels are provided in a String array in the order of the input data and sorted by the
Dendrogram object to match the output order. Clusters are grouped by color based on the number
of elements in the array passed to the SetLineColors method.
using Imsl.Chart2D;
using Imsl.Stat;
using System.Drawing;
public SampleDendrogram() {
/*
1998 test data from 17 school districts in Los Angeles County.
double[,] data = {
{.38, 626.5, 601.3, 605.3},
{.18, 654.0, 647.1, 641.8},
{.07, 677.2, 676.5, 670.5},
{.09, 639.9, 640.3, 636.0},
{.19, 614.7, 617.3, 606.2},
{.12, 670.2, 666.0, 659.3},
{.20, 651.1, 645.2, 643.4},
{.41, 645.4, 645.8, 644.8},
{.07, 683.5, 682.9, 674.3},
{.39, 648.6, 647.8, 643.1},
{.21, 650.4, 650.8, 643.9},
{.24, 637.0, 636.9, 626.5},
{.09, 641.1, 628.8, 629.4},
{.12, 638.0, 627.7, 628.6},
{.11, 661.4, 659.0, 651.8},
{.22, 646.4, 646.2, 647.0},
{.33, 634.1, 632.0, 627.8}};
int nClusters = 4;
int[] iclus = clink.GetClusterMembership(nClusters);
int[] nclus = clink.GetObsPerCluster(nClusters);
Introduction
Quality improvement charts have a variety of uses. In this library the charts are organized into
three broad groups: Shewhart control charts, other control charts and process improvement charts.
The Shewhart control charts were originally described by the statistician Dr. Walter A. Shewhart
(1931). Since this early work, other charts have been developed for engineering and management
analysis of processes. In the 1980s customized charts were developed for other retrospective
analysis of quality management data.
This chapter discusses these IMSL C# Numerical Library Quality Control Charts: IMSL C#
Numerical Library provides these Quality Control Charts:
• “ShewhartControlChart and ControlLimit” on page 67
• “XbarS and SChart” on page 70
• “XbarR and RChart” on page 78
• “XmR” on page 82
• “PChart” on page 84
• “NpChart” on page 87
• “CChart” on page 89
• “UChart” on page 92
• “EWMA” on page 96
• “CuSum” on page 98
• “CuSumStatus” on page 100
• “Pareto Chart” on page 102
• “Cumulative Probability” on page 104
Shewhart Charts
While working for Western Electric in the 1920s, Dr. Shewhart developed a general, practical
approach to statistical monitoring of manufacturing processes. He advised managers on
64 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
implementing these within Western Electric and later published his work in Shewhart
(Montgomery, 1931). All Shewhart control charts share several characteristics in common. First,
the horizontal axis represents time or lot sequence, but they all have different vertical axes,
depending upon the chart time.
Next, all Shewhart control charts have a center line that is drawn parallel to the time axis. This
typically represents the mean of the process, but the value of the process mean can vary depending
upon which data are first used to design the chart. In some cases it will be the mean of the data
plotted, in others it could be the mean calculated from a much larger number of measurements on
the historical operation of the process.
Lastly, all Shewhart control charts have lines drawn to represent either the upper or lower control
limits. In most cases both control lines are present, in others where the data have a natural bound,
such as zero, only one of these control limits might be drawn.
Shewhart control charts are also broadly classified into two groups: variable and attribute data
charts. Variable control charts are used when the quality of interest is a continuous variable, such
as the diameter of a valve. If w is a continuous measure of a quality of interest, with mean µ w and
within-sample standard deviation σ w , then the center line is at µ w and the upper and lower
controls limits are at µ w ± kσ w . Typically k = 3 and the charts are called 3-sigma control charts.
Attribute control charts are used when qualities, not quantities are measured. For example, items
may be characterized as conforming or nonconforming to a specification. Items may also be
characterized as defective or nondefective. Examples of attributes include the number of failures
in a manufacturing run or the number of defects on a computer chip wafer.
When the number of defective or nondefective items are plotted then a PChart or an NpChart are
generally used to describe the non-comformity data. The NpChart is used to plot the number of
defects when all of the sample sizes are equal, and the PChart is used when the sample sizes are
unequal.
If a single item can have multiple defects then a CChart or UChart is used, depending upon
whether the area inspected for defect is consistent or varying. An example of multiple defects per
item would be the count of the number of scratches on mirrors. If all samples have an equal
opportunity for defects use CChart, otherwise use UChart. So to monitor the number of scratches
on mirrors use CChart when all mirrors being made are the same size and use UChart for mirrors
being made that are sized differently.
In IMSL C# Numerical Library the ShewhartControlChart class is the base of a number of
classes; it is not usually used by itself. Most of the charts in this chapter extend
ShewhartControlChart.
The following diagram can be used to determine the appropriate control chart to be used in a given
situation.
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 65
Variable Control Charts:
• XbarR estimates µw and σw using the ranges of the samples. It is best used
when the sample size of a continuous variable is between 2 and 10.
• RChart plots the sample ranges. It is typically used in conjunction with XbarR.
• XbarS estimates µw and σw using the means and standard deviations of the
samples. It is best used when the sample size of a continuous variable is at least
10.
• SChart plots the sample standard deviations. It is typically used in conjunction
with XbarS.
• XmR is a moving range chart. It is used when the sample size of a continuous
variable is one.
66 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
• EWMA (Exponentially Weighted Moving-Average) plots weighted moving
average values. It is used when the sample size of a continuous variable is one.
• PChart plots the rate of defects. It is used when defects are not rare.
• CChart plots the defect count. It is used when defects are rare.
• UChart plots the rate of defects. It is used when defects are rare.
Cumulative Probability
Cumulative probability charts are used if the defect rate is so small that there will be long runs
when the number of defects is zero.
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 67
The ShewhartControlChart uses the ControlLimit class for the upper and lower control
limits and for the center line. Additional control limits, such as the WECO limits, can be added to
a ShewhartControlChart. The line attributes can be used with ControlLimit to modify the
drawing of each control limit.
The ShewhartControlChart class can be used directly when the statistics are computed by the
user. In this example, data from Montgomery, Douglas C., Introduction to Statistical Quality
Control, 4th Ed,., 2001, New York: John Wiley and Sons, p. 406 is plotted. The code explicitly
sets the lower control limit to 7 and the upper control limit to 13.
Further citations throughout this chapter for data plotted from Montgomery appear as parenthetical
citations, e.g. (Montgomery 406).
68 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SampleShewhart() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
ShewhartControlChart xbars = new ShewhartControlChart(axis);
xbars.SetData(data);
xbars.AddLowerControlLimit().SetValue(7);
xbars.AddCenterLine().SetValue(10);
xbars.AddUpperControlLimit().SetValue(13);
}
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 69
public static void Main(string[] argv) {
System.Windows.Forms.Application.Run(new SampleShewhart());
}
}
Centerline = x
s
LCL= x − 3
c4 n
XbarS Example
The process of forging piston rings for automobile engines was monitored. The inside diameters of
25 samples, each containing 5 piston rings, were measured. The center line is at 74.001, the
average diameter of all of the measured piston rings. The upper and lower control limits are
determined by the average of the standard deviations of the 25 samples of 5 rings (Montgomery
215).
The SChart plots the in-sample standard deviations of the observations as well as control limits
computed using
s
UCL =
s +3 1 − c42
c4
Centerline = s
70 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
s
LCL =
s −3 1 − c42
c4
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 71
new double[] {73.985, 74.003, 73.993, 74.015, 73.988},
new double[] {74.008, 73.995, 74.009, 74.005, 74.004},
new double[] {73.998, 74, 73.99, 74.007, 73.995},
new double[] {73.994, 73.998, 73.994, 73.995, 73.99},
new double[] {74.004, 74, 74.007, 74, 73.996},
new double[] {73.983, 74.002, 73.998, 73.997, 74.012},
new double[] {74.006, 73.967, 73.994, 74, 73.984},
new double[] {74.012, 74.014, 73.998, 73.999, 74.007},
new double[] {74, 73.984, 74.005, 73.998, 73.996},
new double[] {73.994, 74.012, 73.986, 74.005, 74.007},
new double[] {74.006, 74.01, 74.018, 74.003, 74},
new double[] {73.984, 74.002, 74.003, 74.005, 73.997},
new double[] {74, 74.01, 74.013, 74.02, 74.003},
new double[] {73.982, 74.001, 74.015, 74.005, 73.996},
new double[] {74.004, 73.999, 73.99, 74.006, 74.009},
new double[] {74.01, 73.989, 73.99, 74.009, 74.014},
new double[] {74.015, 74.008, 73.993, 74, 74.01},
new double[] {73.982, 73.984, 73.995, 74.017, 74.013}
};
public SampleXbarS() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
XbarS xbars = new XbarS(axis, diameter);
xbars.UpperControlLimit.SetTitle("ucl = {0:0.0000}");
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Piston Ring Diameter");
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(73.985, 74.015);
}
SChart Example
This example uses the same piston ring data as was used in the XbarS example on page 70, but
now the standard deviations of the samples are plotted. The center line is at 0.009, the average of
the sample standard deviations.
Often the XbarS and SChart are plotted together. The XbarS class contains the static method
CreateCharts, which creates this pair of plots on a single chart.
72 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 73
new double[] {74, 73.984, 74.005, 73.998, 73.996},
new double[] {73.994, 74.012, 73.986, 74.005, 74.007},
new double[] {74.006, 74.01, 74.018, 74.003, 74},
new double[] {73.984, 74.002, 74.003, 74.005, 73.997},
new double[] {74, 74.01, 74.013, 74.02, 74.003},
new double[] {73.982, 74.001, 74.015, 74.005, 73.996},
new double[] {74.004, 73.999, 73.99, 74.006, 74.009},
new double[] {74.01, 73.989, 73.99, 74.009, 74.014},
new double[] {74.015, 74.008, 73.993, 74, 74.01},
new double[] {73.982, 73.984, 73.995, 74.017, 74.013}
};
public SampleSChart() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
SChart schart = new SChart(axis, diameter);
schart.UpperControlLimit.SetTitle("ucl = {0:0.0000}");
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Standard Deviations of " +
"Piston Ring Diameters");
axis.AxisY.AxisLabel.TextFormat = "0.000";
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(0.0, 0.02);
}
XbarSCombo Example
This example combines the charts in the XbarS and SChart examples. The Viewport attribute of
each is set so that they can appear on the same chart.
74 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 75
new double[] {74.004, 74, 74.007, 74, 73.996},
new double[] {73.983, 74.002, 73.998, 73.997, 74.012},
new double[] {74.006, 73.967, 73.994, 74, 73.984},
new double[] {74.012, 74.014, 73.998, 73.999, 74.007},
new double[] {74, 73.984, 74.005, 73.998, 73.996},
new double[] {73.994, 74.012, 73.986, 74.005, 74.007},
new double[] {74.006, 74.01, 74.018, 74.003, 74},
new double[] {73.984, 74.002, 74.003, 74.005, 73.997},
new double[] {74, 74.01, 74.013, 74.02, 74.003},
new double[] {73.982, 74.001, 74.015, 74.005, 73.996},
new double[] {74.004, 73.999, 73.99, 74.006, 74.009},
new double[] {74.01, 73.989, 73.99, 74.009, 74.014},
new double[] {74.015, 74.008, 73.993, 74, 74.01},
new double[] {73.982, 73.984, 73.995, 74.017, 74.013}
};
public SampleXbarSCombo() {
Chart chart = this.Chart;
ShewhartControlChart[] charts = XbarS.CreateCharts(chart, diameter);
AxisXY axis = (AxisXY)(charts[0].Axis);
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(73.985, 74.015);
axis = (AxisXY)(charts[1].Axis);
axis.AxisY.AxisLabel.TextFormat = "0.000";
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(0.0, 0.02);
((SChart)charts[1]).UpperControlLimit.SetTitle("ucl = {0:0.0000}");
}
XbarSUnequal Example
The example again uses the piston ring data, but now the sample size is not uniform. The upper
and lower control limits are now stair-step lines. The control limits are farther apart for smaller
samples.
It is also possible to plot just the XbarS or SChart with unequal sample sizes.
76 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 77
new double[] {74.012, 74.014, 73.998},
new double[] {74, 73.984, 74.005, 73.998, 73.996},
new double[] {73.994, 74.012, 73.986, 74.005},
new double[] {74.006, 74.01, 74.018, 74.003, 74},
new double[] {73.984, 74.002, 74.003, 74.005, 73.997},
new double[] {74, 74.01, 74.013},
new double[] {73.982, 74.001, 74.015, 74.005, 73.996},
new double[] {74.004, 73.999, 73.99, 74.006, 74.009},
new double[] {74.01, 73.989, 73.99, 74.009, 74.014},
new double[] {74.015, 74.008, 73.993, 74, 74.01},
new double[] {73.982, 73.984, 73.995, 74.017, 74.013}
};
public SampleXbarSUnequal() {
Chart chart = this.Chart;
ShewhartControlChart[] charts = XbarS.CreateCharts(
chart, diameter);
AxisXY axis = (AxisXY)(charts[0].Axis);
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(73.980, 74.020);
((SChart)charts[1]).UpperControlLimit.SetTitle(
"ucl = {0:0.0000}");
}
Centerline = x
3
LCL= x − R
d2 n
78 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
where x is the grand mean (the average of all obeservations), and d 2 is the mean of the
distribution of the ranges of n samples from the normal distribution with mean of zero and
standard deviation of one. The standard deviation of this distribution is d3 . Therefore
d3
R
d2
XbarR Example
This example creates an XbarR chart using the same piston ring data previously used for the
XbarS example. Here the in-sample ranges are used to compute the control limits, rather than the
in-sample standard deviations (Montgomery 215).
(Download Code)
using Imsl.Chart2D;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 79
using Imsl.Chart2D.QC;
public SampleXbarR() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
XbarR xbarr = new XbarR(axis, diameter);
xbarr.UpperControlLimit.SetTitle("ucl = {0:0.0000}");
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Piston Ring Diameter");
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(73.985, 74.015);
}
80 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
XbarRCombo Example
This example combines the XbarR chart with the corresponding RChart into a single chart. This
is done by adjusting the Viewport attribute values for the two subcharts (Montgomery 215).
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 81
new double[] {73.998, 74, 73.99, 74.007, 73.995},
new double[] {73.994, 73.998, 73.994, 73.995, 73.99},
new double[] {74.004, 74, 74.007, 74, 73.996},
new double[] {73.983, 74.002, 73.998, 73.997, 74.012},
new double[] {74.006, 73.967, 73.994, 74, 73.984},
new double[] {74.012, 74.014, 73.998, 73.999, 74.007},
new double[] {74, 73.984, 74.005, 73.998, 73.996},
new double[] {73.994, 74.012, 73.986, 74.005, 74.007},
new double[] {74.006, 74.01, 74.018, 74.003, 74},
new double[] {73.984, 74.002, 74.003, 74.005, 73.997},
new double[] {74, 74.01, 74.013, 74.02, 74.003},
new double[] {73.982, 74.001, 74.015, 74.005, 73.996},
new double[] {74.004, 73.999, 73.99, 74.006, 74.009},
new double[] {74.01, 73.989, 73.99, 74.009, 74.014},
new double[] {74.015, 74.008, 73.993, 74, 74.01},
new double[] {73.982, 73.984, 73.995, 74.017, 74.013}
};
public SampleXbarRCombo() {
Chart chart = this.Chart;
ShewhartControlChart[] charts =
XbarR.CreateCharts(chart, diameter);
AxisXY axis = (AxisXY)(charts[0].Axis);
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(73.985, 74.015);
}
XmR
XmR is a moving range chart. It is used when only samples of size one are available. The moving
range statistic is
MR=
i xi − xi −1
MR
UCL= x + 3
d 2,2
Centerline = x
82 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
MR
LCL= x − 3
d 2,2
Where x is the mean of the observations, MR is the mean of the moving ranges and is d 2,n is
the mean of the distribution of the ranges of n samples from the normal distribution with mean
zero and standard deviation one.
XmR Example
The viscosity of aircraft primer paint was measured. Since the paint is produced in batches, only
single samples are available (Montgomery 251).
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 83
33.75, 33.05, 34.00, 33.81, 33.46, 34.02, 33.68,
33.27, 33.49, 33.20, 33.62, 33.00, 33.54, 33.12, 33.84
};
public SampleXmR() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
XmR xmr = new XmR(axis, viscosity);
}
PChart
The PChart plots the proportion or fraction of defective products.
The production process is assumed to be stable and successive units are assumed independent. So
each unit produced is a realization of a Bernoulli random variable with parameter p, the proportion
defective.
The control limits are at
p (1 − p )
UCL= p + 3
n
Centerline = p
p (1 − p )
LCL= p − 3
n
By default, p is the proportion of defects observed in the data. To use a known p, set the attribute
Center to p.
PChart Example
Defects in the manufacturing of orange juice cans are measured. The number of defects in samples
of 50 cans is counted (Montgomery 290).
The y-axis labels and the title on the control limits have been changed to use a percentage format.
84 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SamplePChart() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
PChart pchart = new PChart(axis, sampleSize, numberDefects);
pchart.LowerControlLimit.SetTitle("lcl = {0:0.00%}");
pchart.CenterLine.SetTitle("center = {0:0.00%}");
pchart.UpperControlLimit.SetTitle("ucl = {0:0.00%}");
axis.AxisX.AxisTitle.SetTitle("Sample Number");
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 85
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Percent Defective");
axis.AxisY.AxisLabel.TextFormat = "P0";
}
PChartUnequal Example
In this example a PChart is computed with unequal sample sizes. The upper and lower control
limits are now stair-step lines (Montgomery 300).
The y-axis labels and the title on the center line have been changed to use a percentage format.
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
86 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
public class SamplePChartUnequal : FrameChart {
static int[] sampleSize = {
100, 80, 80, 100, 110, 110, 100, 100, 90, 90, 110, 120, 120,
120, 110, 80, 80, 80, 90, 100, 100, 100, 100, 90, 90
};
static int[] numberDefects = {
12, 8, 6, 9, 10, 12, 11, 16, 10, 6, 20, 15, 9, 8, 6, 8, 10,
7, 5, 8, 5, 8, 10, 6, 9
};
public SamplePChartUnequal() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
PChart pchart = new PChart(axis, sampleSize, numberDefects);
pchart.CenterLine.SetTitle("center = {0:0.00%}");
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Percent Defective");
axis.AxisY.AxisLabel.TextFormat = "P0";
axis.AxisY.SetWindow(0.0, 0.2);
axis.AxisY.AutoscaleInput = 0;
}
NpChart
NpChart is similar to PChart, except that the number of defects per sample is plotted, not the
sample rate. The position of the control limits are given by
np + 3 np (1 − p )
UCL =
Centerline = np
np − 3 np (1 − p )
LCL =
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 87
NpChart Example
This example uses the orange juice can manufacturing data used earlier in the PChart example. In
this example, the number of defects, rather than the defect rate is plotted.
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SampleNpChart() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
NpChart npchart = new NpChart(axis, sampleSize, numberDefects);
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
88 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
axis.AxisY.AxisTitle.SetTitle("Number Defective");
}
CChart
CChart plots the number of defects or nonconformities.
UCL= c + k c
Centerline = c
LCL= c − k c
Where c is the mean number of defects per sample and k is the value of the ControlLimit
attribute for the line.
CChart Example
The number of defects in samples of 100 printed circuit boards was measured (Montgomery 321).
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 89
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SampleCChart() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
CChart cchart = new CChart(axis, numberDefects);
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Number of Defects");
}
90 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
}
}
CChartOmit Example
This is similar to the previous chart, but two points are omitted from the statistical computations.
To do this, first the censored chart is created with the bad points omitted. The positions of the
control limits in this chart are saved. This censored chart is then removed from the chart.
The second step is to create a chart using later data points, but with the position of the control limit
limits set to the values computed using the censored data.
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 91
static int[] censoredNumberDefects = {
21, 24, 16, 12, 15, /*5,*/ 28, 20, 31, 25, 20, 24, 16,
19, 10, 17, 13, 22, 18, /*39,*/ 30, 24, 16, 19, 17, 15
};
public SampleCChartOmit() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Number Defective");
}
UChart
UChart is a chart for monitoring the defect rate when defects are rare.
u
UCL= u + 3
n
Centerline = u
92 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
u
LCL= u − 3
n
Where u is the observed average number of defects per unit and n is the number of inspection
units.
UChart Example
(Download Code)
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 93
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SampleUChart() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
UChart uchart = new UChart(axis, sampleSize, numberDefects);
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Defects per Unit");
}
UChartUnequal Example
94 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SampleUChartUnequal() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
UChart uchart = new UChart(axis, inspectionUnitsPerRoll,
numberDefects);
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("Defects per Unit");
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 95
}
EWMA
EWMA is the exponentially weighted moving average control chart. It is very effective in detecting
small process shifts, but is slower than Shewhart charts at detecting large process shifts.
The exponentially weighted moving average is defined to be
zi = λ xi + (1 − λ ) zi −1
where 0 < λ ≤ 1 is a constant and the starting value is µ0 , the expected mean.
The control limits in EWMA are defined by the equations:
λ 1 − (1 − λ )2i
UCL = µ0 + 3σ
2−λ
Centerline = µ0
λ
LCL = µ0 − 3σ [1 − (1 − λ ) ]
2i
2−λ
Where µ0 is the historical mean, σ is the historical standard deviation, and i is the sample
number. Because of the presence of i the control limits are stair-steps, not constants.
EWMA Example
The expected mean is 10., the expected standard deviation is 1.0 and λ is 0.10. Also, the control
limit values are 2.7, not the default value of 3 (Montgomery 428).
96 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SampleEWMA() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double lambda = 0.10;
double mean = 10.0;
double stdev = 1.0;
EWMA ewma = new EWMA(axis, data, lambda, mean, stdev);
ewma.LowerControlLimit.ControlLimitValue = -2.7;
ewma.UpperControlLimit.ControlLimitValue = 2.7;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 97
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.SetWindow(9.3, 10.8);
axis.AxisY.AutoscaleInput = 0;
}
CuSum
CuSum is the cumulative sum control chart. It plots the cumulative sum of the deviations of the
expected value. If µ0 is the expected mean for a process and xi are the sample means then the
cumulative sum is
Ci = Ci −1 + ( xi − µ0 )
CuSum Example
The data used is the same as for the EWMA example.
98 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
public SampleCuSum() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
CuSum cusum = new CuSum(axis, data);
cusum.ExpectedMean = 10.0;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 99
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("CuSum");
}
CuSumStatus
CuSumStatus is a tabular or status CuSum chart. The tabular CuSum statistics are
(
C i+ max 0, xi − ( µ0 + K ) + C i+−1
= )
= (
C i− max 0, ( µ0 + K ) − xi + C i−−1 )
By default, both statistics have initial value zero. The parameter K is the slack value (or allowance
or reference value) and µ0 is the expected mean.
+
The CuSumStatus chart contains two bar charts: a bar chart of C above the x-axis and a bar
i
−
chart of C below the x-axis. There are also control limits at plus and minus H. The value of H
i
can be set either as an absolute value or as a relative value h. They are related by H = hσ , where
σ is the standard deviation. By default, bars which are out-of-control are filled red while in-
control bars are green. The data is also plotted on the chart.
+ − + −
The CuSumStatus has a Print method to print the C and C values as well as N and N
i i i i
+ +
, where N is the number of consecutive periods since C rose above zero.
i i
CuSumStatus Example
This example uses the same data as used for the CuSum and EWMA examples. In this example
H = 4σ .
100 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
using System.Drawing;
public SampleCuSumStatus() {
double expectedMean = 10;
double slackValue = 0.5;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 101
new CuSumStatus(axis, data, expectedMean, slackValue);
cusumStatus.RelativeH = 4;
cusumStatus.Print();
axis.AxisX.AxisTitle.SetTitle("Sample Number");
axis.AxisX.AxisLabel.TextFormat = "0";
axis.AxisY.AxisTitle.SetTitle("C+ / C-");
axis.AxisX.SetWindow(0, 30);
axis.AxisX.AutoscaleInput = 0;
cusumStatus.AddDataMarkers();
cusumStatus.DataMarkers.MarkerSize = 0.5;
cusumStatus.DataMarkers.MarkerColor = Color.Blue;
cusumStatus.DataMarkersAxis.AxisY.AxisTitle.SetTitle(
"Original Data");
cusumStatus.BarPlus.GetBarSet(0,0).FillType =
Data.FILL_TYPE_NONE;
cusumStatus.BarMinus.GetBarSet(0,0).FillType =
Data.FILL_TYPE_NONE;
cusumStatus.BarPlus.GetBarSet(0,0).FillOutlineColor =
Color.Green;
cusumStatus.BarMinus.GetBarSet(0,0).FillOutlineColor =
Color.Green;
}
Pareto Chart
A ParetoChart is a frequency distribution of attribute data. The bars are ordered by the number
of defects, with the attribute category having the largest number of defects appearing first.
There is an option to add a cumulative percentage line to the chart. This is the percent of defects
accounted for by the current item and all items to its left. If the cumulative percentage line is
added, a second axis is created on the right representing the cumulative percentage from 0% to
100%. The units of the original axis, which always appear on the left, represent the number of
defects, 36.
ParetoChart Example
The number of defects attributable to 1 of 19 of a variety of causes was collected (Montgomery
179). In this example, the “Incorrect dimensions” defect category has the largest number of
defects, 36.
102 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 103
"Primer cans damaged",
"Voids in casting",
"Delaminated composite",
"Incorrect dimensions",
"Improper test procedure",
"Salt-spray failure"
};
public SampleParetoChart() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
ParetoChart pareto = new ParetoChart(axis, labels, numberDefects);
pareto.LabelType = ParetoChart.LABEL_TYPE_Y;
pareto.TextFormat = "0";
pareto.FillColor = System.Drawing.Color.Blue;
axis.AxisX.AxisLabel.TextAngle = 90;
double[] vp = axis.GetViewport();
vp[0] = 0.1;
vp[3] = 0.7;
axis.SetViewport(vp);
cumulativeLine.Axis.SetViewport(vp);
}
Cumulative Probability
If the defect rate is very small there will be long runs when the number of defects is zero. In this
situation the CChart and UChart are ineffective. An alternative to defect counts is to measure the
time between defects.
If the distribution of defect occurrences is Poisson, then the distribution of times between defects
is exponential. Unfortunately the exponential distribution is highly skewed. Nelson suggested
1/ 3.6
transforming to Weibull random variables using the transformation x = y .
CumulativeProbability Example
The number of hours between failures is measured. A normal probability plot is constructed from
the transformed data. A linear regression to the transformed data is also computed. To fit a
104 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
regression with the normal probability axis, the regression variable needs to be transformed using
the inverse normal cumulative distribution function (Montgomery, Example 6-21, 327).
(Download Code)
using Imsl.Chart2D;
using Imsl.Chart2D.QC;
using Imsl.Stat;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 105
public SampleCumulativeProbability() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double a = ticks[0];
double b = ticks[ticks.Length-1];
chart.ChartTitle.SetTitle("Normal Probability Plot");
axis.AxisX.AxisTitle.SetTitle(
"Transformed Time between failures");
axis.AxisY.AxisTitle.SetTitle("Cummulative Probability");
axis.AxisY.Transform = Axis.TRANSFORM_CUSTOM;
Transform transform = new NormalTransform();
axis.AxisY.CustomTransform = transform;
axis.AxisY.AutoscaleInput = Axis.AUTOSCALE_OFF;
axis.AxisY.SetWindow(a, b);
axis.AxisY.TextFormat = "P1";
axis.AxisY.SetTicks(ticks);
axis.AxisY.MinorTick.IsVisible = false;
int n = timeBetweenFailures.Length;
double[] x = new double[n];
double[] y = new double[n];
for (int i = 0; i < x.Length; i++) {
x[i] = System.Math.Pow(timeBetweenFailures[i], 1.0/3.6);
}
Sort.Ascending(x);
for (int i = 0; i < x.Length; i++) {
y[i] = (double)(i+0.5) / (double)n;
}
Data data = new Data(axis, x, y);
data.DataType = Data.DATA_TYPE_MARKER;
data.MarkerType = Data.MARKER_TYPE_FILLED_CIRCLE;
106 • IMSL C# Chart Programmer’s User Guide Chapter 3: Quality Control and Improvement Charts
private double scaleA, scaleB;
Chapter 3: Quality Control and Improvement Charts IMSL C# Chart Programmer’s User Guide • 107
Chapter 4: Drawing Elements
Line Attributes
All lines, except for those used to draw markers or outline filled regions, are affected by the
attributes described in this section. These include axis lines and lines drawn when a Data node is
rendered with its DataType attribute having its DATA_TYPE_LINE bit set.
Attribute LineColor
LineColor is a Color-valued attribute that determines the color of the line. Its default value is
Color.Black. IMSL charting is largely made up of classes that inherit from ChartNode.
Therefore AbstractChartNode.LineColor should be used to set the line color.
Attribute LineWidth
LineWidth is a double-valued attribute that determines the thickness of the lines. Its default value
is 1.0. IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
Attribute LineDashPattern
LineDashPattern is a double-array-valued attribute that determines the line pattern used to
draw the line. It defaults to a solid line. Alternate entries in the array represent the lengths of the
opaque and transparent segments of the dashes. IMSL charting is largely made up of classes that
inherit from ChartNode. Therefore ChartNode.SetLineDashPattern should be used to set
the line dash pattern.
Some dash patterns are defined. They are DASH_PATTERN_DOT, DASH_PATTERN_DASH and
DASH_PATTERN_DASH_DOT.
Samples
Marker Attributes
Markers are drawn when a Data node is rendered with its DataType attribute having its
DATA_TYPE_MARKER bit set. Drawing of markers is affected by the attributes described in this
section. Note that even though some markers are drawn using lines, the line attributes do not apply
to markers.
An alternative to markers are images, which can be used to draw arbitrary symbols instead of
markers.
Attribute MarkerType
MarkerType is an integer-valued attribute that determines which marker will be drawn. There are
constants defined in ChartNode for the marker types. IMSL charting is largely made up of classes
that inherit from ChartNode. Therefore ChartNode.MarkerType should be used to set the line
dash pattern. The default value is MARKER_TYPE_PLUS. The following are all of the defined
marker types. For clarity, these are drawn larger than normal.
Chapter 4: Drawing Elements IMSL C# Chart Programmer’s User Guide • 109
Attribute MarkerColor
MarkerColor is a Color-valued attribute that determines the color of the marker. Its default
value is Color.Black. IMSL charting is largely made up of classes that inherit from ChartNode.
Therefore AbstractChartNode.MarkerColor should be used to set the marker color.
Attribute MarkerSize
MarkerSize is a double-valued attribute that determines the size of the marker. IMSL charting is
largely made up of classes that inherit from ChartNode. Therefore
AbstractChartNode.MarkerSize should be used to set the marker size. Its default value is
1.0. The actual size of the drawn marker, in pixels, is 0.007*MarkerSize*width, where width is the
width of the Control containing the chart.
Attribute MarkerDashPattern
MarkerDashPattern is a double-array-valued attribute that determines the line pattern used to
draw the marker. IMSL charting is largely made up of classes that inherit from ChartNode.
Therefore ChartNode.SetMarkerDashPattern should be used to set the marker dash pattern.
It defaults to a solid line. Alternate entries in the array represent the lengths of the opaque and
transparent segments of the dashes.
Some dash patterns are defined. They are DASH_PATTERN_DOT, DASH_PATTERN_DASH and
DASH_PATTERN_DASH_DOT.
Samples
FillOutlineColor
FillOutlineColor is a Color-valued attribute that determines the color used to outline the
filled regions. IMSL charting is largely made up of classes that inherit from ChartNode.
Therefore ChartNode.FillOutlineColor should be used to set the fill outline color. The
outline is drawn only if the attribute FillOutlineType has the value FILL_TYPE_SOLID. Its
default value is Color.Black.
FillType
FillType is an integer-value attribute that turns on or off the drawing of the interior of filled
areas. IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
ChartNode.FillType should be used to set the fill outline color. Its value should be
FillColor
FillColor is a Color-valued attribute that determines the color used to fill a region with a solid
color. Its default value is Color.Black. IMSL charting is largely made up of classes that inherit
from ChartNode. Therefore AbstractChartNode.FillColor should be used to set the fill
color.
Gradient
Gradient is a Color array-valued attribute that fills a region with a gradient color. It does not
have a default value. IMSL charting is largely made up of classes that inherit from ChartNode.
Therefore ChartNode.SetGradient should be used to set the gradient.
The value of Gradient should be an array of four colors. These are the colors at the four corners
of a square. In order they are: lower-left, lower-right, upper-right and upper-left.
• If the lower-side colors are equal (color[0] equals color[1]) and the upper-
side colors are equal (color[2] equals color[3]), then a vertical gradient is
drawn.
• If the left-side colors are equal (color[0] equals color[3]) and the right-side
colors are equal (color[1] equals color[2]), then a horizontal gradient is
drawn.
Vertical SetGradient(Color.Yellow,
Color.Yellow, Color.Red,
Color.Red)
Horizontal SetGradient(Color.Yellow,
ColorRed, Color.Red,
Color.Yellow)
Diagonal SetGradient(Color.Yellow,
null, Color.Red, null)
Diagonal SetGradient(null,
Color.Yellow, null, Color.Red)
FillPaint
FillPaint is a Brush-valued attribute that fills a region with a tiled pattern. It does not have a
default value. The method ChartNode.SetFillPaint is used to specify the FillPaint object.
The class FillPaint contains utilities to define some useful paint patterns.
FillPaint.VerticalStripe(10, 5, Color.Yellow,Color.Blue)
FillPaint.HorizontalStripe(10, 5,
Color.Yellow,Color.Blue)
FillPaint.Diamond(36, 5, Color.Blue,Color.Yellow)
FillPaint.Checkerboard(24, Color.Red,Color.Yellow)
The FillPaint attribute can also be set using an Image, which is used to tile filled regions.
Text Attributes
Attribute Font
Text is drawn using Font objects constructed using the FontName, FontSize and FontStyle
attributes. The ChartNode.Font property does not save the Font object, but sets these three
attributes.
This arrangement allows one to specify font size and style at lower nodes and change the font face
at the root node.
Multiline text strings are allowed. They are created by newline characters in the string that creates
the text item.
Attribute FontName
FontName is a string-valued attribute that specifies the logical font name or a font face name.
IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
AbstractChartNode.FontName should be used to set the font name.
Attribute FontStyle
FontStyle is a System.Drawing. FontStyle-valued attribute that specifies the font style. This
can be a bitwise combination of any of the standard types in the FontStyle enumeration. Its default
value is FontStyle.Regular. IMSL charting is largely made up of classes that inherit from
ChartNode. Therefore AbstractChartNode.FontStyle should be used to set the font style.
Attribute TextAngle
TextAngle is an integer-valued attribute that specifies the angle, in degrees, at which text is
drawn. IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
ChartNode.TextAngle should be used to set the text angle. The angle is measured
counterclockwise. Its default value is 0.
Attribute TextColor
TextColor is a Color-valued attribute that specifies the color in which the text is drawn. Its
default value is Color.Black. IMSL charting is largely made up of classes that inherit from
ChartNode. Therefore AbstractChartNode.TextColor should be used to set the text color.
Attribute TextFormat
TextFormat is a Format-valued or a String-valued attribute that specifies how to format string
objects. IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
AbstractChartNode.TextFormat should be used to set the text format.The .NET Framework
provides a wide variety of formatting options and convenient standard format specifiers.
For numeric values, specifiers like “C” for Currency, ‘E” for scientific exponential, and “P” for
Percentage are available. DateTime objects can use “d” for a Short date pattern or “D” for a Long
date pattern and many others. Custom Numeric Format Strings can be created using “0” for a zero
placeholder, "#" for a digit placeholder and others. The default value is “0.00”
Attribute TextFormatProvider
TextFormatProvider is an IFormatProvider-valued attribute that supplies culture-specific
formatting information. IMSL charting is largely made up of classes that inherit from ChartNode.
Therefore AbstractChartNode.TextFormatProvider should be used to set the text format
provider. The default value is null so the default Culture-Info, DateTimeFormatInfo and
NumberFormatInfo are utilized.
Attribute Title
Title is a Text-valued that contains the title for a node. IMSL charting is largely made up of
classes that inherit from ChartNode. Therefore, most chart classes will set a title using method
Samples
Following are sample fonts in styles plain, italic and bold. These may look different on different
platforms.
If the text is drawn at an angle, then the alignment is relative to the horizontally/ vertically aligned
bounding box of the text.
Attribute LabelType
The attribute LabelType takes on one of the values listed below. IMSL charting is largely made
up of classes that inherit from ChartNode. Therefore AbstractChartNode.LabelType should
be used to set the label type.
• LABEL_TYPE_NONE for no label. This is the default.
using Imsl.Chart2D;
using System.Drawing;
public SampleLabels() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
chart.Legend.IsVisible = true;
Annotation
The Annotation class can be used to place text on a chart. In this example a text message is
drawn at (1,7) as its bottom left corner.
Note that it is a two line message. Newlines in the string are reflected in the chart.
The text alignment is set to TEXT_X_LEFT| TEXT_Y_BOTTOM, so (1,7) is the lower left corner of
the text’s bounding box (see “Attribute Title” on page 115).
using Imsl.Chart2D;
using System.Drawing;
public SampleLabelMessage() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
AxisXY
The AxisXY node is the basis of many chart types, including scatter, line, area and error bar. Its
parent node must be the root Chart node.
When an AxisXY node is created, it creates two Axis1D nodes. They can be obtained by using the
methods AxisXY.AxisX and AxisXY.AxisY. Each of the Axis1D nodes in turn creates
additional child nodes, as seen in the diagram below.
Properties can be chained together, so the x-axis line can be retrieved using
axis.AxisX.AxisLine
The code to set the x-axis line to blue is
axis.AxisX. AxisLine.LineColor = Color.Blue;
using Imsl.Chart2D;
using System.Drawing;
public Line()
{
Chart chart = Chart;
AxisXY axis = new AxisXY(chart);
chart.ChartTitle.FontSize = 14;
chart.ChartTitle.FontStyle = System.Drawing.FontStyle.Italic;
chart.ChartTitle.SetTitle("Line Chart");
Window
Window is a double-array-valued attribute that contains the axis limits. Its value is {min, max}.
The AxisXY.SetWindow should be used to set the window . For the above chart, the value of the
Window attribute for the x-axis is [0, 3] and for the y-axis is
[1, 8].
Number
Number is an integer-valued attribute that contains the number of major tick marks along an axis.
IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
AbstractChartNode.Number should be used to set the number of ticks. In an Axis1D node its
default value is 5, which is also the value in the above example.
Density
Density is the number of minor tick marks per major tick mark. IMSL charting is largely made
up of classes that inherit from ChartNode. Therefore AbstractChartNode.Density should be
used to set the Density. Its default value is 4, which is also the value in the example.
Transform
The x- and y-axes may be linear, logarithmic, or customized, as specified by the Transform
attribute. IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
AbstractChartNode.Transform should be used to set the transform. This attribute can have
the values:
• TRANSFORM_LINEAR indicating a linear axis. This is the default.
Autoscale
Autoscaling is used to automatically determine the attribute Window (the range of numbers along
an axis) and the attribute Number (the number of major tick marks). The goal is to adjust the
attributes so that the data fits on the axes and the axes have “nice” numbers as labels.
Autoscaling is done in two phases. In the first (“input”) phase the actual range is determined. In
the second (“output”) phase chart attributes are updated.
Attribute AutoscaleInput
The action of the input phase is controlled by the value of attribute AUTOSCALE_INPUT in the axis
node. IMSL charting is largely made up of classes that inherit from ChartNode. Therefore
AbstractChartNode.AutoscaleInput should be used to indicate how auto scaling is to be
done. It can have one of three values.
• AUTOSCALE_OFF turns off autoscaling.
• AUTOSCALE_DATA scans the values in the Data nodes that are attached to the
axis to determine the data range. This is the default value.
• AUTOSCALE_WINDOW uses the value of the Window attribute to determine the
data range.
Attribute AutoscaleOutput
The value of the AUTOSCALE_OUTPUT attribute can be the bitwise combination of the following
values.
• AUTOSCALE_OFF no attributes updated.
Scientific Notation
If the values along an axis are large, scientific notation is more readable than a decimal format
with many zeros. In this example the y-axis is labeled with scientific notation where each number
has exactly two fractional digits displayed. Use axis.AxisY.AxisLabel.TextFormat and for-
mat pattern, "0.00E0". See Formatting Types in the .Net Framework Developer’s Guide for details
on number formatting pattern.
Date Labels
If the TextFormat attribute for an axis is set to a date format, then the axis is scaled and labeled
as a date/time axis instead of as a real axis.
Date information passed to the Data constructor must be a double or long, usually obtained from
the Ticks property of a DateTime instance.
Skipping Weekends
An additional feature of Date axes is the ability to skip weekends. This feature is often needed for
charting stock price data.
To skip weekends it is necessary to adjust the autoscaling for weekdays-only. This is done by
setting the attribute SkipWeekends to true. It is also necessary to set a custom transformation,
TransformDate, on the axis. This is shown in the following example, which is a modification of
the example in the previous section.
Skipping weekends is intended only for data sets where no weekend data exists. It will not give
the expected results if the data set contains weekend data.
String Labels
Any array of strings can be used to label the tick marks using the SetLabels(String[])
method. The SetLabels(String[]) method sets the Number attribute to the number of strings.
Axis Title
The AxisTitle node controls the titling of an axis. The drawing of this node is controlled by the
“Text Attributes” on page 114.
Axes are titled with the value of the Title attribute in the AxisTitle node. By default, the title
is empty.
(Download Code)
using Imsl.Chart2D;
public SampleAxisTitle() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double[] y = {4, 2, 3, 9};
Data data = new Data(axis, y);
axis.AxisX.AxisTitle.SetTitle("The X Axis");
axis.AxisY.AxisTitle.SetTitle("The Y Axis");
}
More Formatting
An axis title, like any other text string, can have further formatting applied.
The FontName attribute is set to “Serif” in the axis node. This value is then inherited by all of its
ancestor nodes.
The TextColor attribute is set differently in the x-axis and y-axis nodes. Note that this setting is
inherited by both the AxisTitle and nodes within the same axis.
The y-axis title node has its FontSize attribute set to 20, so it is larger. Also, its TextAngle
attribute is set to -90. By default, the y-axis title is drawn at a 90 degree angle, so the -90 degree
setting flips the title from its usual position.
(Download Code)
using Imsl.Chart2D;
public SampleAxisTitleFormatted() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double[] y = {4, 2, 3, 9};
Data data = new Data(axis, y);
axis.FontName = "Serif";
axis.AxisX.TextColor = Color.Blue;
axis.AxisX.AxisTitle.SetTitle("The X Axis");
axis.AxisY.TextColor = Color.DarkGray;
axis.AxisY.AxisTitle.SetTitle("The Y Axis");
axis.AxisY.AxisTitle.TextAngle = -90;
axis.AxisY.AxisTitle.FontSize = 20;
}
Axis Unit
The AxisUnit node controls the placing of a unit tag on an axis. The tag is the value of the
Title attribute in the node. By default, it is empty.
The axis unit is used for labeling only; it has no other effect on the chart.
using Imsl.Chart2D;
public SampleAxisUnit() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double[] y = {4, 2, 3, 9};
Data data = new Data(axis, y);
axis.AxisX.AxisTitle.SetTitle("Time");
axis.AxisY.AxisTitle.SetTitle("Height");
axis.AxisX.AxisUnit.SetTitle("hours");
axis.AxisY.AxisUnit.SetTitle("meters");
}
Attribute TickLength
The length of the tick marks is proportional to the screen size. They can be made relatively longer
or shorter by setting the attribute TickLength. IMSL charting is largely made up of classes that
inherit from ChartNode. Therefore AbstractChartNode.TickLength should be used to set
the tick length. Its default value is 1.0. If its value is negative, the tick marks are drawn in the
opposite direction: i.e., into the center of the plot region rather than away from it.
Attribute Number
Number is the number of major tick marks along an axis. IMSL charting is largely made up of
classes that inherit from ChartNode. Therefore AbstractChartNode.Number should be used to
set the number of ticks.
Attribute Density
Density is the number of minor tick marks in each interval between major tick marks. The minor
ticks are equally spaced in user coordinates. If the Transform attribute in not
TRANSFORM_LINEAR, then they will not be equally spaced on the screen. IMSL charting is largely
made up of classes that inherit from ChartNode. Therefore AbstractChartNode.Density
should be used to set the Density.
Attribute FirstTick
The FirstTick attribute, in an Axis1D node, is the position of the first major tick mark.
Axis1D.FirstTick should be used to set the location of the first tick mark. The default value is
the 0-th element of the Windows attribute.
Attribute TickInterval
The TickInterval attribute, in an Axis1D node, is the interval between tick marks in the user
coordinates. “Axis1D.TickInterval should be used to set the tick interval. If this attribute is
not explicitly set, its value is computed from the attributes Number, Window and Transform.
Example
This example shows the effects of the tick mark attributes. Note that autoscaling is turned off so
that the attribute values are not overridden (see “Autoscale” on page 125).
On the x-axis, there are four major tick marks (Number), starting with 0.5 (FirstTick) at an
interval of 1.5 (TickInterval). There are three minor tick intervals (Density) between each
major tick mark. The tick marks are twice as long and are drawn in the opposite direction as
normal (TickLength).
On the y-axis, the tick mark locations are set explicitly by the attribute Ticks. This automatically
sets the attribute Number. The TickLength is set to -1, so the tick marks are drawn inward (to the
right) instead of outward (to the left).
using Imsl.Chart2D;
using System.Drawing;
public SampleTicks() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
axis.AutoscaleOutput = Axis.AUTOSCALE_OFF;
axis.AxisX.SetWindow(0.0, 6.0);
axis.AxisX.Density = 3;
axis.AxisX.Number = 4;
axis.AxisX.FirstTick = 0.5;
axis.AxisX.TickInterval = 1.5;
axis.AxisX.TickLength = -2;
axis.AxisY.SetWindow(0.0, 10.0);
double[] ticksY = {0.5, 2.0, 3.0, 6.0, 10.0};
axis.AxisY.SetTicks(ticksY);
axis.AxisY.TickLength = -1;
Grid
The Grid node controls the drawing of grid lines on a chart. The Grid is created by Axis1D as its
child. It can be retrieved using the property Axis1D.Grid.
By default, Grid nodes are not drawn. To enable them, set their IsVisible property to true.
Grid nodes control the drawing of the grid lines perpendicular to their parent axis. So the x-axis
Grid node controls the drawing of the vertical grid lines.
Example
In this example, the x-axis grid lines are painted light gray and the y-axis grid lines are pink.
using Imsl.Chart2D;
using System.Drawing;
public SampleGrid() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
axis.AxisX.Grid.IsVisible = true;
axis.AxisY.Grid.IsVisible = true;
axis.AxisX.Grid.LineColor = Color.LightGray;
axis.AxisY.Grid.LineColor = Color.Pink;
Custom Transform
A custom transformation allows a user-defined mapping of data to an axis. A custom transform is
used when the Transform attribute has the value TRANSFORM_CUSTOM. The custom transform is
the value of the CustomTransform attribute set with
AbstractChartNode.CustomTransform.
A custom transform implements the Transform interface. This interface has three methods. One,
SetupMapping, is called first to allow the transformation to initialize itself. The other two,
MapUserToUnit and MapUnitToUser, specify a mapping from the window interval onto [0,1].
These methods must each be the inverse of the other.
using Imsl.Chart2D;
using Imsl.Stat;
using System.Drawing;
public SampleProbability() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
double[] ticks = {
0.001, 0.005, 0.01, 0.02, 0.05, 0.10, 0.20, 0.30,
0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 0.95, 0.98,
0.99, 0.995, 0.999};
double a = ticks[0];
double b = ticks[ticks.Length-1];
axis.AxisX.AxisTitle.SetTitle("x");
axis.AxisY.AxisTitle.SetTitle("Probability");
axis.AxisY.Transform = Axis.TRANSFORM_CUSTOM;
Multiple Axes
An IMSL C# Numerical Library chart can contain any number of axes. Each axis has its own
mapping from the user coordinates to the device (screen) coordinates.
Normally, the x-axis is at the bottom of the chart and the y-axis is to the left. The attribute Type
can be changed to move the x-axis to the top of the chart and/or the y-axis to the right of the chart.
Axis can be moved from the chart edge, either away from the chart or into the middle of the chart
data area, by setting the attribute Cross.
Attribute Type
The attribute Type specifies the position of an x or y-axis. Axis1D.Type should be used to set the
axis type. Applied to the x-axis it can have the values AXIS_X (the default) or AXIS_X_TOP.
Applied to the y-axis, it can have the value AXIS_Y (the default) or AXIS_Y_RIGHT.
Attribute Cross
The Cross attribute specifies the coordinates of the intersection of the x-axis and y-axis.
AxisXY.SetCross should be used to set the coordinates. This can be inside or outside of the
chart body. Cross can be used to place multiple y-axes to the left of the chart or multiple x-axes
below the chart.
Example
Two data sets are plotted, each on its own axis. The first (blue) axis is left in the default position
with the y-axis on the left. The second (pink) axis has its y-axis moved to the left.
(Download Code)
using Imsl.Chart2D;
using System.Drawing;
public SampleAxesLeftRight() {
Chart chart = this.Chart;
Cross Example
Multiple x and y-axes are shown.
The Cross attribute is used to specify that the second (pink) axes intersect at (-8,10), in the pink-
axis coordinate system.
The Viewport attribute is changed in both sets of axes to shrink the size of the chart body and
leave more room to the left and below the chart for the pink axes.
using Imsl.Chart2D;
using System.Drawing;
public SampleAxesCross() {
Chart chart = this.Chart;
Background
Background controls the drawing of the chart’s background. It is created by Chart as its child. It
can be retrieved from a Chart object using the Chart.Background property.
The fill area attributes in the Background node determine how the background is drawn (see “Fill
Area Attributes” on page 111).
The attribute FillType has the global default value of FILL_TYPE_SOLID. The attribute FillColor
attribute is set to Color.White in this node.
using Imsl.Chart2D;
using System.Drawing;
public SampleBackgroundSolid() {
Chart chart = this.Chart;
chart.Background.FillType = ChartNode.FILL_TYPE_SOLID;
chart.Background.FillColor = Color.Pink;
AxisXY axis = new AxisXY(chart);
double[] y = {4, 2, 3, 9};
new Data(axis, y);
}
(Download Code)
using Imsl.Chart2D;
using System.Drawing;
public SampleBackgroundGradient() {
Chart chart = this.Chart;
chart.Background.FillType = ChartNode.FILL_TYPE_GRADIENT;
Pattern Background
To set the background to a color pattern:
• set the attribute FillType to FILL_TYPE_PAINT, and
• set the attribute “FillPaint” on page 113 to the desired pattern.
For example the following code sets the background to yellow/orange checkerboard pattern. See
“Fill Area Attributes” on page 111 for more information on patterns.
using Imsl.Chart2D;
using System.Drawing;
public SampleBackgroundPaint() {
Chart chart = this.Chart;
chart.Background.FillType = ChartNode.FILL_TYPE_PAINT;
Brush brush = FillPaint.Checkerboard(
24, Color.Yellow, Color.Orange);
chart.Background.SetFillPaint(brush);
AxisXY axis = new AxisXY(chart);
double[] y = {4, 2, 3, 9};
new Data(axis, y);
}
Legend
The legend is used to identify data sets. Data nodes that have their Title attribute defined are
automatically included in the legend box.
The Legend node is automatically created by the Chart node as its child. By default, the Legend
is not drawn because its IsVisible property is set to false.
using Imsl.Chart2D;
using System.Drawing;
public SampleSimpleLegend() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
chart.Legend.IsVisible = true;
Legend Example
This example shows more of the attributes that affect a legend. If the legend’s Title attribute is
set, then it is used as a header in the legend box.
The text properties for all of the text strings in the legend box are obtained from the Legend node,
not from the associated Data nodes (see “Text Attributes” on page 114). Here the TextColor is
set to white.
The background of the legend box can be set by changing the fill attributes (see “Fill Area
Attributes” on page 111). By default in the Legend node, FillType is set to FILL_TYPE_NONE and
FillColor is set to Color.LightGray.
The position of the legend box is controlled by its Viewport attribute. The viewport is the region
of the Control, in which the chart is being drawn, that the legend box occupies. The upper left
corner is (0,0) and the lower right corner is (1,1). The default value of the legend viewport is
[0.83, 0.0] by [0.2, 0.2]. The default value of the legend viewport is [0.83, 0.0] by [0.2, 0.2]. The
position of the legend can be controlled by the xmin and ymin parameters of method
SetViewport (note that the xmax and ymax parameters do not affect the legend viewport).
using Imsl.Chart2D;
using System.Drawing;
public SampleLegend() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
Colormaps
Colormaps are mappings from [0,1] to colors. They are a one-dimensional parameterized path
through the color cube. For an example of their use, see Heatmap.
A number of colormaps are predefined in the Colormap class. It is also possible to create new
custom colormaps.
The predefined colormaps are listed in the table below.
Example
In this example two Data nodes are created. Each Data node has a Title defined and a ToolTip
node added as a child. Note that the ToolTip node just has to be created, no methods using it are
normally required.
(Download Code)
using Imsl.Chart2D;
using System.Drawing;
public SampleToolTip() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
Picking
The pick mechanism allows MouseEvents to be associated with chart nodes. The pick mechanism
follows the .NET delegate pattern.
A PickEventHandler is added to a ChartNode, using the node’s PickPerformed method and
the delegate += syntax to define the method to be called when the event occurs.
A pick is fired by calling the method Chart.Pick in the top-level ChartNode. Normally this is
done in response to a mouse click. Mouse clicks can be detected by adding a
MouseEventHandler to the MouseDown property of the chart’s container.
Example
In this example, when a bar is clicked it toggles its color between blue and red.
A MouseEventHandler is added to the Panel, which calls method Chart.Pick.
The pick delegate is added to the chart node bar. This means that it is active for that node and its
children but is not active for the axes and other nodes.
using System.Windows.Forms;
using System.Drawing;
using Imsl.Chart2D;
public SamplePick()
{
chart = this.Chart;
AxisXY axis = new AxisXY(chart);
Bar bar = new Bar(axis, x, y);
bar.BarType = Bar.BAR_TYPE_VERTICAL;
bar.SetLabels(new string[]{"A","B","C", "D"});
bar.FillColor = Color.Blue;
Zoom
IMSL C# Numerical Libraries provides the functionality to implement a variety of zoom policies
instead of implementing a single specific zoom policy. The following example shows how to build
a zoom policy that draws a rubber-band box in a scatter plot and zooms both the x-axis and y-axis
to correspond to the region selected. Zoom out is also implemented.
This code can be used as the basis of an implementation of other zoom policies. For example, with
a function plot one might want to only change the x-axis in response to a zoom and leave the y-
axis alone. In other situations it may be desirable to zoom in or out a fixed amount around the
location of a mouse click.
Example
In the constructor:
• A scatter plot is created,
• Zoom items are added to the main menu,
• A MouseEventHandler is added to the panel. A FrameChart contains a
number of components, including a Panel in which the chart is drawn. It is
important that the MouseEventHandler be added to the component in which the
chart is drawn, so that the MouseEventArgs coordinates are the same as the
device coordinates of the chart.
using System;
using System.Windows.Forms;
using System.Drawing;
using Imsl.Chart2D;
public SampleZoom()
{
panel = this.Panel;
Chart chart = this.Chart;
axis = new AxisXY(chart);
int n = 1000;
double[] x = new double[n];
double[] y = new double[n];
Random ran = new Random();
Printing
Printing from FrameChart
The FrameChart class, used to build most of the examples in this manual, includes a print option
under the file menu. This option prints the chart as large as possible, without distortion, and
centered on the page.
An IMSL C# Numerical Libraries chart can be integrated into an ASP.NET application through
the WebChart component. The WebChart class extends System.Web.UI.WebCon-
trols.Panel and can be added to an ASP.NET application like any other web control.
There are two sides to an ASP.NET application - the user interface and the code-behind class file.
The user interface is often created visually with Visual Studio with the HTML code generated
automatically. The code-behind class file is compiled into an assembly that is deployed with the
web pages. The following example was generated using Visual Studio 2005. The method applies
to other versions of Visual Studio though the code may be different in the details. The code-behind
class file is written in C# like the other examples in this document.
The following code was automatically generated by Visual Studio by dragging a WebChart onto
the page from the Toolbox containing web controls. The WebChart control can be added to the
Toolbox by using the Add Items... dialog and browsing to the IMSL C# Numerical Libraries
assembly, ImslCS.dll. Notice the reference to a WebChart object. This is the object that will be
referenced from the code-behind class file.
</div>
</form>
</body>
164 • IMSL C# Chart Programmer’s User Guide Appendix A: Web Server Application
</html>
For this basic example, the chart is created in the Page_Load method. The Chart object is
obtained from the WebChart and the chart is configured in a manner similar to the desktop form
examples. All chart types and most functionality of the IMSL C# Numerical Libraries is available
through the WebChart control.
(Download Code)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Imsl.Chart2D;
Appendix A: Web Server Application IMSL C# Chart Programmer’s User Guide • 165
Appendix B: Writing a Chart as a
Bitmap Image
166 • IMSL C# Chart Programmer’s User Guide Appendix B: Writing a Chart as a Bitmap Image
(Download Code)
using System;
using System.Drawing;
using System.IO;
using Imsl.Chart2D;
Appendix B: Writing a Chart as a Bitmap Image IMSL C# Chart Programmer’s User Guide • 167
int npoints = 20;
double dx = .5 * Math.PI/(npoints-1);
double[] x = new double[npoints];
double[] y = new double[npoints];
System.Windows.Forms.Application.Run(new SampleImageIO());
}
}
168 • IMSL C# Chart Programmer’s User Guide Appendix B: Writing a Chart as a Bitmap Image
Appendix C: Picture-in-Picture
The picture-in-picture effect can be obtained by using the Viewport attribute. This sets the
fraction of the screen into which the chart is to be drawn. The Viewport attribute’s value is a
double[4] containing {xmin, xmax, ymin, ymax}, on a [0,1] by [0,1] grid with (0,0) at the top-
left.
This chart tree for the above picture consists of a chart with an AxisXY child and a Pie child.
(The Pie class is a subclass of Axis.) The Viewport attribute of the Pie node is set to a non-
default value.
using System;
using System.Windows.Forms;
using System.Drawing;
using Imsl.Chart2D;
slice[0].SetTitle("Red");
slice[0].FillColor = Color.Red;
slice[0].Explode = 0.2;
slice[1].SetTitle("Blue");
slice[1].FillColor = Color.Blue;
slice[2].SetTitle("Black");
slice[2].FillColor = Color.Black;
slice[3].SetTitle("Green");
slice[3].FillColor = Color.Green;
}
C
candlestick charts
plots
candlestick
A candlestick 34
CChart 89
annotation 119 charts
applications, adding charts to 9 bar
Attribute Control Chart simple
CChart 67, 89 simple 42
NpChart 67, 87 bar 42
PChart 67, 84 grouped bar;grouped bar charts
UChart 67, 92 grouped 44
axis CuSum 67
crossed 140 pie 37
layout 122 contour
limits 124 contour 48
setting 121 heatmap
tick marks 124 heatmap 50
titles 126 histograms
custom tranformation of histogram 54
axes 135 labels
units 129 labels for
tick marks labels for 117
tick marks 131 background
axis3d background 142
labels colormaps 150
for axes 126 Control Chart
labels 126 Attribute Control Chart
CChart 89
B NpChart 87
PChart 84
background UChart 92
color 142 Variables Control Chart
gradient background color ControlLimit 68
gradient EWMA 67, 96
gradient 144 Shewhart Control Charts 67
bar XbarR 78
charts XbarS 70
stacked and grouped XmR 82
bar Cumulative Probability 104
stacked and grouped 45 CuSum 98
bar charts cumulative sum chart 98
spacing bars CuSumStatus 100
bar
spacing 44
width of bars in
T
text 114
fonts 114
size 115
style 115
typeface 114
density, of tick marks 124
titles
axis
titles 126
plot
titles for
titles for 119
U
UChart 92
image files;bitmaps;using for image
files; 109, 166
V
Variables Control Chart
EWMA 96
Shewhart Control Charts
ControlChartLimit 68
ShewhartControlChart 67
XbarR 78
XbarS 70
XmR 82
vertical error bar plots
plots
vertical error bar 27