【问题标题】:How to customize the labels of an Infragistics Ultrachart?如何自定义 Infragistics Ultrachart 的标签?
【发布时间】:2013-08-11 04:47:57
【问题描述】:

我正在尝试使用 vb.net 自定义线性超图 X 轴的系列标签。

我查看了documentation from Infragistics,发现我可以使用这个代码:

UltraChart.Axis.Y.Labels.SeriesLabels.FormatString = "<Item_Label>"

可以查看可用标签类型的说明here。 但是,我没有得到预期的结果。我得到 "Row#1",我只想得到 "1"

我已经尝试过 Infragistics 论坛中this post 的第一个回复中使用的方法,其中包括使用带有自定义标签的哈希表。那里使用的代码如下(在 C# 中):

Hashtable labelHash = new Hashtable();
labelHash.Add("CUSTOM", new MyLabelRenderer());
ultraChart1.LabelHash = labelHash;
xAxis.Labels.ItemFormatString = "<CUSTOM>";

public class MyLabelRenderer : IRenderLabel
{
    public string ToString(Hashtable context)
    {
        string label = (string)context["ITEM_LABEL"];
        int row = (int)context["DATA_ROW"];
        int col = (int)context["DATA_COLUMN"];
        //use row, col, current label's text or other info inside the context to get the axis label.
        //the string returned here will replace the current label.
        return label;
    }
}

这种方法也不起作用。 我正在使用 Infragistics NetAdvantage 2011.1。

有人知道如何自定义这些标签以获得“Row#”之后的数字吗?

【问题讨论】:

    标签: vb.net infragistics


    【解决方案1】:

    有不同的方法来解决这个任务。如果您使用 FillSceneGraph 事件,一种可能的解决方案可能是。通过这种方式,您可以获得 TEXT 原语并对其进行修改。例如:

     private void ultraChart1_FillSceneGraph(object sender, Infragistics.UltraChart.Shared.Events.FillSceneGraphEventArgs e)
        {
            foreach (Primitive pr in e.SceneGraph)
            {
                if (pr is Text  &&((Text)pr).labelStyle.Orientation == TextOrientation.VerticalLeftFacing )
                {
                    pr.PE.Fill = Color.Red;
                    ((Text)pr).SetTextString("My custom labels");
                }
            }
        }
    

    【讨论】:

    • 感谢您的回答,但不幸的是我根本没有使用 FillSceneGraph。我一直在寻找解决此问题的方法与此类似(不必只有一行):UltraChart.Axis.Y.Labels.SeriesLabels.FormatString = "&lt;Item_Label&gt;",但我希望能够设置标签的文本,而不是格式化(类似于我们更改文本框的文本时所做的)。如果您知道如何执行此操作,请告诉我。
    • @alhalama,是的,我确实尝试了 Georgi 的建议。当我尝试运行以下代码时出现异常 (InvalidCastException: Unable to cast object of type 'Infragistics.UltraChart.Core.Primitives.GraphicsContext' to type 'Infragistics.UltraChart.Core.Primitives.Text'.):For Each pr1 In e.SceneGraph If (pr1 Is Text And DirectCast(pr1, Text).labelStyle.Orientation = Infragistics.UltraChart.Shared.Styles.TextOrientation.VerticalLeftFacing) Then DirectCast(pr1, Text).SetTextString(i.ToString) End If Next i = 1
    • 在 VB 中,无论第一个条件是真还是假,都评估双方,而在 C# 中 && 仅在第一个条件为真时评估右边。将 And 替换为 AndAlso,您将拥有与 C# 相同的行为,这应该可以解决您的 InvalidCastException。例如:If (pr1 Is Text AndAlso DirectCast(pr1, Text).labelStyle.Orientation = Infragistics.UltraChart.Shared.Styles.TextOrientation.VerticalLeftFacing)
    • @alhalama,我再次尝试在 If 条件下使用 AndAlso 并且不再出现异常。但是,我仍然没有得到我想要的结果(X 轴标签仍然显示 ROW#1 而不是 1)。
    • 能否提供您当前正在使用的更新代码?
    【解决方案2】:

    好的。我将尝试更深入地解释 FormatString 属性。 当您使用此属性时,您可以确定要显示哪些信息(例如:项目值或数据值或系列值)。当然,也可以选择使用您的自定义 FormatString。 例如:

    axisX2.Labels.ItemFormat=AxisItemLabelFormat.Custom; axisX2.Labels.ItemFormatString ="";

    在这种情况下,我们在 X 轴上有代表日期的标签,因此如果您使用这两个属性,则可以确定日期格式(例如 dd/MM/yyyy 或 MM/ddd/yy)。在您的场景中,您的 X 轴上有字符串值。如果您无法在较低级别修改这些字符串值(例如在您的数据库中,通过 TableAdapters SQL 查询、DataSet,即在将您的 DataSource 设置为我们的 UltraChart 之前),那么您可以使用 FillSceneGraph 事件并修改您的 Text 原语。有关此活动的更多详细信息,请访问 http://help.infragistics.com/Help/NetAdvantage/WinForms/2013.1/CLR4.0/html/Chart_Modify_Scene_Graph_Using_FillSceneGraph_Event.html 如果您需要样品或其他帮助,请不要犹豫,在我们的网站上创建一个新的论坛主题 - http://www.infragistics.com/community/forums/
    我很乐意为您提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2010-10-17
      • 2012-09-28
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 2010-11-17
      相关资源
      最近更新 更多