#region 光标捕捉
/// <summary>
/// 点击鼠标保存文本
/// </summary>
private Dictionary<Point, Steema.TeeChart.Tools.Annotation> DicAnnotation = new Dictionary<Point, Steema.TeeChart.Tools.Annotation>();
/// <summary>
/// TeeChart线条的指示工具
/// </summary>
Steema.TeeChart.Tools.CursorTool cursorTool;
/// <summary>
/// 鼠标指示显示的文本
/// </summary>
private Steema.TeeChart.Tools.Annotation annotation;
/// <summary>
/// 初始化线条的提示工具信息
/// </summary>
private void InitTeeChartTipTools(Steema.TeeChart.TChart tChart)
{
//以线形式对标坐标轴
cursorTool = new Steema.TeeChart.Tools.CursorTool(tChart.Chart);
cursorTool.Style = Steema.TeeChart.Tools.CursorToolStyles.Both;
cursorTool.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash;
cursorTool.Pen.Color = Color.Black;
cursorTool.FollowMouse = true;
cursorTool.Active = false;
cursorTool.Change += CursorTool_Change;
//设置提示文本的信息
annotation = new Steema.TeeChart.Tools.Annotation(tChart.Chart);
annotation.Shape.Font.Name = "Arial";
annotation.Shape.Font.Size = 9;
annotation.Shape.Pen.Visible = true;
annotation.Shape.Shadow.Visible = false;
annotation.Shape.ShapeStyle = Steema.TeeChart.Drawing.TextShapeStyle.Rectangle;
annotation.Position = Steema.TeeChart.Tools.AnnotationPositions.LeftBottom;
annotation.TextAlign = StringAlignment.Center;
annotation.Active = false;
for (int i = 0; i < tChart.Series.Count; i++)
{
tChart.Series[i].MouseEnter += Line_MouseEnter;
tChart.Series[i].MouseLeave += Line_MouseLeave;
tChart.Series[i].Click += Line_Click;
}
tChart.MouseLeave += TChart_MouseLeave;
tChart.MouseEnter += TChart_MouseEnter;
}
private void Line_Click(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
foreach (var item in DicAnnotation)
{
if (e.X - item.Key.X > -20 && e.X - item.Key.X < 20 && e.Y - item.Key.Y > -20 && e.Y - item.Key.Y < 20)
{
p = item.Key;
break;
}
}
if (DicAnnotation.ContainsKey(p))
{
var anno = DicAnnotation[p];
DicAnnotation.Remove(p);
anno.Dispose();
}
else
{
var anno = annotation.Clone() as Steema.TeeChart.Tools.Annotation;
DicAnnotation.Add(p, anno);
}
}
/// <summary>
/// 鼠标进入TeeChart的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TChart_MouseEnter(object sender, EventArgs e)
{
cursorTool.Chart = this.tChartMinddle.Chart;
}
/// <summary>
/// 鼠标离开TeeChart的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TChart_MouseLeave(object sender, EventArgs e)
{
cursorTool.Chart = null;
}
/// <summary>
/// 当鼠标进入线条时,将TeeChart的cursorTool工具指示的线条设置为对应的线条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Line_MouseEnter(object sender, EventArgs e)
{
cursorTool.Series = sender as Steema.TeeChart.Styles.Series;
}
/// <summary>
/// 当鼠标离开线条时,将TeeChart的cursorTool工具指示的线条设置为null
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Line_MouseLeave(object sender, EventArgs e)
{
cursorTool.Series = null;
}
/// <summary>
/// 鼠标指示工具改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CursorTool_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
{
try
{
Steema.TeeChart.Tools.CursorTool cursor = sender as Steema.TeeChart.Tools.CursorTool;
if (cursor != null && cursor.Series != null)
{
annotation.Text = string.Format("{0},{1}", cursor.XValue.ToString("f1"), cursor.YValue.ToString("f1"));
annotation.Top = cursor.Series.GetVertAxis.CalcYPosValue(InterpolateLineSeries(cursor.Series, cursor.XValue));
annotation.Left = tChartMinddle.Axes.Bottom.CalcXPosValue(cursor.XValue);
annotation.Top -= 20;//将文本放在鼠标上方
SizeF size = this.CreateGraphics().MeasureString(annotation.Text,
new Font(annotation.Shape.Font.Name, annotation.Shape.Font.Size));
if (annotation.Left + size.Width + 12 >= annotation.Chart.Width)
{
annotation.Left -= (int)size.Width + 12;//防止文本标签超出右边界而看不全
}
}
else
{
//将其设置到控件外部
annotation.Text = "";
annotation.Top = annotation.Chart.Height + 5;
annotation.Left = annotation.Chart.Width + 5;
}
}
catch (Exception ex)
{
annotation.Text = ex.Message;
annotation.Top = 5;
annotation.Left = 5;
}
}
/// <summary>
/// 计算某一点的Y值坐标
/// </summary>
/// <param name="series">曲线</param>
/// <param name="xvalue">对应的X轴的值</param>
/// <returns>计算得到的对应的Y轴的值</returns>
private double InterpolateLineSeries(Steema.TeeChart.Styles.Series series, double xvalue)
{
try
{
int index;
for (index = series.FirstVisibleIndex; index <= series.LastVisibleIndex; index++)
{
if (index == -1 || series.XValues.Value[index] > xvalue) break;
}
// safeguard
if (index < 1)
{
index = 1;
}
else if (index >= series.Count)
{
index = series.Count - 1;
}
// y=(y2-y1)/(x2-x1)*(x-x1)+y1
double dx = series.XValues[index] - series.XValues[index - 1];
double dy = series.YValues[index] - series.YValues[index - 1];
if (dx != 0.0)
{
return dy * (xvalue - series.XValues[index - 1]) / dx + series.YValues[index - 1];
}
else
{
return 0.0;
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
return 0.0;
}
}
private void tChartMinddle_UndoneZoom(object sender, EventArgs e)
{
DicAnnotation.Clear();
}
private void tChartMinddle_Zoomed(object sender, EventArgs e)
{
DicAnnotation.Clear();
}
#endregion
tChart光标捕捉
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 动作捕捉系统本质上是一种定位系统,通常需要在目标物布置定位设备进行追踪。以红外光学为原理的动作捕捉系统,主要由由光...
- 光标丢失怎么办?如何通过增大光标来找到丢失的光标?具体教程如下: 增加光标大小 如果找不到光标,显而易见的做法是将...
- 光标之光标的属性和光标数的限制 光标的属性 %isopen属性案例 %rowcount属性案例 光标的限制 默认情...
- 1: 设置keyboardDisplayRequiresUserAction属性为NO _webView.keyb...