2011-04-06 71 views
0

我有一个甘特图(RangeBar),我用MS Chart控件制作;对于一些较短的系列,标签会显示在栏外;我宁愿设置它,以使标签留在酒吧内并被截断(省略号会很好)。有没有办法做到这一点?我一直在图表和系列的属性中徘徊多年,但没有成功。MS Chart控制系列标签定位

回答

3

我认为你需要设置的属性是BarLabelStyle

如。

chart.Series["mySeries"]["BarLabelStyle"] = "Center"; 

见本Dundas page解释这应该是对于MS图表控制相似或相同的自定义属性。

+0

对不起,这不会改变任何东西对我来说:| – 2011-04-15 22:49:15

+0

我确认这确实显示了在MSChart中以条为中心的标签。 – peterincumbria 2014-03-15 08:56:57

0

在我滚到底我自己使用这个(是的,它的混乱,它会得到收拾,当我有时间):

private static void Chart_PostPaint(object sender, ChartPaintEventArgs e) 
    { 
     Chart c = ((Chart)sender); 
     foreach (Series s in c.Series) 
     { 
      string sVt = s.GetCustomProperty("PixelPointWidth"); 
      IGanttable ig = (IGanttable)s.Tag; 
      double dblPixelWidth = c.ChartAreas[0].AxisY.ValueToPixelPosition(s.Points[0].YValues[1]) - c.ChartAreas[0].AxisY.ValueToPixelPosition(s.Points[0].YValues[0]); 

      s.Label = ig.Text.AutoEllipsis(s.Font, Convert.ToInt32(dblPixelWidth)-dblSeriesPaddingGuess); 

     } 
    } 


public static string AutoEllipsis(this String s, Font f, int intPixelWidth) 
    { 

     if (s.Length == 0 || intPixelWidth == 0) return ""; 


     var result = Regex.Split(s, "\r\n|\r|\n"); 

     List<string> l = new List<string>(); 
     foreach(string str in result) 
     { 
      int vt = TextRenderer.MeasureText(str, f).Width; 
      if (vt < intPixelWidth) 
      { l.Add(str); } 
      else 
      { 
       string strTemp = str; 
       int i = str.Length; 

       while (TextRenderer.MeasureText(strTemp + "…", f).Width > intPixelWidth) 
       { 
        strTemp = str.Substring(0, --i); 
        if (i == 0) break; 
       } 

       l.Add(strTemp + "…"); 
      } 

     } 
     return String.Join("\r\n", l); 

    } 

这似乎很乐意,只要它是Post_Paint工作事件(如果您使用Paint事件,它将停止显示工具提示)