0

I would like to understand if the following is possible. If have two jsFiddle examples with the same dataset. One contains an array of objects while the other is flatten. I have also added some records that are not fully scoped.

Can the filter on the scatter plot work correctly for the Array of Objects dataset?

Data - Flatten : jsFiddle Example

var data = [
    {"_uid":  1, "business": 'Resort',   "state": {name: 'Virginia', startups: 15}},
    {"_uid":  1, "business": 'Resort',   "state": {name: 'Maryland', startups:  6}},
    {"_uid":  3, "business": 'Training', "state": {name: 'Virginia', startups:  4}},
    {"_uid":  3, "business": 'Training', "state": {name: 'Maryland', startups:  2}},
    {"_uid":  3, "business": 'Training', "state": {name: 'D.C.',     startups:  3}},
    {"_uid":  4, "business": 'Sitting'},
    {"_uid":  5, "business": 'Vet',      "state": {name: 'Virginia', startups:  4}},
    {"_uid":  6, "business": 'Parties',  "state": {}},
    {"_uid":  7, "business": 'Grooming', "state": {name: 'Virginia', startups:  4}},
    {"_uid":  7, "business": 'Grooming', "state": {name: 'D.C.',     startups:  3}},
    ];

Data - Array of Objects : jsFiddle Example

var data = [
    {"_uid":  1, "business": 'Resort',   "states": [{state: 'Virginia', startups: 15}, {state: 'Maryland', startups: 6}]},
    {"_uid":  3, "business": 'Training', "states": [{state: 'Virginia', startups: 4}, {state: 'Maryland', startups: 2}, {state: 'D.C.', startups: 3}]},
    {"_uid":  4, "business": 'Sitting'},
    {"_uid":  5, "business": 'Vet',      "states": [{state: 'Virginia', startups: 4}]},
    {"_uid":  6, "business": 'Parties',  "state": {}},
    {"_uid":  7, "business": 'Grooming', "states": [{state: 'Virginia', startups: 4}, {state: 'D.C.', startups: 3}]}
];

The data is displayed correctly for both datasets.

enter image description here

If I select the Pet Business Type - "Grooming" and brush the scatter plot vertically on 3 the filtered data is correct for the Flatten dataset, but not correct for the Array of Objects dataset.

Flatten dataset - Filtered


CORRECT - It is showing that "Training" also has a startup value of "3"

enter image description here

Array of Objects - Filtered


INCORRECT - All of the business types are showing up along with the other state "Virginia" for "Grooming".

enter image description here

I know we are asking a lot out of Crossfilter and dc.js by not flattening the data, but is this possible?

Chris Wolcott
  • 302
  • 1
  • 3
  • 9
  • So, this doesn't really work. You have 2 options: Flatten the data or use `dimension.groupAll` and handle the grouping completely on your own. This use-case is a worthwhile one and is something we should probably consider. Really all you would need would be access to the group key in the normal group API and then you actually could build a group that worked with the array of objects structure. – Ethan Jewett Feb 07 '18 at 16:16
  • @EthanJewett thank for for the response. Hopefully the example is simple enough. I will continue to think through and research your suggestion. Do you have any additional references that might help me work through this? – Chris Wolcott Feb 08 '18 at 14:39
  • @Gordon have you seen any examples or questions with regard to this? – Chris Wolcott Feb 08 '18 at 18:01
  • Not great examples that I'm aware of, sorry. We really ought to get some into the documentation. – Ethan Jewett Feb 09 '18 at 15:50
  • @EthanJewett OK, My colleague (Danny) and I are banging our heads right now trying to figure this out. Do you have any other hints or comments to help direct us? – Chris Wolcott Feb 09 '18 at 16:10
  • 1
    I don't think this is possible at all with crossfilter. Crossfilter can only filter by rows in the original data source - you can do all kinds of things with the aggregation and restructuring the results, but filtering will always be by row. So there is no way to just filter by one of the items within an array within a row. What would the filter look like? It would have to contain startups=3 and there is no way to define a startups dimension. – Gordon Feb 09 '18 at 18:52
  • 1
    Sorry, I must have just skimmed over the filter requirement. @gordon is right, as usual. You’ll have to flatten the data. This is really a lot easier than using groupAll in any case. – Ethan Jewett Feb 10 '18 at 16:19

0 Answers0