使用第三方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# 设置提示文本的资料请关注www.887551.com其它相关文章!