1. 程式人生 > 程式設計 >c# 設定TeeChart控制元件的提示文字

c# 設定TeeChart控制元件的提示文字

  使用第三方Steema的TeeChart控制元件,設定滑鼠放在某一線條點上,顯示某一點的資料標籤問題(虛線型十字游標基準線,放在線上顯示對應點的二維座標軸資料資料),呼叫InitTeeChartTipTools方法即可:

/// <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.Change += CursorTool_Change;
  //設定提示文字的資訊
  annotation = new Steema.TeeChart.Tools.Annotation(tChart.Chart);
  annotation.Shape.Font.Name = "Arial";
  annotation.Shape.Font.Size = 12;
  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;

  for (int i = 0; i < tChart.Series.Count; i++)
  {
    tChart.Series[i].MouseEnter += Line_MouseEnter;
    tChart.Series[i].MouseLeave += Line_MouseLeave;
  }

  tChart.MouseLeave += TChart_MouseLeave;
  tChart.MouseEnter += TChart_MouseEnter;
}

/// <summary>
/// 滑鼠進入TeeChart的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TChart_MouseEnter(object sender,EventArgs e)
{
  cursorTool.Chart=tChartCurve.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 = tChartCurve.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)
  {
    Console.WriteLine(ex.Message);
    return 0.0;
  }
}

以上就是c# 設定TeeChart控制元件的提示文字的詳細內容,更多關於c# 設定提示文字的資料請關注我們其它相關文章!