-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReportsUsageDialog.xaml.cs
106 lines (90 loc) · 4.61 KB
/
ReportsUsageDialog.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Presenter.App_Code;
using Presenter.Resources;
using System.IO;
using System.Windows.Controls.DataVisualization.Charting;
namespace Presenter
{
public partial class ReportsUsageDialog : Window
{
public ReportsUsageDialog()
{
InitializeComponent();
Background = new SolidColorBrush(Config.BackgroundColour);
LibraryList.ItemsSource = new string[] { Labels.ReportsUsageOptionAll }.Union(Directory.GetDirectories(Config.LibraryPath).Select(p => System.IO.Path.GetFileName(p))).Union(new string[] { Labels.ReportsUsageOptionOther });
FromDate.SelectedDate = DateTime.Today.AddYears(-1);
ToDate.SelectedDate = DateTime.Today;
Generate(null, null);
}
private void Generate(object sender, RoutedEventArgs e)
{
DateTime fromD = FromDate.SelectedDate ?? DateTime.Today.AddYears(-1);
DateTime toD = ToDate.SelectedDate ?? DateTime.Today;
string[] libraries = options.Where(c => c.IsChecked ?? false).Select(c => c.Content.ToString().ToLower()).ToArray();
var list = Item.GetUsageStats(fromD, toD, libraries);
//http://stackoverflow.com/questions/992241/what-does-cannot-modify-the-logical-children-for-this-node-at-this-time-because-a
(mainChart.Series[0] as Series).DataContext = list;
mainChart.Height = Math.Max(450, 22 * list.Length);
if ((mainChart.Series[0] as BarSeries).ActualDependentRangeAxis != null)
{
LinearAxis axis = (mainChart.Series[0] as BarSeries).ActualDependentRangeAxis as LinearAxis;
axis.Minimum = 0;
axis.Maximum = list.Max(i => (int?)i.Count); //occasionally the default maximum is insanely high
axis.Interval = Math.Max(Math.Round((axis.Maximum ?? 1) / 30), 1);
}
}
private void Download(object sender, RoutedEventArgs e)
{
DateTime fromD = FromDate.SelectedDate ?? DateTime.Today.AddYears(-1);
DateTime toD = ToDate.SelectedDate ?? DateTime.Today;
string[] libraries = options.Where(c => c.IsChecked ?? false).Select(c => c.Content.ToString().ToLower()).ToArray();
var list = Item.GetUsageStats(fromD, toD, libraries);
StringBuilder output = new StringBuilder();
output.AppendLine(@"{\rtf1\ansi\ansicpg1252\deff0\deflang5129{\fonttbl{\f0\fnil\fcharset0 Arial;}}");
output.AppendLine(@"\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\b\f0\fs22 " + String.Format(Labels.ReportsUsageDocTitle, fromD.ToShortDateString(), toD.ToShortDateString()) + @"\b0\par ");
foreach (ItemUsage item in list)
{
output.AppendLine(item.Name + @"\line");
item.Presentations.ForEach(d => output.AppendLine(@"\tab " + d + @"\line"));
output.AppendLine(@"\tab (" + item.Count + @")\line\line ");
}
output.AppendLine("}");
output.Append((char)0);
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string origFilename = desktop + "\\" + Labels.ReportsUsageDocFilename + " - " + DateTime.Today.ToLongDateString() + ".rtf";
string filename = origFilename;
for (int i = 1; File.Exists(filename); i++)
filename = origFilename.Insert(origFilename.LastIndexOf('.'), " (" + i + ")");
StreamWriter report = new StreamWriter(File.OpenWrite(filename));
report.Write(output.ToString());
report.Close();
System.Diagnostics.Process.Start(filename);
this.Close();
}
protected void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox chkbx = sender as CheckBox;
if (chkbx == options[0])
options.ForEach(b => b.IsChecked = (chkbx.IsChecked ?? false));
else
options[0].IsChecked = false;
}
List<CheckBox> options = new List<CheckBox>();
private void CheckBox_Loaded(object sender, RoutedEventArgs e)
{
options.Add(sender as CheckBox);
}
}
}