OxyPlot:如何使用轴标签格式化程序并显示Y标签?

时间:2016-08-11 13:02:31

标签: c# xamarin.ios oxyplot

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

这是我目前看来最喜欢的图表

enter image description here

这里代替x轴值,这是数字,我想显示太阳,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;
    }
}

1 个答案:

答案 0 :(得分:7)

要在轴上显示标签,您必须指定属性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是readOnly,因此您必须逐个添加项目。