MSChart(Microsoft Chart Controls)图表中的点击和选择
首先就是响应MouseDown事件
this.Chart1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Chart1_MouseDown);
private void Chart1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Call Hit Test Method
HitTestResult result = Chart1.HitTest( e.X, e.Y );
if( result.ChartElementType == ChartElementType.DataPoint )
{
//Create Dialog
Dialog dlg = new Dialog();
dlg.ChartRef = Chart1;
dlg.pointIndex = result.PointIndex;
dlg.ShowDialog(this);
}
else if( result.ChartElementType != ChartElementType.Nothing )
{
string elementType = result.ChartElementType.ToString();
MessageBox.Show( this, "Selected Element is: " + elementType );
}
}
另外就是响应MouseMove事件
this.Chart1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Chart1_MouseMove);
private void Chart1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Call Hit Test Method
HitTestResult result = Chart1.HitTest( e.X, e.Y );
// If a Data Point or a Legend item is selected.
if( result.ChartElementType == ChartElementType.DataPoint ||
result.ChartElementType == ChartElementType.LegendItem ||
result.ChartElementType == ChartElementType.DataPointLabel )
{
// Set cursor type
this.Cursor = Cursors.Hand;
}
else if( result.ChartElementType != ChartElementType.Nothing &&
result.ChartElementType != ChartElementType.PlottingArea )
{
// Set cursor type
this.Cursor = Cursors.Hand;
}
else
{
// Set default cursor
this.Cursor = Cursors.Default;
}
}