首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZedGraph标签

ZedGraph标签
EN

Stack Overflow用户
提问于 2009-05-03 03:28:36
回答 2查看 10.2K关注 0票数 2

在ZedGraph中,如何同时显示每个点的文本标签和XAxis中的所有文本标签?

如果我这样做了

代码语言:javascript
复制
myPane.XAxis.Type = AxisType.Text;
myPane.XAxis.Scale.TextLabels = array_of_string;

我在XAxis上贴上这样的标签

如果我这样做了

代码语言:javascript
复制
for (int i = 0; i < myCurve.Points.Count; i++)
{
    PointPair pt = myCurve.Points[i];
    // Create a text label from the Y data value
    TextObj text = new TextObj(
        pt.Y.ToString("f0"), pt.X, pt.Y + 0.1,
        CoordType.AxisXYScale, AlignH.Left, AlignV.Center);
    text.ZOrder = ZOrder.A_InFront;
    text.FontSpec.Angle = 0;
    myPane.GraphObjList.Add(text);
}

我把标签放在曲线上,就像这样

但是如果我同时这样做,曲线上的标签就会消失。

有没有办法把这两种标签结合起来?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-05-03 05:39:19

在你澄清了问题之后,我改变了我的答案。你只需要记住标签的位置是正确的:

代码语言:javascript
复制
<%
  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();
  };

%>

该代码将生成以下图形:

票数 8
EN

Stack Overflow用户

发布于 2010-06-09 16:37:57

如果轴类型为text,则下面的代码更容易获得点的x坐标;)

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/816256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档