コード例 #1
0
ファイル: PlotViewModel.cs プロジェクト: TNOCS/csTouch
        private List<SortedDictionary<double, double>> GetDataSet(DataSet ds)
        {

            var res = new SortedDictionary<double, List<double>>();

            if (ds.Data!=null && Enumerable.Any(ds.Data))
            foreach (var p in ds.Data)
            {
                double day = DateTimeAxis.ToDouble(p.Key);
                double groupDay = day;

                switch (ds.Grouping)
                {            
                    case GroupingOptions.none:
                        groupDay = DateTimeAxis.ToDouble(p.Key);
                        break;
                    case GroupingOptions.minutes:
                        groupDay = DateTimeAxis.ToDouble(p.Key.Date.AddHours(p.Key.Hour).AddMinutes(p.Key.Minute));
                        break;
                    case GroupingOptions.quarterly:
                        var min = 0;
                        if (p.Key.Minute < 30) min = 15;
                        if (p.Key.Minute < 45) min = 30;
                        if (p.Key.Minute < 60) min = 45;
                        groupDay = DateTimeAxis.ToDouble(p.Key.Date.AddHours(p.Key.Hour).AddMinutes(min));
                        break;
                    case GroupingOptions.hourly:
                        groupDay = DateTimeAxis.ToDouble(p.Key.Date.AddHours(p.Key.Hour));
                        break;
                    case GroupingOptions.daily:
                        groupDay = DateTimeAxis.ToDouble(p.Key.Date);
                        break;
                    case GroupingOptions.weekly:
                        groupDay = DateTimeAxis.ToDouble(p.Key.GetDateTimeForDayOfWeek(DayOfWeek.Monday));
                        break;
                    case GroupingOptions.monthly:                        
                        groupDay = DateTimeAxis.ToDouble(p.Key.GetFirstDayOfTheMonth());
                        break;
                    case GroupingOptions.yearly:
                        groupDay = DateTimeAxis.ToDouble(p.Key.GetFirstDayOfTheYear());
                        break;
                }
                
                if (!res.ContainsKey(groupDay)) res[groupDay] = new List<double>();
                res[groupDay].Add(p.Value);
            }
            else if (ds.DData != null)
            {
                return new List<SortedDictionary<double, double>>() { ds.DData };
            }
            SortedDictionary<double, double> r = new SortedDictionary<double, double>();
            switch (ds.GroupingOperation)
            {
                case GroupingOperations.count:
                    r = (from n in res select new KeyValuePair<double, double>(n.Key, n.Value.Count)).ToSortedDictionary(k => k.Key, k => k.Value);                    
                    break;
                case GroupingOperations.maximum:
                    r = (from n in res select new KeyValuePair<double, double>(n.Key, n.Value.Max())).ToSortedDictionary(k => k.Key, k => k.Value);
                    break;
                case GroupingOperations.minimum:
                    r = (from n in res select new KeyValuePair<double, double>(n.Key, n.Value.Min())).ToSortedDictionary(k => k.Key, k => k.Value);
                    break;
                case GroupingOperations.average:
                    r = (from n in res select new KeyValuePair<double, double>(n.Key, n.Value.Average())).ToSortedDictionary(k => k.Key, k => k.Value);
                    break;
                case GroupingOperations.total:
                    r = (from n in res select new KeyValuePair<double, double>(n.Key, n.Value.Sum())).ToSortedDictionary(k => k.Key, k => k.Value);
                    break;                
            }
            SortedDictionary<double, double> f = new SortedDictionary<double, double>();
            switch (ds.FunctionOperation)
            {
                case FunctionOperations.none:
                    f = r;
                    break;
                case FunctionOperations.reduction:
                    {
                        var m = new MathFunctions();
                        var temp = (from n in r select new Point(n.Key, n.Value)).ToList();
                        var euclistx = new List<double>();
                        var euclisty = new List<double>();
                        for (int i = 0; i < temp.Count - 1; i++)
                        {
                            euclistx.Add(Math.Sqrt((temp[i].X - temp[i + 1].X)*(temp[i].X - temp[i + 1].X)));
                            euclisty.Add(Math.Sqrt((temp[i].Y - temp[i + 1].Y)*(temp[i].Y - temp[i + 1].Y)));
                        }
                        var thresholdx = Convert.ToDouble(euclistx.Average());
                        var thresholdy = Convert.ToDouble(euclisty.Average());
                        var dp = m.DouglasPeuckerReduction(temp, 10*thresholdx);
                        f = (from n in dp select new KeyValuePair<double, double>(n.X, n.Y)).ToSortedDictionary(k => k.Key,
                                                                                                          k => k.Value);
                    }
                    break;
                case FunctionOperations.auto:
                    {
                        if (ds.ReducedData == null)
                            ds.ReducedData = mf.CalculateReduction(r, 0.95); ;

                        f = ds.ReducedData;


                        if (ds.AggregatedData == null)
                        {
                            ds.AggregatedData = CalculateAggregate(f);
                        }
                        var actualsize = Math.Max(_view.Plot.ActualWidth - 60, 20);
                        var v = model.Axes.FirstOrDefault(k => k.Position == AxisPosition.Bottom);
                        var points = f.Where(k => k.Key > v.Minimum && k.Key < v.Maximum).Count();

                        if (points > actualsize / 2)
                        {
                            return ds.AggregatedData;
                        }
                    }
                    break;
                case FunctionOperations.periodic:
                    {
                        var first = r.First();
                        var test = DateTimeAxis.ToDouble(new DateTime());
                        var period = DateTimeAxis.ToDouble(new DateTime() + ds.Periodic)-test;
                        var idx = 1.0;
                        var resultlist = new List<SortedDictionary<double, double>>();
                        foreach (var val in r)
                        {
                           
                            if (val.Key - first.Key > period*idx)
                            {
                                resultlist.Add(f.ToSortedDictionary(k=>k.Key,k=>k.Value));
                                while (val.Key - first.Key > period*idx)
                                    idx++;
                                f.Clear();
                            }
                            f.Add(first.Key + ((val.Key - first.Key) - period*(idx-1)),val.Value);
                        }
                        resultlist.Add(f.ToSortedDictionary(k => k.Key, k => k.Value));
                        return resultlist;
                    }
                // FIXME TODO: Unreachable code
                    // break;
            }
            return new List<SortedDictionary<double, double>>() { f };

        }