The example demonstrates how holidays can be handled on a date axis. Weekends and holidays (25th Dec and 1st Jan) can be shown or collapsed using the check boxes below the chart.
Zooming has been enabled - click and drag with the mouse on the plot area or use the mouse wheel to zoom, double click to unzoom.
XAML + CODE +
<UserControl x:Class="Visiblox.Charts.Examples.HandlingHolidays.HandlingHolidaysExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:charts="clr-namespace:Visiblox.Charts;assembly=Visiblox.Charts"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<UserControl.Resources>
<Style x:Key="NoBorder" TargetType="Border">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="BorderBrush" Value="Black" />
</Style>
<!-- Alternate Style-->
<Style x:Key="AlternateLine" TargetType="charts:LineSeries">
<Setter Property="LineStyle">
<Setter.Value>
<Style TargetType="Path">
<Setter Property="StrokeDashArray" Value="2,2" />
</Style>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="90*" />
<RowDefinition Height="10*" />
</Grid.RowDefinitions>
<!-- Ultimate Trial users should add 'ValidationKey="ENTER TRIAL LICENSE KEY HERE"' to each Chart declaration. -->
<charts:Chart Width="600" Height="350" x:Name="chart" Grid.Row="0" Title="Cumulative daily sales over Christmas period (09-10)" HorizontalAlignment="Center"
LegendVisibility="Collapsed" PlotAreaBorderStyle="{StaticResource NoBorder}" >
<charts:Chart.Behaviour>
<charts:ZoomBehaviour />
</charts:Chart.Behaviour>
<charts:Chart.XAxis>
<charts:DiscontinuousDateTimeAxis LabelFormatString="dd/MM" MajorTickInterval="1" MajorTickIntervalType="Days" ShowMinorTicks="False" ShowMajorGridlines="False" >
<charts:DiscontinuousDateTimeAxis.Range>
<charts:DateTimeRange Minimum="2009-12-23" Maximum="2010-01-05" />
</charts:DiscontinuousDateTimeAxis.Range>
<!-- Add DiscontinuityProvider to allow dates to be excluded in code behind -->
<charts:DiscontinuousDateTimeAxis.DiscontinuityProvider>
<charts:DailyHoursModeProvider/>
</charts:DiscontinuousDateTimeAxis.DiscontinuityProvider>
</charts:DiscontinuousDateTimeAxis>
</charts:Chart.XAxis>
<charts:Chart.YAxis>
<charts:LinearAxis Title="Total units sold" ShowMinorTicks="False" ShowMajorGridlines="False" LabelFormatString="N0" >
<charts:LinearAxis.Range>
<charts:DoubleRange Minimum="0" Maximum="200" />
</charts:LinearAxis.Range>
</charts:LinearAxis>
</charts:Chart.YAxis>
<!-- Define the line series to be shown -->
<charts:Chart.Series>
<charts:DiscontinuousLineSeries x:Name="DiscontinuousLineSeries" LineStrokeThickness="1.5" NullHandlingMode="Styled" NullHandlingStyle="{StaticResource AlternateLine}"/>
</charts:Chart.Series>
</charts:Chart>
<!-- Define CheckBoxes below chart to toggle holidays and weekends -->
<StackPanel Grid.Row="1" HorizontalAlignment="Center" Orientation="Horizontal" Margin="0,15,0,0">
<TextBlock Text="Hide Holidays" Margin="0,0,5,0" />
<CheckBox x:Name="HideHoliday" Checked="HideHoliday_Checked" Unchecked="HideHoliday_Checked" Margin="0,0,15,0" />
<TextBlock Text="Hide Weekends" Margin="0,0,5,0" />
<CheckBox x:Name="HideWeekends" Checked="HideWeekends_Checked" Unchecked="HideWeekends_Checked" />
</StackPanel>
</Grid>
</UserControl>
^ Back To Top
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
namespace Visiblox.Charts.Examples.HandlingHolidays
{
/// <summary>
/// An example for Handling Holidays in a discontinuous date grid
/// </summary>
public partial class HandlingHolidaysExample : UserControl
{
private Random random = new Random(20110810);
/// <summary>
/// The maximum value that will be generated by the random number generator
/// </summary>
private int maximumRandomValue;
/// <summary>
/// Days of the week and holidays to exclude on date axis
/// </summary>
private ObservableCollection<DayOfWeek> excludeWeekend = new ObservableCollection<DayOfWeek>();
private ObservableCollection<DateTime> excludeHolidays = new ObservableCollection<DateTime>();
//define holidays
private DateTime christmas = new DateTime(2009, 12, 25);
private DateTime newYear = new DateTime(2010, 01, 01);
public HandlingHolidaysExample()
{
InitializeComponent();
//Maximum value is maximum of the range of Y axis
maximumRandomValue = Convert.ToInt32(chart.YAxis.Range.Maximum);
// Set axis exclude collections. Initially empty, populated by check box selections
((chart.XAxis as DiscontinuousDateTimeAxis).DiscontinuityProvider as DailyHoursModeProvider).ExcludeDaysOfWeek = excludeWeekend;
((chart.XAxis as DiscontinuousDateTimeAxis).DiscontinuityProvider as DailyHoursModeProvider).IgnoreDates = excludeHolidays;
chart.Series.First().DataSeries = GenerateData();
chart.YAxis.PropertyChanged += HandleZoom;
}
/// <summary>
/// Generate random data
/// </summary>
private IDataSeries GenerateData()
{
var series = new DataSeries<DateTime, double>();
//Next data to add and running total of all data currently added
int data = 60;
int runningTotal = 0;
var xAxisRange = (IRange<DateTime>)chart.XAxis.Range;
DateTime currentDate = xAxisRange.Minimum.Date;
series.Add(new DataPoint<DateTime, double>(currentDate, 0));
currentDate = currentDate.AddDays(1);
while (currentDate < xAxisRange.Maximum.Date)
{
DayOfWeek currentDay = currentDate.DayOfWeek;
// Weekend or holiday, use null
if (UsePreviousData(currentDate))
{
series.Add(null);
}
// Else add some more data
else
{
data = data - 8;
runningTotal = runningTotal + data;
series.Add(new DataPoint<DateTime, double>(currentDate, runningTotal));
}
currentDate = currentDate.AddDays(1);
}
return series;
}
private bool UsePreviousData(DateTime currentDate)
{
return currentDate.DayOfWeek.Equals(DayOfWeek.Saturday) || currentDate.DayOfWeek.Equals(DayOfWeek.Sunday) || currentDate.Equals(christmas) || currentDate.Equals(newYear);
}
/// <summary>
/// Check box handlers to show and hide holidays
/// </summary>
private void HideHoliday_Checked(object sender, System.Windows.RoutedEventArgs e)
{
// If box has just been checked, hide holidays on the chart
if (HideHoliday.IsChecked == true)
{
excludeHolidays.Add(christmas);
excludeHolidays.Add(newYear);
}
// Else remove holidays from excluded list
else
{
excludeHolidays.Clear();
}
UpdateSkipMode();
}
/// <summary>
/// Check box handlers to show and hide weekends
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HideWeekends_Checked(object sender, System.Windows.RoutedEventArgs e)
{
if (HideWeekends.IsChecked == true)
{
excludeWeekend.Add(DayOfWeek.Saturday);
excludeWeekend.Add(DayOfWeek.Sunday);
}
else
{
excludeWeekend.Clear();
}
UpdateSkipMode();
}
private void UpdateSkipMode()
{
if (HideWeekends.IsChecked == true && HideHoliday.IsChecked == true)
chart.Series.OfType<DiscontinuousLineSeries>().First().NullHandlingMode = NullHandlingMode.Skip;
else
chart.Series.OfType<DiscontinuousLineSeries>().First().NullHandlingMode = NullHandlingMode.Styled;
}
void HandleZoom(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Zoom")
{
var yAxis = chart.YAxis as LinearAxis;
//if Y axis zoomed far enough, set major tick interval or show default
yAxis.MajorTickInterval = yAxis.Zoom.Scale < 0.3 ? 2 : 0;
}
}
}
}
^ Back To Top

