public static DataPoint GetPointAtMouse(Chart c, MouseEventArgs e) { var result = c.HitTest(e.X, e.Y); // If the mouse if over a data point if (result.ChartElementType == ChartElementType.DataPoint) { // Find selected data point var point = c.Series[0].Points[result.PointIndex]; return point; } return null; }
/// <summary> /// Mouse Down Event /// </summary> 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) { return; } // Remove data points Chart1.Series[0].Points.Clear(); // If Pie chart is selected if (Chart1.Series[0].ChartType == SeriesChartType.Pie) { // Set Chart Type Chart1.Series[0].ChartType = SeriesChartType.Column; // Add data points Chart1.Series[0].Points.Add(5); Chart1.Series[0].Points.Add(6); Chart1.Series[0].Points.Add(7); Chart1.Series[0].Points.Add(2); // Set Axis labels Chart1.Series[0].Points[0].AxisLabel = "N America"; Chart1.Series[0].Points[1].AxisLabel = "S America"; Chart1.Series[0].Points[2].AxisLabel = "Europe"; Chart1.Series[0].Points[3].AxisLabel = "Asia"; // Remove custom attributes Chart1.Series[0].CustomProperties = ""; // Recalculate and repaint chart Chart1.ChartAreas[0].RecalculateAxesScale(); Chart1.Invalidate(); return; } // Set Label style for pie chart Chart1.Series[0].CustomProperties = "LabelStyle=outside"; // Remove gradient for data points Chart1.Series[0].BackGradientStyle = GradientStyle.None; switch (result.PointIndex) { // N America case 0: // Add data points Chart1.Series[0].ChartType = SeriesChartType.Pie; Chart1.Series[0].Points.Add(3); Chart1.Series[0].Points.Add(2); Chart1.Series[0].Points.Add(8); // Set Axis labels Chart1.Series[0].Points[0].AxisLabel = "Country 1"; Chart1.Series[0].Points[1].AxisLabel = "Country 2"; Chart1.Series[0].Points[2].AxisLabel = "Country 3"; break; // S America case 1: // Add data points Chart1.Series[0].ChartType = SeriesChartType.Pie; Chart1.Series[0].Points.Add(4); Chart1.Series[0].Points.Add(6); Chart1.Series[0].Points.Add(2); // Set Axis labels Chart1.Series[0].Points[0].AxisLabel = "Country 4"; Chart1.Series[0].Points[1].AxisLabel = "Country 5"; Chart1.Series[0].Points[2].AxisLabel = "Country 6"; break; // Europe case 2: // Add data points Chart1.Series[0].ChartType = SeriesChartType.Pie; Chart1.Series[0].Points.Add(5); Chart1.Series[0].Points.Add(7); Chart1.Series[0].Points.Add(2); Chart1.Series[0].Points.Add(3); // Set Axis labels Chart1.Series[0].Points[0].AxisLabel = "Country 7"; Chart1.Series[0].Points[1].AxisLabel = "Country 8"; Chart1.Series[0].Points[2].AxisLabel = "Country 9"; Chart1.Series[0].Points[3].AxisLabel = "Country 10"; break; // Asia case 3: // Add data points Chart1.Series[0].ChartType = SeriesChartType.Pie; Chart1.Series[0].Points.Add(4); Chart1.Series[0].Points.Add(3); Chart1.Series[0].Points.Add(6); Chart1.Series[0].Points.Add(5); Chart1.Series[0].Points.Add(4); // Set Axis labels Chart1.Series[0].Points[0].AxisLabel = "Country 11"; Chart1.Series[0].Points[1].AxisLabel = "Country 12"; Chart1.Series[0].Points[2].AxisLabel = "Country 13"; Chart1.Series[0].Points[3].AxisLabel = "Country 14"; Chart1.Series[0].Points[4].AxisLabel = "Country 15"; break; } Chart1.ChartAreas[0].RecalculateAxesScale(); Chart1.Invalidate(); }
private DateTime GetDateTimeFromPosition(Point pos, Chart chart) { var results = chart.HitTest(pos.X, pos.Y, false, ChartElementType.PlottingArea); foreach (var xVal in from result in results where result.ChartElementType == ChartElementType.PlottingArea select result.ChartArea.AxisX.PixelPositionToValue(pos.X)) { return DateTime.FromOADate(xVal); } return DateTime.Now; }
private Chart CreatChart() { var chart = new Chart(); chart.MouseClick += (o, e) => { HitTestResult f = chart.HitTest(e.X, e.Y); int p = f.PointIndex; if (p > -1) { string check = chart.Series[_SerieName].Points[p].CustomProperties; if (check != ("Exploded=True")) { chart.Series[_SerieName].Points[p].CustomProperties = "Exploded=True"; } else { chart.Series[_SerieName].Points[p].CustomProperties = "Exploded=none"; } } }; var chartArea = new ChartArea(); var legend = new Legend(); chartArea.Area3DStyle.Enable3D = true; chart.ChartAreas.Add(chartArea); chart.Legends.Add(legend); chart.Palette = ChartColorPalette.Pastel; return chart; }