Oxyplot:防止X轴标签重叠 [英] Oxyplot: Prevent X-Axis label from overlapping

查看:475
本文介绍了Oxyplot:防止X轴标签重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我用来获取X轴的函数.

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

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

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?

推荐答案

这就是我解决的方法.

我利用了轴的属性"MajorStep".

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

这篇关于Oxyplot:防止X轴标签重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆