Question

I'm trying to make a custom control to draw a curve with WPF.

I made a control which inherit from Control:

public class Courbe : Control {

    private static readonly CultureInfo CI = new CultureInfo("en-US");
    private Pen _p = new Pen();

    public Courbe() {
        Points = new List<Point>();
    }
    static Courbe() {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Courbe), new FrameworkPropertyMetadata(typeof(Courbe)));
    }

    protected override void OnRender(DrawingContext drawingContext) {
        base.OnRender(drawingContext);
        EcritureEvenement.loggerEvenement("verif point...", TypesEvenements.DEBUG, "");
        if (Points.Count <= 1) return;
        EcritureEvenement.loggerEvenement("plus d'un point", TypesEvenements.DEBUG, "");
        OffsetX = (int)Math.Ceiling(Width / 11);
        OffsetY = (int)Math.Ceiling(Height / 11);

        double differenceX = MaxX - MinX;
        double differenceY = MaxY - MinY;

        double rapportTailleDifferenceX = (Width) / differenceX;
        double rapportTailleDifferenceY = (Height) / differenceY;

        double xMinLigne = rapportTailleDifferenceX * MinX + OffsetX;
        double xMaxLigne = rapportTailleDifferenceX * MaxX + OffsetX;
        double yMinLigne = (Height - (rapportTailleDifferenceY * (MinY - MinY)) - OffsetY);
        double yMaxLigne = (Height - (rapportTailleDifferenceY * (MaxY - MinY)) - OffsetY);
        Point debutAxeX = new Point(xMinLigne, yMinLigne);
        Point finAxeX = new Point(xMaxLigne, yMinLigne);
        Point debutAxeY = new Point(xMinLigne, yMinLigne);
        Point finAxeY = new Point(xMinLigne, yMaxLigne);
        drawingContext.DrawLine(_p, debutAxeX, finAxeX);
        drawingContext.DrawLine(_p, debutAxeY, finAxeY);

        int taillePolice = (int)Math.Floor(Width / 80);

        double dizieme = differenceY / 10;
        for (int i = 0; i < 10; i++) {
            float pointGraduation = (float)(Height - ((dizieme * i) * rapportTailleDifferenceY) - OffsetY);

            FormattedText texte = new FormattedText(dizieme * i + MinY + "", CI, FlowDirection.LeftToRight, new Typeface("Verdana"), taillePolice, Brushes.Black);
            drawingContext.DrawText(texte, new Point(0, pointGraduation - taillePolice));
            Point p1 = new Point(xMinLigne - 5, pointGraduation);
            Point p2 = new Point(xMinLigne + 5, pointGraduation);
            drawingContext.DrawLine(_p, p1, p2);
        }

        PathFigure pf = new PathFigure();

        foreach (Point p in Points) {
            double x1 = (p.X * rapportTailleDifferenceX) + OffsetX;
            double y1 = Height - ((p.Y - MinY) * rapportTailleDifferenceY) - OffsetY;
            Point scaledPoint = new Point((float)x1, (float)y1);
            PathSegment ps = new LineSegment(scaledPoint, false);
            pf.Segments.Add(ps);
        }
        Geometry g = new PathGeometry(new[]{pf});
        drawingContext.DrawGeometry(Brushes.Aqua, new Pen(Brushes.Blue, 5), g);
    }

    #region Properties
    public List<Point> Points { get; set; }
    public int MinX { get; set; }
    public int MaxX { get; set; }
    public int MinY { get; set; }
    public int MaxY { get; set; }
    public int OffsetX { get; set; }
    public int OffsetY { get; set; }

    private Color _penColor;

    public Color PenColor {
        get { return _penColor; }
        set {
            _p = new Pen(new SolidColorBrush(value), 5);
            _penColor = value;
        }
    }

    #endregion
}

My problem is that this line seems not to draw with the pen, only with the brush (if I put null instead of Brushes.Aqua nothing appears).

Thanks you

Edit: If I use DrawLine instead of DrawGeometry it's working:

Point ancienPoint = new Point(-1, -1);
foreach (Point p in Points) {

    double x1 = (p.X * rapportTailleDifferenceX) + OffsetX;
    double y1 = Height - ((p.Y - MinY) * rapportTailleDifferenceY) - OffsetY;
    Point scaledPoint = new Point((float)x1, (float)y1);
    if (ancienPoint.X != -1 && ancienPoint.Y != -1) {
        drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.Blue), 1), ancienPoint, scaledPoint);
    }
    ancienPoint = scaledPoint;
}

But about performances it's about 2 times slower than Graphics.DrawCurve in windows forms. Any way to make it faster?

Edit2: I didn't try to use a native control because when I tried that in windows forms (chart), the control couldn't handle the process (~5000 lines to draw every ~50 ms).

Was it helpful?

Solution

The problem is that you set isStroked to false when you call the LineSegment constructor.

Change

PathSegment ps = new LineSegment(scaledPoint, false);

to

PathSegment ps = new LineSegment(scaledPoint, true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top