score:1
Accepted answer
Assuming that your propertyItem
does exist inside dataset
, the solution is simple: use bracket notation. For instance:
.attr("height", function(d) {
return d[propertyItem] * 4;
});
The reason is that propertyItem
inside your function is just a string. If you do...
d.propertyItem
... instead of looking for the same property of your string, the browser will look for a property literally named propertyItem
. Let's show it:
var obj = {
foo: "this is what I want!",
propertyItem: "this is not what I want..."
};
var propertyItem = "foo";
console.log("obj.propertyItem returns: " + obj.propertyItem)
console.log("obj[propertyItem] returns: " + obj[propertyItem])
So, as I suppose there is no property literally named propertyItem
in your dataset
array, it returns undefined
.
Here is a demo with some dummy data and your code, but using bracket notation:
var dataset = [{
foo: 80,
bar: 10
}, {
foo: 30,
bar: 12
}, {
foo: 70,
bar: 11
}, {
foo: 42,
bar: 17
}];
var w = 400,
h = 200,
barPadding = 10;
var barChart = function(propertyItem) {
if (propertyItem == null) {
propertyItem = "ROBBERY";
}
var svg = d3.select("body").append("svg");
svg.attr("width", w).attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("height", function(d) {
return d[propertyItem] * 4;
})
.attr("width", w / dataset.length - barPadding)
.attr("y", function(d) {
return h - d[propertyItem] * 4;
})
}
barChart("foo")
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>
Source: stackoverflow.com
Related Query
- Add arbitrary amount of tags per object property in a data enter chain
- Passing an object property to D3 'enter()'
- Passing Object function into data() in d3
- D3 chart can't update -- enter and exit property of selection both empty
- D3 v4 returning error for scaleTime when passing in Date object
- D3: passing object in style method is not working
- D3 nested select and sibling from json property of object
- d3.js - Object doesn't support property or method 'map'
- Javascript JQuery : Object doesn't support property or method 'toLowerCase'
- D3 Object Constancy not working with key function. Enter and Update contain all elements every time
- How to filter JavaScript object based on property condition?
- Filter nested array of objects by object property in d3.js
- Changing object variables to abbreviations before passing it to a network graph
- How to get a single property of all children in a nested object using javascript?
- SCRIPT438: Object doesn't support property or method 'attr'
- Question re: TypeScript object property pointers passed into methods
- Getting Undefined property from Object created with d3.js
- Parse csv as object with extra property
- Binding data to existing number of elements based on object property
- Split a string in object property and make new dataset
- Javascript returns undefined when indexing into an array using a property of an object
- Object property is null, but the value entered is not. How do i insert the value properly?
- get object from array based on property
- Passing an object containing an array to D3
- Passing json object from grails controller to a gsp rendering a d3 chart?
- Passing a JSON object from visualwebgui to a htmlbox html page?
- D3JS passing in a JSON object returns an error
- previously logged d3 object doesn't show its __data__ property after removing __data__ with datum(num)
- D3: Passing object array data to a barchart on mouseover
- D3.js: Accessing Object Property inside Array for Creating Line Graph
More Query from same tag
- What's preserving local offset in this d3.drag example?
- Getting d3.events as null in angular2 typescript code
- How to show transition in d3 on one path after another?
- D3.js moving a line and a circle after a button press
- nvd3 repeating months on x-axis
- accessing D3 dynamical json object in Rails view
- d3.json callback function
- How to run Mike Bostock's D3 examples?
- D3 - Stacked bar chart, show all values in tooltip
- How to add pagination in table using d3?
- d3.js - draw file directory with tree layout
- Print text on two lines on the node of mobile patent suit D3
- d3 US state map with markers, zooming transform issues
- D3 bar chart - how to display positive/negative values together?
- d3js tooltip is positioned incorrectly on bootstrap web
- d3 zoom pan to specific region of line graph
- Using d3.js to create multi-line chart from csv file
- (d3) Dynamic binning for single-axis, zoomable timeline?
- d3 grouped bar chart creation issue
- i change the "class" attribute of a node that exist in SVG, but not applied ! why?
- How to underline text within a node's label in d3 force based layouts?
- D3 v4 xAxis zoom return x as NaN making rescale(x) unable to complete
- How do you retrieve the key values of a nested data set in D3
- JSON and D3, Change only children key names but leave parent
- How do I gray out D3 elements based on form input?
- Why D3 is not creating svg elements
- D3 Y Axis Scale Not Visible
- Static Data Architecture for D3 visualization Best Practice
- TypeError: string is undefined (var c, p, i = 0, n = template.length, m = string.length;)
- How to set up custom color scale with multiple sub-parts in D3?