Oxyplot:防止X轴标签重叠

时间:2017-01-19 04:48:33

标签: xamarin label overlap oxyplot

我正在绘制一个ScatterPlot,使用OxyPlot显示最近一个月的数据。但X轴标签重叠。 X轴是日期时间轴。

enter image description here

这是我用来获取X轴的功能。

DateTimeAxis GetXAxis ()
{
    var axis = new DateTimeAxis {
        Position = AxisPosition.Bottom,
        MinorIntervalType = DateTimeIntervalType.Days,
        MinorTickSize = 0,
        MajorTickSize = 0,
        MajorGridlineStyle = LineStyle.None,
        MinorGridlineStyle = LineStyle.None,
        FontSize = 8,
        TextColor = OxyColor.Parse (ColorHex.DarkGray),
        Maximum = DateTimeAxis.ToDouble (DateTime.Now),
        MajorStep = 1,
    };
    if (type == Constants.QUESTION_ANSWER_TYPE_WEEK) {
        axis.Minimum = DateTimeAxis.ToDouble (DateTime.Now.AddDays (-7));
        axis.StringFormat = "ddd";
    } else if (type == Constants.QUESTION_ANSWER_TYPE_MONTH) {
        axis.Minimum = DateTimeAxis.ToDouble (DateTime.Now.AddDays (-30));
        axis.StringFormat = "MMM dd";
    } else {
        axis.StringFormat = "MMM dd";
    }
    return axis;
}

如何防止它们重叠?我需要手动跳过标签吗?或者在oxyplot中有自动执行此操作的设置吗?此外,是否可以在放大和缩小时自动调整标签?

1 个答案:

答案 0 :(得分:3)

这就是我解决它的方法。

我利用了该物业' MajorStep'轴。

var axis = new DateTimeAxis ();
...

DateTime maxDate = .... // Max of DateTime from my data
DateTime minDate = .... // Min of Datetime from my data
double totalDays = (MaxDate - MinDate).TotalDays;
if (totalDays > 8)
    axis.MajorStep = (MaxDate - MinDate).TotalDays / 8; // I want to show only 8 labels on X-Axis

在放大和缩小时调整标签:

axis.AxisChanged += (sender, e) => {
    if (e.ChangeType == AxisChangeTypes.Zoom) {
        axis.MajorStep = (DateTimeAxis.ToDateTime (axis.ActualMaximum) - DateTimeAxis.ToDateTime (axis.ActualMinimum)).TotalDays / 8;
    }
};