Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring specific curves in-front in ZedGraph

I have two curves on the zedgraph control, after plotting both the curves...

PointPairList thresholdList = new PointPairList();
PointPairList powerList = new PointPairList();

private void plotPower()
{
        // Create an object to access ZedGraph Pane
        GraphPane pane = zedGraphControl1.GraphPane;            
        LineItem thresholdLine = new LineItem("thresholdLine");
        LineItem powerLine = new LineItem("powerLine");

        // Set the Threshold Limit
        double thresoldLimit = Convert.ToDouble(numAnalysisThreshold2.Value);

        // Points
        double[] x = new double[]{0, pane.XAxis.Scale.Max};
        double[] y = new double[]{thresoldLimit, thresoldLimit};

        // Set the threshold line curve list
        thresholdList.Add(x, y); 

        // Set the Power Line curve list
        powerdList.Add(XData, YData);

        // Add Curves
        thresholdLine = pane.AddCurve("", thresholdList, Color.Red, SymbolType.None);
        powerLine = pane.AddCurve("", powerList, Color.Red, SymbolType.None);

        // Refresh Chart
        this.Invalidate();
        zedGraphControl1.Refresh();
}

from the above code, I managed to plot the two curves as power line curve over the threshold line curve.

Now my questions is, if I want to bring any one of the curve in front....Is there any method available(ex: bringittoFront()....)...?

Thanks a lot for your time ....:)

like image 233
SanVEE Avatar asked Jun 19 '12 14:06

SanVEE


3 Answers

The GraphPane contains a CurveList property, and the CurveList class is a subclass of List<CurveItem>. If you set the CurveItem.Tag property for each curve that you draw, I believe you should be able to sort the curve items by using the CurveList.Sort(IComparer<CurveItem>) method and using the Tag to represent the sorting order.

UPDATE JUNE 19

Simple example: two lines, the blue line2 with line2.Tag = 2 and the red line1 with line1.Tag = 1. In the initialization line2 is added first to the graph pane, so it will be displayed on top.

void GraphInit()
{
    var line2 = _graph.GraphPane.AddCurve("Second", 
        new[] { 0.1, 0.5, 0.9 }, new[] { 0.1, 0.5, 0.1 }, Color.Blue);
    line2.Tag = 2;

    var line1 = _graph.GraphPane.AddCurve("First", 
        new[] { 0.1, 0.5, 0.9 }, new[] { 0.1, 0.5, 0.9 }, Color.Red);
    line1.Tag = 1;

    _graph.Refresh();
}

Initial display before sorting

To sort, first implement a class that implements IComparer<CurveItem>, and that sorts the curve items in ascending order based on the numerical value of the CurveItem Tag property:

class CurveItemTagComparer : IComparer<CurveItem>
{
    public int Compare(CurveItem x, CurveItem y)
    {
        return ((int)x.Tag).CompareTo((int)y.Tag);
    }
}

To perform re-sorting and update the graph, implement the following event handler for the Sort button:

void SortButtonClick(object sender, EventArgs e)
{
    _graph.GraphPane.CurveList.Sort(new CurveItemTagComparer());
    _graph.Refresh();
}

Now, when clicking the Sort button, the curves will be sorted such that the curve with the lowest tag value, i.e. line1, will instead be drawn on top. Additionally, note that the curve order in the legend is changed along.

Graph after Sort button is clicked

like image 108
Anders Gustafsson Avatar answered Nov 02 '22 23:11

Anders Gustafsson


There is a very easy way. Use the Move() method in the CurveList class. For example in:

zedGraphControl1.GraphPane.CurveList.Move(index,relativePos)

setting relativePos to -1 will move the object one position earlier in the list, while 1 will move it one position later. To move an item to the beginning of the list, use a large negative value (such as -999). To move it to the end of the list, use a large positive value.

like image 22
Andreas Avatar answered Nov 03 '22 01:11

Andreas


And, for who needs it, this is the code for the IComparer class for vb.net:

    Public Class CurveItemTagComparer
    Implements IComparer(Of CurveItem)
    Function Compare(ByVal x As ZedGraph.CurveItem, ByVal y As ZedGraph.CurveItem) As Integer _
    Implements System.Collections.Generic.IComparer(Of CurveItem).Compare
        Return CInt(x.Tag).CompareTo(CInt(y.Tag))
    End Function
End Class

Giovanni

like image 4
Giovanni Avatar answered Nov 03 '22 01:11

Giovanni