private void AddColumn(DateTime start, HistoryRange range, decimal total, List <HistoryDataValue> bucket, Brush brush) { int century = start.Year / 100; int year = start.Year; string label = null; switch (range) { case HistoryRange.All: case HistoryRange.Year: label = start.Year.ToString(); break; case HistoryRange.Month: year = year - (100 * century); label = string.Format("{0:00}/{1:00}", start.Month, year); break; case HistoryRange.Day: label = string.Format("{0:00}", start.Day); break; } ColumnLabel clabel = new Charts.ColumnLabel(label); HistoryChartColumn column = new HistoryChartColumn() { Amount = total, Label = clabel, Values = bucket, Brush = brush }; clabel.Data = column; collection.Add(column); }
void BarChart_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DependencyObject d = e.OriginalSource as DependencyObject; if (e == null) { return; } while (d != null) { FrameworkElement f = d as FrameworkElement; if (f != null && f.DataContext != null) { HistoryChartColumn data = f.DataContext as HistoryChartColumn; if (data != null) { this.selection = data; OnSelectionChanged(); return; } ColumnLabel label = f.DataContext as ColumnLabel; if (label != null) { this.selection = label.Data; OnSelectionChanged(); return; } } d = VisualTreeHelper.GetParent(d); } }
private void ComputeLinearRegression() { // Compute linear regression int count = collection.Count; if (count == 0) { return; } // skip the first and/or last column if they don't seem to have enough data (they may have incomplete year/month). double sum = (from c in collection select c.Values.Count()).Sum(); double avg = sum / count; HistoryChartColumn first = collection[0]; HistoryChartColumn last = collection[count - 1]; double x = 0; List <Point> points = new List <Point>(); foreach (HistoryChartColumn c in collection) { if ((c == last || c == last) && c.Values.Count() < (avg / 2)) { // skip it. continue; } points.Add(new Point(x++, (double)c.Amount)); } double a, b; // y = a + b.x MathHelpers.LinearRegression(points, out a, out b); // create "Average" points that represent this line. x = 0; foreach (HistoryChartColumn c in collection) { double y = a + (b * x); c.Average = (decimal)y; x++; } }