如何在ZedGraph中将特定曲线置于最前面

6

我在zedgraph控件上有两条曲线,绘制完这两条曲线之后...

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();
}

从上述代码中,我成功地将两条曲线绘制成阈值线曲线上的功率线曲线。

现在我的问题是,如果我想将其中一条曲线置于最前面...是否有可用的方法(例如:bringittoFront()....)...?

非常感谢您的时间....:)


1
非常棘手的问题,假设CurveItem可以在多个GraphPanes上使用... - Larry
3个回答

9

GraphPane 包含一个 CurveList 属性,而 CurveList 类是 List<CurveItem> 的子类。如果您为每个要绘制的曲线设置 CurveItem.Tag 属性,则可以使用 CurveList.Sort(IComparer<CurveItem>) 方法并使用 Tag 表示排序顺序来对曲线进行排序。

2021年6月19日更新

简单例子:两条线,蓝色的 line2 具有 line2.Tag = 2,红色的 line1 具有 line1.Tag = 1。在初始化中先添加 line2 到图形面板中,所以它将显示在顶部。

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();
}

排序前的初始显示

要进行排序,首先需要实现一个实现IComparer<CurveItem>接口的类,并根据CurveItemTag属性的数值大小以升序对曲线项进行排序:

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

为了执行重新排序并更新图表,实现以下事件处理程序来处理排序按钮:

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

现在,当点击“排序”按钮时,曲线将按照标签值最低的曲线(即line1)放在最上面绘制。此外,请注意,图例中的曲线顺序也会改变。

3
哇,你让我的一天都变得美好了。我花了很多力气来设置曲线的标记属性。感谢您清晰明了的解释,千万谢谢! :) - SanVEE
这在_graph.GraphPane.CurveList.Sort()行上给我编译错误 - "No overload for method 'Sort' takes 1 arguments" - 参数似乎是Sort(SortType type, int index) - komodosp

7
有一种非常简单的方法。使用 CurveList 类中的 Move() 方法。例如在下面这个例子中: zedGraphControl1.GraphPane.CurveList.Move(index,relativePos)relativePos 设为 -1 将会把对象在列表中移动到前一个位置,而设为 1 则会把它后移一位。要将一个项目移到列表的开头,请使用较大的负值(例如 -999)。要将它移到列表的末尾,请使用较大的正值。

4

如果需要的话,以下是 vb.net 的 IComparer 类的代码:

    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

乔万尼


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接