使用ZedGraph绘制多维数组 [英] Graphing multidimensional array using ZedGraph
问题描述
如标题所示,我需要一些帮助,以使用ZedGraph绘制多维数组中的某些数据的图形.请参见以下代码:
Hi,
As the title suggests, I need some help graphing some data from a multidimensional array using ZedGraph. Please see the following code:
//Here are some variables declared earlier
int counter = 0
double [] y;
double [] x;
y = new double[FinalArray.GetLength(1) * LoopingValue];
x = new double[FinalArray.GetLength(1) * LoopingValue];
ExperimentArray[ LoopingValue, FinalArray.GetLength(1);
private void Plot_Click(object sender, EventArgs e)
{
for (int j = 0; j < LoopingValue; j ++)
{
/*
Excluding a lot of code in this section. Basically a new version of FinalArray is calculated using the value
of the variable ''LoopingValue'', which is why y and x have a length as defined above.
*/
for (int i = 0; i < FinalArray.GetLength(1); i++)
{
y[counter] = FinalArray[0, i];
x[counter] = j + 1;
counter = counter + 1;
}
}
counter = 0;
CreateGraph(zg1);
}
private void CreateGraph(ZedGraphControl zgc)
{
zg1.GraphPane.CurveList.Clear();
GraphPane mypane = zg1.GraphPane;
mypane.Title.Text = "Model Simulation";
mypane.XAxis.Title.Text = "Mass";
mypane.YAxis.Title.Text = "Force (Newtons)";
mypane.YAxis.MajorGrid.IsVisible = true;
BarItem mybar;
for (int i = 0; i < FinalArray.GetLength(1); i++)
{
mybar = mypane.AddBar(i.ToString(), x, y, Color.Blue);
}
mypane.BarSettings.Type = BarType.Stack;
mypane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
mypane.Fill = new Fill(Color.White, Color.White, 45F);
zgc.AxisChange();
zg1.Refresh();
}
}
现在,尽管此代码确实绘制了数据图,但它并不完全是我想要的.标题为X的数组具有以下值:
Now while this code does graph the data, it is not exactly what I am after. The array titled X has values such as the following:
X = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5]
并且数组Y具有(对应)值:
and the array Y has (corresponding) values:
Y = [10,12,15,24,28,26,39,40,56,89,78,90,113,119,124]
所以我需要的是一个堆积的条形图,它在X的点上绘制了Y的3个不同的值.现在,我可以设置3个不同的数组,并按以下方式绘制条以实现我想要实现的目标: br/>
so what I am after is a stacked bar chart with the 3 different values of Y, plotted at the points of X. Now I could set up 3 different arrays and blot the bars in the following manner to achieve what I want to achieve:
//Set up the arrays so that:
y1 = [10 , 24 , 39 , 89 , 113 ]
y2 = [ 12 , 28 , 40 , 78 , 119 ]
y3 = [ 15, 26 , 56 , 90 , 124]
//and then plot the bars:
mybar = mypane.AddBar("1", null, y1, Color.Red);
mybar = mypane.AddBar("2", null, y2, Color.Blue);
mybar = mypane.AddBar("3", null, y3, Color.Green);
但我想避免进行硬编码,因为元素的数量可能会根据应用程序用户如何定义它们而更早地更改.即我可以绘制50点不同的数组(y4,y5,y ......,y50).那么,有什么方法可以在不进行硬编码的情况下绘制出我想要的东西?问题在于创建数组y4,y5,y6,... y100 ........ ect,并能够在单个.AddBar行中调用它们.如果有办法,我是否也可以对.AddBar行中的最后一个参数执行某些操作,以使要绘制的每个堆叠条的颜色都不同?
感谢任何人可以给我的帮助.
but I would like to avoid hardcoding because the number of elements can potentially change depending on how the user of the application defines them eariler. i.e. I could have 50 points different arrays (y4, y5, y......, y50) to plot. So is there any way I could plot what it is I want without hardcoding? The problem lies in creating the arrays y4,y5,y6,...y100........ect, and being able to call them in a single .AddBar line. If there is a way to do this, can I also do something with the last argument in the .AddBar line so that the color is different for each stacked bar I want to plot?
Thanks for any help anyone can give me.
推荐答案
我不知道您是否可以通过一次调用AddBar
来完成此任务. ,但您可以为x
和y
保留一个List< double[] >
,然后根据需要对其进行迭代.
您还可以创建Color
的List
,以在调用AddBar
时进行迭代. KnownColor枚举为您提供了已定义颜色名称的列表,因此您可以执行以下操作:
I don''t know if you''ll be able to do it with a single call toAddBar
, but you could keep aList< double[] >
for thex
s andy
s and then iterate over them as needed.
You can also create aList
ofColor
s to iterate over when callingAddBar
. The KnownColor enum gives you a list of defined color names, so you can do something like this:
foreach (string colorName in Enum.GetNames(typeof(KnownColor)))
{
}
以获取所有已知的颜色名称,或者您可以选择最喜欢的颜色名称并创建包含这些名称的列表.每次调用AddBar
时,只需将List
中的下一个元素用作第三个参数,即可获得漂亮的彩色图形.
to get all the known color names or you could just pick the ones you like most and create a List with those in it. Each time you call AddBar
just use the next element in the List
as the third param to get a nice multi-colored graph.
这篇关于使用ZedGraph绘制多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!