Click here to Skip to main content
14,735,876 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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;

private void CreateGraph(ZedGraphControl zgc)


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


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]

and the array Y has (corresponding) values:

Y = [10,12,15,24,28,26,39,40,56,89,78,90,113,119,124]

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

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.

1 solution

I don't know if you'll be able to do it with a single call to AddBar, but you could keep a List< double[] > for the xs and ys and then iterate over them as needed.

You can also create a List of Colors to iterate over when calling AddBar. 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)))

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900