2013-12-10 84 views
0

我使用Microsoft.DataVisualization.Charting并希望在点击它时获取点的值。从鼠标点击获取系列值

我的问题:我想要正好即使它的价值我点击,即使它只有一个值计算的图表和2点之间。

实施例:3分:P(0; 3),P(1; 6),P(3; 12)

当我点击在x值2我想获得9的结果,如果所述线是线性的。

目前,我这样做:

  HitTestResult[] hits = chart.HitTest(e.X, e.Y, false, ChartElementType.PlottingArea); 

      //DataInformation save the DateTime and Value for later use 
      DataInformation[] dinfo = new DataInformation[hits.Length]; 

      foreach (ChartArea area in chart.ChartAreas) 
      { 
       area.CursorX.LineWidth = 0; //clear old lines 
      } 

      for (int i = 0; i < hits.Length; i++) //for all hits 
      { 
       if (hits[i].ChartElementType == ChartElementType.PlottingArea) 
       { 
        //val saves the x-value clicked in the ChartArea 
        double val = hits[i].ChartArea.AxisX.PixelPositionToValue(e.X); 
        DataPoint pt = chart.Series[hits[i].ChartArea.Name].Points.Last(elem => elem.XValue < val); 

        dinfo[i].caption = hits[i].ChartArea.Name; 
        dinfo[i].value = pt.YValues[0].ToString(); 

        //hits[i].ChartArea.CursorX.Position = pt.XValue; 
       } 
      } 

这显示出正确的价值观为每现有数据点,但不是点击点。

我怎样才能得到确切的价值?

回答

0

看来,没有办法得到确切的价值。我改为OxyPlot。 OxyPlot可以更快地显示数据,并且您可以获得任意点的精确值。