public static DataRow[] Convert(AnalyticsDataResponse data) {
     List<DataRow> rows = new List<DataRow>();
     foreach (AnalyticsDataRow row in data.Rows) {
         DataRow temp = new DataRow();
         for (int i = 0; i < data.ColumnHeaders.Length; i++) {
             AnalyticsDataColumnHeader column = data.ColumnHeaders[i];
             string name = column.Name.Substring(3);
             string value = row.Cells[i].Value;
             switch (column.DataType) {
                 case "INTEGER":
                     temp.Cells[name] = Int32.Parse(value);
                     break;
                 case "STRING":
                     temp.Cells[name] = value;
                     break;
                 case "TIME":
                     temp.Cells[name] = Double.Parse(value);
                     break;
                 default:
                     temp.Cells[name] = value + " (" + column.DataType + ")";
                     break;
             }
         }
         rows.Add(temp);
     }
     return rows.ToArray();
 }
Пример #2
0
        public static ChartData GetLineChartData(AnalyticsDataResponse apiResults)
        {            
            // Get the amount of dimensions and metrics
            int dimensions  = apiResults.ColumnHeaders.Count(x => x.ColumnType == "DIMENSION");
            int metrics     = apiResults.ColumnHeaders.Count(x => x.ColumnType == "METRIC");

            var chartLabels = new string[]{};

            if (apiResults.ColumnHeaders.Count() == 5)
            {
                chartLabels = apiResults.Rows.Select(row => row.Cells[2] + "/" + row.Cells[1] + "/" + row.Cells[0]).ToArray();
            }
            else
            {
                chartLabels = apiResults.Rows.Select(row => row.Cells[1] + "/" + row.Cells[0]).ToArray();
            }

            // Initialize the data object
            ChartData cd = new ChartData
            {
                labels      = chartLabels,
                datasets    = new LineChartDataSet[metrics]
            };

            // Add a dataset for each metric
            for (int metric = 0; metric < metrics; metric++)
            {

                // Initialize the data object
                LineChartDataSet ds = cd.datasets[metric] = new LineChartDataSet();
                ds.fillColor        = GetFillColor(metric);
                ds.strokeColor      = GetStrokeColor(metric);
                ds.pointColor       = GetFillColor(metric);
                ds.pointStrokeColor = GetStrokeColor(metric);
                ds.pointHighlightFill = GetPointHighlightFillColor();
                ds.pointHighlightStroke = GetPointHighlightStrokeColor(metric);
                ds.data             = new object[apiResults.Rows.Length];

                for (int row = 0; row < apiResults.Rows.Length; row++)
                {
                    // Get the value
                    string value = apiResults.Rows[row].Cells[dimensions + metric].Value;

                    // Set the value with the proper type
                    if (Regex.IsMatch(value, "^[0-9]+$"))
                    {
                        ds.data[row] = Int32.Parse(value);
                    }
                    else
                    {
                        ds.data[row] = value;
                    }
                }
            }

            return cd;
            
        }
Пример #3
0
        public static dynamic GetGeoChartData(AnalyticsDataResponse apiResults)
        {
            List<object> geoChartData = new List<object>();

            var headerRow = new[] {"Country", "Visits"};
            geoChartData.Add(headerRow);

            foreach (var row in apiResults.Rows)
            {
                //Get Data out of the api results
                var country     = row.Cells[0];
                var visits      = Convert.ToInt32(row.Cells[1]);

                //Create an array
                var dataRow = new object[] { country, visits };

                geoChartData.Add(dataRow);
            }

            //Return the object
            return geoChartData;
        }
        ///// <summary>
        ///// Returns an array with exactly two elements. The first element will be data for the
        ///// previous preiod (as defined by properties <code>PreviousStartDate</code> and
        ///// <code>PreviousEndDate</code>). The second element will be data for the current period
        ///// (as defined by <code>CurrentStartDate</code> and <code>CurrentEndDate</code>).
        ///// </summary>
        ///// <param name="key"></param>
        ///// <param name="options"></param>
        internal void GetCachedDataPreviousAndCurrent(string key, out AnalyticsDataResponse previous, out AnalyticsDataResponse current, AnalyticsDataOptions options) {

            previous = GetCachedData(key + "_Previous", new AnalyticsDataOptions {
                StartDate = PreviousStartDate,
                EndDate = PreviousEndDate,
                Metrics = options.Metrics,
                Dimensions = options.Dimensions,
                Filters = options.Filters,
                Sorting = options.Sorting,
                MaxResults = options.MaxResults
            });

            current = GetCachedData(key + "_Current", new AnalyticsDataOptions {
                StartDate = CurrentStartDate,
                EndDate = CurrentEndDate,
                Metrics = options.Metrics,
                Dimensions = options.Dimensions,
                Filters = options.Filters,
                Sorting = options.Sorting,
                MaxResults = options.MaxResults
            });

        }
        /// <summary>
        /// Gets an instance of <var>AnalyticsDataResponse</var> from the specified
        /// <var>JsonObject</var>.
        /// </summary>
        /// <param name="obj">The instance of <var>JsonObject</var> to parse.</param>
        public static AnalyticsDataResponse Parse(JsonObject obj) {
            
            // Check whether "obj" is NULL
            if (obj == null) return null;

            // Check for any API errors
            if (obj.HasValue("error")) {
                JsonObject error = obj.GetObject("error");
                throw new GoogleApiException(error.GetInt("code"), error.GetString("message"));
            }

            // Initialize the response object
            AnalyticsDataResponse response = new AnalyticsDataResponse {
                Query = obj.GetObject("query", AnalyticsDataQuery.Parse),
                ColumnHeaders = obj.GetArray("columnHeaders", AnalyticsDataColumnHeader.Parse)
            };

            // Parse the rows
            JsonArray rows = obj.GetArray("rows");
            if (rows == null) {
                response.Rows = new AnalyticsDataRow[0];
            } else {
                response.Rows = new AnalyticsDataRow[rows.Length];
                for (int i = 0; i < rows.Length; i++) {
                    response.Rows[i] = new AnalyticsDataRow {
                        Index = i,
                        Cells = rows.GetArray(i).Cast<string>()
                    };
                }
            }
            
            return response;

        }