C# Chart使用总结 2 ---------chart悬停选定数值操作
1、用鼠标悬停事件处理
private void Form1_Load(object sender, EventArgs e) { //connStr = connPath1 + conn2; fileName = connPath1 + conn2; connStr = "Provider= Microsoft.Ace.OLEDB.12.0;Data Source =" + connPath1 + conn2; ; chart1.MouseHover += new EventHandler(chart1_MouseHover); } //直接用鼠标悬停事件处理 void chart1_MouseHover(object sender, EventArgs e) { if (chart1.Series.Count != 0)//这里判断是否包含Series,也可以用Series.Contains() { for (int i = 0; i < chart1.Series["Series1"].Points.Count; i++) { chart1.Series["Series1"].Points[i].ToolTip = "#VAL";//获取值,可以酌量添加 } } }
2、用getTollTip事件处理
//在显示工具提示之前发生以获取工具提示文本 chart1.GetToolTipText += new EventHandler<ToolTipEventArgs>(chart_GetToolTipText); void chart_GetToolTipText(object sender, ToolTipEventArgs e) { if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) { int i = e.HitTestResult.PointIndex; DataPoint dp = e.HitTestResult.Series.Points[i]; //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。 e.Text = string.Format("次数:{0};数值:{1:F3} ", dp.XValue, dp.YValues[0]); } }