ZedGraph 解决标注数字拖拽超出谱图区域问题
image.png
通过修改源码解决这个问题
1.修改GraphPane.cs
增加 MasterPane变量用于接收 MasterPane对象信息传输,因为要使用到MasterPane对象中计位置的方法
private MasterPane _masterPane;
public MasterPane MasterPane
{
get { return _masterPane; }
set { _masterPane = value; }
}
2.修改MasterPane.cs 将当前对象在绘制文字的时候传输到GraphPane对象中
image.png
3.修改GraphObjList.cs 文件
增加 MasterPane变量用于接收 MasterPane对象信息传输
private MasterPane _masterPane;
public MasterPane MasterPane
{
get { return _masterPane; }
set { _masterPane = value; }
}
增加计算方法
/// <summary>
/// 计算是否显示文本
/// </summary>
/// <param name="g"></param>
/// <param name="pix"></param>
/// <param name="textObj"></param>
/// <param name="scaleFactor"></param>
/// <returns></returns>
private bool CalcIsShowText(Graphics g, PointF pix, TextObj textObj, float scaleFactor)
{
//获取文字的高度
SizeF fontSize = textObj.FontSpec.MeasureString(g, textObj.Text, scaleFactor);
float orgY = pix.Y;
float orgX = pix.X;
double textPointMaxX = orgX + fontSize.Width/2;
double textPointMinX = orgX - fontSize.Width/2;
double textPointMinY = orgY - fontSize.Height;
double textPointMaxY = orgY;
foreach (GraphPane pane in _masterPane.PaneList)
{
double startChartX = pane.Chart._rect.X;
double maxX = pane.Chart._rect.X + pane.Chart._rect.Width;
double minX = pane.Chart._rect.X;
double maxY = pane.Chart._rect.Y + pane.Chart._rect.Height;
double minY = pane.Chart._rect.Y;
if ((textPointMinX > minX && textPointMaxX < maxX) && (textPointMinY > minY && textPointMaxY < maxY))
{
return true;
}
}
return false;
}
修改 Draw 方法
public void Draw(Graphics g, PaneBase pane, float scaleFactor,
ZOrder zOrder)
{
// Draw the items in reverse order, so the last items in the
// list appear behind the first items (consistent with
// CurveList)
for (int i = this.Count - 1; i >= 0; i--)
{
GraphObj item = this[i];
TextObj textObj = (TextObj)this[i];
if (item.ZOrder == zOrder && item.IsVisible)
{
PointF pix = item.Location.Transform(pane);
if (!CalcIsShowText(g, pix, textObj, scaleFactor))
{
continue;
}
Region region = null;
if (item.IsClippedToChartRect && pane is GraphPane)
{
region = g.Clip.Clone();
g.SetClip(((GraphPane)pane).Chart._rect);
}
item.Draw(g, pane, scaleFactor);
if (item.IsClippedToChartRect && pane is GraphPane)
g.Clip = region;
}
}
}
修改完成 ,运行效果
image.png