tChart光标捕捉优化

   #region 光标捕捉
    /// 
    /// 点击鼠标保存文本
    /// 
    private List StorageAnnos = new List();
    /// 
    /// TeeChart线条的指示工具
    /// 
    Steema.TeeChart.Tools.CursorTool cursorTool;
    /// 
    /// 鼠标指示显示的文本
    /// 
    private Steema.TeeChart.Tools.Annotation annotation;
    /// 
    /// 初始化线条的提示工具信息
    /// 
    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 = true;
        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 = true;
        annotation.Shape.ShapeStyle = Steema.TeeChart.Drawing.TextShapeStyle.Rectangle;
        annotation.Position = Steema.TeeChart.Tools.AnnotationPositions.LeftBottom;
        annotation.TextAlign = StringAlignment.Center;
        annotation.Active = true;

        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;
        tChart.MouseUp += TChart_MouseUp;
        tChart.Zoomed += tChart_Zoomed;
        tChart.UndoneZoom += tChart_Zoomed;
    }

    /// 
    /// 去除选中标记
    /// 
    /// 
    /// 
    private void TChart_MouseUp(object sender, MouseEventArgs e)
    {
        _ = Task.Run(async () =>
        {
            await Task.Delay(50);
            StorDeAnnos deAnnos = new StorDeAnnos();
            bool isBreak = false;
            for (var i = 0; i < StorageAnnos.Count; i++)
            {
                for (var j = 0; j < StorageAnnos[i].annotations.Count; j++)
                {
                    StorDeAnnos anno = StorageAnnos[i].annotations[j];
                    if (anno.anno == null) continue;
                    if (anno.anno.Left - 20 < e.X && anno.anno.Top < e.Y && (anno.anno.Left + anno.anno.Width) > e.X && (anno.anno.Top + anno.anno.Height + 20) > e.Y)
                    {
                        if ((DateTime.Now - anno.dateTime).TotalSeconds > 1)
                        {
                            deAnnos = anno;
                            isBreak = true;
                            break;
                        }
                    }
                }
                if (isBreak) break;
            }

            if (isBreak && StorageAnnos.Exists(a => a.annotations.Exists(o => o == deAnnos)))
            {
                deAnnos.anno.Dispose();
                StorageAnnos.ForEach(a => a.annotations.Remove(deAnnos));
            }
        });
    }

    /// 
    /// 保存注释
    /// 
    /// 
    /// 
    private void Line_Click(object sender, MouseEventArgs e)
    {
        Line line = sender as Line;
        if (StorageAnnos.Exists(a => a.line == line))
        {
            List annotations = StorageAnnos.Find(a => a.line == line).annotations;
            bool oldanno = false;
            for (var i = 0; i < annotations.Count; i++)
            {
                StorDeAnnos anno = annotations[i];
                if (anno.anno == null) continue;
                if (anno.anno.Left - 20 < e.X && anno.anno.Top < e.Y && (anno.anno.Left + anno.anno.Width) > e.X && (anno.anno.Top + anno.anno.Height + 20) > e.Y)
                {
                    oldanno = true;
                    break;
                }
            }
            if (!oldanno)
            {
                var anno = annotation.Clone() as Steema.TeeChart.Tools.Annotation;
                annotations.Add(new StorDeAnnos() { anno = anno });
            }
        }
        else
        {
            var anno = annotation.Clone() as Steema.TeeChart.Tools.Annotation;
            StorageAnnos.Add(new StorAnnos() { line = line, annotations = new List() { new StorDeAnnos() { anno = anno } } });
        }
    }

    /// 
    /// 鼠标进入TeeChart的事件
    /// 
    /// 
    /// 
    private void TChart_MouseEnter(object sender, EventArgs e)
    {
        cursorTool.Chart = this.tChart1.Chart;
    }

    /// 
    /// 鼠标离开TeeChart的事件
    /// 
    /// 
    /// 
    private void TChart_MouseLeave(object sender, EventArgs e)
    {
        cursorTool.Chart = null;
    }

    /// 
    /// 当鼠标进入线条时,将TeeChart的cursorTool工具指示的线条设置为对应的线条
    /// 
    /// 
    /// 
    private void Line_MouseEnter(object sender, EventArgs e)
    {
        cursorTool.Series = sender as Steema.TeeChart.Styles.Series;
    }

    /// 
    /// 当鼠标离开线条时,将TeeChart的cursorTool工具指示的线条设置为null
    /// 
    /// 
    /// 
    private void Line_MouseLeave(object sender, EventArgs e)
    {
        cursorTool.Series = null;
    }
    /// 
    /// 鼠标指示工具改变事件
    /// 
    /// 
    /// 
    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 = tChart1.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;
        }
    }
    /// 
    /// 计算某一点的Y值坐标
    /// 
    /// 曲线
    /// 对应的X轴的值
    /// 计算得到的对应的Y轴的值
    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)
        {

            return 0.0;
        }
    }
    /// 
    /// tChart放大缩小
    /// 
    /// 
    /// 
    private void tChart_Zoomed(object sender, EventArgs e)
    {
        _ = Task.Run(async () =>
        {
            await Task.Delay(50);
            for (var i = 0; i < StorageAnnos.Count; i++)
            {
                Line line = StorageAnnos[i].line;
                for (var j = 0; j < StorageAnnos[i].annotations.Count; j++)
                {
                    StorDeAnnos anno = StorageAnnos[i].annotations[j];
                    if (anno.anno == null) continue;
                    int x = (int)Convert.ToDouble(anno.anno.Text.Split(',')[0]);
                    anno.anno.Top = line.CalcYPos(x) - 20;
                    anno.anno.Left = line.CalcXPos(x);
                    if ((anno.anno.Left + anno.anno.Width + 12 >= anno.anno.Chart.Width))
                    {
                        anno.anno.Left -= (int)anno.anno.Width + 12;
                    }
                }
            }
        });
    }

    class StorAnnos
    {
        public Line line { get; set; }
        public List annotations { get; set; } = new List();
    }
    class StorDeAnnos
    {
        public DateTime dateTime { get; set; } = DateTime.Now;
        public Annotation anno { get; set; }
    }
    #endregion

你可能感兴趣的:(tChart光标捕捉优化)