【问题标题】:How to get Y-axis value in TeeChart when mousepointer left click鼠标左键单击时如何在 TeeChart 中获取 Y 轴值
【发布时间】:2016-03-14 15:24:30
【问题描述】:

当鼠标左键单击图表时,我正在尝试获取多个系列的 y 轴值。

private void mainTChart_MouseDown(object sender, MouseEventArgs e)
{
     if (e.Button == MouseButtons.Left)
     {
          mainTChart.Axes.Bottom.CalcMinMax(ref minTime, ref maxTime);
          cdens = mainTChart.Series[0].YValues.Value[e.Y];
          cvisc = mainTChart.Series[1].YValues.Value[e.Y];
          //cvisc = mainTChart.Series[1].YScreenToValue(e.Y);
     }
}

上面代码的问题是我有时会出现索引异常。 我将鼠标 Y 坐标作为values[list] 的索引。 有时我的鼠标 y 坐标索引高于 values[list] 的范围。

谁能建议我如何获取鼠标点击点的索引。还是将鼠标坐标转换为索引?

如何获取所有系列的 y 轴值?

【问题讨论】:

    标签: c# charts teechart


    【解决方案1】:

    上面代码的问题是我摆脱了索引异常 有时。

    您应该检查索引是否不同于 -1,这意味着没有点被点击。

    谁能建议我如何获取鼠标点击点的索引。或者 将鼠标坐标转换为索引?

    您应该使用系列的Clicked 方法。 TeeChart for .NET forumfor example 上提供了大量示例:

    public Form1()
    {
      InitializeComponent();
      InitializeChart();
    }
    
    private Steema.TeeChart.Styles.FastLine fastLine1;
    
    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;
      tChart1.MouseMove += new MouseEventHandler(tChart1_MouseMove);
    
      fastLine1 = new Steema.TeeChart.Styles.FastLine(tChart1.Chart);
      fastLine1.FillSampleValues();
      fastLine1.MouseEnter += new EventHandler(fastLine1_MouseEnter);
    }
    
    private int x, y;
    
    void tChart1_MouseMove(object sender, MouseEventArgs e)
    {
      x = e.X;
      y = e.Y;
    }
    
    void fastLine1_MouseEnter(object sender, EventArgs e)
    {
      int index = fastLine1.Clicked(x, y);
    
      if (index != -1)
      {
        tChart1.Header.Text = index.ToString();
      }
    }
    

    如何获取所有系列的y轴值?

    在 TeeChart 功能演示中有一个示例。例如What's New?\Welcome !\New in Series\Interpolating line series。该演示可以在 TeeChart 的程序组中找到。下面是示例代码:

    public class Line_Interpolate : Steema.TeeChart.Samples.BaseForm
    {
        private Steema.TeeChart.Styles.Line line1;
        private Steema.TeeChart.Styles.Line line2;
        private Steema.TeeChart.Styles.Line line3;
        private Steema.TeeChart.Tools.CursorTool cursorTool1;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.ComponentModel.IContainer components = null;
        private Steema.TeeChart.Tools.GridBand gridBand1;
        private double xval;
    
        /// <summary>
        /// Calculate y=y(x) for arbitrary x. Works fine only for line series with ordered x values.
        /// </summary>
        /// <param name="series"></param>
        /// <param name="firstindex"></param>
        /// <param name="lastindex"></param>
        /// <param name="xvalue"></param>
        /// <returns>y=y(xvalue) where xvalue is arbitrary x value.</returns>
        private double InterpolateLineSeries(TeeChart.Styles.Custom series, int firstindex, int lastindex, double xvalue)
        {
          int index;
          for (index=firstindex; index<=lastindex; 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;
        }
    
        private double InterpolateLineSeries(TeeChart.Styles.Custom series,double xvalue)
        {
          return InterpolateLineSeries(series,series.FirstVisibleIndex,series.LastVisibleIndex,xvalue);
        }
    
        public Line_Interpolate()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
    
            foreach (TeeChart.Styles.Series s in tChart1.Series)
        s.FillSampleValues(20);
        }
    
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
    
        private void cursorTool1_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
        {
          xval = e.XValue;
          tChart1.Header.Text = "";
          for (int i=0; i<tChart1.Series.Count; i++)
            if (tChart1.Series[i] is TeeChart.Styles.Custom)
            {
              tChart1.Header.Text += tChart1.Series[i].Title + ": Y("+e.XValue.ToString("0.00")+")= ";
              tChart1.Header.Text += InterpolateLineSeries(tChart1.Series[i] as Steema.TeeChart.Styles.Custom,e.XValue).ToString("0.00")+"\r\n";
            }
        }
    
        private void Line_Interpolate_Load(object sender, System.EventArgs e)
        {
          cursorTool1_Change(tChart1,new Steema.TeeChart.Tools.CursorChangeEventArgs());
        }
    
        private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
          if (checkBox1.Checked)
          {
            int xs = tChart1.Axes.Bottom.CalcXPosValue(xval);
            int ys;
            g.Brush.Visible = true;
            g.Brush.Solid = true;
            for (int i=0; i<tChart1.Series.Count; i++)
              if (tChart1.Series[i] is TeeChart.Styles.Custom)
              {
                ys = tChart1.Series[i].GetVertAxis.CalcYPosValue(InterpolateLineSeries(tChart1.Series[i] as Steema.TeeChart.Styles.Custom,xval));
                g.Brush.Color = tChart1.Series[i].Color;
                g.Ellipse(new Rectangle(xs-4,ys-4,8,8));
              }
          }
        }
    }
    

    【讨论】:

    • Calvet 通过鼠标点击获取系列索引工作正常。我怀疑它仅在单击该系列时有效,但如果在系列上方单击一点则不起作用。我能理解。如果单击图表中的其他位置(不在系列上),是否可以获得系列索引?不知道有没有可能!
    • @verendra 我不确定你的问题,但我敢打赌,我发布的插值示例可能会回答这个问题。你看了吗?如果这没有帮助,请编辑您的问题并包含一个简单的代码 sn-p 我们可以“按原样”运行以重现问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多