1

I have done like this for creating click event handler for one part of the chart using mschart control, the chart is like this enter image description here

and the code like this

private void targetChartmouse_Click(object sender, MouseEventArgs e)
{ 
  try
  {
    var pos = e.Location;
    var results = kpiChartControl.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
    foreach (var result in results)
    {
      if (result.ChartElementType == ChartElementType.DataPoint)
      { 
        //do something....
      }
    }        
  }
}

It's working fine when we click on the chart (in every section of chart), but I want to do something only when we click on the live(green), not on every part. Is it possible to find the region of live(green)

Is it possible using c#?

I am doing winforms application

Modified Code

     public void targetChartmouse_Click(object sender, MouseEventArgs e)
   {
  Series statusseries = new Series();
  Series liveseries = null;
  Title title;
  string area;

  //Series totalserries;
  try
  {
    var pos = e.Location;
    var results = kpiChartControl.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
    foreach (var result in results)
    {
      if (result.ChartElementType == ChartElementType.DataPoint)
      {

          DataTable accepts = null;
          accepts = KPIData.livemembersmembershiptype(mf);

          DataTable membershiptypes = null;
          membershiptypes = KPIData.MembershipTotals(dtStartDate.Value, dtEndDate.Value, mf);

          area = "subchart";
          kpiChartControl.ChartAreas.Add(area);
          statusseries = kpiChartControl.Series.Add(area);
          statusseries.ChartArea = area;

          title = kpiChartControl.Titles.Add("Live Status  members  By MemberShip Type");
          title.DockedToChartArea = area;
          title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
          title.Alignment = ContentAlignment.TopLeft;
          kpiChartControl.Titles.Add("").DockedToChartArea = area;
          kpiChartControl.Titles.Add("Live status membership types").DockedToChartArea = area;


          area = "";
          kpiChartControl.Titles.Add("").DockedToChartArea = area;




          foreach (Title titles in kpiChartControl.Titles)
          {
            titles.IsDockedInsideChartArea = false;
          }


          foreach (ChartArea chartArea in kpiChartControl.ChartAreas)
          {
            chartArea.Area3DStyle.Enable3D = true;
            chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;
            //chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview;
          }

          if (area == "subchart")
          {
            foreach (Series chartSeries in kpiChartControl.Series)
            {
              chartSeries.ChartType = SeriesChartType.StackedColumn;
              chartSeries["ColumnDrawingStyle"] = "SoftEdge";
              chartSeries["LabelStyle"] = "Top";
              chartSeries.IsValueShownAsLabel = true;
              chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
            }
          }
          else if (area == "subchart")
          {
            foreach (Series chartSeries in kpiChartControl.Series)
            {
              chartSeries.ChartType = SeriesChartType.Pie;

              //chartSeries["PieLabelStyle"] = "Outside";
              chartSeries["PieLabelStyle"] = "Inside";
              chartSeries["DoughnutRadius"] = "30";
              chartSeries["PieDrawingStyle"] = "SoftEdge";

              chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;

            }

          }

          foreach (Legend legend in kpiChartControl.Legends)
          {
            legend.Enabled = false;
          }

          if (membershiptypes == null)
          {
            statusseries.Points.Clear();
            statusseries.Points.AddXY("no status", 0);

          }
          if (accepts == null)
          {
            liveseries.Points.Clear();
            liveseries.Points.AddXY("no live", 0);

          }
          kpiChartControl.Series["subchart"].Points.DataBindXY(accepts.Rows, "mshipname", accepts.Rows, "count");
          kpiChartControl.Series[0].Points.DataBindXY(membershiptypes.Rows, "Status", membershiptypes.Rows, "Value");

        }

        foreach (Series chartSeries in kpiChartControl.Series)
        {
          foreach (DataPoint point in chartSeries.Points)
          {

            switch (point.AxisLabel)
            {
              case "Silver membership": point.Color = Color.Green; break;
              case "Gold Membership": point.Color = Color.Blue; break;
              //case "Refused": point.Color = Color.Red; break;
              case "Weekend Peak": point.Color = Color.Cyan; break;
              case "prspect": point.Color = Color.Indigo; break;

            }
            point.Label = string.Format("{0:0}", point.YValues[0]);
          }

        }

        foreach (Series chartSeries in kpiChartControl.Series)
        {
          foreach (DataPoint point in chartSeries.Points)
          {
            switch (point.AxisLabel)
            {
              case "New": point.Color = Color.Cyan; break;
              case "Live": point.Color = Color.Green; break;
              case "Defaulter": point.Color = Color.Red; break;
              case "Cancelled": point.Color = Color.Orange; break;
              case "Completed": point.Color = Color.Blue; break;
              case "Frozen": point.Color = Color.Violet; break;
            }

            point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
          }
        }

    }
  }
  catch
  {
  }

}
user682417
  • 1,478
  • 4
  • 25
  • 46

2 Answers2

1

This might work:

try
{
  var pos = e.Location;
  var results = kpiChartControl.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
  foreach (var result in results)
  {
    if (result.ChartElementType == ChartElementType.DataPoint)
    {
      if (result.Series.Points[result.PointIndex].AxisLabel == "Live")
      {
        Console.WriteLine("success?");
      }
    }
  }  
}
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
0

Maybe you can acquire the DatePoint using [result.PointIndex(http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.hittestresult.pointindex.aspx) and result.Series.

if(result.Series.Points.Item[result.PointIndex].Label == "Live") {
    // ...
}
marc
  • 6,103
  • 1
  • 28
  • 33
  • Updated from Series to DataPoint. – marc Jul 26 '11 at 08:43
  • `theSeries` is the `Series` your Diagram gets feeded with. You can e.g. acquire it using `Series theSeries = theChart.Series.Item(0)`. – marc Jul 26 '11 at 08:51
  • *What* exactly is not working? Is it not compiling? Do you get an error? Have you checked the `Label` using e.g. a MsgBox? – marc Jul 26 '11 at 09:38
  • points .items take a look at my entire code.......in modified part in my question – user682417 Jul 26 '11 at 09:46
  • Oh, a little typo, it should be just `Item`. – marc Jul 26 '11 at 09:58
  • Please first try to do exactly: `MessageBox.Show(theSeries.Points.Item[result.PointIndex].Label);` inserted just in your first chunk of code and tell me what the messagebox shows. Comment out all the other code. – marc Jul 26 '11 at 10:09
  • In above code which one i have to take either statusseries or liveseries – user682417 Jul 26 '11 at 10:12
  • Take result.Series: `MessageBox.Show(result.Series.Points.Item[result.PointIndex].Label);`. Imho my last edit should get it to work. – marc Jul 26 '11 at 10:25
  • what extension? When you type in exactly what I wrote, what compiler error do you get? – marc Jul 26 '11 at 11:14
  • Error 1 'System.Windows.Forms.DataVisualization.Charting.DataPointCollection' does not contain a definition for 'item' and no extension method 'item' accepting a first argument of type 'System.Windows.Forms.DataVisualization.Charting.DataPointCollection' could be found (are you missing a using directive or an assembly reference?) this is error i have got – user682417 Jul 26 '11 at 11:19
  • again. in your first snippet, instead of `// do something...`, put `MessageBox.Show(result.Series.Points.Item[result.PointIndex].Label);`. Then start the app and click on the chart. The label should be output. Place exactly this code in! – marc Jul 26 '11 at 11:32