C# Line Chart How to Create Vertical Line

空扰寡人 提交于 2019-12-24 02:35:13

问题


I have a line chart. For example like this;

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

For example;

How can i do this?


回答1:


This solution lets you drag the Annotation line left and right and updates the X- and Y-values in a (right aligned) title field.

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!

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;

This will create the Annotations:

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);

This will move the label with the line:

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();
}

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;
}

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 vaies a lot the width of the label ought to be calculated accordingly!

Here is a snapshot of my example :



来源:https://stackoverflow.com/questions/25801257/c-sharp-line-chart-how-to-create-vertical-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!