PIAPS_CW/WinFormsApp/FormStatistics.cs

176 lines
6.6 KiB
C#

using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using DataModels.Enums;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace WinFormsApp
{
public partial class FormStatistics : Form
{
ISupplyLogic _supplyLogic;
ISupplierLogic _supplierLogic;
public FormStatistics(ISupplyLogic supplyLogic, ISupplierLogic supplierLogic)
{
InitializeComponent();
_supplyLogic = supplyLogic;
_supplierLogic = supplierLogic;
}
private void FormStatistics_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
chart.Series.Clear();
if (comboBox1.SelectedIndex == 0)
{
var list = _supplyLogic.ReadList(new SupplySearchModel()
{
DateStart = dateTimePickerTo.Value,
DateEnd = dateTimePickerFrom.Value,
});
if (checkBoxPending.Checked)
{
var date = dateTimePickerTo.Value;
chart.Series.Add("Pending");
for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++)
{
var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Pending).ToList().Count;
DataPoint dataPoint = new DataPoint();
dataPoint.XValue = i;
dataPoint.IsVisibleInLegend = false;
dataPoint.Label = $"{date.Year}.{date.Month}";
dataPoint.SetValueY(count);
chart.Series["Pending"].Points.Add(dataPoint);
date = date.AddMonths(1);
}
}
if (checkBoxArriving.Checked)
{
var date = dateTimePickerTo.Value;
chart.Series.Add("Arriving");
for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++)
{
var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Arriving).ToList().Count;
DataPoint dataPoint = new DataPoint();
dataPoint.XValue = i;
dataPoint.IsVisibleInLegend = false;
dataPoint.Label = $"{date.Year}.{date.Month}";
dataPoint.SetValueY(count);
chart.Series["Arriving"].Points.Add(dataPoint);
date = date.AddMonths(1);
}
}
if (checkBoxCompleted.Checked)
{
var date = dateTimePickerTo.Value;
chart.Series.Add("Completed");
for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++)
{
var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Completed).ToList().Count;
DataPoint dataPoint = new DataPoint();
dataPoint.XValue = i;
dataPoint.IsVisibleInLegend = false;
dataPoint.Label = $"{date.Year}.{date.Month}";
dataPoint.SetValueY(count);
chart.Series["Completed"].Points.Add(dataPoint);
date = date.AddMonths(1);
}
}
}
if (comboBox1.SelectedIndex == 1)
{
var supplylist = _supplyLogic.ReadList(new SupplySearchModel()
{
DateStart = dateTimePickerTo.Value,
DateEnd = dateTimePickerFrom.Value,
});
var supplierlist = _supplierLogic.ReadList(null);
foreach (var supplier in supplierlist)
{
var date = dateTimePickerTo.Value;
chart.Series.Add(supplier.Name);
for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++)
{
var count = supplylist.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Completed && x.SupplierId == supplier.Id).ToList().Count;
DataPoint dataPoint = new DataPoint();
dataPoint.XValue = i;
dataPoint.IsVisibleInLegend = false;
dataPoint.Label = $"{supplier.Name}";
dataPoint.SetValueY(count);
chart.Series[supplier.Name].Points.Add(dataPoint);
date = date.AddMonths(1);
}
}
}
}
private void chart_Click(object sender, EventArgs e)
{
}
private void dateTimePickerFrom_ValueChanged(object sender, EventArgs e)
{
LoadData();
}
private void dateTimePickerTo_ValueChanged(object sender, EventArgs e)
{
LoadData();
}
private int diff_month(DateTime d1, DateTime d2)
{
return (d1.Year - d2.Year) * 12 + d1.Month - d2.Month;
}
private void checkBoxPending_CheckedChanged(object sender, EventArgs e)
{
LoadData();
}
private void checkBoxArriving_CheckedChanged(object sender, EventArgs e)
{
LoadData();
}
private void checkBoxCompleted_CheckedChanged(object sender, EventArgs e)
{
LoadData();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != 0)
{
checkBoxArriving.Visible = false;
checkBoxCompleted.Visible = false;
checkBoxPending.Visible = false;
}
else
{
checkBoxArriving.Visible = true;
checkBoxCompleted.Visible = true;
checkBoxPending.Visible = true;
}
LoadData();
}
}
}