public DatasetJsonObj ParseAllData(IDataSetStore store, List <DataCriteria> Criterias, LayoutObj layObj, IDataStructureObject kf, ISet <ICodelistObject> codelists)
        {
            if (PageNumber > 0)
            {
                return(ParseAllDataPage(1));
            }

            string DominantFreq = CalculateDominantFrequency(Criterias, kf, codelists);


            CurrentPageIndex = 1;
            int            NObservationForPage = WebClientSettings.Instance.NObservationForPage;
            DatasetJsonObj dataset             = new DatasetJsonObj()
            {
                series = new Dictionary <string, Dictionary <string, string> >()
            };
            List <string> sort = new List <string>();

            sort.AddRange(layObj.axis_y);
            sort.AddRange(layObj.axis_x);
            store.SetSort(sort);
            store.SetCriteria(Criterias);
            IDataReader datareader = store.CreateDataReader(false);

            try
            {
                int ActualRecordRegistred = 0;
                PageNumber = 0;
                JavaScriptSerializer ser = new JavaScriptSerializer();
                ser.MaxJsonLength = int.MaxValue;
                while (datareader.Read())
                {
                    List <string> Colonne = new List <string>();
                    layObj.axis_x.ForEach(axisX => Colonne.Add(GetFromReader(datareader, axisX, DominantFreq, kf.TimeDimension.Id)));

                    List <string> Righe = new List <string>();
                    layObj.axis_y.ForEach(axisY => Righe.Add(GetFromReader(datareader, axisY, DominantFreq, kf.TimeDimension.Id)));

                    string OBSVal = (string)datareader[kf.PrimaryMeasure.Id];

                    string serieString = string.Join("+", Righe);

                    if (!dataset.series.ContainsKey(serieString))
                    {
                        if (ActualRecordRegistred >= NObservationForPage)
                        {
                            //NormalizeDataset(dataset, layObj.axis_x, kf, codelists);
                            FileInfo fi = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                            File.WriteAllText(fi.FullName, ser.Serialize(dataset));

                            ActualRecordRegistred = 0;
                            PageNumber++;
                            dataset = null;
                            dataset = new DatasetJsonObj()
                            {
                                series = new Dictionary <string, Dictionary <string, string> >()
                            };
                        }

                        //dataset.series[serieString] = AllPossibleValues(layObj.axis_x, kf, codelists);
                        dataset.series[serieString] = new Dictionary <string, string>();
                    }
                    dataset.series[serieString][string.Join("+", Colonne)] = OBSVal;
                    ActualRecordRegistred++;
                }

                FileInfo fiLastPage = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                File.WriteAllText(fiLastPage.FullName, ser.Serialize(dataset));
                PageNumber++;
                if (PageNumber == 1)
                {
                    return(dataset);
                }

                return(ParseAllDataPage(1));
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Commit();
            }
        }
Example #2
0
        public DatasetJsonObj ParseAllData(IDataSetStore store, List<DataCriteria> Criterias, LayoutObj layObj, IDataStructureObject kf, ISet<ICodelistObject> codelists)
        {
            if (PageNumber > 0)
                return ParseAllDataPage(1);

            string DominantFreq = CalculateDominantFrequency(Criterias, kf, codelists);

            CurrentPageIndex = 1;
            int NObservationForPage = WebClientSettings.Instance.NObservationForPage;
            DatasetJsonObj dataset = new DatasetJsonObj() { series = new Dictionary<string, Dictionary<string, string>>() };
            List<string> sort = new List<string>();
            sort.AddRange(layObj.axis_y);
            sort.AddRange(layObj.axis_x);
            store.SetSort(sort);
            store.SetCriteria(Criterias);
            IDataReader datareader = store.CreateDataReader(false);
            try
            {
                int ActualRecordRegistred = 0;
                PageNumber = 0;
                JavaScriptSerializer ser = new JavaScriptSerializer();
                ser.MaxJsonLength = int.MaxValue;
                while (datareader.Read())
                {
                    List<string> Colonne = new List<string>();
                    layObj.axis_x.ForEach(axisX => Colonne.Add(GetFromReader(datareader, axisX, DominantFreq, kf.TimeDimension.Id)));

                    List<string> Righe = new List<string>();
                    layObj.axis_y.ForEach(axisY => Righe.Add(GetFromReader(datareader, axisY, DominantFreq, kf.TimeDimension.Id)));

                    string OBSVal = (string)datareader[kf.PrimaryMeasure.Id];

                    string serieString = string.Join("+", Righe);

                    if (!dataset.series.ContainsKey(serieString))
                    {
                        if (ActualRecordRegistred >= NObservationForPage)
                        {
                            //NormalizeDataset(dataset, layObj.axis_x, kf, codelists);
                            FileInfo fi = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                            File.WriteAllText(fi.FullName, ser.Serialize(dataset));

                            ActualRecordRegistred = 0;
                            PageNumber++;
                            dataset = null;
                            dataset = new DatasetJsonObj() { series = new Dictionary<string, Dictionary<string, string>>() };
                        }

                        //dataset.series[serieString] = AllPossibleValues(layObj.axis_x, kf, codelists);
                        dataset.series[serieString] = new Dictionary<string, string>();
                    }
                    dataset.series[serieString][string.Join("+", Colonne)] = OBSVal;
                    ActualRecordRegistred++;

                }

                FileInfo fiLastPage = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                File.WriteAllText(fiLastPage.FullName, ser.Serialize(dataset));
                PageNumber++;
                if (PageNumber == 1)
                    return dataset;

                return ParseAllDataPage(1);

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Commit();
            }
        }
        private List <serieType> BuildChart(
            IDataSetStore store,
            IDataStructureObject kf,
            Dictionary <string, ICodelistObject> codelists)
        {
            List <string> sort = new List <string>();

            foreach (string col in store.GetAllColumns())
            {
                if (col != kf.TimeDimension.Id && col != kf.PrimaryMeasure.Id)
                {
                    sort.Add(col);
                }
            }
            store.SetSort(sort);

            // Dimensione sull'asse X
            string XConcept =
                (!string.IsNullOrEmpty(ChartObj.DimensionAxe)) ?
                ChartObj.DimensionAxe :
                kf.TimeDimension.Id;
            // Codici sull'asse X
            Dictionary <string, int> XPosition = new Dictionary <string, int>();

            if (codelists.ContainsKey(XConcept))
            {
                ICodelistObject CCodes = codelists[XConcept];
                for (int i = 0; i < CCodes.Items.Count; i++)
                {
                    XPosition.Add(CCodes.Items[i].Id, i);
                }
            }

            #region Dimensione usata per la descrizione
            string DescConcept  = string.Empty;
            bool   single_serie = true;
            foreach (var obj in ChartObj.Criteria)
            {
                if (//obj.Key != kf.TimeDimension.Id &&
                    obj.Key != kf.FrequencyDimension.Id)
                {
                    DescConcept = obj.Key;
                    if (obj.Value.Count > 1 && obj.Key != XConcept)
                    {
                        single_serie = false;
                        DescConcept  = obj.Key;
                        break;
                    }
                }
            }

            bool inLegend = !single_serie;
            inLegend = inLegend || (single_serie && ChartObj.ObsValue.Count > 1);

            #endregion

            List <serieType> series   = new List <serieType>();
            List <serieType> series_s = new List <serieType>();
            var v = new Dictionary <string, decimal>();

            //IDataReader datareader = store.CreateDataReader(true);
            IDataReader datareader = store.CreateDataReader(false);
            while (datareader.Read())
            {
                decimal obs = 0;
                object  vt  = null;
                object  vc  = null;

                var obs_val   = datareader[kf.PrimaryMeasure.Id];
                var xcode     = (string)datareader[XConcept];
                var xCodeName = (string)TextTypeHelper.GetText(codelists[XConcept].GetCodeById(xcode).Names, this.ChartObj.Configuration.Locale);

                //fabio 12/08/2015
                //string customKeyCode = (ChartObj.CustomKey != null) ? datareader[ChartObj.CustomKey].ToString() : string.Empty;
                string customKeyCode = (ChartObj.CustomKey != null && ChartObj.CustomKey != "") ? datareader[ChartObj.CustomKey].ToString() : string.Empty;
                var    customSerie   = (ChartObj.CustomChartType != null) ?
                                       (!string.IsNullOrEmpty(customKeyCode)) ?
                                       (from c in ChartObj.CustomChartType where c.code == customKeyCode select c).FirstOrDefault() :
                                       null : null;

                string serieKey  = string.Empty;
                string serieName = string.Empty;

                bool is_obs_value = false;
                try
                {
                    obs          = Convert.ToDecimal(obs_val.ToString(), cFrom);
                    is_obs_value = true;
                    obs_val      = Math.Round(obs, 1);
                }
                catch
                {
                    //fabio 12/08/2015 aggiunta
                    obs_val      = null;
                    is_obs_value = true;

                    //fabio 12/08/2015 eliminata
                    //is_obs_value = false;
                }

                // if not time serie no varation
                if (XConcept == kf.TimeDimension.Id)
                {
                    #region Calcolo variazioni

                    if (is_obs_value)
                    {
                        var time_p = xcode;
                        int anno   = 0;
                        int period = 0;

                        bool _errTimePeriod = false;

                        bool _annual = !((string)time_p).Contains("-");
                        bool _quater = false;
                        bool _seme   = false;

                        #region ESTRAGGO ANNO E PERIOD
                        if (_annual)
                        {
                            _errTimePeriod = !(int.TryParse(((string)time_p), out anno));
                        }
                        else
                        {
                            _errTimePeriod = !(int.TryParse(((string)time_p).Split('-')[0], out anno));

                            string _p = ((string)time_p).Split('-')[1];

                            if (_quater = _p.StartsWith("Q"))
                            {
                                _p = _p.Substring(1);
                            }
                            if (_seme = _p.StartsWith("S"))
                            {
                                _p = _p.Substring(1);
                            }

                            _errTimePeriod = !(int.TryParse(_p, out period));
                        }
                        #endregion

                        if (!_errTimePeriod)
                        {
                            string serieKeyStr = string.Empty;
                            string _sep        = string.Empty;
                            foreach (var dim in kf.DimensionList.Dimensions)
                            {
                                serieKeyStr += ((dim.Id != XConcept) ? _sep + datareader[dim.Id] : string.Empty);
                                _sep         = "+";
                                if (dim.Id == DescConcept)
                                {
                                    serieName =
                                        TextTypeHelper.GetText(
                                            codelists[DescConcept].GetCodeById(datareader[dim.Id].ToString()).Names,
                                            this.ChartObj.Configuration.Locale);
                                }
                            }
                            serieKey = serieKeyStr;

                            string vi_k = string.Empty;
                            string vf_k = string.Empty;

                            // Calcolo variazione congiunturale

                            vf_k = serieKeyStr + anno + "_" + (period);
                            if (!_annual)
                            {
                                if (period == 1)
                                {
                                    if (_seme)
                                    {
                                        vi_k = serieKeyStr + (anno - 1) + "_2";
                                    }
                                    else if (_quater)
                                    {
                                        vi_k = serieKeyStr + (anno - 1) + "_4";
                                    }
                                    else
                                    {
                                        vi_k = serieKeyStr + (anno - 1) + "_12";
                                    }
                                }
                                else
                                {
                                    vi_k = serieKeyStr + anno + "_" + (period - 1);
                                }
                            }
                            else
                            {
                                vi_k = serieKeyStr + (anno - 1) + "_" + (period);
                            }

                            var vi = (v.ContainsKey(vi_k.ToString())) ? (object)v[vi_k] : null;

                            try
                            {
                                decimal _vi;
                                // non specificare il cFrom nella conversione
                                // poichè vi è il valore gia convertito
                                _vi = Convert.ToDecimal(vi.ToString());
                                //_vi = Convert.ToDecimal(vi.ToString(), cFrom);
                                if (_vi == 0)
                                {
                                    vc = null;
                                }
                                else
                                {
                                    vc = Math.Round((((obs - _vi) / _vi) * 100), 1);
                                }
                            }
                            catch {
                                vc = null;
                            }

                            // Calcolo variazione tendenziale
                            vi_k = serieKeyStr + (anno - 1) + "_" + (period);
                            vf_k = serieKeyStr + anno + "_" + (period);
                            vi   = (v.ContainsKey(vi_k.ToString())) ? (object)v[vi_k] : null;

                            try
                            {
                                decimal _vi;
                                _vi = Convert.ToDecimal(vi.ToString());
                                //_vi = Convert.ToDecimal(vi.ToString(), cFrom);
                                if (_vi == 0)
                                {
                                    vc = null;
                                }
                                else
                                {
                                    vt = Math.Round((((obs - _vi) / _vi) * 100), 1);
                                }
                            }
                            catch
                            {
                                vt = null;
                            }

                            v.Add(vf_k, obs);
                        }
                    }
                    #endregion
                }
                else
                {
                    // Retrive unique key and label serie
                    string serieKeyStr = string.Empty;
                    string _sep        = string.Empty;
                    foreach (var dim in kf.DimensionList.Dimensions)
                    {
                        serieKeyStr += ((dim.Id != XConcept) ? _sep + datareader[dim.Id] : string.Empty);
                        _sep         = "+";
                        if (dim.Id == DescConcept)
                        {
                            serieName =
                                TextTypeHelper.GetText(
                                    codelists[DescConcept].GetCodeById(datareader[dim.Id].ToString()).Names,
                                    this.ChartObj.Configuration.Locale);
                        }
                    }
                    serieKey = serieKeyStr;
                }

                #region Primary Serie
                object primary_obs =
                    (ChartObj.ObsValue[0] == "v") ? (is_obs_value) ? obs_val : null :
                    (ChartObj.ObsValue[0] == "vt") ? vt :
                    (ChartObj.ObsValue[0] == "vc") ? vc : null;

                bool      isNew    = false;
                serieType newSerie = null;
                newSerie = series.Find(s => s.serieKey == ChartObj.ObsValue[0] + "_" + serieKey);//SerieName);

                if (newSerie == null)
                {
                    string _type = (customSerie != null) ? customSerie.chartType.ToString() : ChartObj.ChartType;

                    string _name = (ChartObj.ObsValue[0] == "vt") ? Messages.label_varTrend + "% " :
                                   (ChartObj.ObsValue[0] == "vc") ? Messages.label_varCyclical + "% " :
                                   (single_serie)? Messages.label_varValue:    string.Empty;

                    if (!single_serie)
                    {
                        _name += ((customSerie != null) ? customSerie.title.ToString() : serieName);
                    }

                    isNew    = true;
                    newSerie = new serieType()
                    {
                        name          = _name,
                        serieKey      = ChartObj.ObsValue[0] + "_" + serieKey,
                        showInLegend  = inLegend,
                        type          = _type,
                        dataPoints    = new List <dataPointType>(),
                        axisYType     = "primary",
                        lineThickness = 1f,
                        markerType    = "circle", //"circle", "square", "cross", "none"
                        markerSize    = (_type == "bubble" || _type == "scatter") ? 10f : 1f,
                    };
                }

                newSerie.dataPoints.Add(new dataPointType()
                {
                    label      = xCodeName,
                    legendText = xCodeName,
                    y          = primary_obs,
                    x          = XPosition[xcode]
                });

                if (isNew)
                {
                    series.Add(newSerie);
                }
                #endregion

                // if not time serie no secondary
                if (XConcept == kf.TimeDimension.Id)
                {
                    #region Seconday serie
                    if (ChartObj.ObsValue.Count > 1)
                    {
                        string _type = (customSerie != null) ? customSerie.chartType.ToString() : ChartObj.ChartType;
                        string _name = (ChartObj.ObsValue[1] == "vt") ? Messages.label_varTrend + "% " :
                                       (ChartObj.ObsValue[1] == "vc") ? Messages.label_varCyclical + "% " : Messages.label_varValue + " ";

                        if (!single_serie)
                        {
                            _name += ((customSerie != null) ? customSerie.title.ToString() : serieName);
                        }
                        object secondary_obs =
                            (ChartObj.ObsValue[1] == "v") ? (is_obs_value) ? obs_val : null :
                            (ChartObj.ObsValue[1] == "vt") ? vt :
                            (ChartObj.ObsValue[1] == "vc") ? vc : null;

                        bool      isNew_s    = false;
                        serieType newSerie_s = null;
                        newSerie_s = series_s.Find(s => s.serieKey == ChartObj.ObsValue[1] + "_" + serieKey);//SerieName);
                        if (newSerie_s == null)
                        {
                            isNew_s    = true;
                            newSerie_s = new serieType()
                            {
                                name          = _name,
                                serieKey      = ChartObj.ObsValue[1] + "_" + serieKey,
                                showInLegend  = inLegend,
                                type          = _type,
                                dataPoints    = new List <dataPointType>(),
                                axisYType     = "secondary",
                                lineThickness = 1f,
                                markerType    = "circle", //"circle", "square", "cross", "none"
                                markerSize    = (_type == "bubble" || _type == "scatter") ? 10f : 1f,
                            };
                        }
                        newSerie_s.dataPoints.Add(new dataPointType()
                        {
                            label      = xCodeName,
                            legendText = xCodeName,
                            y          = secondary_obs,
                            x          = XPosition[xcode]
                        });

                        if (isNew_s)
                        {
                            series_s.Add(newSerie_s);
                        }
                    }
                    #endregion
                }
            }
            series.AddRange(series_s);

            #region Series
            foreach (var serie in series)
            {
                var sortedCodes = serie.dataPoints.OrderBy <dataPointType, int>(o => int.Parse(o.x.ToString())).ToArray();
                serie.dataPoints.Clear();
                serie.dataPoints.AddRange(sortedCodes);

                for (int i = 0; i < serie.dataPoints.Count; i++)
                {
                    serie.dataPoints[i].x = i;
                }
            }
            #endregion

            return(series);
        }