Skip to main content

How to: Use Custom Measure Units in an Automatic Date-Time Scale Mode

  • 3 minutes to read

To use a custom Date-Time measure unit for an Automatic scale mode, assign an object of a class implementing the IDateTimeMeasureUnitsCalculator interface to the DateTimeScaleOptions.AutomaticMeasureUnitsCalculator property of AxisBase.DateTimeScaleOptions.

    private void Form1_Load(object sender, EventArgs e) {
        chart.Series.Add(GenerateSeries(10000));

        XYDiagram diagram = chart.Diagram as XYDiagram;
        if (diagram == null) return;

        diagram.AxisX.DateTimeScaleOptions.AggregateFunction = AggregateFunction.Average;
        diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Automatic;
        diagram.AxisX.DateTimeScaleOptions.AutomaticMeasureUnitsCalculator = new CustomDateTimeMeasureUnitsCalculator();

        diagram.AxisY.WholeRange.AlwaysShowZeroLevel = false;
    }
class CustomDateTimeMeasureUnitsCalculator : IDateTimeMeasureUnitsCalculator {
    const int daysInWeek = 7;
    const int daysInMonth = 30;
    const int daysInQuarter = 4 * daysInMonth;
    const int daysInYear = 365;

    const int minCount = 5;

    public DateTimeMeasureUnit CalculateMeasureUnit(
            IEnumerable<Series> series, 
            double axisLength, 
            int pixelsPerUnit, 
            double visualMin, 
            double visualMax, 
            double wholeMin, 
            double wholeMax) {
        // Calculate visual range in msecs.
        double visualRange = visualMax - visualMin;
        TimeSpan ts = TimeSpan.FromMilliseconds(visualRange);
        if (ts.TotalDays >= 1.0d) {
            if (ts.TotalDays <= minCount * daysInWeek)
                return DateTimeMeasureUnit.Day;
            if (ts.TotalDays <= minCount * daysInMonth)
                return DateTimeMeasureUnit.Week;
            if (ts.TotalDays <= minCount * daysInQuarter)
                return DateTimeMeasureUnit.Month;
            if (ts.TotalDays <= minCount * daysInYear)
                return DateTimeMeasureUnit.Quarter;
            else
                return DateTimeMeasureUnit.Year;
        }
        else if (ts.TotalHours >= 20.0d)
            return DateTimeMeasureUnit.Hour;
        else if (ts.TotalMinutes >= 20.0d)
            return DateTimeMeasureUnit.Minute;
        else if (ts.TotalSeconds >= 20.0d)
            return DateTimeMeasureUnit.Second;
        else
            return DateTimeMeasureUnit.Millisecond;
    }
}