简体   繁体   English

使用Zedgraph在foreach循环中创建多条曲线

[英]multiple curves in foreach loop using Zedgraph

I'd like to populate a Zedgraph Pane with multiple graphs from a listbox with multiple curves. 我想用多条曲线的列表框中的多张图填充Zedgraph窗格。 My method currently populates these curves, but they all have the same color and symbol type. 我的方法当前填充这些曲线,但是它们都具有相同的颜色和符号类型。 Any suggestions on having each added curve be assigned a different color? 关于为每个添加的曲线分配不同颜色的任何建议?

public void btnMakePlt_Click(object sender, EventArgs e)
    {
        try
        {
            // Initialize graph window

            GraphPane myPane = zgcMain.GraphPane;

            // clear plt

            myPane.CurveList.Clear();

            zgcMain.Refresh();

            // Use this foreach to plot curves in the Selected Curves Listbox

            foreach (var item2 in lstSelectedCurves.Items)
            {

                // turn CurveName into String

                txtCurve = item2.ToString();

                //Invoke IPmathCurveMethods, populate List 

                PointPairList list = new PointPairList();

                double[] CurveDataVal = PlotIPdataStuffValues(txtCurve);
                double[] CurveDataDepth = PlotIPdataStuffDepths(txtCurve);


                for (int i = 0; i < CurveDataVal.Length; i++)
                {

                    double z1 = CurveDataDepth[i];
                    double z2 = CurveDataVal[i];

                    if (z2 <= 0)
                    {
                        z2 = double.NaN;
                    }

                    //add to list

                    list.Add(z2, z1);

                }

                LineItem myCurve = myPane.AddCurve(txtCurve, list, Color.Red, SymbolType.None);

                zgcMain.Refresh();

            }

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error making your plot \n" + ex.Message + "\n" + ex.StackTrace);
        }
    }

You can either set up a list of colors that you select and use it in your foreach loop or you can use the auto color rotator provided in zedgraph as shown below. 您可以设置选择的颜色列表并在foreach循环中使用它,也可以使用zedgraph中提供的自动颜色旋转器,如下所示。 Note that there's a limit to the range of colors and it will loop back. 请注意,颜色范围是有限制的,它将循环返回。

// Use this foreach to plot curves in the Selected Curves Listbox

var colorRotator = new ColorSymbolRotator();

var currentColor = Color.Black;

foreach (var item2 in lstSelectedCurves.Items)
{

    // turn CurveName into String

    txtCurve = item2.ToString();

    //Invoke IPmathCurveMethods, populate List 

    PointPairList list = new PointPairList();

    double[] CurveDataVal = PlotIPdataStuffValues(txtCurve);
    double[] CurveDataDepth = PlotIPdataStuffDepths(txtCurve);


    for (int i = 0; i < CurveDataVal.Length; i++)
    {

        double z1 = CurveDataDepth[i];
        double z2 = CurveDataVal[i];

        if (z2 <= 0)
        {
            z2 = double.NaN;
        }

        //add to list

        list.Add(z2, z1);

    }

    LineItem myCurve = myPane.AddCurve(txtCurve, list, currentColor, SymbolType.None);

    currentColor = colorRotator.NextColor;

    zgcMain.Refresh();

}

You can reset the color rotator at any point if, say you want to repeat only a set number of color changes and re-use those. 如果您只想重复设置一定数量的颜色更改并重新使用它们,则可以随时重置颜色旋转器。 I think that the first color is black (index = 0) by default. 我认为默认情况下,第一种颜色是黑色(索引= 0)。

colorRotator.NextColorIndex = 0; colorRotator.NextColorIndex = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM