233 lines
8.1 KiB
C#
233 lines
8.1 KiB
C#
///
|
|
/// Copyright (c) 2016-2019 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using Common;
|
|
using Results.Entities;
|
|
using ResultsBrowser.Resources;
|
|
using ResultsBrowser.Output.Printers.Statistics;
|
|
|
|
namespace ResultsBrowser.Forms
|
|
{
|
|
public partial class StatisticsDlg : Form
|
|
{
|
|
IList<WaterMeter> waterMeters;
|
|
StatisticsOptionsDlg options;
|
|
|
|
IList<string> rowValues;
|
|
IList<string> columnValues;
|
|
|
|
int[,] occurances;
|
|
|
|
string title;
|
|
|
|
|
|
public StatisticsDlg(IList<WaterMeter> waterMeters, StatisticsOptionsDlg options,
|
|
DateTime startDate, DateTime endDate)
|
|
{
|
|
InitializeComponent();
|
|
|
|
this.waterMeters = waterMeters;
|
|
this.options = options;
|
|
|
|
if (!string.IsNullOrEmpty(Program.LocalSettings.TimePeriodFormat) && (startDate != new DateTime(0)) && (endDate != new DateTime(0)))
|
|
{
|
|
title = string.Format(Program.LocalSettings.TimePeriodFormat, startDate, endDate);
|
|
}
|
|
else
|
|
{
|
|
title = Strings.Statistics;
|
|
}
|
|
|
|
Text = title;
|
|
|
|
closeButton.Text = Strings.OkBtnText;
|
|
printButton.Text = Strings.Print;
|
|
}
|
|
|
|
private void StatisticsDlg_Load(object sender, EventArgs e)
|
|
{
|
|
Calculate();
|
|
}
|
|
|
|
bool Calculate()
|
|
{
|
|
try
|
|
{
|
|
rowValues = new List<string>();
|
|
columnValues = new List<string>();
|
|
|
|
///
|
|
/// Find discrete values of rows and columns
|
|
///
|
|
foreach (var wm in waterMeters)
|
|
{
|
|
string value = options.RowItem.Print(wm);
|
|
if (options.RowItem2 != null) value = value + " | " + options.RowItem2.Print(wm);
|
|
if (options.RowItem3 != null) value = value + " | " + options.RowItem3.Print(wm);
|
|
if (!rowValues.Contains(value)) rowValues.Add(value);
|
|
|
|
if (options.TwoDim)
|
|
{
|
|
value = options.ColumnItem.Print(wm);
|
|
if (options.ColumnItem2 != null) value = value + " | " + options.ColumnItem2.Print(wm);
|
|
if (options.ColumnItem3 != null) value = value + " | " + options.ColumnItem3.Print(wm);
|
|
if (!columnValues.Contains(value)) columnValues.Add(value);
|
|
}
|
|
}
|
|
|
|
///
|
|
/// Calculate occurances
|
|
///
|
|
int columnsCount = Math.Max(1, columnValues.Count);
|
|
occurances = new int[rowValues.Count, columnsCount];
|
|
|
|
foreach (var wm in waterMeters)
|
|
{
|
|
for (int row = 0; row < rowValues.Count; row++)
|
|
{
|
|
string rowValue = options.RowItem.Print(wm);
|
|
if (options.RowItem2 != null) rowValue = rowValue + " | " + options.RowItem2.Print(wm);
|
|
if (options.RowItem3 != null) rowValue = rowValue + " | " + options.RowItem3.Print(wm);
|
|
|
|
if (options.TwoDim)
|
|
{
|
|
for (int column = 0; column < columnsCount; column++)
|
|
{
|
|
string columnValue = options.ColumnItem.Print(wm);
|
|
if (options.ColumnItem2 != null) columnValue = columnValue + " | " + options.ColumnItem2.Print(wm);
|
|
if (options.ColumnItem3 != null) columnValue = columnValue + " | " + options.ColumnItem3.Print(wm);
|
|
|
|
if (rowValue == rowValues[row] && columnValue == columnValues[column])
|
|
{
|
|
occurances[row, column]++;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (rowValue == rowValues[row]) occurances[row, 0]++;
|
|
}
|
|
}
|
|
}
|
|
|
|
///
|
|
/// Update ListBox
|
|
///
|
|
statisticsListView.View = View.Details;
|
|
statisticsListView.GridLines = true;
|
|
statisticsListView.FullRowSelect = true;
|
|
|
|
statisticsListView.Columns.Add(" ", 200);
|
|
for (int column = 0; column < columnsCount; column++)
|
|
{
|
|
statisticsListView.Columns.Add((options.TwoDim ? columnValues[column] : Strings.Count), 70);
|
|
}
|
|
|
|
for (int row = 0; row < rowValues.Count; row++)
|
|
{
|
|
ListViewItem lvi = new ListViewItem(rowValues[row]);
|
|
for (int column = 0; column < columnsCount; column++) lvi.SubItems.Add(occurances[row, column].ToString());
|
|
statisticsListView.Items.Add(lvi);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
MessageBox.Show(exc.Message, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void closeButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
|
|
private void printButton_Click(object sender, EventArgs e)
|
|
{
|
|
int columnsCount = Math.Max(1, columnValues.Count);
|
|
string[,] table = new string[rowValues.Count + 1, columnsCount + 1];
|
|
|
|
table[0, 0] = string.Empty;
|
|
for (int column = 0; column < columnsCount; column++)
|
|
{
|
|
table[0, column + 1] = (options.TwoDim ? columnValues[column] : Strings.Count);
|
|
}
|
|
|
|
for (int row = 0; row < rowValues.Count; row++)
|
|
{
|
|
table[row + 1, 0] = rowValues[row];
|
|
for (int column = 0; column < columnsCount; column++) table[row + 1, column + 1] = occurances[row, column].ToString();
|
|
}
|
|
|
|
switch (MessageBox.Show(Strings.Yes_Portrait_No_Landscape, Strings.Page_orientation, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
|
|
{
|
|
case DialogResult.Yes:
|
|
new StatisticsPrintDocument(title, table, PageOrientation.Portrait).Print();
|
|
break;
|
|
case DialogResult.No:
|
|
new StatisticsPrintDocument(title, table, PageOrientation.Landscape).Print();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void exportButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (rowValues == null || columnValues == null || occurances == null) return;
|
|
|
|
SaveFileDialog dlg = new SaveFileDialog();
|
|
dlg.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
//IList<MonitoringDB.ItemSpec> items = currentQR.ProjectedItems;
|
|
|
|
try
|
|
{
|
|
using (TextWriter writer = new StreamWriter(dlg.FileName, false, System.Text.Encoding.UTF8))
|
|
{
|
|
if (options.TwoDim)
|
|
{
|
|
/// Write column headers
|
|
for (int i = 0; i < columnValues.Count; i++)
|
|
{
|
|
writer.Write(string.Format(";\"{0}\"", columnValues[i]));
|
|
}
|
|
writer.WriteLine();
|
|
}
|
|
else
|
|
{
|
|
writer.WriteLine(string.Format("\"{0}\";\"{1}\"", options.RowItem.Caption, Strings.Count));
|
|
}
|
|
|
|
int nrColumns = Math.Max(1, columnValues.Count);
|
|
|
|
/// Write rows, row header first
|
|
for (int j = 0; j < rowValues.Count; j++)
|
|
{
|
|
writer.Write(string.Format("\"{0}\"", rowValues[j]));
|
|
for (int i = 0; i < nrColumns; i++)
|
|
{
|
|
writer.Write(string.Format(";{0}", occurances[j,i]));
|
|
}
|
|
writer.WriteLine();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
MessageBox.Show(exc.Message, Strings.Error_exporting_results, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|