Move chart point label to another position and connect them with an arrow

杀马特。学长 韩版系。学妹 提交于 2019-12-13 06:54:47

问题


I want to add 26 points into chart and label them from a to z.

How do I move the label to a nearby position from its original position and how to use an arrow or other symbols to connect the label and corresponding point? The demo chart looks like this :


回答1:


The DataPoint Labels are being placed pretty much automatically and you have only very few options to influence their position.

There are SmartLabels but they only allow you to control the behaviour when it comes to avoiding them to overlap.

So I think instead of Labels you will have to resort to Annotations.

Infact to replicate your image you need one LineAnnotation and one TextAnnotation per DataPoint you want labelled, anchored to that DataPoint..:

Lets assume you are adding the DataPoints to your Series S1one by one:

int p = S1.Points.AddXY(yourXValue, yourYValue);
string s = yourLabelText;
DataPoint dp = S1.Points[p];

Now you need to create one TextAnnotation, or to be precise a RectangleAnnotation, which is a subclass of TextAnnotation allowing for background and creating a border:

RectangleAnnotation ta = new RectangleAnnotation();
ta.AnchorDataPoint = dp;
ta.AnchorOffsetX = 5;     // *
ta.AnchorOffsetY = 3;     // *
ta.AnchorAlignment = ContentAlignment.BottomLeft;
ta.Text = s;
chart1.Annotations.Add(ta);

Note how I set the positioning here (*) to move the annotation a little up and right. The numbers are not pixels but percentages of the chart size. You need to experiment a little to find values you like. The Advantage is that the distance will scale when you resize or zoom the chart.

Now we add the arrow:

LineAnnotation la = new LineAnnotation();
la.SetAnchor(dp);
la.AnchorOffsetX = 0.5;   // (**)
la.AnchorOffsetY = -0.5;  // (**)
la.StartCap = LineAnchorCapStyle.Arrow;
la.Width =    ta.AnchorOffsetX - la.AnchorOffsetX;
la.Height = - ta.AnchorOffsetY + la.AnchorOffsetY;  // (***)           
chart1.Annotations.Add(la);

(**) I move the starting point a little off the DataPoint so the arrow won't overlap the circle of the point.

(***) Note that the height needs to point upward from the point, so it is negative!

You will want to fine-tune some of the numbers..



来源:https://stackoverflow.com/questions/36191246/move-chart-point-label-to-another-position-and-connect-them-with-an-arrow

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