Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Line Chart How to Create Vertical Line

I have a line chart. For example like this;

enter image description here

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;

enter image description here

enter image description here

How can i do this?

like image 731
MOD Avatar asked Sep 12 '14 05:09

MOD


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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

Here is a snapshot of my example :

Annotation

like image 144
TaW Avatar answered Sep 22 '22 01:09

TaW