Search code examples
c#drawzedgraph

Redraw only a part of the pane


I'm using ZedGraph to display curves (with 10k+ points) on my application.

I've added vertical bars to allow the user to see the value of a point (kind of cross-hair). Every time the user moves the mouse, the whole control is redrawn:

bool stackedGraphControl1_MouseMoveEvent(ZedGraphControl sender, MouseEventArgs e)
{
    DrawCursors(e.Location);
    Refresh();
    return false;
}

With more than 2 curves, the computer becomes laggy. One solution is to draw only the vertical bars (remove the Refresh() call and draw only the vertical bars). This is way faster but the graphical result is funny:

enter image description here

But not very useful. I understand that all the vertical bars are drawn again and again without being removed.

Is there a way to redraw (ie: delete and draw) only the bars (which are in the GraphObjList of my panes) ?


Solution

  • In the past I have accomplished this functionality by overriding the OnPaint of the ZedGraphControl to create a layered drawing effect.

    Paint ZedGraph to an intermediate Bitmap or BufferedGraphics. Saving this Bitmap for future painting, and invalidating the bitmap when other things change (axis values, curves added)

    Future OnPaint calls blit this Bitmap to the graphics object and manually drawing any GraphObjs on top of this.

    MouseMove events just update the properties of the indicator and force a redraw with the cached zedgraph image.