Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        protected virtual void DoWrite(IDataQueue queue)
        {
            var fmt = new BedValueFormatter(new DataFormatSettings());

            try
            {
                int index = 0;
                while (!queue.IsEof)
                {
                    var record = queue.GetRecord();
                    record.ReadValue(0);
                    fmt.ReadFrom(record);
                    string name = OutputFileNameTemplate;
                    name = name.Replace("#FILE#", fmt.GetText());
                    name = name.Replace("#INDEX#", (index + 1).ToString());
                    string fn = Path.Combine(OutputDirectory, name);
                    record.ReadValue(1);
                    switch (record.GetFieldType())
                    {
                    case TypeStorage.String:
                        using (var tw = new StreamWriter(fn, false, m_encoding))
                        {
                            tw.Write(record.GetString());
                        }
                        break;

                    case TypeStorage.ByteArray:
                        using (var fw = new FileInfo(fn).OpenWrite())
                        {
                            var data = record.GetByteArray();
                            fw.Write(data, 0, data.Length);
                        }
                        break;
                    }
                    index++;
                }
            }
            finally
            {
                queue.CloseReading();
            }
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        /// <summary>
        /// writes content of data queue into output DBF file
        /// </summary>
        /// <param name="queue"></param>
        protected override void DoWrite(IDataQueue queue)
        {
            var dbf       = new DbfFile();
            var formatter = new BedValueFormatter(FormatSettings);

            try
            {
                if (File.Exists(GetWorkingFileName()))
                {
                    File.Delete(GetWorkingFileName());
                }
                dbf.Create(GetWorkingFileName());

                ITableStructure ts = queue.GetRowFormat;
                foreach (var col in ts.Columns)
                {
                    DbfColumn.DbfColumnType type;
                    int len = 0, scale = 0;
                    switch (col.DataType.Code)
                    {
                    case DbTypeCode.Array:
                    case DbTypeCode.Generic:
                    case DbTypeCode.Text:
                    case DbTypeCode.Xml:
                        type = DbfColumn.DbfColumnType.Memo;
                        break;

                    case DbTypeCode.Blob:
                        type = DbfColumn.DbfColumnType.Binary;
                        break;

                    case DbTypeCode.Datetime:
                        var dtype = (DbTypeDatetime)col.DataType;
                        if (dtype.SubType == DbDatetimeSubType.Date)
                        {
                            type = DbfColumn.DbfColumnType.Date;
                        }
                        else
                        {
                            type = DbfColumn.DbfColumnType.Character;
                            len  = DateTime.UtcNow.ToString("s").Length;
                        }
                        break;

                    case DbTypeCode.Float:
                        type  = DbfColumn.DbfColumnType.Number;
                        len   = 18;
                        scale = DefaultNumericScale;
                        break;

                    case DbTypeCode.Int:
                        if (AllowFoxProInteger)
                        {
                            type = DbfColumn.DbfColumnType.Integer;
                        }
                        else
                        {
                            type = DbfColumn.DbfColumnType.Number;
                            len  = 18;
                        }
                        break;

                    case DbTypeCode.Logical:
                        type = DbfColumn.DbfColumnType.Boolean;
                        break;

                    case DbTypeCode.Numeric:
                        type  = DbfColumn.DbfColumnType.Number;
                        len   = 18;
                        scale = ((DbTypeNumeric)col.DataType).Scale;
                        break;

                    case DbTypeCode.String:
                        var stype = (DbTypeString)col.DataType;
                        if (stype.IsBinary)
                        {
                            type = DbfColumn.DbfColumnType.Binary;
                        }
                        else if (stype.Length <= 254)
                        {
                            type = DbfColumn.DbfColumnType.Character;
                            len  = stype.Length;
                            if (len <= 0)
                            {
                                len = DefaultStringLength;
                            }
                        }
                        else
                        {
                            type = DbfColumn.DbfColumnType.Memo;
                        }
                        break;

                    default:
                        type = DbfColumn.DbfColumnType.Character;
                        len  = DefaultStringLength;
                        break;
                    }
                    dbf.Header.AddColumn(col.ColumnName, type, len, scale);
                }

                var orec = new DbfRecord(dbf.Header);
                while (!queue.IsEof)
                {
                    var record = queue.GetRecord();
                    orec.Clear();
                    for (int i = 0; i < ts.Columns.Count; i++)
                    {
                        record.ReadValue(i);
                        formatter.ReadFrom(record);
                        orec[i] = formatter.GetText();
                    }
                    dbf.Write(orec);
                }
            }
            finally
            {
                dbf.Close();
                queue.CloseReading();
            }
            FinalizeBulkCopy();
        }
Esempio n. 5
0
        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);
        }