private void chiaTheoChieuSau(List <double> arrCotCanKiem, int cotCanKiem, int sogio) { int i, j, k, temp, value; double s = 0; double t = 0; double avg = 0; string item; List <ClassBin> bins = new List <ClassBin>(); ClassBin x = new ClassBin(); //Khoi tao cac gio k = 0; temp = 1; value = (int)(arrCotCanKiem.Count / sogio + 0.5); for (i = 0; i < arrCotCanKiem.Count - 1; i++) { if (temp == 1) { x.Min = arrCotCanKiem[i]; x.IndexItem = new List <int>(); } x.Sum += arrCotCanKiem[i]; x.Count++; x.IndexItem.Add(i); if (temp == value) { x.Max = arrCotCanKiem[i]; if (arrCotCanKiem[i + 1] != arrCotCanKiem[i]) { k++; bins.Add(x); x = new ClassBin(); temp = 1; } } else { temp++; } } if (temp == 0) //bin moi { s = arrCotCanKiem[arrCotCanKiem.Count - 1]; x = new ClassBin(s, s); x.IndexItem.Add(i); x.Sum += s; x.Count++; } else //bin cu, con co the chua them phantu { s = arrCotCanKiem[arrCotCanKiem.Count - 1]; x.Max = s; x.IndexItem.Add(i); x.Sum += s; x.Count++; } bins.Add(x); //show bins Form_ThongBao frm = new Form_ThongBao(); frm.soGio = bins.Count; frm.thuocTinh = cotCanKiem; frm.tenThuocTinh = dataGridView1.Columns[cotCanKiem].Name;//.Columns[i].Name frm.bins = bins; frm.ShowDialog(); if (frm.res) { //khu nhieu cho mau for (i = 0; i < dataGridView1.Rows.Count; i++) { for (j = 0; j < bins.Count; j++) { double.TryParse(dataGridView1.Rows[i].Cells[cotCanKiem].Value.ToString(), out s); if (s >= bins[j].Min && s <= bins[j].Max) { // s thuoc bins[j] dataGridView1.Rows[i].Cells[cotCanKiem].Value = string.Format("{0:#,0.####}", bins[j].Sum / bins[j].Count); } } } } }
private void chiaTheoChieuRong(int cotCanKiem, int sogio) { int i, j, k; double item = 0; double avg = 0; double Min = timMin(cotCanKiem); double Max = timMax(cotCanKiem); double value = (Max - Min) / sogio; List <ClassBin> bins = new List <ClassBin>(); ClassBin temp; //Khoi tao cac gio for (i = 0; i < sogio - 1; i++) { temp = new ClassBin(Min + i * value, Min + (i + 1) * value); bins.Add(temp); } temp = new ClassBin(Min + (sogio - 1) * value, Max); bins.Add(temp); //tinh gia tri cua gio int vt = 0; for (i = 0; i < bins.Count; i++) { bins[i].IndexItem = new List <int>(); for (j = 0; j < dataGridView1.Rows.Count; j++) { double.TryParse(dataGridView1.Rows[j].Cells[cotCanKiem].Value.ToString(), out item); if (item >= bins[i].Min && item < bins[i].Max) { bins[i].IndexItem.Add(j); bins[i].Sum += item; bins[i].Count++; } if (i == bins.Count - 1 && item == Max) { bins[bins.Count - 1].IndexItem.Add(j); bins[bins.Count - 1].Sum += item; bins[bins.Count - 1].Count++; } } } Form_ThongBao frm = new Form_ThongBao(); frm.soGio = sogio; frm.thuocTinh = cotCanKiem; frm.bins = bins; frm.ShowDialog(); if (frm.res) { for (i = 0; i < bins.Count; i++) { avg = bins[i].Sum / bins[i].Count; for (k = 0; k < bins[i].IndexItem.Count; k++) { dataGridView1.Rows[bins[i].IndexItem[k]].Cells[cotCanKiem].Value = string.Format("{0:#,0.####}", avg); } } } }