Chapter Contents


SAS/AF Software: Class Dictionary

Using a Color Variable in a Chart Control

The default color is Green for bars and boxes in bar and box charts. You can use the barColor attribute to assign a different color to all of the bars and boxes.

Note:   You cannot control color in line and area charts.  [cautionend]

To make color a response variable in a bar or box chart, you can specify a numeric variable whose values are indexed into the defined color range to determine the colors to use in the chart.

To assign the color variable, use the colorVariable, color2Variable, and color3Variable attributes, depending on how many color variables you want to analyze. The colors can represent the Sum, Mean, or Frequency statistic for the dependent variables. To specify the statistic represented by the color variables, use the colorVariableStatistic, color2VariableStatistic, and color3VariableStatistic attributes. Use the faceColorMode attribute to determine whether the color applies to the entire bar or just the bar tops.

For a Chart control named chart1, the following code sets the colorVariable to Sales and the colorVariableStatistic to Sum:

chart1.colorVariable = 'sales';
chart1.colorVariableStatistic = 'sum';

The color is applied in a range of color values from the color that represents the lowest data values to the color that represents the highest data values. The color ranges used depend on the color variable:

Color Variable Range (low to high)
colorVariable yellow to red
color2Variable dark green to light green
color3Variable dark blue to light blue

The following figures shows the default color range for a bar chart with the colorVariable attribute assigned:


To change the default color values that represent a color variable's values, you can define an alternative color range in either of two ways:

Creating a Color List in a Chart Control

To supply your own color list of up to 64 colors:

In build mode, you can define a color list by selecting the colorList, color2List, or color3List attribute, and then selecting the button that appears in the Value column. This opens the List Editor so that you can define the list of colors.

To define a color list in SCL code, make an SCL list, populate it with the list of color values, and then assign the list to the control's colorList, color2List, or color3List attribute. Your code might resemble the following for a control named chart1:

/* Make the list */
clist = makelist(3);
rc = setitemc(clist,'Red',1);
rc = setitemc(clist,'Yellow',2);
rc = setitemc(clist,'Green',3);

/* varName is the color variable's name */

The colors in your specified list are distributed evenly across the range of values in the color variable. Thus, if you define three colors in the list, only three colors are used. The first color in the list represents the lowest third of the color variable's values, the second color in the list represents the middle third of the color variable's values, and the third color represents the highest third:


You can also create a list of blended colors. The following code creates a color series that ranges from red to blue:

rcolor= ' ';
gcolor = ' ';
bcolor = ' ';
r = 255;
g = 0;
b = 0;
fmt = 'hex2.';
clist = makelist(16);
do i = 1 to 16;
  rcolor = putn(r,fmt);
  gcolor = putn(g, fmt);
  bcolor = putn(b, fmt);
  cval = 'CX' || rcolor || gcolor || bcolor;
  retc = setitemc(clist,cval,i);
  r = r - 14;
  b = b + 14;

/* varName is the color variable's name */

The colors in the list are distributed evenly across the range of values in the color variable:


To return to using the default colors, assign an empty list to the colorList attribute:

clist = makelist();
/* varName is the color variable's name */

Using a Range Entry in a Chart Control

You can use a Range entry as the color source for a Chart control. A Range entry is a catalog entry of type RANGE. The entry defines a range having up to 24 segments, each of which is assigned a numeric range, a color, and an attribute. For example, segment 1 might be assigned the numeric range from 0 to 5, the color Blue, and the attibute Blinking; segment 2 might be assigned the numeric range from 6 to 10, the color Green, and the attribute Highlight; and so on. A Chart control supports the numeric ranges and the colors assigned to the segments, but it does not support the attributes (Blinking, Highlight, Reverse, Underline, and Hirev).

When used with a Chart control, the numeric ranges that are defined in the Range entry's segments are indexed into the values of the color variable that is assigned to the Chart control. So, using the segments defined above, the values from 0 to 5 in the color variable will be represented by Blue in the graph, and the values from 6 to 10 will be represented by Green, and so on.

To use a Range entry, you must do all of the following:

To create a Range entry, use the Range Entry window, which you can open from the SAS command line. If you need help defining the segments, use the window's Help button to get help. The following command opens the Range Entry window to create a temporary Range entry in the WORK library:

build work.color.myrange.range

After creating the Range entry, you can use its color definitions as the color source in a Chart control. For example, if you used the command above to create a Range entry, the following code applies that entry's colors to a Chart control named chart1:

/* varName is the color variable's name */

Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.