none
Identifying mouse click on chart bar RRS feed

  • Question

  • Group

         I'm working with System.Windows.Forms.DataVisualization.Charting and as attached file, I need to identify when the user clicks on any of the bars of the chart where based on the click would give a display of the product that was clicked, can do something similar to this?

    Example:

        - User clicked on the last bar of the green color chart so I would be giving a display of the product, in the case (Product D).

    Thursday, July 12, 2018 12:37 AM

All replies

  • Hi neibala,

    Thank you for posting here.

    For your question, if you want to get the product name when you click on the chart, please try the code below.

    private void Chart_Load(object sender, EventArgs e)
            {           
                //chart2
                // Data arrays.
                string[] seriesArray = { "ProdutoA", "ProdutoB", "ProdutoC", "ProdutoD" };
                double[] pointsArray = { 20, 10, 70, 50 };
    
                // Set palette.
                this.chart2.Palette = ChartColorPalette.SeaGreen;
    
                // Set title.
                this.chart2.Titles.Add("Tabela de Produtos");
    
                // Add series.
                for (int i = 0; i < seriesArray.Length; i++)
                {
                    // Add series.
                    Series series = this.chart2.Series.Add(seriesArray[i]);
    
                    // Add point.
                    series.Points.Add(pointsArray[i]);
                }
            }
    
            ToolTip tooltip = new ToolTip();
    
            private void Chart2_MouseClick(object sender, MouseEventArgs e)
            {
                HitTestResult result = new HitTestResult();
                result = chart2.HitTest(e.X, e.Y);
                tooltip.Show(result.Series.Name, this.chart2);
            } 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 16, 2018 4:11 AM
    Moderator
  • Wendy Zang / Group

         As I'm still trying to find the right structure in charting, see why based on your code below, is it always returning the value zero (0) for PointIndex ?

    (int index = result.PointIndex;)

      private void chart1_MouseClick(object sender, MouseEventArgs e)
     {
    
                // Call Hit Test Method
                HitTestResult result = chart1.HitTest(e.X, e.Y);
                int index = result.PointIndex;
    
                if (result.ChartElementType == ChartElementType.DataPoint)
                {
                    DataPoint selectedDataPoint = (DataPoint)result.Object;
                    MessageBox.Show(result.Series.Name + ": Y-value: " + selectedDataPoint.YValues[0] + " indice " + index.ToString());
                }
    
                else if (result.ChartElementType == ChartElementType.LegendItem)
                {
                    string elementType = result.ChartElementType.ToString();
                    MessageBox.Show(this, "Selected Element is: " + elementType + " indice " + index.ToString());
                }
    
                else if (result.ChartElementType != ChartElementType.Nothing)
                {
                    string elementType = result.ChartElementType.ToString();
                    MessageBox.Show(this, "Selected Element is: " + elementType);
                }
    
            }

        

    Monday, July 16, 2018 11:45 PM