score:0
Accepted answer
i was able to find two issues in your code.
the
clippath
rectangle used in svgdefs
is also getting updated withinxbrushed
function. the problem is with the selector you used.you should have used
main.selectall(".rect") //which selects all elements with class rect in main selection.
instead of
main.selectall("rect") //which selects all rect elements in main selection which includes clippath rect also.
within the
xbrushed
function.you will need to apply the
clip-path
attribute to the group element which holds the rectangle bar elements.
working code snippet:
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 860 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var min_margin = {
top: height,
right: margin.right + 10,
bottom: margin.bottom,
left: margin.left + 10
},
min_height = 10,
min_width = 860 - min_margin.left - min_margin.right;
var x = d3.scale.ordinal()
.rangeroundbands([0, width], .2);
var min_x = d3.scale.ordinal().rangeroundbands([0, width], .2);
var y = d3.scale.linear()
.range([height, 0]);
var xaxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var min_xaxis = d3.svg.axis()
.scale(min_x)
.orient("bottom");
var main_xzoom = d3.scale.linear()
.range([0, width])
.domain([0, width]);
var yaxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, "%");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var main = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
main.append("defs").append("clippath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height + min_height + margin.bottom);
var mini_x_append = svg.append("g")
.attr("transform", "translate(" + min_margin.left + "," + (margin.top + height) + ")")
.attr("width", min_width);
var data = [{
"letter": "a~q112",
"frequency": 0.08167
}, {
"letter": "b~12",
"frequency": 0.01492
}, {
"letter": "c~c",
"frequency": 0.02782
}, {
"letter": "d~1a",
"frequency": 0.04253
}, {
"letter": "e~d",
"frequency": 0.12702
}, {
"letter": "f~x",
"frequency": 0.02288
}, {
"letter": "g~v",
"frequency": 0.02015
}, {
"letter": "h~xs",
"frequency": 0.06094
}, {
"letter": "i~za",
"frequency": 0.06966
}, {
"letter": "j~mj",
"frequency": 0.00153
}, {
"letter": "k~th",
"frequency": 0.00772
}, {
"letter": "l~ws",
"frequency": 0.04025
}, {
"letter": "m~qjl",
"frequency": 0.02406
}, {
"letter": "n~i",
"frequency": 0.06749
}, {
"letter": "o~p",
"frequency": 0.07507
}, {
"letter": "p~zs",
"frequency": 0.01929
}, {
"letter": "q~rip",
"frequency": 0.00095
}, {
"letter": "r~hi",
"frequency": 0.05987
}, {
"letter": "s~eop",
"frequency": 0.06327
}, {
"letter": "t~tl",
"frequency": 0.09056
}, {
"letter": "u~se",
"frequency": 0.02758
}, {
"letter": "v~wh",
"frequency": 0.00978
}, {
"letter": "w~jl",
"frequency": 0.0236
}, {
"letter": "x~y",
"frequency": 0.0015
}, {
"letter": "y~ty",
"frequency": 0.01974
}, {
"letter": "z~o",
"frequency": 0.00074
}, {
"letter": "a~q12",
"frequency": 0.08167
}, {
"letter": "b~122",
"frequency": 0.01492
}, {
"letter": "c~c2",
"frequency": 0.02782
}, {
"letter": "d~1a2",
"frequency": 0.04253
}, {
"letter": "e~d2",
"frequency": 0.12702
}, {
"letter": "f~x2",
"frequency": 0.02288
}, {
"letter": "g~v2",
"frequency": 0.02015
}, {
"letter": "h~xs2",
"frequency": 0.06094
}, {
"letter": "i~za2",
"frequency": 0.06966
}, {
"letter": "j~mj2",
"frequency": 0.00153
}, {
"letter": "k~th2",
"frequency": 0.00772
}, {
"letter": "l~ws2",
"frequency": 0.04025
}, {
"letter": "m~qjl2",
"frequency": 0.02406
}, {
"letter": "n~i2",
"frequency": 0.06749
}, {
"letter": "o~p2",
"frequency": 0.07507
}, {
"letter": "p~zs2",
"frequency": 0.01929
}, {
"letter": "q~rip2",
"frequency": 0.00095
}, {
"letter": "r~hi2",
"frequency": 0.05987
}, {
"letter": "s~eo2p",
"frequency": 0.06327
}, {
"letter": "t~tl2",
"frequency": 0.09056
}, {
"letter": "u~se2",
"frequency": 0.02758
}, {
"letter": "v~wh2",
"frequency": 0.00978
}, {
"letter": "w~jl2",
"frequency": 0.0236
}, {
"letter": "x~y2",
"frequency": 0.0015
}, {
"letter": "y~ty2",
"frequency": 0.01974
}, {
"letter": "z~o2",
"frequency": 0.00074
}];
x.domain(data.map(function(d) {
return d.letter;
}));
min_x.domain(data.map(function(d) {
return d.letter;
}));
y.domain([0, d3.max(data, function(d) {
return d.frequency;
})]);
var xbrush = d3.svg.brush().x(min_x).on("brush", xbrushed);
// add the x axis
main.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (height + min_height) + ")")
.attr("clip-path", "url(#clip)")
.call(xaxis)
.selectall(".tick text")
.call(wrap, x.rangeband());
var x_arc = d3.svg.arc()
.outerradius(min_height / 2)
.startangle(0)
.endangle(function(d, i) {
return i ? -math.pi : math.pi;
});
var brush_x_grab = mini_x_append.append("g")
.attr("class", "x brush")
.call(xbrush);
brush_x_grab.selectall(".resize").append("path")
.attr("transform", "translate(0," + min_height / 2 + ")")
.attr("d", x_arc);
brush_x_grab.selectall("rect").attr("height", min_height);
main.append("g")
.attr("class", "y axis")
.call(yaxis);
var bar = main.append("g")
.attr("clip-path", "url(#clip)")
.selectall(".rect")
.data(data)
.enter().append("rect")
.attr("class", "rect")
.attr("x", function(d) {
return x(d.letter);
})
.attr("width", x.rangeband())
.attr("y", function(d) {
return y(d.frequency);
})
.attr("height", function(d) {
return height - y(d.frequency);
});
// called to re-draw the bars on the main chart when the brush on the x axis
// has been altered.
function xbrushed() {
var originalrange = main_xzoom.range();
main_xzoom.domain(xbrush.empty() ? originalrange : xbrush.extent());
x.rangeroundbands([main_xzoom(originalrange[0]), main_xzoom(originalrange[1])], .2);
// min_x.rangeroundbands([0, x.rangeband()], 0);
main.selectall(".rect")
.data(data)
.attr("width", function(d) {
return x.rangeband();
})
.attr("x", function(d) {
// alert("d is"+json.stringify(d));
return x(d.letter);
});
main.select("g.x.axis").call(xaxis).selectall(".tick text").call(wrap, x.rangeband());
};
// this comes from the example at http://bl.ocks.org/mbostock/7555321
// for wrapping long axis tick labels
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
linenumber = 0,
lineheight = 1.1, // ems
y = text.attr("y"),
dy = parsefloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
// console.log("wrap is"+json.stringify(word));
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getcomputedtextlength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++linenumber * lineheight + dy + "em").text(word);
}
}
});
};
// set the initial brush selections.
// svg.select(".x.brush").call(xbrush.extent(main_xzoom.domain()));
svg.select(".x.brush").call(xbrush.extent([0, 110]));
//svg.select(".y.brush").call(ybrush.extent(mini_y0.domain()));
// forces a refresh of the brushes and main chart based
// on the selected extents.
xbrushed();
//ybrushed();
function type(d) {
d.frequency = +d.frequency;
return d;
}
g.axis path,
g.axis line {
fill: none;
stroke: black;
shape-rendering: crispedges;
}
g.brush rect.extent {
fill-opacity: 0.2;
}
.resize path {
fill-opacity: 0.2;
}
.bar {
fill: steelblue;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispedges;
}
.x.axis path {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
edit:
i found it is more easy to implement this functionality by converting the json data you have to the same format used at this link.
var nesteddata;
var main_margin = {
top: 25,
right: 80,
bottom: 60,
left: 70
},
width = 900 - main_margin.left - main_margin.right,
mini_x_height = 10;
main_height = 525 - main_margin.top - main_margin.bottom,
mini_x_margin = {
top: main_height,
right: main_margin.right + 10,
bottom: main_margin.bottom,
left: main_margin.left + 10
},
mini_x_width = 900 - mini_x_margin.left - mini_x_margin.right,
mini_y_margin = {
top: main_margin.top + 10,
right: 0,
bottom: main_margin.bottom + 10,
left: 0
},
mini_y_width = 10,
mini_y_height = 525 - mini_y_margin.top - mini_y_margin.bottom;
var color = d3.scale.category10();
// x0 is the groups scale on the x axis.
var main_x0 = d3.scale.ordinal().rangeroundbands([0, width], 0.2);
var mini_x0 = d3.scale.ordinal().rangeroundbands([0, width], 0.2);
var main_xzoom = d3.scale.linear()
.range([0, width])
.domain([0, width]);
// x1 is the series scale on the x axis.
var main_x1 = d3.scale.ordinal();
var mini_x1 = d3.scale.ordinal();
// y is the value scale on the y axis.
var main_y0 = d3.scale.linear().range([main_height, 0]);
var mini_y0 = d3.scale.linear().range([mini_y_height, 0]);
var main_xaxis = d3.svg.axis()
.scale(main_x0)
.orient("bottom");
var mini_xaxis = d3.svg.axis()
.scale(mini_x0)
.orient("bottom");
var main_yaxis = d3.svg.axis()
.scale(main_y0)
.orient("left");
var mini_yaxis = d3.svg.axis()
.scale(mini_y0)
.orient("left");
var svg = d3.select("#chart").append("svg")
.attr("width", width + main_margin.left + main_margin.right)
.attr("height", main_height + main_margin.top + main_margin.bottom);
var main = svg.append("g")
.attr("transform", "translate(" + main_margin.left + "," + main_margin.top + ")");
main.append("defs").append("clippath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", main_height + mini_x_height + main_margin.bottom);
var mini_x = svg.append("g")
.attr("transform", "translate(" + mini_x_margin.left + "," + (main_margin.top + main_height) + ")")
.attr("width", mini_x_width);
var mini_y = svg.append("g")
.attr("width", mini_y_width)
.attr("transform", "translate(" + (main_margin.left - mini_y_width) + ", " + mini_y_margin.top + ")")
.attr("height", mini_y_height);
var data = [{
key: 'mechanical',
values: [{
key: 'gear',
value: 11
}, {
key: 'bearing',
value: 8
}, {
key: 'motor',
value: 3
}, {
key: 'bearing',
value: 8
}, {
key: 'motor',
value: 3
}, {
key: 'bearing',
value: 8
}, {
key: 'motor',
value: 3
}]
}, {
key: 'electrical',
values: [{
key: 'switch',
value: 19
}, {
key: 'plug',
value: 12
}, {
key: 'cord',
value: 11
}, {
key: 'fuse',
value: 3
}, {
key: 'bulb',
value: 2
}]
}, {
key: 'hydraulic',
values: [{
key: 'pump',
value: 4
}, {
key: 'leak',
value: 3
}, {
key: 'seals',
value: 1
},{
key: 'switch',
value: 19
}, {
key: 'plug',
value: 12
}, {
key: 'cord',
value: 11
}, {
key: 'fuse',
value: 3
}, {
key: 'bulb',
value: 2
}]
}];
var res = [];
data.foreach(function(d){
res.push(d.values.map(function(o){
o.item=o.key;
o.subject = d.key; return o; }));
});
data = [].concat.apply([], res);
res.sort(function(a,b){ return a.subject<b.subject });
//console.log(data);
var seriesvalues = d3.set(data.map(function(x) {
return x.item;
})).values().sort(d3.ascending);
nesteddata = d3.nest()
.key(function(d) {
return d.subject;
})
.sortkeys(d3.ascending)
.sortvalues(function(a, b) {
return a.item - b.item;
})
.entries(data);
var groupvalues = d3.set(data.map(function(x) {
return x.subject;
})).values();
// define the axis domains
main_x0.domain(groupvalues);
mini_x0.domain(groupvalues);
main_x1.domain(seriesvalues).rangeroundbands([0, main_x0.rangeband()], 0);
mini_x1.domain(seriesvalues).rangeroundbands([0, main_x0.rangeband()], 0);
main_y0.domain([0, d3.max(nesteddata, function(d) {
return d3.max(d.values, function(d) {
return d.value;
});
})]);
mini_y0.domain(main_y0.domain());
var xbrush = d3.svg.brush().x(mini_x0).on("brush", xbrushed);
var ybrush = d3.svg.brush().y(mini_y0).on("brush", ybrushed);
// add the x axis
main.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (main_height + mini_x_height) + ")")
.attr("clip-path", "url(#clip)")
.call(main_xaxis)
.selectall(".tick text")
.call(wrap, main_x0.rangeband());
// add the y axis
main.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (-mini_y_width) + ", 0)")
.call(main_yaxis)
.append("text")
.attr("transform", "rotate(-90), translate(" + -(main_height / 2) + ", " + -(mini_y_width + main_margin.left - 20) + ")")
.attr("dy", ".71em")
.style("text-anchor", "middle")
.text("value");
var x_arc = d3.svg.arc()
.outerradius(mini_x_height / 2)
.startangle(0)
.endangle(function(d, i) {
return i ? -math.pi : math.pi;
});
var brush_x_grab = mini_x.append("g")
.attr("class", "x brush")
.call(xbrush);
brush_x_grab.selectall(".resize").append("path")
.attr("transform", "translate(0," + mini_x_height / 2 + ")")
.attr("d", x_arc);
brush_x_grab.selectall("rect").attr("height", mini_x_height);
var y_arc = d3.svg.arc()
.outerradius(mini_y_width / 2)
.startangle(-(math.pi / 2))
.endangle(function(d, i) {
return i ? -((3 * math.pi) / 2) : ((math.pi) / 2);
});
var brush_y_grab = mini_y.append("g")
.attr("class", "y brush")
.call(ybrush);
brush_y_grab.selectall(".resize").append("path")
.attr("transform", "translate(" + (mini_y_width / 2) + ", 0)")
.attr("d", y_arc);
brush_y_grab.selectall("rect").attr("width", mini_y_width);
// create the main bars
var bar = main.selectall(".bars")
.data(nesteddata)
.enter().append("g")
.attr("clip-path", "url(#clip)")
.attr("class", function(d) {
return d.key + "-group bar";
});
bar.selectall("rect")
.data(function(d) {
return d.values;
})
.enter().append("rect")
.attr("class", function(d) {
return d.item;
})
.attr("transform", function(d) {
return "translate(" + main_x0(d.subject) + ",0)";
})
.attr("width", function(d) {
return main_x1.rangeband();
})
.attr("x", function(d) {
return main_x1(d.item);
})
.attr("y", function(d) {
return main_y0(d.value);
})
.attr("height", function(d) {
return main_height - main_y0(d.value);
})
.style("fill", function(d) {
return color(d.item);
});
// draws the series items onto a legend
var legend = svg.selectall(".legend")
.data(seriesvalues.slice())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) {
return "translate(50," + (main_margin.top + (i * 20)) + ")";
});
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) {
return d;
});
// called to re-draw the bars on the main chart when the brush on the x axis
// has been altered.
function xbrushed() {
var originalrange = main_xzoom.range();
main_xzoom.domain(xbrush.empty() ? originalrange : xbrush.extent());
main_x0.rangeroundbands([main_xzoom(originalrange[0]), main_xzoom(originalrange[1])], .1);
main_x1.rangeroundbands([0, main_x0.rangeband()], 0);
bar.selectall("rect")
.attr("transform", function(d) {
return "translate(" + main_x0(d.subject) + ",0)";
})
.attr("width", function(d) {
return main_x1.rangeband();
})
.attr("x", function(d) {
return main_x1(d.item);
});
main.select("g.x.axis").call(main_xaxis).selectall(".tick text").call(wrap, main_x0.rangeband());
};
// called to re-draw the bars on the main chart when the
// brush on the y axis has been altered.
function ybrushed() {
main_y0.domain(ybrush.empty() ? mini_y0.domain() : ybrush.extent());
bar.selectall("rect")
.attr("y", function(d) {
return main_y0(d.value);
})
.attr("height", function(d) {
return main_height - main_y0(d.value);
});
main.select("g.y.axis").call(main_yaxis);
};
// this comes from the example at http://bl.ocks.org/mbostock/7555321
// for wrapping long axis tick labels
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
linenumber = 0,
lineheight = 1.1, // ems
y = text.attr("y"),
dy = parsefloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getcomputedtextlength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++linenumber * lineheight + dy + "em").text(word);
}
}
});
};
// set the initial brush selections.
// svg.select(".x.brush").call(xbrush.extent(main_xzoom.domain()));
svg.select(".x.brush").call(xbrush.extent([0, 610]));
svg.select(".y.brush").call(ybrush.extent(mini_y0.domain()));
// forces a refresh of the brushes and main chart based
// on the selected extents.
xbrushed();
ybrushed();
//});
g.axis path,
g.axis line {
fill: none;
stroke: black;
shape-rendering: crispedges;
}
g.brush rect.extent {
fill-opacity: 0.2;
}
.resize path {
fill-opacity: 0.2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="chart"></div>
Source: stackoverflow.com
Related Query
- X axis label not displayed in bar chart with scroll and zoom feature - d3js, Brushing
- dc.js Stacked Bar Chart having only one column and with elastic X Axis is not rendered properly
- Grouped bar chart with zoom and updateable data error
- d3 bar chart with fixed bar width and fixed spacing to align in center axis line
- D3.js bar chart - axis and labels not working / transitioning
- D3 Bar and Linear Chart With Multiple Axis
- D3 bar chart not working properly with all negative and positive values
- d3 bar chart with custom x axis and bar width
- dc.js - Chart not rendering (single row bar chart - horizontal row), with the exception of legend, axes and tick marks/values (which do render)
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
- How can I make a bar chart starting from the 0 point of the y axis and not from the bottom of the svg?
- 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: Unit Bar Chart with Brush and Zoom (transpose)
- Right Axis not displaying on Dual Axis Grouped Bar and Line Chart D3
- D3 Line chart - display value label on Y axis tick and a not scale
- Issues with x and y axis of a bar chart with d3
- D3.js bar chart not selecting or binding "date" data to Y axis label "text" elements on first ajax request
- Make simple bar chart using C3 with separate columns on the x axis
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- d3 bar chart y axis ticks and grid lines above max value
- How to use x and width in a bar chart with scaleTime?
- C3 / D3 bar chart with horizontal scroll
- Y axis label not displaying large numbers - Multi-Bar Chart
- DC.js bar chart with date axis
- Make a chart with area charts having both positive and negative y axis in dc or d3.js
- Trouble using DC.js (crossfilter and d3 convenience library) - bar chart not showing values
- With D3, how can I set attribute ("fill", "none") of axis path and line, but not text (without editing stylesheet)
- d3.js stacked bar chart with positive and negative values
- Need help lining up x axis ticks with bars in D3.js bar chart
- dc.js bar chart with ordinal x axis scale doesn't render correctly
More Query from same tag
- How to install datawrapper locally?
- Error: <rect> attribute x: Expected length, "NaN"
- Testing d3 with Node.js, JSDOM and Jasmine
- D3.js with update button adds new data to old instead of replacing old data
- Atom autocomplete ternjs not working
- C3JS - Cannot read property 'category10' of undefined
- I can't understand how this .csv file is read
- 'Point-along-path' d3 visualization performance issue
- How to make a Scatter plot with D3 using a JSON file as input
- How to have only one brush show on page load when using multiple charts in dc.js/d3.js
- D3.js: "On the fly" added elements to array are not refreshing the svg graphic
- D3 nested selection with json: how to apply .force() to nested elements?
- How to create responsive svg using d3.js
- D3js-Get the first and the last decile
- D3 Visualization of network data (nodes and links) via json file
- No access to my data in the callback function of d3.csv() method
- Append HTML to existing tooltip in d3
- Does any middleware conflict with javascript regex?
- Simple way to use existing object as a clipping path?
- Is there a way to make Internet Explorer understand this.style.setProperty?
- Bar chart: how could I align the data with countries when sorting in D3 v5?
- Callback function issue with d3.csv() in d3.js
- D3.js - Conditional apply nest.key() function to array elements
- Exiting unused nodes in d3 force directed graph
- Bringing data from PHP into D3.JS
- Placing label in the middle of a link in d3 js tree layout
- D3.js v5 making clones of selection.clones
- How to get the height and width of text in a span not the whole span width and height
- Accessing MySQL database in d3 visualization
- Integrating Google Calendar data with D3.js based Calendar