/
ChartHelper.cs
170 lines (140 loc) · 5.89 KB
/
ChartHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Analytics.Models;
using Skybrud.Social.Google.Analytics.Responses;
namespace Analytics
{
public static class ChartHelper
{
public static ChartData GetChartData(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");
// Initialize the data object
ChartData cd = new ChartData
{
labels = apiResults.Rows.Select(row => row.Cells[0].Value).ToArray(),
datasets = new ChartDataSet[metrics]
};
// Add a dataset for each metric
for (int metric = 0; metric < metrics; metric++)
{
// Initialize the data object
ChartDataSet ds = cd.datasets[metric] = new ChartDataSet();
ds.fillColor = GetFillColor(metric);
ds.strokeColor = GetStrokeColor(metric);
ds.highlightFill = GetHighlightFillColor(metric);
ds.highlightStroke = GetHighlightStrokeColor(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;
}
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;
}
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].Value;
var visits = Convert.ToInt32(row.Cells[1].Value);
//Create an array
var dataRow = new object[] { country, visits };
geoChartData.Add(dataRow);
}
//Return the object
return geoChartData;
}
public static string GetFillColor(int pos)
{
return pos % 2 == 0 ? "rgba(245, 112, 32, 0.5)" : "rgba(151, 187, 205, 0.5)";
}
public static string GetStrokeColor(int pos)
{
return pos % 2 == 0 ? "rgba(245, 112, 32, 0.8)" : "rgba(151, 187, 205, 0.8)";
}
public static string GetHighlightFillColor(int pos)
{
return pos % 2 == 0 ? "rgba(245, 112, 32, 0.75)" : "rgba(151, 187, 205, 0.75)";
}
public static string GetHighlightStrokeColor(int pos)
{
return pos % 2 == 0 ? "rgba(245, 112, 32, 1)" : "rgba(151, 187, 205, 1)";
}
public static string GetPointHighlightFillColor()
{
return "#fff";
}
public static string GetPointHighlightStrokeColor(int pos)
{
return pos % 2 == 0 ? "rgba(245, 112, 32, 1)" : "rgba(151, 187, 205, 1)";
}
}
}