如何在ZedGraph中引入特定曲线

San*_*VEE 5 c# plot zedgraph curves

在绘制两条曲线后,我在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();
}
Run Code Online (Sandbox Code Playgroud)

从上面的代码中,我设法将两条曲线绘制为超过阈值线曲线的电源线曲线.

现在我的问题是,如果我想把前面任何一条曲线带到前面......有没有任何方法可用(例如:bringittoFront()....)......?

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

And*_*son 8

GraphPane包含CurveList属性和CurveList类的子类List<CurveItem>.如果为绘制的每条曲线设置CurveItem.Tag属性,我相信您应该能够使用该CurveList.Sort(IComparer<CurveItem>)方法对曲线项进行排序,并使用它Tag来表示排序顺序.

更新6月19日

简单的例子:两行,蓝色line2line2.Tag = 2,红色line1line1.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();
}
Run Code Online (Sandbox Code Playgroud)

排序前的初始显示

要进行排序,首先要实现一个实现的类IComparer<CurveItem>,然后根据CurveItem Tag属性的数值按升序对曲线项进行排序:

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

要执行重新排序和更新图形,请为" 排序"按钮实现以下事件处理程序:

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

现在,当单击" 排序"按钮时,将对曲线进行排序,使得具有最低标记值的曲线(即line1,将在顶部绘制).此外,请注意图例中的曲线顺序会一直更改.

单击

  • 哇,你刚刚结束了我的一天....我为了设置曲线的标签属性而苦苦挣扎....你是*并感谢百万的明确解释...... :) (3认同)

小智 6

有一种非常简单的方法.使用该类中的Move()方法CurveList.例如:

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

设置relativePos-1将对象在列表中较早的位置移动一个位置,而1稍后将其移动一个位置.要将项目移动到列表的开头,请使用较大的负值(例如-999).要将其移动到列表的末尾,请使用较大的正值.