如何在Crossfilter中过滤度量、组合维度并获取过滤后度量展示示例?
Got it, let's break this down step by step since you're working with Crossfilter (I can tell from the ndx1.groupAll() and dimension() calls). Here's how to filter your existing measure and combine dimensions effectively:
1. Filtering Your Existing Measure with a Single Dimension
Your current volume measure calculates the total size across the entire dataset using groupAll(). To filter this measure with a dimension, you can use the dimension's filter() method—this restricts the Crossfilter dataset to only rows matching the filter, and your volume value will automatically update to reflect the filtered subset.
Example Code:
// Assume your dataset and Crossfilter instance are set up like this const data = [ { region: "North", size: 100 }, { region: "South", size: 200 }, { region: "North", size: 150 }, { region: "East", size: 300 } ]; const ndx1 = crossfilter(data); // Your original global volume measure var volume = ndx1.groupAll().reduceSum(function(d) {return d.size;}); console.log("Global total:", volume.value()); // Output: 750 // Create your region dimension var regionDim = ndx1.dimension(function(d) { return d.region; }); // Filter the dimension to only include "North" region regionDim.filter("North"); console.log("North region total:", volume.value()); // Output: 250 (100 + 150) // Reset the filter if you want to go back to the full dataset regionDim.filterAll();
If you don't want to modify the global dataset filter state, you can instead create a grouped measure tied to the dimension, then fetch the value for your desired filter:
var regionGroup = regionDim.group().reduceSum(function(d) { return d.size; }); console.log("North region total (via group):", regionGroup.get("North").value); // Output: 250
2. Combining Multiple Dimensions (Filtering with Multiple Criteria)
To filter your measure using multiple dimensions at once, simply apply filters to each relevant dimension—Crossfilter automatically uses the intersection of all filters to narrow down the dataset.
Example Code:
// Add a second dimension (e.g., product category) var categoryDim = ndx1.dimension(function(d) { return d.category; }); // Apply filters to both dimensions regionDim.filter("North"); categoryDim.filter("Small"); // Now volume.value() returns the total size for North region + Small category console.log("North + Small total:", volume.value());
If you want to create a combined dimension (grouping by multiple fields at once), return an array as the dimension key. This lets you calculate metrics for unique combinations of values:
// Create a combined dimension for region + category var combinedDim = ndx1.dimension(function(d) { return [d.region, d.category]; }); // Create a grouped measure for the combined dimension var combinedGroup = combinedDim.group().reduceSum(function(d) { return d.size; }); // Get the total for a specific combination (North + Large) console.log("North + Large total:", combinedGroup.get(["North", "Large"]).value);
3. Full Working Example
Here's a complete example tying it all together:
// Sample dataset with multiple fields const data = [ { region: "North", category: "Small", size: 100 }, { region: "South", category: "Large", size: 200 }, { region: "North", category: "Large", size: 150 }, { region: "East", category: "Small", size: 300 }, { region: "North", category: "Small", size: 50 } ]; const ndx1 = crossfilter(data); // Global volume measure const globalVolume = ndx1.groupAll().reduceSum(d => d.size); console.log("Global total:", globalVolume.value()); // 800 // Filter with single dimension const regionDim = ndx1.dimension(d => d.region); regionDim.filter("North"); console.log("North region total:", globalVolume.value()); // 300 // Add second dimension filter const categoryDim = ndx1.dimension(d => d.category); categoryDim.filter("Small"); console.log("North + Small total:", globalVolume.value()); // 150 // Reset filters regionDim.filterAll(); categoryDim.filterAll(); // Combined dimension grouping const combinedDim = ndx1.dimension(d => [d.region, d.category]); const combinedGroup = combinedDim.group().reduceSum(d => d.size); console.log("North + Large total:", combinedGroup.get(["North", "Large"]).value); // 150
Hope these examples clarify how to filter your measure and work with combined dimensions! Let me know if you need further tweaks for your specific use case.
内容的提问来源于stack exchange,提问作者BVB1392

