c# - 使用 zedGraph 在 C# 中绘制图形

标签 c# graph zedgraph

我需要创建一个具有以下属性的图表:
X 轴是学校名称。
Y 轴用于类名。
在点 (x,y) 中,我需要放一个点,它的颜色将代表学生的数量(颜色越深意味着学生越多)。
我正在使用 ZedGraph(使用该示例:http://zedgraph.org/wiki/index.php?title=Gradient-By-Value_Demo),但我不知道如何将点(并确定它的暗级别)放在正确的位置(将其与学校名称和类(class)名称进行比较)。
另外,我不知道如何让 X 轴和 Y 轴显示学校名称和类(class)名称。
我怎样才能做到这一点? (它不必在 zedGraph 中)。
非常感谢!

最佳答案

问题是 ZedGraph 以一种有点奇怪的方式处理文本类型的比例尺。因此,当您同时拥有两种文本类型的刻度时,几乎不可能正确显示数据。

不过你可以骗过ZG一点点。

整个技巧是使用隐藏比例尺的坐标显示数据,同时显示第二个假比例尺。

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

关于c# - 使用 zedGraph 在 C# 中绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2049771/

相关文章:

c# - 将水平条形图更改为垂直条形图

c# - 如何在字符串中间添加一个符号字符,而不是在 C# 中的字符串开头或结尾

c# - LINQ:如何绑定(bind)到串联字符串列表?

c# - UOW + Repository + Autofac 加载两个不同的 DbContext

python - 使用 matplotlib 将 csv 文件数据绘制成折线图

algorithm - 使用数组而不是 Kruskals 算法的不相交集来加快合并和查找时间

java - 创建有向图

c# - 为什么单击 ZedGraph 时我的图表上会出现垂直线?

c# - 多段 ZedGraph 曲线

c# - Zedgraph 缩放在某些系统上无法正常工作