Kamil Solecki
Kamil Solecki

Reputation: 1117

WPF Canvas as a math graphing area

In my graphic calculator, I am using canvas as a plane on which graphs of functions are rendered. I have noticed that whenever I input complex functions (I use bezier curves for that, so the more complex the function -> the more points I need to render for the graph to be smooth-looking) it gets quite laggy and overally doesn't perform well.

Which of these solutions are the least computationally costly / thus are the best approach?

1. (My current solution) Using huge Canvas inside a ScrollViewer that limits the view area

Cons I have found:

2. Using just Canvas that is not so big, and management of panning / zooming through TransformMatrix

Cons I think there might be (solution not tried):

3. Some other solutions?

Any help will be greatly appreciated.

Upvotes: 1

Views: 429

Answers (1)

Mike Strobel
Mike Strobel

Reputation: 25623

If you can avoid it, you really don't want to be using discrete visuals to represent things like charts/graphs; they are very heavy, and are constantly participating in input handling and layout. I would recommend using WPF's drawing/geometry APIs directly. I would suggest implementing a custom control which performs its own rendering (see OnRender). I would also suggest implementing IScrollInfo so you can manage your own scrolling: you will always know the viewport size and position, which gives you the option of invalidating your geometry when scrolling, and then rebuilding only the parts of the graph that are actually in view. That should reduce the overhead significantly.

Upvotes: 1

Related Questions