Draw a Graph in C# using zedGraph

menachem picture menachem · Jan 12, 2010 · Viewed 16.9k times · Source

I need to create a graph that have the following properties:
The X Axis is for schools names.
The Y Axis is for classes names.
In Point (x,y) I need to put a dot that it's color will represent the number of students (darker means more students).
I'm using ZedGraph (using that sample: http://zedgraph.org/wiki/index.php?title=Gradient-By-Value_Demo), but I don't know how to put the dot (and to determine it's dark-level) in the correct position (compare it to school's name and class's name).
Also, I don't know how to make the X and Y axis to show the school's name and the class's name.
How can I do that? (It's NOT have to be in zedGraph).
many thanks!

Answer

Gacek picture Gacek · Jan 14, 2010

The problem is that ZedGraph is treating a Text-type scale in a little bit strange way. So it's almost impossible to display correctly data when you have both scales of Text type.

But you can fool ZG a little bit.

The whole trick is to display the data using coordinates of hidden scale, while displaying second, fake scale.

string[] schools = { "A", "B", "C" };
string[] classes = { "cl. 1", "cl. 2", "cl. 3" };

var pane = zg1.GraphPane;
Random x = new Random();

// Hide the basic scale, show the second with text labels
pane.X2Axis.Type = AxisType.Text;
pane.X2Axis.IsVisible = true;
pane.Y2Axis.Type = AxisType.Text;
pane.Y2Axis.IsVisible = true;
pane.XAxis.Scale.IsVisible = false;
pane.YAxis.Scale.IsVisible = false;

pane.X2Axis.Scale.TextLabels = schools;
pane.Y2Axis.Scale.TextLabels = classes;

// Main problem - synchronize the scales correctly            
pane.XAxis.Scale.Min = -0.5;
pane.XAxis.Scale.Max = schools.Count() - 0.5;
pane.YAxis.Scale.Min = -0.5;
pane.YAxis.Scale.Max = classes.Count() - 0.5;

pane.YAxis.MajorGrid.IsZeroLine = false;

// generate some fake data
PointPairList list = new PointPairList();
   for(int i=0;i<schools.Count();i++)
      for (int j = 0; j < classes.Count(); j++)
      {
          list.Add(new PointPair(i, j, x.Next(30)));
      }

   var pointsCurve = pane.AddCurve("", list, Color.Transparent);
   pointsCurve.Line.IsVisible = false;
   // Create your own scale of colors.
   pointsCurve.Symbol.Fill = new Fill(new Color[] { Color.Blue, Color.Green, Color.Red });
   pointsCurve.Symbol.Fill.Type = FillType.GradientByZ;
   pointsCurve.Symbol.Fill.RangeMin = 0;
   pointsCurve.Symbol.Fill.RangeMax = 30;
   pointsCurve.Symbol.Type = SymbolType.Circle;

            pane.AxisChange();
            zg1.Refresh();