【问题标题】:OxyPlot: How to use the axis label formatter and show Y labels?OxyPlot:如何使用轴标签格式化程序并显示 Y 标签?
【发布时间】:2016-12-18 05:48:07
【问题描述】:

我正在为我的 Xamarin.iOS 项目使用 Oxyplot 来绘制条形图。

这就是我的图表目前的样子

这里的 x 轴值不是数字,我想显示 sun、mon true、wed.....

我可以看到 CategoryAxis 有一个名为 LabelFormatter 的方法,它返回 Func<double, string>,但是我该如何使用它呢?

还有为什么 Y 轴标签没有显示?

public class MyClass
{
    /// <summary>
    /// Gets or sets the plot model that is shown in the demo apps.
    /// </summary>
    /// <value>My model.</value>
    public PlotModel MyModel { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="OxyPlotSample.MyClass"/> class.
    /// </summary>
    public MyClass()
    {

        var model = new PlotModel { Title = "ColumnSeries" };
        model.PlotAreaBorderColor = OxyColors.Transparent;
        // A ColumnSeries requires a CategoryAxis on the x-axis.

        model.Axes.Add(new CategoryAxis()
        {
            Position = AxisPosition.Bottom,
            MinorTickSize = 0,
            MajorTickSize = 0,
            //MajorGridlineStyle = LineStyle.Solid,
            //MinorGridlineStyle = LineStyle.Solid,
        });

        model.Axes.Add(new LinearAxis()
        {
            AxislineStyle = LineStyle.None,
            Position = AxisPosition.Left,
            MinorTickSize = 0,
            MajorTickSize = 0,
            MajorGridlineStyle = LineStyle.Solid,
            MinorGridlineStyle = LineStyle.Solid,
            Minimum = 0,
            Maximum = 400,
        });

        var series = new ColumnSeries();
        series.Items.Add(new ColumnItem() { Value = 200, Color = OxyColors.Orange});
        series.Items.Add(new ColumnItem(200));
        series.Items.Add(new ColumnItem(300));
        series.Items.Add(new ColumnItem(100));
        series.Items.Add(new ColumnItem(200));
        series.Items.Add(new ColumnItem(100));
        series.Items.Add(new ColumnItem(130));

        model.Series.Add(series);

        this.MyModel = model;
    }
}

【问题讨论】:

    标签: c# xamarin.ios oxyplot


    【解决方案1】:

    要在轴上显示标签,您必须指定属性MajorStep,Oxyplot 将仅绘制与主要步骤匹配的标签。

    model.Axes.Add(new LinearAxis()
    {
        MajorStep = 10,
        Position = AxisPosition.Left,
        ...
    });
    

    要修改带有日期名称的标签,您可以使用DateTimeAxis 而不是LinearAxis

    model.Axes.Add(new DateTimeAxis()
    {
        StringFormat = "ddd",
        Position = AxisPosition.Bottom,
        ...
    });
    

    如果您想要更个性化的内容,则必须使用 LabelFormatter 属性。

    编辑:

    CategoryAxis 中的标签:

    var categoryAxis = new CategoryAxis()
    {
        Position = AxisPosition.Bottom,
        ...
    };
    
    categoryAxis.ActualLabels.Add("Mon");
    categoryAxis.ActualLabels.Add("Tue");
    categoryAxis.ActualLabels.Add("Wed");
    categoryAxis.ActualLabels.Add("Thu");
    categoryAxis.ActualLabels.Add("Fri");
    categoryAxis.ActualLabels.Add("Sat");
    categoryAxis.ActualLabels.Add("Sun");
    
    Model.Axes.Add(categoryAxis);
    

    CategoryAxis.ActualLabels 是只读的,因此您必须逐个添加项目。

    【讨论】:

    • 但是,要绘制一个列系列,我不是必须添加一个 CategoryAxis() 作为 X 轴吗?
    • 我已经编辑了答案,我想这就是你想要实现的。
    • 谢谢。这样可行。我想你是关于我问题的第二部分.. 显示 y 水平。 -_-
    • 目前显示 Y 值为 0,下一个 Y 值为 500,只有向上滚动才能看到。如何在每条网格线上显示 Y 值???
    • 您是否指定了 Major Step = 100?因为我已经尝试过了,它有效。可能我没看懂你的问题,能再详细点吗?
    猜你喜欢
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 2015-12-26
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多