ZedGraph labels

后端 未结 2 548
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 06:28

In ZedGraph, how do I show text labels for each point and in the XAxis all together?

If I do

myPane.XAxis.Type = AxisType.Text;
myPane.         


        
相关标签:
2条回答
  • 2021-01-15 07:06

    If the axis type is text, the code below is easier to get x-coordinates of the points ;)

    for (int tPoint = 0; tPoint < curve.Points.Count; tPoint++)
    {
        TextObj text = new TextObj(curve.Points[tPoint].Y.ToString(), curve.Points[tPoint].X, curve.Points[tPoint].Y + 10);
    }
    
    0 讨论(0)
  • 2021-01-15 07:09

    I've changed my answer after you clarified the question. You just have to remember to position the labels correctly:

    <%
      System.Collections.Generic.List<ZedGraphWebPointPair> points = new System.Collections.Generic.List<ZedGraphWebPointPair>();
      for (int i = 0; i < 15; i++)
      {
        // Let's have some fun with maths
        points.Add(new ZedGraphWebPointPair
        {
          X = i,
          Y = Math.Pow(i - 10, 2) * -1 + 120
        });
      }
    
      System.Collections.Generic.List<string> XAxisLabels = new System.Collections.Generic.List<string>();
    
      TestGraph.CurveList.Add(new ZedGraphWebLineItem { Color = System.Drawing.Color.Red });
      TestGraph.XAxis.Scale.FontSpec.Size = 9;
    
      int j = 1;
      foreach (ZedGraphWebPointPair point in points)
      {
        // Add the points we calculated
        TestGraph.CurveList[0].Points.Add(point);
    
        // Add the labels for the points
        TestGraph.GraphObjList.Add(new ZedGraphWebTextObj
        {
          Location =
          {
            CoordinateFrame = ZedGraph.CoordType.XChartFractionYScale,
            // Make sure we position them according to the CoordinateFrame
            X = Convert.ToSingle(j) / points.Count - 0.05f,
            Y = Convert.ToSingle(point.Y) + 3f,
            AlignV = ZedGraph.AlignV.Top
          },
          Text = point.Y.ToString(),
          FontSpec = { Angle = 90, Size = 9, Border = { IsVisible = false } }
        });
    
        // Add the labels for the XAxis
        XAxisLabels.Add(String.Format("P{0}", j));
    
        j++;
      }
    
      TestGraph.RenderGraph += delegate(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane mp)
      {
        ZedGraph.GraphPane gp = mp[0];
        gp.XAxis.Type = ZedGraph.AxisType.Text;
        gp.XAxis.Scale.TextLabels = XAxisLabels.ToArray();
      };
    
    %>
    

    That code will produce this graph:

    0 讨论(0)
提交回复
热议问题