C# MS图表控件系列标签定位
我有一个用MS图表控件制作的甘特图(范围栏);对于一些较短的系列,标签将显示在栏外;我更喜欢设置它,使标签保持在条内并被截断(带省略号会更好)。有没有办法做到这一点?我已经在图表和系列的属性中摸索了很久,但没有成功。我认为您需要设置的属性是C# MS图表控件系列标签定位,c#,label,mschart,series,C#,Label,Mschart,Series,我有一个用MS图表控件制作的甘特图(范围栏);对于一些较短的系列,标签将显示在栏外;我更喜欢设置它,使标签保持在条内并被截断(带省略号会更好)。有没有办法做到这一点?我已经在图表和系列的属性中摸索了很久,但没有成功。我认为您需要设置的属性是BarLabelStyle 例如 请参阅这篇文章,其中解释了MS图表控件的自定义属性应该类似或相同。我认为您需要设置的属性是BarLabelStyle 例如 请参阅这篇文章,其中解释了MS Chart控件的自定义属性应该类似或相同。最后,我使用了它来滚动我自己
BarLabelStyle
例如
请参阅这篇文章,其中解释了MS图表控件的自定义属性应该类似或相同。我认为您需要设置的属性是
BarLabelStyle
例如
请参阅这篇文章,其中解释了MS Chart控件的自定义属性应该类似或相同。最后,我使用了它来滚动我自己的属性(是的,它很乱,我有时间时会整理好):
private static void Chart_PostPaint(对象发送方,ChartPaintEventArgs e)
{
图c=((图)发送者);
foreach(c系列中的系列s)
{
字符串sVt=s.GetCustomProperty(“PixelPointWidth”);
IGanttable ig=(IGanttable)s.Tag;
double dblPixelWidth=c.ChartAreas[0]。AxisY.ValueToPixelPosition(s.Points[0]。YValue[1])-c.ChartAreas[0]。AxisY.ValueToPixelPosition(s.Points[0]。YValue[0]);
s、 Label=ig.Text.AutoEllipsis(s.Font,Convert.ToInt32(dblPixelWidth)-dblSeriesPaddingGuess);
}
}
公共静态字符串自动省略号(此字符串为s,字体为f,整数为intPixelWidth)
{
如果(s.Length==0 | | intPixelWidth==0)返回“”;
var result=Regex.Split(s,“\r\n |\r |\n”);
列表l=新列表();
foreach(结果中的字符串str)
{
intvt=TextRenderer.MeasureText(str,f).Width;
if(vtintPixelWidth)
{
strTemp=str.Substring(0,--i);
如果(i==0)中断;
}
l、 添加(strTemp+“…”);
}
}
返回字符串。Join(“\r\n”,l);
}
只要是
Post_Paint
事件(如果您使用Paint
事件,它将停止显示工具提示),这似乎工作得非常愉快。最后,我用这个来滚动我自己的(是的,它很乱,我有时间时会整理好):
private static void Chart_PostPaint(对象发送方,ChartPaintEventArgs e)
{
图c=((图)发送者);
foreach(c系列中的系列s)
{
字符串sVt=s.GetCustomProperty(“PixelPointWidth”);
IGanttable ig=(IGanttable)s.Tag;
double dblPixelWidth=c.ChartAreas[0]。AxisY.ValueToPixelPosition(s.Points[0]。YValue[1])-c.ChartAreas[0]。AxisY.ValueToPixelPosition(s.Points[0]。YValue[0]);
s、 Label=ig.Text.AutoEllipsis(s.Font,Convert.ToInt32(dblPixelWidth)-dblSeriesPaddingGuess);
}
}
公共静态字符串自动省略号(此字符串为s,字体为f,整数为intPixelWidth)
{
如果(s.Length==0 | | intPixelWidth==0)返回“”;
var result=Regex.Split(s,“\r\n |\r |\n”);
列表l=新列表();
foreach(结果中的字符串str)
{
intvt=TextRenderer.MeasureText(str,f).Width;
if(vtintPixelWidth)
{
strTemp=str.Substring(0,--i);
如果(i==0)中断;
}
l、 添加(strTemp+“…”);
}
}
返回字符串。Join(“\r\n”,l);
}
只要是
Post_Paint
事件(如果您使用Paint
事件,它将停止显示工具提示),这似乎工作得非常愉快。对不起,这对我来说没有任何改变:|我确认这确实显示了以MSChart为中心的条形图内的标签。对不起,这对我来说没有任何改变:|我确认这确实显示了以MSChart为中心的条内标签。
chart.Series["mySeries"]["BarLabelStyle"] = "Center";
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);
}