使用 Zedgraph 的 foreach 循环中的多条曲线
multiple curves in foreach loop using Zedgraph
我想用多条曲线列表框中的多张图填充 Zedgraph 窗格。我的方法目前填充这些曲线,但它们都具有相同的颜色和符号类型。关于为每条添加的曲线分配不同颜色的任何建议?
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);
}
}
您可以设置您 select 的颜色列表并在 foreach 循环中使用它,或者您可以使用 zedgraph 中提供的自动颜色旋转器,如下所示。请注意,颜色范围是有限制的,它会循环返回。
// 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();
}
如果您只想重复一定数量的颜色更改并重新使用这些颜色更改,您可以随时重置颜色旋转器。我认为默认情况下第一种颜色是黑色(index = 0)。
colorRotator.NextColorIndex = 0;
我想用多条曲线列表框中的多张图填充 Zedgraph 窗格。我的方法目前填充这些曲线,但它们都具有相同的颜色和符号类型。关于为每条添加的曲线分配不同颜色的任何建议?
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);
}
}
您可以设置您 select 的颜色列表并在 foreach 循环中使用它,或者您可以使用 zedgraph 中提供的自动颜色旋转器,如下所示。请注意,颜色范围是有限制的,它会循环返回。
// 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();
}
如果您只想重复一定数量的颜色更改并重新使用这些颜色更改,您可以随时重置颜色旋转器。我认为默认情况下第一种颜色是黑色(index = 0)。
colorRotator.NextColorIndex = 0;