1

我有包含多个系列数据的折线图。系列中的点彼此之间几乎没有接近,因此由于这个原因,标签彼此重叠。是否有任何支持库可以自己处理点标签。

或者是否有任何智能逻辑可以识别最近的点并相应地设置标签的位置?

4

1 回答 1

0

也许尝试将IsPreventLabelOverlap属性设置为 true。不幸的是,这通常只会删除重叠的标签,而不是简单地将它们展开。因此,考虑到这一点,请参见下文。

没有一个库可以满足您的要求,但是可以postpaint选择。不幸的是,Zedgraph 没有修复重叠的标签(我尝试了很长时间但没有运气)。然而,有一种解决方法,但它很乏味,你必须真正考虑在哪里放置图形标签。请参阅下面的代码,了解添加标签的简单方法:

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

这将为您创建一个标签,您可以选择在哪里绘制它。然而,这是棘手的部分。您基本上需要找出图形在屏幕上的位置以及该点在该图形上的位置。非常麻烦,但如果它是静态图,那么它应该不是问题。我知道,这是一种 hack,但它确实有效,而且似乎是所有人都想出的。

于 2013-03-02T08:48:05.937 回答