Exemple #1
0
 private void DoLoadChartData()
 {
     m_curdata = null;
     if (m_proc != null && m_data != null)
     {
         m_curdata = m_proc.LoadChartData(m_data);
     }
     if (m_structure == null)
     {
         m_structure = m_data.GetStructure(null);
     }
 }
        public override ChartData LoadChartData(ITabularDataView data)
        {
            var fmt = new BedValueFormatter(new DataFormatSettings());
            var res = new ChartData();

            res.ValueDefs           = new ChartData.ValueDef[1];
            res.ValueDefs[0]        = new ChartData.ValueDef();
            res.ValueDefs[0].Column = Column;
            res.ValueDefs[0].Label  = Texts.Get("s_count");

            var dct = new Dictionary <string, int>();
            var tbl = data.GetStructure(null);

            int colindex = tbl.Columns.GetNames().IndexOfEx(Column);

            data.LoadAllRows(new TableDataSetProperties(), (row) =>
            {
                row.ReadValue(colindex);
                fmt.ReadFrom(row);
                string curval = fmt.GetText();
                if (!dct.ContainsKey(curval))
                {
                    dct[curval] = 0;
                }
                dct[curval] += 1;
            });

            var pairs = new List <KeyValuePair <string, int> >(dct);

            pairs.SortByKey(p => p.Value);
            pairs.Reverse();
            foreach (var pair in pairs)
            {
                var item = new ChartData.DataItem();
                item.Label  = pair.Key;
                item.Values = new double[] { pair.Value };
                res.Items.Add(item);
            }

            return(res);
        }
        public override ChartData LoadChartData(ITabularDataView data)
        {
            var fmt = new BedValueFormatter(new DataFormatSettings());
            var res = new ChartData();

            res.ValueDefs = ValueDefs.ToArray();
            var tbl      = data.GetStructure(null);
            var colnames = tbl.Columns.GetNames();
            int labindex = colnames.IndexOfEx(LabelColumn);

            int[] valindexes = new int[ValueDefs.Count];
            for (int i = 0; i < ValueDefs.Count; i++)
            {
                valindexes[i] = colnames.IndexOfEx(ValueDefs[i].Column);
            }

            var pg = new TablePageProperties {
                Count = 128
            };
            var table = data.LoadTableData(pg);

            foreach (var row in table.Rows)
            {
                var item = new ChartData.DataItem();
                row.ReadValue(labindex);
                fmt.ReadFrom(row);
                item.Label  = fmt.GetText();
                item.Values = new double[ValueDefs.Count];
                for (int i = 0; i < ValueDefs.Count; i++)
                {
                    row.ReadValue(valindexes[i]);
                    fmt.ReadFrom(row);
                    Double.TryParse(fmt.GetText(), NumberStyles.Number, CultureInfo.InvariantCulture, out item.Values[i]);
                }
                res.Items.Add(item);
            }
            return(res);
        }
        public override ChartData LoadChartData(ITabularDataView data)
        {
            var fmt = new BedValueFormatter(new DataFormatSettings());
            var res = new ChartData();

            res.ValueDefs = new ChartData.ValueDef[Items.Count];
            for (int i = 0; i < Items.Count; i++)
            {
                res.ValueDefs[i]        = new ChartData.ValueDef();
                res.ValueDefs[i].Column = Items[i].Column;
                res.ValueDefs[i].Label  = Items[i].Expression + (Items[i].Expression != TimeLineExpression.Count ? " " + Items[i].Column : "");
            }

            var tbl      = data.GetStructure(null);
            var colnames = tbl.Columns.GetNames();

            int[] valindexes = new int[Items.Count];
            for (int i = 0; i < Items.Count; i++)
            {
                valindexes[i] = colnames.IndexOfEx(Items[i].Column);
            }
            int dtcolindex = colnames.IndexOfEx(DateColumn);

            if (dtcolindex < 0)
            {
                return(res);
            }

            var dct = new Dictionary <string, TimeLineGroup>();

            var smallstep = SmallStep;

            if (!UseStructuredTime)
            {
                smallstep = Step;
            }

            // go through all rows
            data.LoadAllRows(new TableDataSetProperties(), (row) =>
            {
                row.ReadValue(dtcolindex);
                fmt.ReadFrom(row);
                string curdt = fmt.GetText();
                string key   = ExtractKey(curdt, Step);
                if (key != null)
                {
                    TimeLineGroup item;
                    if (!dct.ContainsKey(key))
                    {
                        item = new TimeLineGroup {
                            Key = key
                        };
                        dct[key] = item;
                    }
                    else
                    {
                        item = dct[key];
                    }

                    var inkey = ExtractKey(curdt, smallstep);
                    if (inkey != null)
                    {
                        double[] vals;

                        if (!item.Items.ContainsKey(inkey))
                        {
                            vals = new double[Items.Count + 1];
                            for (int i = 0; i < Items.Count; i++)
                            {
                                vals[i] = Double.NaN;
                            }
                            item.Items[inkey] = new ChartData.DataItem {
                                Label = inkey, Values = vals
                            };
                        }
                        else
                        {
                            vals = item.Items[inkey].Values;
                        }
                        vals[Items.Count] += 1; // count
                        for (int i = 0; i < Items.Count; i++)
                        {
                            if (valindexes[i] < 0)
                            {
                                if (Double.IsNaN(vals[i]))
                                {
                                    vals[i] = 0;
                                }
                                vals[i] += 1; // count
                            }
                            else
                            {
                                row.ReadValue(valindexes[i]);
                                fmt.ReadFrom(row);
                                double myval;
                                if (Double.TryParse(fmt.GetText(), NumberStyles.Number, CultureInfo.InvariantCulture, out myval))
                                {
                                    AdaptValue(ref vals[i], myval, Items[i].Expression);
                                }
                            }
                        }
                    }
                }
            });

            // summarize results
            var pairs = new List <KeyValuePair <string, TimeLineGroup> >(dct);

            pairs.SortByKey(p => p.Key);
            foreach (var pair in pairs)
            {
                var item = pair.Value;

                foreach (var itm in item.Items.Values)
                {
                    for (int i = 0; i < Items.Count; i++)
                    {
                        if (Double.IsNaN(itm.Values[i]))
                        {
                            itm.Values[i] = 0;
                        }
                        if (Items[i].Expression == TimeLineExpression.Avg && itm.Values[Items.Count] > 0)
                        {
                            itm.Values[i] /= itm.Values[Items.Count];
                        }
                    }
                }

                if (UseStructuredTime)
                {
                    double[] vals = new double[Items.Count];
                    for (int i = 0; i < Items.Count; i++)
                    {
                        switch (Items[i].TimeSelector)
                        {
                        case TimeGroupSelector.Sum:
                            vals[i] = 0;
                            foreach (var val in item.Items.Values)
                            {
                                vals[i] += val.Values[i];
                            }
                            break;

                        case TimeGroupSelector.Avg:
                            vals[i] = 0;
                            foreach (var val in item.Items.Values)
                            {
                                vals[i] += val.Values[i];
                            }
                            if (item.Items.Count > 0)
                            {
                                vals[i] /= item.Items.Count;
                            }
                            break;

                        case TimeGroupSelector.Min:
                            vals[i] = Double.NaN;
                            foreach (var val in item.Items.Values)
                            {
                                if (Double.IsNaN(vals[i]) || val.Values[i] < vals[i])
                                {
                                    vals[i] = val.Values[i];
                                }
                            }
                            if (Double.IsNaN(vals[i]))
                            {
                                vals[i] = 0;
                            }
                            break;

                        case TimeGroupSelector.Max:
                            vals[i] = Double.NaN;
                            foreach (var val in item.Items.Values)
                            {
                                if (Double.IsNaN(vals[i]) || val.Values[i] > vals[i])
                                {
                                    vals[i] = val.Values[i];
                                }
                            }
                            if (Double.IsNaN(vals[i]))
                            {
                                vals[i] = 0;
                            }
                            break;

                        case TimeGroupSelector.First:
                        {
                            string key = item.Items.Keys.Min();
                            if (key != null)
                            {
                                vals[i] = item.Items[key].Values[i];
                            }
                        }
                        break;

                        case TimeGroupSelector.Last:
                        {
                            string key = item.Items.Keys.Min();
                            if (key != null)
                            {
                                vals[i] = item.Items[key].Values[i];
                            }
                        }
                        break;

                        case TimeGroupSelector.Middle:
                        {
                            var keys = new List <string>(item.Items.Keys);
                            keys.Sort();
                            if (keys.Count > 0)
                            {
                                if (keys.Count % 2 == 1)
                                {
                                    vals[i] = item.Items[keys[keys.Count / 2]].Values[i];
                                }
                                else
                                {
                                    vals[i] = (
                                        item.Items[keys[keys.Count / 2 - 1]].Values[i]
                                        + item.Items[keys[keys.Count / 2]].Values[i]
                                        ) / 2;
                                }
                            }
                        }
                        break;
                        }
                    }
                    res.Items.Add(new ChartData.DataItem {
                        Label = pair.Key, Values = vals
                    });
                }
                else
                {
                    var itm = item.Items.First().Value;
                    res.Items.Add(itm);
                }
            }
            return(res);
        }