开发者

Microsoft Silverlight charting odd interval

开发者 https://www.devze.com 2023-02-15 01:14 出处:网络
In a simple Microsoft chart control in Silverlight I have the days of one month as dates on the X axis and double values on the Y axis. I would like to display every second day on the X axis but those

In a simple Microsoft chart control in Silverlight I have the days of one month as dates on the X axis and double values on the Y axis. I would like to display every second day on the X axis but those days should be the odd days.

If I set the IntervalType="Days" and Interval="2" the numbering always starts with day 2. Even if I pu开发者_JAVA技巧t a dummy date in front or in the end or both.

Instead of: __ 02 __ 04 __ 06 __ 08 __ 10 ...

I need: 01 __ 03 __ 05 __ 07 ...

How can I achieve this in the simplest way?


Example for a set 31.01 -> 1.02 -> 3.02 instead of 31.01 -> 2.02. In that case the only one way is to write the custom Axis similar to the DateTimeAxis.

At first copy to your project the following files:

  • c:\Program Files\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Source\Source code.zip\Controls.DataVisualization.Toolkit\EnumerableFunctions.cs
  • c:\Program Files\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Source\Source code.zip\Controls.DataVisualization.Toolkit\ValueHelper.cs

Copy these files with exactly the same namespace, they are internal so there will not be a name conflict. Next, add the extended class for DateTimeIntervalType:

namespace System.Windows.Controls.DataVisualization.Charting
{
    /// <summary>
    /// A date time interval.
    /// </summary>
    public enum ExtendedDateTimeIntervalType
    {
        /// <summary>
        /// Automatically determine interval.
        /// </summary>
        Auto = 0,

        /// <summary>
        /// Interval type is milliseconds.
        /// </summary>
        Milliseconds = 1,

        /// <summary>
        /// Interval type is seconds.
        /// </summary>
        Seconds = 2,

        /// <summary>
        /// Interval type is minutes.
        /// </summary>
        Minutes = 3,

        /// <summary>
        /// Interval type is hours.
        /// </summary>
        Hours = 4,

        /// <summary>
        /// Interval type is days.
        /// </summary>
        Days = 5,

        /// <summary>
        /// Interval type is weeks.
        /// </summary>
        Weeks = 6,

        /// <summary>
        /// Interval type is months.
        /// </summary>
        Months = 7,

        /// <summary>
        /// Interval type is years.
        /// </summary>
        Years = 8,

        /// <summary>
        /// Interval type is odd days
        /// </summary>
        OddDays = 9
    }
}

To make the new member OddDays work, I've changed the class DataTimeRangeAxis. Here is a link on pastebin, because programmers of SO don't pay attention to such trifle as answers with long explanation.

Change the namespace SilverlightApplication3 to whatever your want (except System.Windows.Controls.DataVisualization.Charting).

Also I've commented the code at the last function, because it contains many dependencies and I didn't want to copy extra files to the application. The axis works fine without this code, probably this function isn't used at all.

The most important part of the class is in the function IncrementDateTime:

        //The interval type forced by a user, not actual interval type
        if (this.IntervalType == ExtendedDateTimeIntervalType.OddDays)
        {
            DateTime newDate;
            if(span != TimeSpan.Zero) //automatically created interval
                newDate = date.Add(span);
            else newDate = date.AddDays(interval); //else use the interval which is set by a user

            //find the nearest odd day
            while (newDate.Day % 2 != 1)
                newDate = newDate.AddDays(1);
            //update span
            span = newDate - date;
        }

Xaml will look so:

        <charting:Chart.Axes>
            <local:DateTimeAxis IntervalType="OddDays" Orientation="X" Interval="1"/>
        </charting:Chart.Axes>

You can set Interval="2" instead of 1, but it will skip the day in the set 31.01-1.02-3.02, so it is better to use the value 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消