简体   繁体   English

ZedGraph点标签重叠

[英]ZedGraph Point Labels overlapping

I have line charts with multiple series data. 我有带有多个系列数据的折线图。 points in series are little close to each other so due to that reason, label overlaps eachother. 序列中的各个点几乎彼此不靠近,因此由于这个原因,标签彼此重叠。 Is there any supportive lib which can handle point labels on its own. 是否有任何支持lib可以自己处理点标签。

Or Is there any smart logic that can identify closest points and set the location of label accordingly?? 还是有任何智能逻辑可以识别最接近的点并相应地设置标签的位置?

Maybe try the IsPreventLabelOverlap property to true. 也许尝试将IsPreventLabelOverlap属性设置为true。 Unfortunately this usually just erases the labels that overlap and doesn't simply spread them out. 不幸的是,这通常只会擦除重叠的标签,而不会简单地将它们散开。 So with that in mind, see below. 因此,请记住以下内容。

There is not a library which does what you asked, but there is the postpaint option. 没有一个库postpaint您的要求,但是有postpaint选项。 Zedgraph unfortunately does not fix overlapping labels (I tried for a long time and with no luck). 不幸的是,Zedgraph无法修复重叠的标签(我尝试了很长时间并且没有运气)。 However there is a work around but it is tedious and you have to put some real thought into where to place your graphic labels. 但是,有一个解决方法,但是它很繁琐,您必须对放置图形标签的位置进行一些实际考虑。 See code below for a simple way to add a label: 有关添加标签的简单方法,请参见下面的代码:

protected void Chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
  if (e.ChartElement is Chart)
{
// create text to draw
String TextToDraw;
TextToDraw = "Chart Label"

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

This will create a label for you and you can choose where to draw it. 这将为您创建一个标签,您可以选择在何处绘制标签。 However that is the tricky part. 但这是棘手的部分。 You need to basically find out where your graph is on your screen and where the point is on that graph. 您基本上需要找出图形在屏幕上的位置以及点在该图形上的位置。 Very cumbersome but if it is a static graph then it shouldn't be an issue. 非常麻烦,但是如果它是静态图,那么就不应该成为问题。 It's a hack, I know, but it works and it's all anyone has seemed to come up with. 我知道这是一种黑客手段,但它确实有效,而且所有人似乎都想出了所有办法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM