score:1
Accepted answer
You need to calculate the new value for y in the array every time draw() is being called.
Also, I just restructured the update pattern in draw() and it updates the y attribute of the second rect when the window is resized.
See code below and a plunker here: https://embed.plnkr.co/iCczR7wlSwbO46JDMPr9/
Please mark the answer as correct when you are done!
var width = window.innerWidth;
var height = window.innerHeight;
var svg = d3.select('body')
.append("svg")
.attr("height", height)
.attr("width", width);
var _data = [];
draw();
d3.select(window).on("resize", draw);
function draw() {
calcData();
width = window.innerWidth;
svg.attr("width", width);
//add rects
var rects = svg.selectAll('rect')
.data(_data);
console.log(_data[1].y);
console.log(rects);
var xScale = d3.scale.ordinal()
.domain(_data)
.rangeBands([10, width - 10]);
rects.exit().remove();
rects.enter()
.append('rect')
.attr("fill", function(d, i) {
return d.color;
});
//update logic
rects.attr("x", function(d) {
return xScale(d);
})
.attr("y", function(d) {
console.log(d.color, d.y);
return d.y;
})
.attr("width", xScale.rangeBand() * 0.95)
.attr("height", 20);
}
function calcData(){
_data = [
{
"color": "yellow",
"y": 0
},
{
"color": "green",
"y": window.innerHeight - window.innerHeight / 5 //<- this data especially the hight value does not change .data(_data) on widow resize is there a way to change the data entered or at least the "y" value on window resize?
}
];
}
Source: stackoverflow.com
Related Query
- D3js value of .data(...) field does not change on window resize. Is there a way to change it?
- D3js v4: scaleOrdinal does not have the rangePoints()
- Brushing on ordinal data does not work
- d3js v4 x-axis-label is there but not visible
- Best way to solve typescript error - property does not exist on type
- d3 multiline update path and transition path does not work with nested data
- Is there a way to add different shapes depending on different data to my force-directed graph?
- Resize centering force / change center - d3js v4
- D3 + React Native - Data point value not updating when X position changes
- the data is changed, but reloading does not fetch new data, D3
- How can I resize my d3.js script? is there a way to put this into a div tag where I can resize my bar chart smaller?
- D3.js version3 scale.ordinal() not reading in my data the way I would like
- Circle Not Generating For All Value In csv File In ScatterPlot d3js
- Data Join with Custom Key does not work as expected
- d3.js data not refreshing upon change in the file
- D3.js - exit() section does not remove all data
- D3 line chart does not show tool tip and data points properly
- d3js does not enter into line function
- D3 on Angular: Changing data object does not refresh chart
- Is there a way to keep aspect ratio fixed for text but not other svg elements?
- js var does not get value of document.getElementById( ).value
- Is there a way to make a D3 legend change based on user input to radio buttons?
- Table based on csv data not updating properly d3js v4
- d3 does not load tsv data in python flask script
- d3 chart redraw not working on window resize
- Is there a way to change the type of the subcharts in c3.js
- How to set color of node based on data value in D3JS
- change client data transforms value in meteor.js
- D3js legend color does not match to the map color javascript
- Why the maximum variable take the d3.max() value but the minimum one d3.min() does not work properly?
More Query from same tag
- Switch for making background map from Mapbox visible/invisible in D3
- dimplejs line series tooltip customisation
- HTML update on slider
- d3.js support images per node
- Retrieving Distance to Top of a Dynamically Added Element
- d3js example not working with php's json_encode
- How can I specify a custom XML/SVG namespace with D3.js?
- How to set the background-color of a D3.js svg?
- Histogram bars disappeared when added a min value to the y domain - D3
- Sizing of SVG foreignObject element to fit HTML element
- d3js svg text and shapes on same line
- How to move a shape based on Id
- How to pass the tick value on to the tooltip
- Why does the whole SVG disappear when I set map opacity to 0 in D3?
- D3 ordinal scale, left and right margins for tickvalues
- Outputting Value of Promise during multiple parses
- How to remove node in D3 force layout?
- D3 - Reading from .json and attempting to plot points - nothing displayed
- inexplicable inability to render city name labels on a d3.js map: out of scope NAME?
- Create a tooltip that displays Y-position on a line (d3.js)
- How to switch from d3.svg.line to a choice that allows different coloured segments?
- Why do mouseover events not work for all states in d3.js projection?
- C3.js make area chart not opaque
- adding clickable zones to an svg
- Pie layout using multiple values within a single object
- D3, plotting to map, possible data formatting
- d3.js How to pass parameter to php for query condition
- d3 is undefined error
- D3.js IE9 error on checking nodes in graphdata
- Filling two overlapping areas in d3.js