Mschart实现十字光标的方式

  private void ChartTrend_MouseMove(object sender, MouseEventArgs e)
        {
            HitTestResult result = ChartTrend.HitTest(e.X, e.Y);///根据鼠标点获取元素点
            if (result != null && result.Object != null)
            {
                if (result.ChartElementType == ChartElementType.DataPoint)////根据元素的类型再进行处理
                {
                    DataPoint selectedDataPoint = (DataPoint)result.Object;///dataPoint的位置和鼠标的位置不一致。获取选择的元素点

                    int index = result.PointIndex;///可以根据元素点得到索引位置
                    InitSelectValue(index, dt.Rows.Count);///根据数据索引点获取数据并进行处理
                    ChartTrend.ChartAreas[0].CursorX.Position = selectedDataPoint.XValue;////游标的位置x轴位置用的是这个位置

                    ChartTrend.ChartAreas[0].CursorX.IsUserEnabled = true;///可以显示
                }
            }
          
        }

  private void ChartTrend_GetToolTipText(object sender, ToolTipEventArgs e)
        {

            if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
            {
                int i = e.HitTestResult.PointIndex;
                DataRow dr = dt.Rows[i];
                //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。 
                e.Text = string.Format("时间:{0};\n流量:{1:F3} ", dr["dataTime"].ToString(),decimal.Parse(dr["FLOW"].ToString()));
            } 
        }

 

   private void InitSelectValue(int index,int totalIndex)
        {
            if (index > 0 && index <= totalIndex)
            {
                txtFlow.Text = dt.Rows[index]["FLOW"].ToString();
                txtGetTime.Text = dt.Rows[index]["dataTime"].ToString();

                txtStationID.Text = dt.Rows[index]["stationNo"].ToString();
                txtStationName.Text = dt.Rows[index]["stationName"].ToString();
           
       ////     可以进行处理了/
           
            }
           
        }

 

posted @ 2018-06-06 10:34  laolv  阅读(524)  评论(0)    收藏  举报