简体   繁体   English

使用zedGraph在C#中绘制图形

[英]Draw a Graph in C# using zedGraph

I need to create a graph that have the following properties: 我需要创建一个具有以下属性的图形:
The X Axis is for schools names. X轴用于学校名称。
The Y Axis is for classes names. Y轴用于类名。
In Point (x,y) I need to put a dot that it's color will represent the number of students (darker means more students). 在Point(x,y)中,我需要设置一个点,它的颜色代表学生数量(较暗意味着更多的学生)。
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). 我使用ZedGraph(使用示例: http://zedgraph.org/wiki/index.php?title=Gradient-By-Value_Demo ),但我不知道如何把点(并确定它的黑暗-level)在正确的位置(将其与学校的名称和班级名称进行比较)。
Also, I don't know how to make the X and Y axis to show the school's name and the class's name. 另外,我不知道如何让X和Y轴显示学校的名字和班级的名字。
How can I do that? 我怎样才能做到这一点? (It's NOT have to be in zedGraph). (它不必在zedGraph中)。
many thanks! 非常感谢!

The problem is that ZedGraph is treating a Text-type scale in a little bit strange way. 问题是ZedGraph以一种奇怪的方式处理文本类型的比例。 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. 但你可以稍微欺骗ZG。

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

I don't do quite this in my project, but I do change the color based on some criteria. 我在我的项目中没有做到这一点,但我确实根据一些标准改变了颜色。 It should be pretty easy for you to modify. 你应该很容易修改。 Look at the svn depot in stochfit.sourceforge.net at the graphing classes. 在图形类中查看stochfit.sourceforge.net中的svn depot。 You may also want to take a look at the version of zedgraph I have in my depot, some image capture and a scaling bug were fixed. 您可能还想查看我的库中的zedgraph版本,修复了一些图像捕获和缩放错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM