score:6
v.Speed is always NaN
in your current example. Because +x.Type
attempts to convert a string like "Apple" into a number and fails. If you just want to count, then add or subtract 1
in your reducer, rather than v.Speed
. Then you need to update your sel_stack
code and chart code to handle this change, of course.
Here's a working example for the 2 types in your data. You'll have to update it to handle arbitrary numbers of types, probably by building an array of all your types up front and then looping through it to add stacks to the chart: http://codepen.io/anon/pen/GjjyOv?editors=1010
var data = [ {"Name":"Abby","Type":"Apple"}, {"Name":"Abby","Type":"Banana"}, {"Name":"Bob","Type":"Apple"} ]
var ndx = crossfilter(data)
var xdim = ndx.dimension(function (d) {return d.Name;});
In the reducer, just add and subtract 1 to count:
var ydim = xdim.group().reduce(
function(p, v) {
p[v.Type] = (p[v.Type] || 0) + 1;
return p;},
function(p, v) {
p[v.Type] = (p[v.Type] || 0) - 1;
return p;},
function() {
return {};
});
sel_stack no longer takes a number, but a key:
function sel_stack(valueKey) {
return function(d) {
return d.value[valueKey];
};}
var chart = dc.barChart("#chart");
Here we hard-code the stack key, for the purpose of the example:
chart
.x(d3.scale.ordinal().domain(xdim))
.dimension(xdim)
.group(ydim, "Apple", sel_stack('Apple'))
.xUnits(dc.units.ordinal);
Again, the other hard-coded stack key. You'll need to recreate the loop after creating some sort of data structure that holds all of your stack values.
//for(var i = 2; i<6; ++i)
chart.stack(ydim, 'Banana', sel_stack('Banana'));
chart.render();
Source: stackoverflow.com
Related Query
- How to create a stacked bar chart using dc.js?
- d3/dc.js - How to create a stacked bar chart while telling crossfilter to treat elements in an array as separate records?
- How to create vertically grouped bar chart in d3.js using json data?
- how to create a stacked bar chart with images in d3
- How to create a % difference arrow line with value in a bar chart using D3.js
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- How to use log scale to show proper stacked bar chart using d3 JS?
- How to create a d3 stacked bar chart that only iterates through columns 1 and 2 of data file?
- How to give a Bar in a Stacked Bar Chart a minimal height using D3?
- Need help using d3.js to create Stacked Bar Chart
- Create Stacked Bar chart for Timeseries X axis using C3.js
- How do you create a stacked bar chart in d3 when each individual segment is an object?
- How to create multi color vertical bar chart in d3.js using specific json data?
- how to create labels for data in donut chart using d3.js
- How to create stacked row chart with one row with dc.js?
- how to create multiline chart using dc.js
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- How to create Stacked Line Chart D3, Multiple Y Axis and common X Axis
- how to create pie chart using dc.js
- Stacked Bar Chart using d3.js stack layout; the bar sections not always in correct place
- How to make grouped stacked bar chart in d3js?
- d3.js create stacked bar chart from values in object
- How do I create a multiline chart using d3js with json formatted for nvd3?
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- How to show values stacked bar chart utilizing dc.js and d3.js?
- Create Grouped Stacked Bar Chart
- How to create a stacked barchart using d3?
- Create a D3 Bar Chart Using a Object
- Using dc.js on nested JSON to create bar chart
- Using JSON in d3v4 stacked bar chart
More Query from same tag
- d3.js set attributes in v4
- How to dynamically update this D3JS force diagram
- what means a type like SomeType<T, U, V> in typescript?
- D3: Mouseover to display data in top right corner
- Two labels on one node in a D3 force layout
- Add text/label onto links in D3 force directed graph
- if statement to slide between data sets d3.js
- populate tooltip value in d3 using attributes / scale
- Angular 6 when component is routed to the d3/event-drops data points don't display in Safari
- Javascript library d3 call function
- How to use d3.symbols with an ordinal scale
- SVG element loses event handlers if moved around the DOM
- Labels on a d3 line chart
- Starting a d3 axis timescale from zero
- d3 svg charts on ie8 (Windows XP)
- Draw a circle on gisMap
- D3 - mouse event handling on a map
- How does d3.js allow us to use second parameter of function as index of dataset?
- how to access data of a d3 SVG Element
- angular d3 not rendering uniquely with *ngFor
- D3: Circle Points don't update when changing slider
- Selecting JavaScript drowndown option causes D3 treemap to change
- Plot polygon using D3
- D3.js - How to highlight the biggest and smallest (minimum and max) columns in a bar chart?
- d3 with Highchart Jasper Export Issue using Custom Visualization Component(CVC)
- Selecting d3 data subset based on column
- How to create a stacked bar chart using dc.js?
- stacked bar chart has white space in between, y0 starting at wrong spot?
- Creating Pie Chart Dynamically with nvd3 and AngualrJs
- topojson / D3 / Map with longitude + latitude