windowsforms-5783-controls-and-libraries-chart-control-axes-constant-lines.md
A constant line is a line marker that extends over the chart and indicates an axis value. The constant line is always perpendicular to its axis.
To add a constant line at design time, expand the Diagram item in the Chart Control’s Properties window. Expand the AxisX or AxisY item and click the ellipsis button for the ConstantLines property.
Click the Add button to create a constant line. Use the ConstantLine.AxisValue property to specify the constant line’s value.
The table below shows constant lines for X- and Y-axis.
|
Axis / Constant Line Value
|
Result
| | --- | --- | |
X-axis
ConstantLine.AxisValue = 0:02:30
|
| |
Y-axis
|
|
The following code demonstrates how to add constant lines at runtime:
XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
ConstantLine temperatureLine = new ConstantLine("Optimal Temperature: 53°C", 53);
diagram.AxisY.ConstantLines.Add(temperatureLine);
ConstantLine timeLine = new ConstantLine("Time: 00:02:30", "00:02:30");
diagram.AxisX.ConstantLines.Add(timeLine);
Dim diagram As XYDiagram = CType(chartControl1.Diagram, XYDiagram)
Dim temperatureLine As ConstantLine = New ConstantLine("Optimal Temperature: 53°C", 53)
diagram.AxisY.ConstantLines.Add(temperatureLine)
Dim timeLine As ConstantLine = New ConstantLine("Time: 00:02:30", "00:02:30")
diagram.AxisX.ConstantLines.Add(timeLine)
Use the ConstantLine.Visible property to hide/show a constant line.
temperatureLine.Visible = false;
temperatureLine.Visible = False
Note
The Chart Control does not take into account constant lines and strips when it calculates axis ranges. You can extend an axis range as described in the Configure Visual and Whole Ranges help section to include the constant line’s value in the range.
A user should click the Add Vertical Constant Line and Add Horizontal Constant Line buttons in the Chart’s Ribbon or Toolbars to add a constant line.
The Title.Text property specifies a constant line’s title. The ConstantLineTitle.ShowBelowLine and ConstantLineTitle.Alignment properties specify the title’s position and alignment.
Use the TitleBase.TextColor, TitleBase.EnableAntialiasing, and TitleBase.DXFont properties to customize the title’s appearance.
Set the TitleBase.Visible property to true to display the title.
ConstantLine temperatureLine = new ConstantLine();
temperatureLine.AxisValue = 53;
temperatureLine.Title.Text = "Optimal Temperature: 53°C";
temperatureLine.Title.ShowBelowLine = true;
temperatureLine.Title.Alignment = ConstantLineTitleAlignment.Far;
temperatureLine.Title.TextColor = Color.DarkBlue;
temperatureLine.Title.EnableAntialiasing = DefaultBoolean.True;
temperatureLine.Title.DXFont = new DXFont("Tahoma", 9F);
temperatureLine.Title.Visible = true;
XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
diagram.AxisY.ConstantLines.Add(temperatureLine);
Dim temperatureLine As ConstantLine = New ConstantLine()
temperatureLine.AxisValue = 53
temperatureLine.Title.Text = "Optimal Temperature: 53°C"
temperatureLine.Title.ShowBelowLine = True
temperatureLine.Title.Alignment = ConstantLineTitleAlignment.Far
temperatureLine.Title.TextColor = Color.DarkBlue
temperatureLine.Title.EnableAntialiasing = DefaultBoolean.[True]
temperatureLine.Title.DXFont = New DXFont("Tahoma", 9F)
temperatureLine.Title.Visible = True
Dim diagram As XYDiagram = CType(chartControl1.Diagram, XYDiagram)
diagram.AxisY.ConstantLines.Add(temperatureLine)
Use the ConstantLine.ShowBehind property to display a constant line over or behind a series.
| Property Value | Result |
|---|---|
| ConstantLine.ShowBehind = false | |
| ConstantLine.ShowBehind = true |
ConstantLine constantLine = new ConstantLine("Value = 2 millions", 2);
constantLine.ShowBehind = false;
Dim constantLine As ConstantLine = New ConstantLine("Value = 2 millions", 2)
constantLine.ShowBehind = False
The constant line’s title is always shown over series.
You can display the constant line’s description in the chart legend.
Set the ConstantLine.ShowInLegend property to true and specify the ConstantLine.LegendText property.
ConstantLine temperatureLine = new ConstantLine("Optimal Temperature: 53°C", 53);
temperatureLine.ShowInLegend = true;
temperatureLine.LegendText = "Optimal Temperature";
XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
diagram.AxisY.ConstantLines.Add(temperatureLine);
Dim temperatureLine As ConstantLine = New ConstantLine("Optimal Temperature: 53°C", 53)
temperatureLine.ShowInLegend = True
temperatureLine.LegendText = "Optimal Temperature"
Dim diagram As XYDiagram = CType(chartControl1.Diagram, XYDiagram)
diagram.AxisY.ConstantLines.Add(temperatureLine)
Use the ConstantLine.Color, ConstantLine.LineStyle.Thickness, and ConstantLine.LineStyle.DashStyle properties to customize the constant line’s appearance.
ConstantLine temperatureLine = new ConstantLine("Optimal Temperature: 53°C", 53);
temperatureLine.Color = Color.DarkRed;
temperatureLine.LineStyle.DashStyle = DashStyle.Dot;
temperatureLine.LineStyle.Thickness = 2;
Dim temperatureLine As ConstantLine = New ConstantLine("Optimal Temperature: 53°C", 53)
temperatureLine.Color = Color.DarkRed
temperatureLine.LineStyle.DashStyle = DashStyle.Dot
temperatureLine.LineStyle.Thickness = 2
Enable the following options to allow users delete, move, and rename a constant line.
RuntimeDeletion specifies whether a user can delete the constant line with the Delete key.
RuntimeMoving specifies whether a user can move a constant line. When a user moves a constant line, the ChartControl.ConstantLineMoved event occurs.
RuntimeTitleEditing specifies whether a user can edit the title.
ConstantLine temperatureLine = new ConstantLine("Optimal Temperature: 53°C", 53);
temperatureLine.RuntimeDeletion = true;
temperatureLine.RuntimeMoving = true;
temperatureLine.RuntimeTitleEditing = true;
Dim temperatureLine As ConstantLine = New ConstantLine("Optimal Temperature: 53°C", 53)
temperatureLine.RuntimeDeletion = True
temperatureLine.RuntimeMoving = True
temperatureLine.RuntimeTitleEditing = True
See Also