Pre loader

Tag: Ray

0 votes
12k views

I have modified the single ended ray as found in another answer to be an infinite ray that extends both ends of the line until the touch the sides of the plot.

I would like to implement a label similar to in VerticalLineAnnotation but am unsure how I go about adding additional elements to the annotation’s template.

I have included my modified version of the rayannotation below.

/// <summary>
/// Defines a read-only or editable Line annotation, which draws a Ray to the edges of the chart viewport, depending on two X,Y coordinates
/// e.g. if X Y coordinates are placed inside the chart, then the line extends until it hits the edge or the chart viewport
/// </summary>
public class RayAnnotation : LineAnnotation
{
    private Line _line;
    private Line _ghostLine;

    /// <summary>
    /// Initializes a new instance of the <see cref="RayAnnotation" /> class.
    /// </summary>
    public RayAnnotation()
    {
        // Reuse LineAnnotation control template from SciChart
        DefaultStyleKey = typeof(LineAnnotation);

    }

    /// <summary>
    /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />.
    /// </summary>
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        AnnotationRoot = GetAndAssertTemplateChild<Grid>("PART_LineAnnotationRoot");
        _line = GetAndAssertTemplateChild<Line>("PART_Line");
        _ghostLine = GetAndAssertTemplateChild<Line>("PART_GhostLine");
    }

    /// <summary>
    /// Override in derived classes to handle specific placement of the annotation at the given <see cref="AnnotationCoordinates" />
    /// </summary>
    /// <param name="coordinates">The normalised <see cref="AnnotationCoordinates" /></param>
    protected override void PlaceAnnotation(AnnotationCoordinates coordinates)
    {

        // Calculate gradient of line
        double m = (coordinates.Y1Coord - coordinates.Y2Coord) / (coordinates.X1Coord - coordinates.X2Coord);
        double b = coordinates.Y1Coord - m * coordinates.X1Coord;

        if (double.IsNaN(m) || double.IsInfinity(m))
        {
            // Possible divide by zero above, just draw a single point for now
            _line.X1 = coordinates.X1Coord;
            _line.Y1 = 0;
            _line.X2 = coordinates.X1Coord;
            _line.Y2 = base.ParentSurface.ModifierSurface.ActualHeight;
            _ghostLine.X1 = coordinates.X1Coord;
            _ghostLine.Y1 = 0;
            _ghostLine.X2 = coordinates.X1Coord;
            _ghostLine.Y2 = base.ParentSurface.ModifierSurface.ActualHeight;
            return;
        }

        // Calulate projected X,Y point that touches the right/top edge, or left/bottom edge of the viewport
        double projectedX1 = 0;
        double projectedX2 = base.ParentSurface.ModifierSurface.ActualWidth;
        double projectedY1 = m * projectedX1 + b;
        double projectedY2 = m * projectedX2 + b;

        if (projectedY1 < 0 || projectedY1 > base.ParentSurface.ModifierSurface.ActualHeight)
        {
            bool isDown = projectedY1 < 0;
            projectedY1 = isDown ? 0 : base.ParentSurface.ModifierSurface.ActualHeight;
            projectedX1 = (projectedY1-b) / m;
        }

        if (projectedY2 < 0 || projectedY2 > base.ParentSurface.ModifierSurface.ActualHeight)
        {
            bool isDown = projectedY2 < 0;
            projectedY2 = isDown ? 0 : base.ParentSurface.ModifierSurface.ActualHeight;
            projectedX2 = (projectedY2-b) / m;
        }

        //Console.WriteLine("Projected x{0:0.00},y{1:0.00},x{2:0.00},y{3:0.00}", projectedX1, projectedY1, projectedX2, projectedY2);

        // Apply Calculated Line
        _line.X1 = projectedX1;
        _line.Y1 = projectedY1;
        _line.X2 = projectedX2;
        _line.Y2 = projectedY2;
        _ghostLine.X1 = projectedX1;
        _ghostLine.Y1 = projectedY1;
        _ghostLine.X2 = projectedX2;
        _ghostLine.Y2 = projectedY2;
    }
}
  • Hugoagogo asked 6 years ago
  • last active 6 years ago
1 vote
3k views

Hello,

Is there away to extend a LineAnnotationViewModel to the right? I see there is a ExtendedLineAnnotaion
But that only seems to be available in the DrawingTools API.

Thank you

  • Leland asked 8 months ago
  • last active 8 months ago
Showing 2 results

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies