@@ -25,6 +25,31 @@ angular.module('crunchinatorApp.models').service('Category', function(Model, API
2525 return response . categories ;
2626 } ;
2727
28+ /**
29+ * This links companies and investors to the category object so that when filtering
30+ * by categories we have access to the companies and investors it contains
31+ *
32+ * @param {object } companiesById An object/hash of all companies keyed by their IDs
33+ * @param {object } investorsById An object/hash of all categories keyed by their IDs
34+ */
35+ Category . prototype . linkModels = function ( companiesById , investorsById ) {
36+ _ . each ( this . all , function ( category ) {
37+ category . companies = [ ] ;
38+ category . investors = [ ] ;
39+
40+ _ . each ( category . company_ids , function ( companyId ) {
41+ category . companies . push ( companiesById [ companyId ] ) ;
42+ } ) ;
43+
44+ _ . each ( category . investor_ids , function ( investorId ) {
45+ category . investors . push ( investorsById [ investorId ] ) ;
46+ } ) ;
47+
48+ category . companies = _ . compact ( category . companies ) ;
49+ category . investors = _ . compact ( category . investors ) ;
50+ } ) ;
51+ } ;
52+
2853 /**
2954 * Sets up a crossfilter object on all of the model's data
3055 * Sets up a list of named dimensions used in the filter list to filter datasets
@@ -35,7 +60,32 @@ angular.module('crunchinatorApp.models').service('Category', function(Model, API
3560 this . dimensions = {
3661 byId : crossCategories . dimension ( function ( category ) { return category . id ; } ) ,
3762 byCompanies : crossCategories . dimension ( function ( category ) { return category . company_ids ; } ) ,
38- byInvestors : crossCategories . dimension ( function ( category ) { return category . investor_ids ; } )
63+ byInvestors : crossCategories . dimension ( function ( category ) { return category . investor_ids ; } ) ,
64+ byTotalFunding : crossCategories . dimension ( function ( category ) {
65+ return _ . pluck ( category . companies , 'total_funding' ) ;
66+ } ) ,
67+ byFundingPerRound : crossCategories . dimension ( function ( category ) {
68+ return _ . pluck ( _ . flatten ( _ . pluck ( category . companies , 'funding_rounds' ) ) , 'raised_amount' ) ;
69+ } ) ,
70+ byMostRecentFundingRound : crossCategories . dimension ( function ( category ) {
71+ return _ . map ( category . companies , function ( company ) {
72+ return _ . max ( company . funding_rounds , function ( round ) {
73+ return round . funded_on ? d3 . time . format ( '%x' ) . parse ( round . funded_on ) : 0 ;
74+ } ) . raised_amount ;
75+ } ) ;
76+ } ) ,
77+ byStatuses : crossCategories . dimension ( function ( category ) {
78+ return _ . pluck ( category . companies , 'status' ) ;
79+ } ) ,
80+ byAcquiredOn : crossCategories . dimension ( function ( category ) {
81+ return _ . compact ( _ . pluck ( category . companies , 'acquired_on' ) ) ;
82+ } ) ,
83+ byFundingRoundMonth : crossCategories . dimension ( function ( category ) {
84+ return _ . compact ( _ . pluck ( _ . flatten ( _ . pluck ( category . companies , 'funding_rounds' ) ) , 'funded_on' ) ) ;
85+ } ) ,
86+ byFoundedOn : crossCategories . dimension ( function ( category ) {
87+ return _ . compact ( _ . pluck ( category . companies , 'founded_on' ) ) ;
88+ } )
3989 } ;
4090
4191 this . byName = crossCategories . dimension ( function ( category ) { return category . name ; } ) ;
@@ -74,10 +124,63 @@ angular.module('crunchinatorApp.models').service('Category', function(Model, API
74124 } ) ;
75125 } ,
76126 byTotalFunding : function ( ) {
77- /*var ids = _.uniq(_.flatten(_.pluck(this.filterData.ranges, 'category_ids')));
78- this.dimensions.byId.filter(function(id) {
79- return (ids.length === 0 || ids.indexOf(id) > -1);
80- });*/
127+ var self = this ;
128+ var range = this . filterData . ranges ;
129+ this . dimensions . byTotalFunding . filter ( function ( company_funding ) {
130+ return self . fallsWithinRange ( company_funding , range ) ;
131+ } ) ;
132+ } ,
133+ byFundingPerRound : function ( ) {
134+ var self = this ;
135+ var range = this . filterData . ranges ;
136+ this . dimensions . byFundingPerRound . filter ( function ( company_funding ) {
137+ return self . fallsWithinRange ( company_funding , range ) ;
138+ } ) ;
139+ } ,
140+ byMostRecentFundingRound : function ( ) {
141+ var self = this ;
142+ var range = this . filterData . mostRecentRoundRanges ;
143+ this . dimensions . byMostRecentFundingRound . filter ( function ( company_funding ) {
144+ return self . fallsWithinRange ( company_funding , range ) ;
145+ } ) ;
146+ } ,
147+ byStatus : function ( ) {
148+ var statuses = this . filterData . statuses ;
149+ this . dimensions . byStatuses . filter ( function ( company_statuses ) {
150+ if ( statuses . length === 0 ) { return true ; }
151+
152+ for ( var i = 0 ; i < company_statuses . length ; i ++ ) {
153+ var company_status = company_statuses [ i ] ;
154+ if ( _ . contains ( statuses , company_status ) ) {
155+ return true ;
156+ }
157+ }
158+ return false ;
159+ } ) ;
160+ } ,
161+ byAcquiredOn : function ( ) {
162+ var self = this ;
163+ var range = this . filterData . acquiredDate ;
164+ var format = this . format ;
165+ this . dimensions . byAcquiredOn . filter ( function ( company_acquired_on ) {
166+ return self . fallsWithinRange ( _ . map ( company_acquired_on , format . parse ) , range ) ;
167+ } ) ;
168+ } ,
169+ byFoundedOn : function ( ) {
170+ var self = this ;
171+ var range = this . filterData . foundedDate ;
172+ var format = this . format ;
173+ this . dimensions . byFoundedOn . filter ( function ( company_founded_on ) {
174+ return self . fallsWithinRange ( _ . map ( company_founded_on , format . parse ) , range ) ;
175+ } ) ;
176+ } ,
177+ byFundingRoundMonth : function ( ) {
178+ var self = this ;
179+ var range = this . filterData . fundingActivity ;
180+ var format = this . format ;
181+ this . dimensions . byFundingRoundMonth . filter ( function ( funding_round_dates ) {
182+ return self . fallsWithinRange ( _ . map ( funding_round_dates , format . parse ) , range ) ;
183+ } ) ;
81184 }
82185 } ;
83186
0 commit comments