Labels (Chart Controls)

In the Chart control, you can use axis labels and data point labels to make your charts more informative.

Using Axis Labels

The Chart control labels chart area axes in three different ways, from the lowest to the highest priority:

  • Labeling axes numerically

  • Labeling axes using text

  • Labeling axes using custom labels

These methods differ primarily in how the label text is generated. You can use a different method for each individual axis. For example, use a set of custom labels for the Y axis and text labels for the X axis.

Labeling Axes Numerically

If an axis does not contain custom labels, and the plotted data do not have axis labels, the Chart control automatically displays axis labels at an interval specified by the Axis.Interval property. For more information on the property, see Axis

Labeling Axes Using Text

You can label the axis according to your particular data set using the DataPoint.AxisLabel property. Once you set this property with a label string, the chart displays the label string on the axis at the position of the data point.

Note

When the Chart control contains a large data set, it may selectively display axis labels. To make sure that all axis labels are visible, set the Axis.Interval property to 1.

The following code example sets two labels on the axis for the two data points.

Chart1.Series("Default").Points(0).AxisLabel = "First Point"
Chart1.Series("Default").Points(1).AxisLabel = "Second Point"
Chart1.Series["Default"].Points[0].AxisLabel = "First Point";
Chart1.Series["Default"].Points[1].AxisLabel = "Second Point";

Labeling Axes Using Custom Labels

Use custom labels to provide custom text for axes. Custom labels are implemented using the CustomLabels collection property. If you use custom labels, the chart area does not display axis labels from data points.

When using CustomLabel objects, you must set the ToPosition and FromPosition properties of each CustomLabel object, and these properties must specify the width of the label's text area. Do not assign the same value to both of these properties because they represent a range. To position a custom label directly beneath a data point and its associated tick mark, set the FromPosition property to the value of that data point's axis minus half of the tick mark interval, and set the ToPosition property to the value of that data point's axis plus half of the tick mark interval. For example, if an axis has an interval of 1 (1, 2, 3,…), and you want to use a custom label at X=2, then set the ToPosition and FromPosition properties to 1.5 and 2.5, respectively.

The RowIndex property specifies on which row the custom label is displayed. If a custom label is used in the first label row, the chart area does not display any labels from the axis scale. The only labels permitted in the second row and beyond are custom labels.

Axis Label Styles and Formats

You can set the axis label styles using the Axis object's LabelStyle property. Label style properties you set in this property, such as LabelStyle.Font, apply to an axis' labels. If the axis labels are too close to each other, you can set the LabelStyle.LabelsAutoFit or LabelStyle.Offset property to True.

When labeling axes numerically, you can also format the numbers using the LabelStyle.Format property.

The following code shows how to set the format of the primary Y axis to currency.

Chart1.ChartAreas("Default").AxisY.LabelStyle.Format = "C"
Chart1.ChartAreas["Default"].AxisY.LabelStyle.Format = "C";

The following code displays the day of week on the primary X axis. For more information about displaying date and time values as axis labels, see Working with Date and Time Values.

Chart1.ChartAreas("Default").AxisX.LabelStyle.Format = "dddd"
Chart1.ChartAreas["Default"].AxisX.LabelStyle.Format = "dddd";

Labeling Data Points

You can display data point labels in the plot area. To display the data point value as labels, use the IsValueShownAsLabel property in the Series or DataPoint object. To display other text as data point labels, use the Series.Label property.

To use data in the labels, use keywords in the Series.Label property. At run time, the chart replaces the keyword with the appropriate values. For example, the following code displays a multi-line label for each data point, with the Y value on the first line and the X value on the second line.

Chart1.Series("Series1").Label = "Y = #VALY" + ControlChars.Lf + "X = #VALX"
Chart1.Series["Series1"].Label = "Y = #VALY\nX = #VALX";

For more information, see Keywords.

Using Smart Labels

Depending on the chart type, you can use the Series.SmartLabelStyle property to set labels for data points that do not collide. This is useful when data points are clustered together.

Note

The Series.SmartLabelStyle property has no effect on the bar, range bar, pie, doughnut, range column, stacked area, 100% stacked area, stacked bar, 100% stacked bar chart types.

To enable smart labels, use the following code.

Chart1.Series("Series1").SmartLabelStyle.Enabled = true
Chart1.Series["Series1"].SmartLabelStyle.Enabled = true;

You can then use properties in SmartLabelStyle such as CalloutStyle and CalloutLineColor to control the appearance of the smart labels.

See Also

Reference

System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting

Other Resources

Using Chart Controls