Repositioning chart point label - c#

I have a Chart control that contains multiple overlapping Line series like this:
As you can see, the labels of the intersection points of the lines overlap the lines themselves. I tried using the following to no avail:
intersectPoints.SmartLabelStyle.Enabled = true;
intersectPoints.SmartLabelStyle.IsMarkerOverlappingAllowed = false;
I'm pretty sure this is because MarkerOverlapping only applies to Markers in the series the label belongs to.
I'm looking for how to accomplish any one of several solutions:
1) Manually adjust x-y position of Point label
2) Increase opacity of label so that it appears "above" overlapping lines
3) Use some other SmartLabelStyle tool.
4) If no other solution is available, possibly overwriting the draw method for the chart?

Related

add space between .net chart and lable

How can I move the labes down?
I would like to add some space between the chart and the labels. It is possible?
I'm using System.Web.UI.DataVisualization.Charting.Chart
Like in this edited in mspaint:
This answer will be under the assumption that you are using CustomLabels on the X-axis:
It is possible, but I am not aware of such a setting. But I can offer a pragmatic solution (i.e. a bit of a hack): Namely you reenable the gridtickmarks on the X-axis, but set Color to Transparent, after that you can adjust the distance to the axis by using MajorTickMark.Size (default 1), the tick marks will not be visible:
chart.ChartAreas[0].AxisX.MajorTickMark.Enabled = true;
chart.ChartAreas[0].AxisX.MajorTickMark.LineColor = Color.Transparent;
chart.ChartAreas[0].AxisX.MajorTickMark.Size = 1;
// You can increase MajorTickMark.Size to increase distance to the axis
And incase someone want to have both visible tickmarks and labels on an axis:
Look into using DataPoint.AxisLabel in your series rather than using CustomLabels on the X-axis. DataPoint.AxisLabel will add a label on the axis for each datapoint. The axis-labels will then have the same distance to the axis as normal interval labels. There is a trick to it, that if the first and last data point in a series have an AxisLabel set the axis interval numbers will be hidden and leave only the axis labels.

How to add an arrow in a chart pointing some data

I am currently working on a module to create charts to display data.
I use System.Windows.Forms.DataVisualization.Charting.Chart.
I have two striplines showing the the average result we got and another one showing what we want.
So far I was really happy with what I had but I want to add explicit arrow to point these lines. And I can't figure out how to do it.
I saw that Line Annotation might be of help but I couldn't find a way to do what I wanted.
Here is an example of what I would like to do :
You have a choice of using
Annotations or
GDI+ drawing
In both cases the challenge is to get the positions right.
The more natural way to go is using Annotations, so let's look at this first:
There are various types but different capabilities; text can be displayed by RectangleAnnotation or a TextAnnotation. Lines and arrowheads can only be displayed by LineAnnotations. So we need a pair of Line- plus TextAnnotation for each of your two lines.
Like many other chart elements annotations are positioned in percentages of their respective containers; this makes things rather tricky at times.
To place a line annotation all to the right of the chart you could set its X property to 100; to let it go to the left you set the width to a negative number. The problems are starting after that..
To find out where the right edge of the ChartArea is you need to code the Pre- or PostPaint event and use the ToRectangleF method.
To find out the y-value you will want to calculate it from a data value; for this you can use the AxisY.ValueToPixelPosition method, which converts to pixels, from which you can calculate the percantage using the chart's ClientArea along with the ChartArea percentage size.
Complicated? Yup. Annotations get a lot simpler to use if you can anchor them to a certain DataPoint; but yours are outside the ChartArea..
Here is a function that should help when doing the calculations:
double PercentFromValue(Chart chart, ChartArea ca, double value)
{
Axis ay = ca.AxisY;
RectangleF car = ca.Position.ToRectangleF();
double py = ay.ValueToPixelPosition(value);
int caHeight = (int)(chart.ClientRectangle.Height * car.Height / 100f);
return 100d * py / caHeight;
}
Note that it will only work reliably when called from of of the Pre/PostPaint events..
So this is an example of a PrePaint event that positions a LineAnnotation lAnn:
private void chart1_PrePaint(object sender, ChartPaintEventArgs e)
{
Rectangle cr = chart1.ClientRectangle;
ChartArea ca = chart1.ChartAreas[0];
RectangleF car = ca.Position.ToRectangleF();
lAnn.Width = car.Width - lAnn.X;
lAnn.Y = PercentFromValue(chart1, ca, someDataValue);
}
When you insert a valid DataPoint YValue the starting y-position will be set. You can play with it until you find a nice combination of setting the four position properties..
When creating and adding the four(!) annotations you may want to keep class level references, so you won't have to refer to them from the Annotations collection..
For the LineAnnotation you will want to set the linewidth, color and the capstyle, either using the EndCap or the StartCap:
lAnn.EndCap = LineAnchorCapStyle.Arrow;
GDI+ drawing is more straight-forward, provided you know where you want to draw the lines and the text.
It is also done in the PrePaint event, again using the ValueToPixelPosition to find the pixelposition of the two data lines.. Other than that is all the usual stuff with Graphics.DrawLine, a Pen with and Start- or EndCap and Graphics.DrawString or maybe TextRenderer.DrawText..
I'm frankly not sure which way I would choose..
create triangle image and set the marker image as:
Chart1.Series.Points.AddXY(0, 10);
Chart1.Series.Points.AddXY(20,10);
Chart1.Series[0].Points[1].MarkerImage = "TriangleImage.bmp";
Chart1.Series[0].Points[1].MarkerImageTransparentColor = Color.White;
or
Chart1.Series[0].Points[1].MarkerStyle = MarkerStyle.Triangle;

Differentiating lines in chart

I'm wondering if there is a way to differentiate multiple lines in a chart (using Windows Forms Chart) through means other than different colors. In Excel you can make small triangles or other shapes appear on different lines in a graph, and I was wondering if you could do something similar in C# for sets of 3 or more lines.
You can change the shape of the marker (http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.markerstyle(v=vs.110).aspx), for point-type charts:
chart1.Series["MySeries"].MarkerStyle = MarkerStyle.Square;
You can also set background colors and gradients / hatching (http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart.backgradientstyle(v=vs.110).aspx) for bar-type charts:
chart1.Series["MySeries"].BackGradientStyle = GradientStyle.DiagonalLeft;
You can also specify line thickness and/or style for line-type charts:
chart1.Series["MySeries"].BorderWidth = 4;
chart1.Series["MySeries"].BorderDashStyle = ChartDashStyle.Dash;

How do I add a tooltip or overlay to a specific section of a chart?

I have a chart that displays several lines showing signal strengths over a frequency band.
Each chart is composed of one 'area' and four 'series'. On the parent form there are several graphs like the one shown below. All of them are created dynamically and will have different widths.
What I am trying to do is add a tooltip or annotation (or something) when the mouse hovers over a specific area of the chart as shown in the mockup below:
If the mouse moved to the other side of the chart a different channel number and frequency would be shown in a box surrounding that area of the chart.
It doesn't have to be exactly as shown in the mockup although an outline would be preferred in order to show the user how wide the channel is regardless of the waveform shown in that area at the time. For example, the waveform shown above might only be 8MHz wide but channel 1 itself might have an allocation that is 10MHz wide (the device varies its bandwidth based on its offered load.)
The X axis is MHz and a channel is defined in terms of MHz so it would be ideal to define the outline in terms of the X axis instead of pixels.
Also, note that this is a realtime chart that is updated up to 10 times per second. Therefore it would be best if the information was not required to be updated each time new data arrived.
I was able to combine a couple of items to make the following solution:
[credit LICEcap]
The highlight is a rectangle filled in the 'OnPaint' method of the chart control.
The text is a simple TextAnnotation that is applied during the mousemove event.
It took quite a bit of coordinate conversion to get all the pieces in the right spot - especially the text. I needed to convert between pixels, position and value.
The first conversion was to pixels in order to center the text using MeasureString. I then converted it from pixel location to X axis value and then needed to convert it to position since the annotation requires using 'position' coordinates. There is not a function to convert from pixels to position. There is a pixels to value and a value to position which is the way I went.
I don't claim this to be the best or even a proper way to do it but it works. If anyone else has a better solution or a way to improve my code please post.
Here's my code for positioning the text:
double temp = chart1.ChartAreas[0].AxisX.ValueToPixelPosition(Convert.ToDouble(ce.sChannelFrequency) * 1000);
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1))) {
SizeF size = graphics.MeasureString(freq.Text, new Font("eurostile", 13, FontStyle.Bold, GraphicsUnit.Pixel));
temp -= (size.Width/2+10);
}
if (temp < 0) temp = 0;
temp = chart1.ChartAreas[0].AxisX.PixelPositionToValue(temp);
freq.X = chart1.ChartAreas[0].AxisX.ValueToPosition(temp);

Multiple curves using ZedGraph

I have a strange thing happening while using ZedGraph.
I am using the same item to add multiple curves. Like:
ZedGraph LineItem curve_3;
curve_3 = pane.AddCurve("", xx_1, yy, xxyy);
I call the above lines multiple times to add multiple points. But when I remove the curve, only the last added curve gets removed and left all stays on the pane.
this.zedGraph_RenderedTrack.GraphPane.CurveList.Remove(curve_3);
I am not finding a way that will clear all the curves added. Is there a to do it?
My actual requirement is that I have to add the different lines dynamically on the pane, but I don't need to display the label information and all of them should be plotted by a single click and removed by a single click.
You are holding only the last curve in this code:
ZedGraph LineItem curve_3;
curve_3 = pane.AddCurve("", xx_1, yy, xxyy);
Use collection like List<LineItem> to remember all the curves.
List<LineItem>.foreach(r => this.zedGraph_RenderedTrack.GraphPane.CurveList.Remove(r);
)
If you want to remove all curves from your graph pane, simply use the CurveList.Clear() method:
this.zedGraph_RenderedTrack.GraphPane.CurveList.Clear();

Categories

Resources