【问题标题】:How to bring specific curves in-front in ZedGraph如何在 ZedGraph 中将特定曲线置于前面
【发布时间】:2023-04-10 22:57:01
【问题描述】:

绘制两条曲线后,我在 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()......)......?

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

【问题讨论】:

  • 非常有问题,假设 CurveItem 可以用于 几个 GraphPanes...

标签: c# plot zedgraph curves


【解决方案1】:

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

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

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

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

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

现在,当单击 排序 按钮时,将对曲线进行排序,从而将具有最低标记值的曲线(即 line1)绘制在顶部。另外,请注意图例中的曲线顺序会随之改变。

【讨论】:

  • 哇,你让我很开心......我为设置曲线的标签属性付出了很多努力......你是*并且感谢一百万的明确解释...... .:)
  • 这给了我在 _graph.GraphPane.CurveList.Sort() 行上的编译错误 - “方法 'Sort' 没有重载需要 1 个参数” - 参数似乎是 Sort(SortType type, int index)
【解决方案2】:

而且,对于需要它的人,这是 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

乔瓦尼

【讨论】:

    【解决方案3】:

    有一个非常简单的方法。在CurveList 类中使用Move() 方法。例如在:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 2010-09-14
      • 2015-06-28
      相关资源
      最近更新 更多