2012-09-13 32 views
4

我將註釋添加到c#折線圖。我想改變文字方向,但看不到任何設置以允許這樣做。c#圖表垂直文本註釋

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); 

將註釋正確添加到圖形中,矩形和文本從左向右運行。我想定位它來上下運行。有關如何實現這一目標的任何建議?

+1

我很好奇,如果你曾經找到這個問題的解決方案,或者如果你有它的工作。 – tmwoods

回答

0

您不能使用註釋庫來旋轉註釋。您必須使用postpaintprepaintHere是如何使用後期事件的一個很好的例子。希望這可以幫助。我會包括來自下面的鏈接代碼:

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); 
} 

}

編輯:我只是意識到這個例子實際上不旋轉文本。我知道你必須使用這個工具,所以我會嘗試找到一個使用旋轉文本的postpaint的例子。

編輯2:啊。 SO上的here。基本上你需要使用e.Graphics.RotateTransform(270);屬性(該行將旋轉270度)。

+0

不幸的是,除非你真的在Paint事件本身做到這一點,否則它似乎是不可能的! e.ChartGraphics.Graphics不適用於旋轉 – TaW