1
votes

I want to create a chart and i am using the charts from the toolbox in Visual studio 2010. This is the first time i am trying to do this and so far i have learned the basics of the chart like how to create datapoints and add them to the series in order to get the chart. What i wanted to know is that if its possible to make the datapoints visible to the user (like the dots in a point chart) and the user to be able to click on a datapoint and get a specific result. What i was thinking is that since i am creating the datapoint is there an option to make it "clickable" or do i have to create seperate mouseclick events for every datapoint. Excuse me for my ignorance but i am new to this. Where can i find a guide that will help me to create the chart. Here is a part of my code so far:

chart1.Series[0].IsVisibleInLegend = false;
chart1.Series[0].ChartType = SeriesChartType.FastLine;

DateTime sday = Convert.ToDateTime(earnings1.dataGridView1[0, 0].Value.ToString());
chart1.Series[0].XValueType = ChartValueType.String;
int i = 0;
        foreach (DataGridViewRow dgvr in earnings1.dataGridView1.Rows)
        {
            decimal testing = Convert.ToDecimal(earnings1.dataGridView1[1, i].Value);
            testing = decimal.Truncate(testing);

            var point = new DataPoint(i+1, Convert.ToDouble(testing));

            point.Label = testing.ToString();
            point.Font = new Font("Century Gothic", 8, FontStyle.Bold);
            chart1.Series[0].Points.Add(point);
            chart1.Series[0].Points[i].AxisLabel = sday.ToString("dd/MM/yyyy");
            sday = sday.AddDays(1);
            i++;
          }

what i am doing is that i have a datagridview with 2 columns. One with the date and the other column with a decimal number. I created a chart with the dates on the x axis (custom labeled since the dates are from a mysql database and the format is yyyy:mm:dd and i convert it to dd/MM/yyyy) and on the y axis the values. I am creating a datapoint a each day with its value aand i plot them on the chart. How can i click now on the points so that for example when i click on the value at 24/May/2012 to do something like show some other information from the database at that specific date. Thank you in advance for your support, any help will be truly appreciated. Sincerely George Georgiou

2

2 Answers

0
votes

you cant add click events on datapoints. What you can do though is to add annotations on the datapoints and use the is selected event handler on the annotations. Pretty nice since you can set them properties such as font, size, border type, color etc..

0
votes

Code copied off Microsoft site and works on c# 2017

private void chtSales_MouseClick(object sender, MouseEventArgs e)
{
   var r = chtSales.HitTest(e.X, e.Y);
   if(r.ChartElementType == ChartElementType.DataPoint)
   {
       int index = r.PointIndex;
        MessageBox.Show(index.ToString());
    }

}

index returns an integer between 0 and the total number of datapoints - 1.