Profile Picture

WPF Trend Line from Custom Tool Class?

Posted By 2 Months Ago
Author
Message
Posted 2 Months Ago
View Quick Profile
Forum Newbie

Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: Forum Members
Last Active: 2 days ago @ 10:51 AM
Posts: 0, Visits: 56
I am writing a WPF app and using the WPF Chart control. I have a custom tool class derived from NDragTool. In the MouseUp() handler, I'm trying to add a trend line to a chart control already populated with a data set. My code looks like this:

    var tradingChart = chartControl.Charts[0] as NCartesianChart;
    var newFreeTrendLine = new NTrendLine();
    newFreeTrendLine.BeginPoint = new NPointD(0.0f, 0.0f);
    newFreeTrendLine.EndPoint = new NPointD(100.0f, 100.0f);
    var trendLineStrokeStyle = new NStrokeStyle(10.0f, NColor.FromString("Blue"), LinePattern.Solid, 0, 1);
    newFreeTrendLine.StrokeStyle = trendLineStrokeStyle;
   if (tradingChart.Series.Count > 1)
    {
      tradingChart.Series.RemoveAt(0);
    }
    tradingChart.Series.Insert(0, newFreeTrendLine);
chartControl.Refresh();

I'm not seeing a trend line when I refresh. Can you tell me what I am doing wrong?

Posted 2 Months Ago
View Quick Profile
Forum Newbie

Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: Forum Members
Last Active: 2 days ago @ 10:51 AM
Posts: 0, Visits: 56
Please disregard this post. I have figured this out.

The important thing I learned with trend line points is that they are not in the coordinate system used by the mouse. Rather, they are in the coordinate system used by the chart's axes. If, for example, the Y axis of a chart goes from 0 to 100, and one of the endpoints of the trendline need to by halfway up the chart, then the Y value of the endpoint must be 50.

For those interested, I have written a method that takes in mouse event arguments and outputs a Nevron point whose values match the original mouse point but are translated to be the point relative to the chart axes. Here is the code:

  /// <summary>
   /// Trnasforms a location embedded in a mouse event to coordinates relative to the chart model.
   /// </summary>
   /// <param name="chartControl">
   /// The chart control that sent the supplied mouse event.
   /// </param>
   /// <param name="e">
   /// The mouse event containing a mouse location.
   /// </param>
   /// <returns>
   /// A point describing the mouse location referenced by the mouse event scaled to the chart's
   /// scale model.
   /// </returns>
   private NPointD TransformMousePositionToChartPoint(NChartControl chartControl, NMouseEventArgs e)
   {
    var chart = GetCartesianChart(chartControl);
    var ptViewPoint = new Point(e.Location.X, e.Location.Y);
    var fDepthValue = 50.0f;

    var primaryXAxis = chart.Axis(StandardAxis.PrimaryX);
    var primaryYAxis = chart.Axis(StandardAxis.PrimaryY);
    var depthAxis = chart.Axis(StandardAxis.Depth);

    NVector3DF vecModelPoint = new NVector3DF();

    if (chart.TransformClientToModel(primaryXAxis, primaryYAxis, depthAxis, fDepthValue, ptViewPoint, ref vecModelPoint))
    {
      var transformedPoint = new NPointD();
      transformedPoint.X = primaryXAxis.TransformModelToScale(vecModelPoint.X, true);
      transformedPoint.Y = primaryYAxis.TransformModelToScale(vecModelPoint.Y, true);
      return transformedPoint;
    }
    throw new NotSupportedException($"{nameof(TransformMousePositionToChartPoint)}(): Call to TransformClientToModel() failed.");
   }





Similar Topics


Reading This Topic