问题
I have a .net 4.0 point chart in my app. I would like to capture the mouse click on a data marker. When the user clicks on a particular point, I'd like to go to the row in the bound table where the data came from.
Is this functionality built-in to the .net chart control?
EDIT: I found that I may have actually wanted the cursor position value rather than requiring the user to click on a specific data point. Once I have the cursor location, that value can be used to find the row in the dataset that is closest to the mouse click. I accepted the answer to my original question below as it was a correct answer to what I initially requested.
The solution to my 'real' problem was found in the post by user quinn in the post Showing Mouse Axis Coordinates on Chart Control
{
var chartArea = _chart.ChartAreas[0];
var xValue = chartArea.AxisX.PixelPositionToValue(x);
var yValue = chartArea.AxisY.PixelPositionToValue(y);
return new Tuple<double, double>(xValue, yValue);
}
回答1:
You can try this:
protected void Page_Load(object sender, EventArgs e)
{
foreach (DataPoint dp in this.Chart1.Series["YourSeriesName"].Points)
{
dp.PostBackValue = "#VALX,#VALY";
}
}
protected void Chart1_Click(object sender, ImageMapEventArgs e)
{
string[] pointData = e.PostBackValue.Split(',');
// Add click event code here
}
You need to set OnClick="Chart1_Click"
in asp:Chart
.
Or if you have multiple series on your chart you can set PostBack on Series directly and pass information about the series.
来源:https://stackoverflow.com/questions/9691000/get-mouse-click-event-from-microsoft-chart-control-click-on-data-marker