Drop lines in MSChart - c#

I am using MSChart in my C# program to create line graphs, and am unable to find any options to create "drop lines" i.e. lines dropping down from each data point to the X-Axis.
Can anyone help?
Thanks.

I solved my problem using the LineAnnotation object:
int index = 1;
foreach (DataPoint _point in series1.Points)
{
LineAnnotation annotation = new LineAnnotation();
annotation.LineColor = Color.Black;
annotation.LineWidth = 4;
annotation.LineDashStyle = ChartDashStyle.Dot;
annotation.AxisX = chartArea1.AxisX;
annotation.AxisY = chartArea1.AxisY;
annotation.AnchorX = index;
annotation.AnchorY = 0;
annotation.Height = _point.YValues[0];
annotation.Width = 0;
annotation.IsSizeAlwaysRelative = false;
annotation.ClipToChartArea = "ChartArea1";
chart1.Annotations.Add(annotation);
index++;
}
Funnily enough, the above code behaved strangely when I anchored the annotation to the data point itself, drawing some lines up and some lines down even though all my points are in the same quadrant. After a lot of playing around, I managed to solve the problem by anchoring the annotation to the appropriate "dropped" point on the X-axis.

Related

Dynamically add Chart to Windows Forms - shows up blank

I'm trying to add a Chart control dynamically to a Form (using C#, this should all be .NET 4.0), but it's always blank (only the background color shows). I tried the same code in a Form that already has a control and it works, so I imagine it's some initialization function I should call (I tried Invalidate() on the control and Refresh() on both control and the panel it's being placed in, made no difference). I went through the few similar posts I found, tried throwing in any other commands they used (BeginInit() is from one such post) but no luck so far. Any ideas?
BTW I want to display 6-9 charts (position, speed and acceleration in 3D space) so I'd rather add them dynamically than have 9 sets of assignments. Here's the code that adds the charts to the panel:
foreach (KeyValuePair<string, List<double>> p in b.storedValues)
{
Control c = getChartForData(p);
panel1.Controls.Add(c);
c.Invalidate();
c.Refresh();
break;
}
And the function that creates each chart:
private Chart getChartForData(KeyValuePair<string, List<double>> data)
{
Chart c = new Chart();
((System.ComponentModel.ISupportInitialize)c).BeginInit();
c.Series.Clear();
c.BackColor = Color.White;
c.Height = 300;
c.Width = 500;
c.Palette = ChartColorPalette.Bright;
Series s = new Series(data.Key);
s.ChartType = SeriesChartType.Spline;
double maxValue = 0;
//NOTE: Going logarithmic on this, too big numbers
for (int i = 0; i < data.Value.Count; i++)
{
maxValue = Math.Max(Math.Log10(data.Value[i]), maxValue);
}
for (int i = 0; i < data.Value.Count; i++)
{
s.Points.AddXY(i,Math.Log10(data.Value[i]) * c.Height / maxValue);
}
c.Series.Add(s);
return c;
}
Many thanks in advance.
When you create a Chart yourself, in code, it does not contain any ChartArea.
Therefore, nothing is displayed.
I'm guessing that the designer generates some code to initialize a default chart area when you drag and drop a chart control onto the form.
Also, you should really let the chart control handle the layout, instead of calculating the desired position of each point based on the height of the chart control.
I would go as simple as possible to get something that's working, and then you can tweak the range of the axis afterwards. You can also set an axis to be logarithmic.
Start with trying out this minimal version, and make sure that displays something, before you complicate things. This works for me.
private Chart getChartForData(string key, List<double> data)
{
Chart c = new Chart();
Series s = new Series(key);
s.ChartType = SeriesChartType.Spline;
for (int i = 0; i < data.Count; i++)
{
s.Points.AddXY(i, data[i]);
}
c.Series.Add(s);
var area = c.ChartAreas.Add(c.ChartAreas.NextUniqueName());
s.ChartArea = area.Name;
// Here you can tweak the axis of the chart area - min and max value,
// where they display "ticks", and so on.
return c;
}

How to add right border of grid in Chart contol

Here is my code for setting properties of chart which is attached above:
chart2.ChartAreas[0].CursorX.IsUserEnabled = true;
chart2.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart2.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart2.ChartAreas[0].AxisX.Title = "t";
chart2.ChartAreas[0].AxisY.Title = "w(t)";
chart2.ChartAreas[0].AxisX.Minimum = classes[0].First();
chart2.ChartAreas[0].AxisX.Maximum = classes[m - 1].Last();
chart2.ChartAreas[0].AxisX.Interval = delta_t;
chart2.ChartAreas[0].AxisX.LabelStyle.Format = "{0:0.####}";
I need to add right border of grid so that it will be as it is shown below:
The right border is missing since your data don't nicely fit into the area.
There are many ways to fix this.
Here is the simplest one:
chart2.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
chart2.ChartAreas[0].AxisY2.LabelStyle.Enabled = false;
This adds a secondary Y-Axis and turns off its labels.
You can style it as needed:
chart2.ChartAreas[0].AxisY2.MajorTickMark.Enabled = false;
chart2.ChartAreas[0].AxisY2.LineWidth = 3;
You could also draw a line or add an annotation but this is by far the easiest solution.

How can I hide vertical line

I want to remove the lines I showed in the picture Because it is a very absurd views (not only marked two)
I am trying this code but it does not do anything
chart1.ChartAreas.First().AxisX.LineColor = Color.FromArgb(50, Color.Black);
chart1.ChartAreas.First().AxisX.LineWidth = 0;
chart1.ChartAreas.First().AxisX.InterlacedColor = Color.White;
I am changing Interval like this , it working but above code does not work
chart1.ChartAreas.First().AxisX.Interval = 1;
The answer from How to delete grid lines from Chart in WindowsForm?
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;

Chart set Vertical Bar Marker

On my Chart, I want to put an obvious red vertical bar which goes from a specific point on the plot down to the x-axis. Is there a way to do this? Based on the documentation, it doesn't seem like this option is available or perhaps I'm looking in the wrong area.
The most obvious way is to add a VerticalLineAnnotation.
Here is an example:
First I set up a few things:
int yourPointIndex = 635;
Series S1 = chart1.Series[0];
ChartArea CA1 = chart1.ChartAreas[0];
Now I create the Annotation and style it a little:
VerticalLineAnnotation LA = new VerticalLineAnnotation();
LA.LineColor = Color.Red;
LA.LineWidth = 9;
LA.IsInfinitive = false;
LA.AnchorDataPoint = S1.Points[yourPointIndex]; ;
Now I position it with the Point in question:
LA.X = S1.Points[yourPointIndex].XValue;
LA.Y = S1.Points[yourPointIndex].YValues[0];
// this makes the bar go down to the zero axis
LA.Height = LA.Y;
// this makes it go down all the way to the x-axis:
LA.Height = LA.Y - CA1.AxisY.Minimum;
// we should clip it to our chartarea:
LA.ClipToChartArea = CA1.Name;
Finally it is added to the Annotations collection of the Chart.
chart1.Annotations.Add(LA);
Note that Annotations can be adorned and made to be moveable..
Note: The code above was written for and tested with Winforms but the MS Chart control is rather similar in all its versions..

Coordinates of rectangle annotation varies for the same location in other similar pages in PDFTron WebViewer

We are trying to create rectangle annotation to highlight some text in a page (form) based on supplied coordinates. While using the same coordinates to create rectangle annotation for the same text in same location in other similar pages, the annotation is getting created somewhere else instead of the desired text location. These issue may be because of the offset difference in the pages as we are suspecting. Is there any way to resolve this issue? Any assistance will be highly appreciated. Thank you.
I am using below code to create rectangle annotation over a text based on supplied coordinates (hardcoded here just for example)
var docViewer = readerControl.docViewer;
var am = docViewer.GetAnnotationManager();
// Create Rectangle over text with coordinates
var rectAnnot = new Annotations.RectangleAnnotation();
rectAnnot.X = (644);
rectAnnot.Y = (736);
rectAnnot.Width = (125);
rectAnnot.Height = (15);
rectAnnot.NoMove = true;
rectAnnot.NoResize = true;
docViewer.ZoomTo(1.5, 644, 736);
rectAnnot.PageNumber = 1;
rectAnnot.Author = this.currUser;
rectAnnot.FillColor = new Annotations.Color(255, 255, 0);
rectAnnot.StrokeColor = new Annotations.Color(255, 0, 0);
rectAnnot.StrokeThickness = 1;
rectAnnot.Opacity = 0.5;
am.AddAnnotation(rectAnnot);
Now by using the same coordinates to create the same annotation in other similar forms having different margins/offset, the annotation is placed elsewhere instead of placing over the same text located in the same location as it is in the previous form.

Categories