Reputation: 8001
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?
Upvotes: 2
Views: 1267
Reputation: 8001
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;
}
};
Upvotes: 4