Oxyplot: Prevent X-Axis label from overlapping

余生长醉 提交于 2019-12-10 23:09:58

问题


I'm plotting a ScatterPlot showing the last one month's data using OxyPlot. But the X axis labels are overlapping. X axis is a date time axis.

Here's the function that I use to get X Axis.

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;
}

How can I prevent them from overlapping? Do I need manually skip labels? or is there a setting in oxyplot which automatically does this? Also, Is it possible to adjust the labels automatically when zooming in and out?


回答1:


This is how I solved it.

I made use of the property 'MajorStep' of the axis.

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

And to adjust the labels while Zooming In and Out:

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


来源:https://stackoverflow.com/questions/41733951/oxyplot-prevent-x-axis-label-from-overlapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!