Profile Picture

How to detect mouse click for Axis labels?

Posted By Alexandr S. 12 Years Ago
Author
Message
Nevron Support
Posted 12 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009

Hi Alexandr,

The following code snippet shows how to find the closest axis tick value (provided that the step is fixed):

  private void Form1_Load(object sender, EventArgs e)
  {
   NChart chart = nChartControl1.Charts[0];

   NBarSeries bar = new NBarSeries();

   bar.Values.Add(10);
   bar.Values.Add(20);
   bar.Values.Add(30);

   chart.Series.Add(bar);

   NLinearScaleConfigurator scaleY = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator;

   scaleY.MajorTickMode = MajorTickMode.CustomStep;
   scaleY.CustomStep = 5;

   nChartControl1.MouseDown += new MouseEventHandler(nChartControl1_MouseDown);
  }

  void nChartControl1_MouseDown(object sender, MouseEventArgs e)
  {
   NHitTestResult result = nChartControl1.HitTest(e.X, e.Y);

   if (result.Axis != null && result.Axis.AxisId == (int)StandardAxis.PrimaryY)
   {
    NViewToScale1DTransformation viewToScale = new NViewToScale1DTransformation(nChartControl1.View.Context, nChartControl1.Charts[0], (int)StandardAxis.PrimaryY, (int)StandardAxis.PrimaryX);

    double scaleValue = 0;
    if (viewToScale.Transform(new NPointF(e.X, e.Y), ref scaleValue))
    {
     double tickValue = (int)Math.Round(scaleValue / 5) * 5;

     MessageBox.Show("Closest tick value[" + tickValue.ToString() + "]");
    }
   }
  }

Hope this helps - let us know if you meet any problems or have any questions.



Best Regards,
Nevron Support Team



Alexandr S.
Posted 12 Years Ago
View Quick Profile
Junior Member

Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)

Group: Forum Members
Last Active: 8 Years Ago
Posts: 23, Visits: 6
this pseudo code looks perfect) but how to implement this? I mean FindLabelClosestToPoint(point). I know the NAxis object - how I detect what scale label was clicked?

Nevron Support
Posted 12 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009

Hi Alexandr,

The axis does not distinguish between custom and automatic labels for hit testing purposes. You can implement a workaround that basically returns the label whose value is closest to the transformed viewport to scale coordinate on that axis. In pseudo code this will look like:

currentChartElemnt = chart.HitTest(point);

if (currentChartElement is axis)
{
point = ConvetPointFromViewToScale(point)

FindLabelClosestToPoint(point);
}

Let us know if you meet any problems or have any questions.



Best Regards,
Nevron Support Team



Alexandr S.
Posted 12 Years Ago
View Quick Profile
Junior Member

Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)Junior Member (22 reputation)

Group: Forum Members
Last Active: 8 Years Ago
Posts: 23, Visits: 6
I need to detect clicks for axis labels. For example I have X axis with labels "1,2,3,4... etc". I want to know when user click on "1" or "2" label and so on. I tried HitTest function, it correct returns that Axis object is clicked, but there is no info which label was clicked. Could you help here?



Similar Topics


Reading This Topic