C # chart vertical text annotation

I am adding annotations to a C # line chart. I would like to change the orientation of the text, but I do not see any settings to allow this.

RectangleAnnotationannotation = new RectangleAnnotation(); annotation.AnchorDataPoint = chart1.Series[0].Points[x]; annotation.Text = "look an annotation"; annotation.ForeColor = Color.Black; annotation.Font = new Font("Arial", 12); ; annotation.LineWidth = 2; chart1.Annotations.Add(annotation); 

Annotations are added to the graph correctly, and the rectangle and text go from left to right. I want to orient him to run up and down. Any suggestions on how to achieve this?

+4
source share
1 answer

You cannot rotate annotations using the annotation library. You must use postpaint or prepaint . Here is a good example of how to use the post-paint event. Hope this helps. I will include the code at the link below:

 protected void Chart1_PostPaint(object sender, ChartPaintEventArgs e) { if (e.ChartElement is Chart) { // create text to draw String TextToDraw; TextToDraw = "Printed: " + DateTime.Now.ToString("MMM d, yyyy @ h:mm tt"); TextToDraw += " -- Copyright © Steve Wellens"; // get graphics tools Graphics g = e.ChartGraphics.Graphics; Font DrawFont = System.Drawing.SystemFonts.CaptionFont; Brush DrawBrush = Brushes.Black; // see how big the text will be int TxtWidth = (int)g.MeasureString(TextToDraw, DrawFont).Width; int TxtHeight = (int)g.MeasureString(TextToDraw, DrawFont).Height; // where to draw int x = 5; // a few pixels from the left border int y = (int)e.Chart.Height.Value; y = y - TxtHeight - 5; // a few pixels off the bottom // draw the string g.DrawString(TextToDraw, DrawFont, DrawBrush, x, y); } 

}

EDIT: I just realized that this example does not actually rotate the text. I know that you should use this tool, so I will try to find an example using postpaint, which rotates the text.

EDIT 2: Ahh. Right here in SO format. Basically you need to use the e.Graphics.RotateTransform(270); property e.Graphics.RotateTransform(270); (this line will rotate 270 degrees).

0
source

Source: https://habr.com/ru/post/1433983/


All Articles