NHitTestResult DataPointIndex always 0


https://www.nevron.com/Forum/Topic9430.aspx
Print Topic | Close Window

By Daniel Csimszi - 9 Years Ago
Hello,

I would like to get the X value of a point at user mouse click. I thought it will be easy as in theory I should be just using the NHitTestResult at the mouse click event. Unfortunately my DataPointIndex is always 0, the parent node perfectly found but nothing else.

I am not sure if it is to do with my chart setup of with something else so I thought the best would be to post my code:
I have a class which needs an NChartControl to set up the relevant chart:

public HistoryGraphControl(Nevron.Chart.WinForm.NChartControl nChartControl1)
   {
       NDataZoomTool m_DataZoomTool = new NDataZoomTool();
       this.nChartControl1 = nChartControl1;
       nChartControl1.MouseDown += new MouseEventHandler(nChartControl1_MouseClick);
       
   #region chart set up

    // configure the chart
    m_Chart = (NCartesianChart)nChartControl1.Charts[0];

    NStandardFrameStyle frameStyle = (NStandardFrameStyle)nChartControl1.BackgroundStyle.FrameStyle;
    frameStyle.Visible = false;
    m_Chart.Location = new NPointL(new NLength(0, NRelativeUnit.ParentPercentage), new NLength(10, NRelativeUnit.ParentPercentage));
    m_Chart.Size = new NSizeL(new NLength(99, NRelativeUnit.ParentPercentage), new NLength(84, NRelativeUnit.ParentPercentage));


    NCartesianChart chart = (NCartesianChart)nChartControl1.Charts[0];
    chart.Projection.SetPredefinedProjection(PredefinedProjection.Orthogonal);
    chart.BoundsMode = BoundsMode.Stretch;

    NRangeSelection rangeSelection = new NRangeSelection();
    rangeSelection.ZoomOutResetsAxis = true;
    chart.RangeSelections.Add(rangeSelection);
    chart.Axis(StandardAxis.PrimaryX).ScrollBar.Visible = true;
    chart.Axis(StandardAxis.PrimaryY).ScrollBar.Visible = true;

    rightAxes = m_Chart.Axis(StandardAxis.SecondaryY);
    rightAxes.Anchor = new NDockAxisAnchor(AxisDockZone.FrontRight, true);
    rightAxes.Visible = true;
    ((NStandardScaleConfigurator)rightAxes.ScaleConfigurator).LabelStyle.TextStyle = new NTextStyle(new Font("Verdana", 12), Color.Black);

    //set up axis X
    NAxis axiss = m_Chart.Axis(StandardAxis.PrimaryX);
    NDateTimeScaleConfigurator dateTimeScale = new NDateTimeScaleConfigurator();
    dateTimeScale.MajorGridStyle.LineStyle.Pattern = LinePattern.Dot;
    dateTimeScale.MajorGridStyle.SetShowAtWall(ChartWallType.Back, true);
    dateTimeScale.MajorGridStyle.SetShowAtWall(ChartWallType.Floor, false);
    dateTimeScale.MaxTickCount = 10;
    dateTimeScale.CustomStep = new NDateTimeSpan(1, NDateTimeUnit.Hour);
    dateTimeScale.MajorTickMode = MajorTickMode.AutoMaxCount;
    dateTimeScale.AutoDateTimeUnits = new NDateTimeUnit[] { NDateTimeUnit.Hour };
    dateTimeScale.RoundToTickMin = false;
    dateTimeScale.RoundToTickMax = false;
    dateTimeScale.EnableUnitSensitiveFormatting = false;
    dateTimeScale.LabelValueFormatter = new NDateTimeValueFormatter("dd-MMM-yy \r\n HH:mm");
    axiss.ScaleConfigurator = dateTimeScale;
    

    nChartControl1.Settings.AnimationInterval = 50;
    m_DataZoomTool.AnimateZooming = true;
    m_DataZoomTool.AnimationSteps = 10;

    m_Legend = new NLegend();
    m_Legend.Mode = LegendMode.Manual;
    m_Legend.Margins = new NMarginsL(10, 10, 10, 3);


    m_Chart.DisplayOnLegend = m_Legend;
    m_Legend.Visible = true;
    m_Legend.Dock = DockStyle.Bottom;
    
    m_Legend.FitAlignment = ContentAlignment.MiddleCenter;
    m_Legend.BoundsMode = BoundsMode.Fit;

    m_Legend.Data.ExpandMode = (LegendExpandMode)1;
    Array values = Enum.GetValues(typeof(ContentAlignment));
    m_Legend.FitAlignment = (ContentAlignment)values.GetValue(7);


    nChartControl1.Panels.Add(m_Legend);

    nChartControl1.Controller.Selection.Add(chart);
    nChartControl1.Controller.Tools.Add(new NAxisScrollTool());
    nChartControl1.Controller.Tools.Add(m_DataZoomTool);

    NStandardScaleConfigurator scaleConfiguratorX = (NStandardScaleConfigurator)m_Chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator;
    scaleConfiguratorX.Title.Text = "Date/Time";
    scaleConfiguratorX.LabelStyle.TextStyle = new NTextStyle(new Font("Verdana", 12), Color.Black);
    scaleConfiguratorX.Title.TextStyle.FontStyle = new NFontStyle("Verdana", 12, FontStyle.Regular);

    NStandardScaleConfigurator scaleConfiguratorY = (NStandardScaleConfigurator)m_Chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator;
    scaleConfiguratorY.Title.Text = "Scale 1";
    scaleConfiguratorY.LabelStyle.TextStyle = new NTextStyle(new Font("Verdana", 12), Color.Black);
    scaleConfiguratorY.Title.TextStyle.FontStyle = new NFontStyle("Verdana", 12, FontStyle.Regular);

    NStandardScaleConfigurator scaleConfiguratorY2 = (NStandardScaleConfigurator)m_Chart.Axis(StandardAxis.SecondaryY).ScaleConfigurator;
    scaleConfiguratorY2.Title.Text = "Scale 2";
    scaleConfiguratorY2.LabelStyle.TextStyle = new NTextStyle(new Font("Verdana", 12), Color.Black);
    scaleConfiguratorY2.Title.TextStyle.FontStyle = new NFontStyle("Verdana", 12, FontStyle.Regular);

    #endregion

  }

  void nChartControl1_MouseClick(object sender, MouseEventArgs e)
   {
    NHitTestResult hitTestResult = nChartControl1.HitTest(e.X, e.Y);
    Console.WriteLine("Data point [" + hitTestResult.DataPointIndex.ToString() + "] from series [" + ((NSeriesBase)hitTestResult.Object.ParentNode).Name + "]");

   }

  //add values to the graph
   public void addValues(List<Tuple<DateTime, double>> values, string lineName, Color graphColor, bool leftAxis, double hourOffset, string graphType)
   {
    colors.Add(graphColor);
    switch (graphType)
    {
      case "X":
       addXLine(lineName, leftAxis, graphColor);
       break;
      default:
       addNormalLine(lineName, leftAxis, graphColor);
       break;
    }

    

    for (int i = 0; i < values.Count; i++)
    {
      NLineSeries line = (NLineSeries)lineList[lineList.Count - 1];
      line.XValues.Add((values[i].Item1.AddHours(hourOffset)).ToOADate());
      line.Values.Add(values[i].Item2);
      line.BorderStyle.Color = graphColor;
    }

    nChartControl1.Refresh();
   }

public void addXLine(string name, bool leftAxis, Color color)
   {
    if (name == null || name.Length < 0)
      name = "Excel data";
    // add a line series
    NLineSeries m_Line = new NLineSeries();
    m_Line = (NLineSeries)m_Chart.Series.Add(SeriesType.Line);
    m_Line.Name = name;
    m_Line.InflateMargins = true;
    m_Line.DataLabelStyle.Visible = false;
    m_Line.MarkerStyle.Visible = true;
    m_Line.MarkerStyle.PointShape = PointShape.Cylinder;
    m_Line.MarkerStyle.Width = new NLength(1.5f, NRelativeUnit.ParentPercentage);
    m_Line.MarkerStyle.Height = new NLength(1.5f, NRelativeUnit.ParentPercentage);
    m_Line.SamplingMode = SeriesSamplingMode.Enabled;

    m_Line.DataLabelStyle.VertAlign = VertAlign.Center;
    m_Line.DataLabelStyle.Format = "<value>";
    m_Line.DataLabelStyle.TextStyle.FillStyle = new NColorFillStyle(Color.White);
    m_Line.DataLabelStyle.TextStyle.FontStyle.EmSize = new NLength(1.4f, NRelativeUnit.RootPercentage);
    m_Line.DataLabelStyle.TextStyle.BackplaneStyle.Visible = false;


    m_Line.Legend.Mode = SeriesLegendMode.Series;
    m_Line.UseXValues = true;
    m_Line.BorderStyle.Color = color;

    if (leftAxis)
    {
      m_Line.DisplayOnAxis(StandardAxis.PrimaryY, true);
      m_Line.DisplayOnAxis(StandardAxis.SecondaryY, false);
    }
    else
    {
      m_Line.DisplayOnAxis(StandardAxis.PrimaryY, false);
      m_Line.DisplayOnAxis(StandardAxis.SecondaryY, true);
    }

    addLegend(name, color);
    lineList.Add(m_Line);
    colors.Add(color);
   }


When I click on any of the data points I get the following output:
Data point [0] from series ["line name"]

It would be appreciated if you could let me know the reason why I do not get the right data point index.

Regards,
Daniel
By Nevron Support - 9 Years Ago
Hi Daniel,

The problem is that you enable line sampling:
ENPieLabelModem_Line.SamplingMode = SeriesSamplingMode.Enabled;
in which case the line series does not record per data point interactivity information for performance reasons.
By Daniel Csimszi - 9 Years Ago
Thank you, it did fix my problem.