【发布时间】:2025-11-29 09:55:01
【问题描述】:
我有包含多个系列数据的折线图。系列中的点彼此之间几乎没有接近,因此由于这个原因,标签彼此重叠。 是否有任何支持库可以自己处理点标签。
或者是否有任何智能逻辑可以识别最近的点并相应地设置标签的位置??
【问题讨论】:
-
我很好奇你是否曾经让这个工作。你取得了一些成功吗?
标签: c# charts zedgraph linechart
我有包含多个系列数据的折线图。系列中的点彼此之间几乎没有接近,因此由于这个原因,标签彼此重叠。 是否有任何支持库可以自己处理点标签。
或者是否有任何智能逻辑可以识别最近的点并相应地设置标签的位置??
【问题讨论】:
标签: c# charts zedgraph linechart
也许尝试将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,但它确实有效,而且似乎是所有人都想出的。
【讨论】: