Draw a graph in C # using zedGraph

I need to create a graph that has the following properties:
The X axis is for school names.
The Y axis is for class names.
In Point (x, y) I need to put a point at which the color will represent the number of students (darker means more students).
I am using ZedGraph (using this sample: http://zedgraph.org/wiki/index.php?title=Gradient-By-Value_Demo ), but I do not know how to put a point (and determine this dark level) in the correct position ( compare it with the school name and class name).
In addition, I do not know how to make the X and Y axes to display the school name and class name.
How can i do this? (This should NOT be in zedGraph).
many thanks!

+3
source share
2 answers

The problem is that ZedGraph spoils the text type scale a bit. Therefore, it is almost impossible to display the correct data when you have both scales of type Text.

But you can trick ZG a bit.

The whole trick is to display data using the coordinates of the hidden scale when displaying a 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();
+2
source

I do not do this in my project, but change the color based on some criteria. This should be pretty easy to change. Look at the svn repository at stochfit.sourceforge.net for graph classes. You can also take a look at the version of zedgraph that I have in my warehouse, some image capture and zoom error have been fixed.

0
source

Source: https://habr.com/ru/post/1728180/


All Articles