private void chart1_MouseClick(object sender, MouseEventArgs e) { var pos = e.Location; clickPosition = pos; var results = chart1.HitTest(pos.X, pos.Y, false, ChartElementType.PlottingArea); foreach (var result in results) { if (result.ChartElementType == ChartElementType.PlottingArea) { double xVal = Math.Round(result.ChartArea.AxisX.PixelPositionToValue(pos.X), 1); double yVal = Math.Round(result.ChartArea.AxisY.PixelPositionToValue(pos.Y), 1); if (addNewData.Checked) { this.dataTable.Rows.Add(xVal, yVal, categorySelector.Text); refreshChart(sender, e); } else { int k = Convert.ToInt16(kField.Text); bool weighted = weightedBox.Checked; DataTable dt = new DataTable(); dt = (DataTable)this.dataGridView1.DataSource; string categoryGuess = KNN_calculator.guess(dt, xVal, yVal, k, weighted); // Add to chart. chart1.Series["Original Data"].Points.AddXY(xVal, yVal); int point_count = chart1.Series["Original Data"].Points.Count - 1; chart1.Series["Original Data"].Points[point_count].MarkerStyle = MarkerStyle.Square; chart1.Series["Original Data"].Points[point_count].MarkerSize = 10; if (categoryGuess.Length > 1) { chart1.Series["Original Data"].Points[point_count].Color = Color.Gray; } else { chart1.Series["Original Data"].Points[point_count].Color = catColor(categoryGuess); }; chart1.Invalidate(); } } } }
private void drawZoneButton_Click(object sender, EventArgs e) { // Work out data boundaries. // (x1,y1) is bottom LH corner. // (x2,y2) is top RH corner. double x, y, x1 = 0, y1 = 0, x2 = 0, y2 = 0; for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++) { var xcell = dataGridView1.Rows[rows].Cells[0].Value; var ycell = dataGridView1.Rows[rows].Cells[1].Value; var catcell = dataGridView1.Rows[rows].Cells[2].Value; if (xcell != DBNull.Value && ycell != DBNull.Value && catcell != DBNull.Value) { // Read next data pair. x = Convert.ToDouble(xcell); y = Convert.ToDouble(ycell); // Update boundaries. if (rows == 0) { x1 = x; y1 = y; x2 = x; y2 = y; } else { x1 = Math.Min(x1, x); y1 = Math.Min(y1, y); x2 = Math.Max(x2, x); y2 = Math.Max(y2, y); } } } // Guess category at each point in zone. int k = Convert.ToInt16(kField.Text); bool weighted = weightedBox.Checked; double interval = Convert.ToDouble(intervalBox.Text); int marker_size = Convert.ToInt16(markerSizeBox.Text); DataTable dt = new DataTable(); dt = (DataTable)this.dataGridView1.DataSource; for (double xVal = x1; xVal <= x2; xVal += interval) { for (double yVal = y1; yVal <= y2; yVal += interval) { // Guess category. string categoryGuess = KNN_calculator.guess(dt, xVal, yVal, k, weighted); // Add to chart. chart1.Series["Original Data"].Points.AddXY(xVal, yVal); int point_count = chart1.Series["Original Data"].Points.Count - 1; chart1.Series["Original Data"].Points[point_count].MarkerStyle = MarkerStyle.Square; chart1.Series["Original Data"].Points[point_count].MarkerSize = marker_size; if (categoryGuess.Length > 1) { chart1.Series["Original Data"].Points[point_count].Color = Color.Gray; } else { chart1.Series["Original Data"].Points[point_count].Color = catColor(categoryGuess); }; } } chart1.Invalidate(); }