C#折线图如何创建垂直线 [英] C# Line Chart How to Create Vertical Line
问题描述
我有一个折线图。例如这样;
I have a line chart. For example like this;
我想画一条垂直线,底部有一个标签。我希望将标签拖到图表的x点上方时将其垂直线移动。我将得到带有标签的线与Y轴匹配的y点
I want to draw a vertical line that have a label at the bottom of it. And i want that label move through x points of chart with its vertical line when i drag it above those x points. I will get the y points where that line with label matches Y axis
例如;
我该怎么做?
推荐答案
此解决方案使您可以左右拖动注释行,并更新(右对齐)标题字段中的X和Y值。
This solution lets you drag the Annotation line left and right and updates the X- and Y-values in a (right aligned) title field.
更新:我已经对缩放进行了更正,并添加了一个可以显示Y值的例程。对此有局限性,请参见下文!
Update: I have corrected a few things wrt to scaling and added a routine that can display the Y-Values. There are limitations to this, see below!
为便于在移动事件中引用,我在类级别声明了一些变量。 (您也可以对 sender
进行强制转换,并按名称或索引引用它们。)
For easier reference in the move-event I declare a few variables at class level. (You could also do casts of the sender
instead and reference them by name or index..)
ChartArea CA;
Series S1;
VerticalLineAnnotation VA;
RectangleAnnotation RA;
这将创建注释
:
CA = chart1.ChartAreas[0]; // pick the right ChartArea..
S1 = chart1.Series[0]; // ..and Series!
// factors to convert values to pixels
double xFactor = 0.03; // use your numbers!
double yFactor = 0.02; // use your numbers!
// the vertical line
VA = new VerticalLineAnnotation();
VA.AxisX = CA.AxisX;
VA.AllowMoving = true;
VA.IsInfinitive = true;
VA.ClipToChartArea = CA.Name;
VA.Name = "myLine";
VA.LineColor = Color.Red;
VA.LineWidth = 2; // use your numbers!
VA.X = 1;
// the rectangle
RA = new RectangleAnnotation();
RA.AxisX = CA.AxisX;
RA.IsSizeAlwaysRelative = false;
RA.Width = 20 * xFactor; // use your numbers!
RA.Height = 8 * yFactor; // use your numbers!
VA.Name = "myRect";
RA.LineColor = Color.Red;
RA.BackColor = Color.Red;
RA.AxisY = CA.AxisY;
RA.Y = -RA.Height ;
RA.X = VA.X - RA.Width / 2;
RA.Text = "Hello";
RA.ForeColor = Color.White;
RA.Font = new System.Drawing.Font("Arial", 8f);
chart1.Annotations.Add(VA);
chart1.Annotations.Add(RA);
这将使标签随行移动:
private void chart1_AnnotationPositionChanging(object sender,
AnnotationPositionChangingEventArgs e)
{
// move the rectangle with the line
if (sender == VA) RA.X = VA.X - RA.Width / 2;
// display the current Y-value
int pt1 = (int)e.NewLocationX;
double step = (S1.Points[pt1 + 1].YValues[0] - S1.Points[pt1].YValues[0]);
double deltaX = e.NewLocationX - S1.Points[pt1].XValue;
double val = S1.Points[pt1].YValues[0] + step * deltaX;
chart1.Titles[0].Text = String.Format(
"X = {0:0.00} Y = {1:0.00}", e.NewLocationX, val);
RA.Text = String.Format("{0:0.00}", val);
chart1.Update();
}
如果要添加此事件以使Line捕捉到下一个数据点:
If you want to add this event to make the Line snap to the next data point:
private void chart1_AnnotationPositionChanged(object sender, EventArgs e)
{
VA.X = (int)(VA.X + 0.5);
RA.X = VA.X - RA.Width / 2;
}
请注意,Y值的计算采用某种线形。
Note that the calculation of the Y-values assumes some kind of line graphics.
我添加了几行以在标签中显示值。如果该文本的长度相差很大,则应该相应地计算标签的宽度!
I have added a few lines to display the value in the label. If the length of that text varies a lot, the width of the label ought to be calculated accordingly!
这是我的示例的快照:
这篇关于C#折线图如何创建垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!