Search code examples
c#winformszedgraph

Positioning an ImageObj in ZedGraph


I'm using ZedGraph to plot some data. I need to show marked areas of the line graph. To do that, I use a red exclamation point.

I'd like the exclamation point to appear above the top border of the chart, as in the picture below. The exclamation point should scroll with the horizontal scrolling of the chart, but should remain fixed above the top border of the chart even when scrolling vertically.

So far, I've tried this (appears in the form_shown event):

ZedGraph.ImageObj io = new ZedGraph.ImageObj(global::ZedGraphJunk.Properties.Resources.alarm, 0d, -0.08d, 5d, 21d);
io.IsVisible = true;
io.IsScaled = false;
io.Location.CoordinateFrame = ZedGraph.CoordType.XScaleYChartFraction;

this.MasterZedGraph.GraphPane.GraphObjList.Add(io);
this.MasterZedGraph.Refresh();

This almost works, except that if the window resizes and grows taller, the exclamation point disappears. This is because the -0.08d tells ZedGraph to draw it above the top border of the chart, but the position is relative to the size of the entire pane (the entire area that contains the chart). Thus, if the pane gets bigger, that y position of the ImageObj sets it further away from the top border of the chart.

Is there a way to fix the position of the exclamation point so that no matter the pane size, it stays just above the top border of the chart?

enter image description here


Solution

  • Looks like extending the ImageObj class and overriding the Draw method should solve the problem.

    I'll mark this as the answer in a few days, but I am open to other solutions.