score:2
Accepted answer
Since you need to know both current entry weight (for the width), as well as the sum of all previous entries' weights (for the offset), a pie layout seems like an unexpectedly decent fit for the task (since it provides start and end angles for the slices). All you need to do is to map the angle to the width.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 200)
var data = [{
freq: 60,
weight: 10
}, {
freq: 25,
weight: 10
}, {
freq: 55,
weight: 20
}];
var xScale = d3.scaleLinear()
.domain([0, Math.PI * 2])
.range([0, 500]);
var yScale = d3.scaleLinear()
.domain([0, 80])
.range([200, 0]);
var pie = d3.pie()
.sortValues(null)
.value(function(d){ return d.weight; });
var adjustedData = pie(data);
var rects = svg.selectAll('rect')
.data(adjustedData);
rects.enter()
.append('rect')
.style('fill', 'blue')
.style('stroke', 'black')
.merge(rects)
.attr('x', function(d) { return xScale(d.startAngle); })
.attr('width', function(d) { return xScale(d.endAngle) - xScale(d.startAngle); })
.attr('y', function(d) { return yScale(d.data.freq); })
.attr('height', function(d) { return yScale(0) - yScale(d.data.freq); });
</script>
</body>
Source: stackoverflow.com
Related Query
- D3 Bar Chart with Variable X and Y(variable width and height for bars)
- How to use x and width in a bar chart with scaleTime?
- d3 bar chart with fixed bar width and fixed spacing to align in center axis line
- d3.js bar chart with pos & neg bars (win/loss) for each record
- Transitioning a bar chart with negative values for the width
- d3 bar chart with custom x axis and bar width
- NVD3 Line Plus Bar With Focus Chart only displaying half width of first and last bar
- D3js v5 Trying to select multiple bars in bar chart using brush and saving values to variable and table
- Showing a bar chart with expected value and current value for a series
- If condition never met and hence my bars in a simple bar chart are always blue, while I want them to be red for values below a certain number
- vertical bars not aligned with the x-axis label and last bar rendered outside the x-axis range - D3 js - simple bar with fixed width
- d3 - Update Width and X for smooth transition in Horizontal Stacked Bar Chart
- How can I set max value(range) for y-axis and bar width of a bar chart using nvd3.js
- Y and Height Values for Stacked Bar Chart in D3
- How to draw a Bar Chart with time on xAxis and one or more numeric data for yAxis in d3js?
- D3.js: How to get the computed width and height for an arbitrary element?
- Auto width and height for SVG image
- Maximum width for column in bar chart
- Special donut chart with different rings/arcs for positive and negative values
- d3.js bar chart sorting: can't figure out how to sort x-axis labels along with bars
- Adding Error Bars to Grouped Bar Chart with D3.js
- Mix bar and line charts with Rickshaw (d3 based graphing library for js)
- D3 Zoom with Scrollbars used as panning, Scrollbar width and height adjusts to zoom scale
- D3 treemap: reserve min width and height for the smallest node
- d3.js stacked bar chart with positive and negative values
- How do I implement d3's enter update exit pattern for an area chart with focus and context?
- viewBox attribute with svg height and width attributes set
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
- Need help lining up x axis ticks with bars in D3.js bar chart
- Sorting the bars in a bar chart with dc.js
More Query from same tag
- How can I get custom SVG paths to zoom correctly in D3?
- Identify D3.js Graph
- horizontal legend using d3 gets cut off on right side
- Calculate mean json with d3.js
- how to make d3. charts customizable?
- Make SVGs on top of leaflet map not respond to pan events
- positioning xAxis and yAxis with d3.js
- Show values on Y Axis as it is with Higher and lower limits NVD3 Line Chart
- Box plot with dual dimension and dual x-axis using dc.js
- Line chart based on formula
- How to increase the space between axis and axis label?
- How to highlight only one datapoint in c3.js line graph?
- Find extent for multiple data dimensions in d3
- How to update text in area chart when I change csv file on click in d3?
- Filtering my data causes the y-axis to get screwed up
- D3.js - update a single element
- d3v4 How can i set a bar chart to distinct colours
- Changing the buckets for dc histogram with d3 domain call
- Why do I need a 00 time zone offset to display values correctly in d3v4?
- D3 TopoJSON U.S. map resizing in Angular
- d3.js / CoffeeScript: Access execution context (this) of both class and path in mouseover
- d3 - change path value when equals infinity
- How to translate an Observable Notebook example into javascript?
- D3.js v5 Get data from value
- Problems using json recursive in treemap using D3PLUS
- Loading D3 from a local variable
- d3 handling mouse events in charts
- What is the correct way of doing a small multiples bar chart set in d3 v5?
- D3 js what kind of scale to use for unbalanced data
- Using d3 selection.join to create a new line each time