233 lines
8.3 KiB
C#
233 lines
8.3 KiB
C#
///
|
|
/// Copyright (c) 2016-2022 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using ProductionTracing.Resources;
|
|
|
|
namespace ProductionTracing.Forms
|
|
{
|
|
public partial class StatisticsDlg : Form
|
|
{
|
|
IList<object[]> results;
|
|
IList<ItemSpec> items;
|
|
|
|
bool twoDim;
|
|
int rowItemIx;
|
|
int columnItemIx;
|
|
bool filterEnabled;
|
|
int filteredItemIx;
|
|
string filteredValue;
|
|
|
|
ItemSpec rowItem;
|
|
ItemSpec columnItem;
|
|
ItemSpec filteredItem;
|
|
|
|
IList<string> rowValues;
|
|
IList<string> columnValues;
|
|
int[,] occurances;
|
|
|
|
public StatisticsDlg(QueryResults results, bool twoDim, int rowItemIx, int columnItemIx, bool filterEnabled, int filteredItemIx, string filteredValue)
|
|
{
|
|
InitializeComponent();
|
|
|
|
Text = results.Description;
|
|
okButton.Text = Strings.OkBtnText;
|
|
|
|
this.results = results.Results;
|
|
this.items = results.ProjectedItems;
|
|
|
|
this.twoDim = twoDim;
|
|
this.rowItemIx = rowItemIx;
|
|
this.columnItemIx = columnItemIx;
|
|
this.filterEnabled = filterEnabled;
|
|
this.filteredItemIx = filteredItemIx;
|
|
this.filteredValue = filteredValue;
|
|
|
|
rowItem = items[rowItemIx].Clone();
|
|
if (twoDim)
|
|
{
|
|
columnItem = items[columnItemIx].Clone();
|
|
}
|
|
if (filterEnabled)
|
|
{
|
|
filteredItem = items[filteredItemIx].Clone();
|
|
}
|
|
|
|
Localize();
|
|
}
|
|
|
|
private void StatisticsDlg_Load(object sender, EventArgs e)
|
|
{
|
|
Calculate();
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
okButton.Text = Strings.OkBtnText;
|
|
exportButton.Text = Strings.Export;
|
|
}
|
|
|
|
bool Calculate()
|
|
{
|
|
try
|
|
{
|
|
rowValues = new List<string>();
|
|
columnValues = new List<string>();
|
|
|
|
///
|
|
/// Find discrete values of rows and columns
|
|
///
|
|
foreach (var result in results)
|
|
{
|
|
if (!filterEnabled || filteredItem.Print(result[filteredItemIx]).Equals(filteredValue))
|
|
{
|
|
string value = rowItem.Print(result[rowItemIx]);
|
|
if (!rowValues.Contains(value)) rowValues.Add(value);
|
|
|
|
if (twoDim)
|
|
{
|
|
value = columnItem.Print(result[columnItemIx]);
|
|
if (!columnValues.Contains(value)) columnValues.Add(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
///
|
|
/// Sort discrete values of rows and columns
|
|
///
|
|
int dummy;
|
|
bool rowValuesAreInts = true;
|
|
foreach (var rVal in rowValues) if (!int.TryParse(rVal, out dummy)) { rowValuesAreInts = false; break; }
|
|
if (rowValuesAreInts) rowValues = rowValues.OrderBy(x => int.Parse(x)).ToList();
|
|
else rowValues = rowValues.OrderBy(x => x).ToList();
|
|
|
|
if (twoDim)
|
|
{
|
|
bool colValuesAreInts = true;
|
|
foreach (var cVal in columnValues) if (!int.TryParse(cVal, out dummy)) { colValuesAreInts = false; break; }
|
|
if (colValuesAreInts) columnValues = columnValues.OrderBy(x => int.Parse(x)).ToList();
|
|
else columnValues = columnValues.OrderBy(x => x).ToList();
|
|
}
|
|
|
|
///
|
|
/// Calculate occurances
|
|
///
|
|
int columns = Math.Max(1, columnValues.Count);
|
|
occurances = new int[rowValues.Count, columns];
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (!filterEnabled || filteredItem.Print(result[filteredItemIx]).Equals(filteredValue))
|
|
{
|
|
for (int row = 0; row < rowValues.Count; row++)
|
|
{
|
|
if (twoDim)
|
|
{
|
|
for (int column = 0; column < columns; column++)
|
|
{
|
|
if (rowItem.Print(result[rowItemIx]).Equals(rowValues[row]) && columnItem.Print(result[columnItemIx]).Equals(columnValues[column]))
|
|
{
|
|
occurances[row, column]++;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (rowItem.Print(result[rowItemIx]).Equals(rowValues[row])) occurances[row, 0]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///
|
|
/// Update ListBox
|
|
///
|
|
statisticsListView.View = View.Details;
|
|
statisticsListView.GridLines = true;
|
|
statisticsListView.FullRowSelect = true;
|
|
|
|
statisticsListView.Columns.Add((twoDim ? string.Empty : rowItem.Caption), 200);
|
|
for (int column = 0; column < columns; column++)
|
|
{
|
|
statisticsListView.Columns.Add((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 < columns; 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 okButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
|
|
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<ItemSpec> items = currentQR.ProjectedItems;
|
|
|
|
try
|
|
{
|
|
using (TextWriter writer = new StreamWriter(dlg.FileName, false, Encoding.UTF8))
|
|
{
|
|
if (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}\"", 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|