先看效果:
项目遇到一个业务需求,需要根据双击不同的节点,做不同的操作。
在网上查ChartControl点击事件,发现有一个着重点:ChartControl.RuntimeHitTesting属性一定要设为True。
但是!!!新版的ChartControl.RuntimeHitTesting被弃用了!!!!(头大)
跑去翻官方的API文档,发现这样一个属性:ChartControl.SelectionMode(开心~~~)
官方给的描述是这样的:
Gets or sets a value which specifies how the chart elements are selected.
ChartControl控件中为:
参数说明:
Single:
Only one series element can be selected. Click or tap a series element to select it.
Multiple :
You can select multiple series elements simultaneously. Click or tap a series element to select it. Hold Ctrl and click a series element to deselect it.
Extended:
The Extended mode combines the Single and Multiple selection modes' behaviors.
this.chartControl1.SelectionMode = DevExpress.XtraCharts.ElementSelectionMode.Single;
SO,问题解决了!!
贴源码:
using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DemoBar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
double[] hz = { 1, 2, 3, 4};
double[] zbb = { 9, 8, 7, 6};
Series series = new Series();
for (int i = 0; i < hz.Length; i++)
{
series.Points.Add(new SeriesPoint(hz[i], zbb[i]));
}
chartControl1.Series.Add(series);
}
private void chartControl1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ChartHitInfo hitInfo = chartControl1.CalcHitInfo(e.X, e.Y);
if (hitInfo.SeriesPoint != null)
MessageBox.Show(hitInfo.SeriesPoint.Argument+","+ hitInfo.SeriesPoint.Values[0]);
else
MessageBox.Show("失败了!");
}
}
}