dataSeries.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { Color3 } from "babylonjs";
  2. /**
  3. * Class used to store data to display
  4. * @see http://doc.babylonjs.com/how_to/chart3d
  5. */
  6. export class DataSeries {
  7. /** Gets or sets the label of the series */
  8. public label: string;
  9. /** Gets or sets the color associated with the series */
  10. public color: Color3;
  11. /** Gets or sets the list of dimensions (used to filter data) */
  12. public dimensions: Array<string>;
  13. /** Gets or sets the list of values (data to display) */
  14. public data: Array<any>;
  15. /**
  16. * Apply a list of filters to the data and return a list
  17. * @param filters defines the filters to apply
  18. * @returns an array containing the filtered data
  19. */
  20. public getFilteredData(filters: {[key: string]: string}): Array<any> {
  21. let filteredData = new Array<any>();
  22. this.data.forEach(element => {
  23. let isValid = false;
  24. for (var filter in filters) {
  25. if (!filters.hasOwnProperty(filter)) {
  26. continue;
  27. }
  28. var filterValue = filters[filter];
  29. isValid = (element[filter] === filterValue);
  30. if (!isValid) {
  31. break;
  32. }
  33. }
  34. if (isValid) {
  35. filteredData.push(element);
  36. }
  37. });
  38. return filteredData;
  39. }
  40. /**
  41. * Get the different values of a dimension
  42. * @param key defines the dimension name
  43. * @returns An array of values
  44. */
  45. public getDimensionValues(key: string): Array<any> {
  46. var result = new Array<any>();
  47. this.data.forEach((entry) => {
  48. var value = entry[key];
  49. if (result.indexOf(value) === -1) {
  50. result.push(value);
  51. }
  52. });
  53. return result;
  54. }
  55. /**
  56. * Create a new DataSeries containing testing values
  57. * @returns the new DataSeries
  58. */
  59. public static CreateFakeData(): DataSeries {
  60. var series = new DataSeries();
  61. series.label = "Product #1";
  62. series.color = new Color3(1.0, 0, 0);
  63. series.dimensions = ["Year", "Country"];
  64. series.data = [
  65. {
  66. "Year": 2014,
  67. "Country": "France",
  68. "value": 10
  69. },
  70. {
  71. "Year": 2014,
  72. "Country": "USA",
  73. "value": 200
  74. },
  75. {
  76. "Year": 2014,
  77. "Country": "India",
  78. "value": 400
  79. },
  80. {
  81. "Year": 2014,
  82. "Country": "UK",
  83. "value": 180
  84. },
  85. {
  86. "Year": 2014,
  87. "Country": "Germany",
  88. "value": 400
  89. },
  90. {
  91. "Year": 2014,
  92. "Country": "Australia",
  93. "value": 24
  94. },
  95. {
  96. "Year": 2014,
  97. "Country": "China",
  98. "value": 540
  99. },
  100. {
  101. "Year": 2014,
  102. "Country": "Japan",
  103. "value": 150
  104. },
  105. {
  106. "Year": 2015,
  107. "Country": "France",
  108. "value": 12
  109. },
  110. {
  111. "Year": 2015,
  112. "Country": "USA",
  113. "value": 120
  114. },
  115. {
  116. "Year": 2015,
  117. "Country": "India",
  118. "value": 480
  119. },
  120. {
  121. "Year": 2015,
  122. "Country": "UK",
  123. "value": 10
  124. },
  125. {
  126. "Year": 2015,
  127. "Country": "Germany",
  128. "value": 80
  129. },
  130. {
  131. "Year": 2015,
  132. "Country": "Australia",
  133. "value": 230
  134. },
  135. {
  136. "Year": 2015,
  137. "Country": "China",
  138. "value": 490
  139. },
  140. {
  141. "Year": 2015,
  142. "Country": "Japan",
  143. "value": 120
  144. }
  145. ];
  146. return series;
  147. }
  148. /**
  149. * Create a new DataSeries containing testing spatial values
  150. * @returns the new DataSeries
  151. */
  152. public static CreateFakeSpatialData(): DataSeries {
  153. var series = new DataSeries();
  154. series.label = "Spatial Data";
  155. series.color = new Color3(0, 1.0, 0);
  156. series.dimensions = ["Year", "Country"];
  157. series.data = [
  158. {
  159. "Year": 2014,
  160. "Country": "France",
  161. "value": 10,
  162. "latitude": 46.63728,
  163. "longitude": 2.338262
  164. },
  165. {
  166. "Year": 2014,
  167. "Country": "USA",
  168. "value": 200,
  169. "latitude": 39.83333,
  170. "longitude": -98.58334
  171. },
  172. {
  173. "Year": 2014,
  174. "Country": "India",
  175. "value": 400,
  176. "latitude": 23.40601,
  177. "longitude": 79.45809
  178. },
  179. {
  180. "Year": 2014,
  181. "Country": "UK",
  182. "value": 180,
  183. "latitude": 54.56089,
  184. "longitude": -2.212512
  185. },
  186. {
  187. "Year": 2014,
  188. "Country": "Germany",
  189. "value": 400,
  190. "latitude": 51.20247,
  191. "longitude": 10.3822
  192. },
  193. {
  194. "Year": 2014,
  195. "Country": "Australia",
  196. "value": 24,
  197. "latitude": -25.58524,
  198. "longitude": 134.5041
  199. },
  200. {
  201. "Year": 2014,
  202. "Country": "China",
  203. "value": 540,
  204. "latitude": 36.55309,
  205. "longitude": 103.9754
  206. },
  207. {
  208. "Year": 2014,
  209. "Country": "Japan",
  210. "value": 150,
  211. "latitude": 36.28165,
  212. "longitude": 139.0773
  213. },
  214. {
  215. "Year": 2015,
  216. "Country": "France",
  217. "value": 12,
  218. "latitude": 46.63728,
  219. "longitude": 2.338262
  220. },
  221. {
  222. "Year": 2015,
  223. "Country": "USA",
  224. "value": 120,
  225. "latitude": 39.83333,
  226. "longitude": -98.58334
  227. },
  228. {
  229. "Year": 2015,
  230. "Country": "India",
  231. "value": 480,
  232. "latitude": 23.40601,
  233. "longitude": 79.45809
  234. },
  235. {
  236. "Year": 2015,
  237. "Country": "UK",
  238. "value": 10,
  239. "latitude": 54.56089,
  240. "longitude": -2.212512
  241. },
  242. {
  243. "Year": 2015,
  244. "Country": "Germany",
  245. "value": 80,
  246. "latitude": 51.20247,
  247. "longitude": 10.3822
  248. },
  249. {
  250. "Year": 2015,
  251. "Country": "Australia",
  252. "value": 230,
  253. "latitude": -25.58524,
  254. "longitude": 134.5041
  255. },
  256. {
  257. "Year": 2015,
  258. "Country": "China",
  259. "value": 490,
  260. "latitude": 36.55309,
  261. "longitude": 103.9754
  262. },
  263. {
  264. "Year": 2015,
  265. "Country": "Japan",
  266. "value": 120,
  267. "latitude": 36.28165,
  268. "longitude": 139.0773
  269. }
  270. ];
  271. return series;
  272. }
  273. }