Winform程序。
ChartControl.RuntimeHitTesting属性一定要设为True。
Line Series markers的Visible一定要弄成True。CalcHitInfo的SeriesPoint一直为null,最后跑到devexpress support center上问的。我的dev版本是13.1.5,设置属性的方法是Series->View->MarkerVisibility。有的版本可能是Series -> LineMarkerOptions -> Visible。
我的是以曲线图Spline为例,下面就是代码。
1.鼠标点击点弹出Messagebox
private void chartControl4_MouseClick(object sender, MouseEventArgs e)
{
ChartHitInfo hitInfo = chartControl4.CalcHitInfo(e.Location);
if (hitInfo.SeriesPoint != null)
{
MessageBox.Show(hitInfo.SeriesPoint.Values[0].ToString());
}
}
2.鼠标移动用ToolTipController显示值
外面定义
ToolTipController toolTipController = new ToolTipController();
下面是dev的源码
private void chartControl4_MouseMove(object sender, MouseEventArgs e)
{
ChartHitInfo hitInfo = chartControl4.CalcHitInfo(e.Location);
StringBuilder builder = new StringBuilder();
if (hitInfo.InDiagram)
builder.AppendLine("In diagram");
if (hitInfo.InNonDefaultPane)
builder.AppendLine("In non-default pane: " + hitInfo.NonDefaultPane.Name);
if (hitInfo.InAxis)
{
builder.AppendLine("In axis: " + hitInfo.Axis.Name);
if (hitInfo.AxisLabelItem != null)
builder.AppendLine(" Label item: " + hitInfo.AxisLabelItem.Text);
if (hitInfo.AxisTitle != null)
builder.AppendLine(" Axis title: " + hitInfo.AxisTitle.Text);
}
if (hitInfo.InChartTitle)
builder.AppendLine("In chart title: " + hitInfo.ChartTitle.Text);
if (hitInfo.InLegend)
builder.AppendLine("In legend");
if (hitInfo.InSeries)
builder.AppendLine("In series: " + ((Series)hitInfo.Series).Name);
if (hitInfo.InSeriesLabel)
{
builder.AppendLine("In series label");
builder.AppendLine(" Series: " + ((Series)hitInfo.Series).Name);
}
if (hitInfo.SeriesPoint != null)
{
builder.AppendLine(" Argument: " + hitInfo.SeriesPoint.Argument);
if (!hitInfo.SeriesPoint.IsEmpty)
builder.AppendLine(" Value: " + hitInfo.SeriesPoint.Values[0]);
}
if (builder.Length > 0)
toolTipController.ShowHint("Hit-testing results:\n" + builder.ToString(), chartControl4.PointToScreen(e.Location));
else
toolTipController.HideHint();
}
private void chartControl4_MouseLeave(object sender, EventArgs e)
{
toolTipController.HideHint();
}
3.另一种鼠标移动显示信息的方法,用CustomDrawCrosshair事件,从别人那里学习的。这种还可以显示图片。
private void chartControl4_CustomDrawCrosshair(object sender, CustomDrawCrosshairEventArgs e)
{
foreach (CrosshairElement element in e.CrosshairElements)
{
SeriesPoint point = element.SeriesPoint;
element.LabelElement.MarkerImage = Image.FromFile(@"F:\Resources\Add.png");// 设置图片路径
element.LabelElement.MarkerImageSizeMode = ChartImageSizeMode.Stretch;
element.LabelElement.MarkerSize = new Size(100, 100); // 大小
element.LabelElement.Text = point.Values[0].ToString();//显示要显示的文字
}
}
这里有devexpress用CustomDrawCrosshair事件显示点信息的DemoHow to: Show a Tooltip with a Series Point's Data