dataset created with d3.nest isn't drawing correctly - d3.js
I am trying to visualize a dataset to see major players in the pangolin trade. I want each country to be part of a categorical scale on the y-axis, and the x-axis is the date, and I want each trade instance to be represented as a circle, with x being the date it happened, y being the position for that particular country, and r being the number of instances accumulated so far (represented in my data as r). Basically my data is from 2010 to 2016, and includes the country name where pangolins are either imported to or exported from, the count (accumulated trade instances up until this date), and the date (yyyy-mm-dd).
Below is part of my data, which I store in the variable trade:
[{"date":"2010-10-22","country":"Thailand","rank":4,"count":1},{"date":"2010-10-28","country":"Malaysia","rank":2,"count":1},{"date":"2010-11-8","country":"Thailand","rank":4,"count":2},{"date":"2010-11-18","country":"Nepal","rank":7,"count":1},{"date":"2010-11-22","country":"China","rank":5,"count":1},{"date":"2010-11-22","country":"China","rank":5,"count":2},{"date":"2010-11-27","country":"India","rank":1,"count":1},{"date":"2010-11-28","country":"India","rank":1,"count":2},{"date":"2010-11-28","country":"India","rank":1,"count":3},{"date":"2010-11-30","country":"India","rank":1,"count":4},{"date":"2010-12-17","country":"Malaysia","rank":2,"count":2},{"date":"2010-12-22","country":"Vietnam","rank":3,"count":1},{"date":"2011-01-3","country":"Nepal","rank":7,"count":2},{"date":"2011-02-12","country":"Myanmar","rank":8,"count":1},{"date":"2011-02-25","country":"Malaysia","rank":2,"count":3},{"date":"2011-02-26","country":"Malaysia","rank":2,"count":4},{"date":"2011-03-2","country":"South Africa","rank":18,"count":1},{"date":"2011-03-2","country":"Rwanda","rank":35,"count":1},{"date":"2011-03-2","country":"Mozambique","rank":22,"count":1},{"date":"2011-03-2","country":"Kenya","rank":12,"count":1},{"date":"2011-03-3","country":"China","rank":5,"count":3},{"date":"2011-02-21","country":"Vietnam","rank":3,"count":2},{"date":"2011-03-24","country":"Malaysia","rank":2,"count":5},{"date":"2011-04-4","country":"Malaysia","rank":2,"count":6},{"date":"2011-03-25","country":"India","rank":1,"count":5},{"date":"2011-03-26","country":"Malaysia","rank":2,"count":7},{"date":"2011-04-2","country":"Nepal","rank":7,"count":3},{"date":"2011-04-20","country":"Thailand","rank":4,"count":3},{"date":"2011-05-11","country":"China","rank":5,"count":4},{"date":"2011-05-11","country":"China","rank":5,"count":5},{"date":"2011-05-26","country":"Indonesia","rank":6,"count":1},{"date":"2011-05-26","country":"India","rank":1,"count":6},{"date":"2011-05-29","country":"Indonesia","rank":6,"count":2},{"date":"2011-06-6","country":"India","rank":1,"count":7},{"date":"2011-06-7","country":"Mozambique","rank":22,"count":2},{"date":"2011-06-5","country":"India","rank":1,"count":8},{"date":"2011-06-12","country":"Malaysia","rank":2,"count":8},{"date":"2011-06-13","country":"Singapore","rank":21,"count":1},{"date":"2011-06-14","country":"Malaysia","rank":2,"count":9},{"date":"2011-06-17","country":"India","rank":1,"count":9},{"date":"2011-06-19","country":"India","rank":1,"count":10},{"date":"2011-06-26","country":"Thailand","rank":4,"count":4},{"date":"2011-06-30","country":"India","rank":1,"count":11},{"date":"2011-07-4","country":"Malaysia","rank":2,"count":10},{"date":"2011-07-5","country":"Zimbabwe","rank":14,"count":1},{"date":"2011-07-12","country":"Indonesia","rank":6,"count":3},{"date":"2011-07-18","country":"Indonesia","rank":6,"count":4},{"date":"2011-07-27","country":"Nepal","rank":7,"count":4},{"date":"2011-08-16","country":"Nepal","rank":7,"count":5},{"date":"2011-08-19","country":"Namibia","rank":33,"count":1},{"date":"2011-08-23","country":"India","rank":1,"count":12},{"date":"2010-09-17","country":"Myanmar","rank":8,"count":2},{"date":"2011-09-1","country":"Zimbabwe","rank":14,"count":2},{"date":"2011-09-13","country":"Indonesia","rank":6,"count":5},{"date":"2011-09-13","country":"Malaysia","rank":2,"count":11},{"date":"2011-09-13","country":"Myanmar","rank":8,"count":3},{"date":"2011-09-21","country":"Malaysia","rank":2,"count":12},{"date":"2011-09-26","country":"Thailand","rank":4,"count":5},{"date":"2011-09-30","country":"Indonesia","rank":6,"count":6},{"date":"2011-10-1","country":"Sri Lanka","rank":19,"count":1},{"date":"2011-10-6","country":"India","rank":1,"count":13},{"date":"2011-10-7","country":"India","rank":1,"count":14},{"date":"2011-10-18","country":"Indonesia","rank":6,"count":7},{"date":"2011-10-18","country":"Indonesia","rank":6,"count":8},{"date":"2011-10-18","country":"Indonesia","rank":6,"count":9},{"date":"2011-10-22","country":"India","rank":1,"count":15},{"date":"2011-10-24","country":"India","rank":1,"count":16},{"date":"2011-11-28","country":"United States","rank":32,"count":1},{"date":"2011-12-15","country":"Vietnam","rank":3,"count":3},{"date":"2011-12-27","country":"Thailand","rank":4,"count":6},{"date":"2012-01-4","country":"Philippines","rank":15,"count":1},{"date":"2012-01-5","country":"Kenya","rank":12,"count":2},{"date":"2012-01-6","country":"Philippines","rank":15,"count":2},{"date":"2012-01-17","country":"Philippines","rank":15,"count":3},{"date":"2012-01-24","country":"China","rank":5,"count":6},{"date":"2012-02-22","country":"Malaysia","rank":2,"count":13},{"date":"2012-03-1","country":"Malaysia","rank":2,"count":14},{"date":"2012-03-19","country":"Pakistan","rank":11,"count":1},{"date":"2012-03-21","country":"Malaysia","rank":2,"count":15},{"date":"2012-03-23","country":"Vietnam","rank":3,"count":4},{"date":"2012-04-27","country":"Vietnam","rank":3,"count":5},{"date":"2012-04-23","country":"Belgium","rank":31,"count":1},{"date":"2012-06-7","country":"Thailand","rank":4,"count":7},{"date":"2012-06-7","country":"Thailand","rank":4,"count":8}];
and here is my code:
var margin = {left:120, top:20, right:0, bottom:50};
var width = 1000;
var height = 800;
var data=trade;
var tradeByCountry = d3.nest()
.key(function(d) { return d.country; })
.entries(trade);
console.log(tradeByCountry);
tradeByCountry.forEach(function(country){
country['number']=country.values.length;
console.log(country);
});
var country_colors = ["#393b79","#5254a3", '#6b6ecf', '#9c9ede', '#637939', '#8ca252','#b5cf6b','#cedb9c',
'#8c6d31','#bd9e39','#e7ba52','#e7cb94','#843c39','#ad494a','#d6616b','#e7969c','#7b4173','#a55194',
'#ce6dbd','#de9ed6', '#9467bd', '#c5b0d5','#3182bd', '#6baed6','#17becf','#9edae5','#e6550d','#fd8d3c','#fdae6b',
'#31a354','#74c476','#a1d99b','#d62728','#ff9896','#7f7f7f','#c7c7c7'];
var colors = d3.scale.ordinal()
.domain(d3.range(tradeByCountry.length))
.range(country_colors);
tradeByCountry.sort(function(x, y){
return d3.descending(x.number, y.number);
})
var countriesArray = [];
tradeByCountry.forEach(function(country){
countriesArray.push(country.key);
});
console.log(countriesArray);
var x = d3.time.scale()
.rangeRound([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var timeFormat = d3.time.format("%Y-%m-%d");
x.domain([timeFormat.parse('2010-10-22'),timeFormat.parse('2016-12-30')]);
y.domain(countriesArray);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("date");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "translate(35,-25)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("countries");
svg.selectAll(".dot")
.data(tradeByCountry)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 7)
.attr("cx", function(d) { return 0; })
.attr("cy", function(d,i) { return (height/tradeByCountry.length)*i; })
.style("fill", function(d,i) { return colors(i); });
svg.selectAll("text.labels")
.data(tradeByCountry)
.enter()
.append("text")
.text(function(d) {return d.key})
.attr("x", 0)
.attr("y", function(d,i) { return (height/tradeByCountry.length)*i; })
.attr("transform", "translate(-120,5)")
.attr("fill",function(d,i) { return colors(i); });
svg.selectAll('.line')
.data(tradeByCountry)
.enter().append('line')
.attr("x1", 0) // x position of the first end of the line
.attr("y1",function(d,i) { return (height/tradeByCountry.length)*i; })
.attr("x2", width) // x position of the second end of the line
.attr("y2", function(d,i) { return (height/tradeByCountry.length)*i; })
.style("stroke",function(d,i) { return colors(i); })
.attr("id", function(d){return d.key});
//PROBLEM HERE!!!!!!!!!!!
var g = svg
.selectAll("g")
.data(tradeByCountry)
.enter().append("g")
.selectAll(".dot")
.data(function(d) {return d.values;})
.enter().append("circle")
.attr("id", function(d){ return d.count;})
.attr("r", function(d){return d.count;})
.attr("cx", function(d,i) { console.log (d.date);return (x(timeFormat.parse(d.date))); })
.attr("cy", function(d,i) { return (height/tradeByCountry.length)*i; })
.style("fill", function(d,i) { return colors(i); });
I am having trouble specifically at the very last part of my code, where I can't get all the circles to draw and not in the correct r, as I would like r to increase as the trade accumulates over time for a country, instead it seems that r is different according to country:
Two things, firstly in your example .selectAll("g") at the point you mark as having the error was picking up g elements in the axes, so the first few countries weren't getting their data displayed. This doesn't appear to be the case in your screenshot but it was happening given the code you posted, so I just qualified those gs with the .country class.
The second thing, and what was causing your specific problem was this line:
.attr("cy", function(d,i) { return (height/tradeByCountry.length)*i; })
I'm guessing you thought the index variable i here was still tied to the tradeByCountry array, which it would have been after selectAll("g.country").data(tradebyCountry) but at this point we've now made a nested selection .selectAll(".dot") on d.values for each country's data so i is now indexing those list of values. So that line of code above will take the values for each country and separate them all vertically, always starting from the top row - when in fact you want them on the same line, just separated by country.
What you wanted (I = India, M = Malaysia, T = Thailand)
I--- I0 I1 I2
M--- M0M1 M2
T--- T0 T1 T2
What you were getting
I--- I0 M0 T0
M--- M1 I1 T1
T--- I2 T2 M2
This was also hidden by the fact .attr("fill") also had the same mistake so the colours on each row were consistent (I've used d.rank instead to fix it).
Solution 1: To make sure the values stay on the same correct line use i before you do the nested selection so it still refers to the countries like this, which will offset each g element by the correct amount:
.attr("transform", function(d,i) {
return "translate(0,"+(height/tradeByCountry.length)*i+")";
})
and simply set cy to be zero for everything you add to this g element and they'll all be in a straight line (and on the right line)
Full code at the PROBLEM HERE! stage:
var g = svg
.selectAll("g.country")
.data(tradeByCountry)
.enter().append("g")
.attr("class", "country")
.attr("transform", function(d,i) {
return "translate(0,"+(height/tradeByCountry.length)*i+")";
})
.selectAll(".dot")
.data(function(d) {return d.values;})
.enter().append("circle")
.attr("class", "dot")
.attr("id", function(d){ return d.country+"_"+d.date+"_"+d.count;})
.attr("r", function(d){ return d.count;})
.attr("cx", function(d,i) { console.log (d.date);return (x(timeFormat.parse(d.date))); })
.attr("cy", function(d,i) { return 0; })
.style("fill", function(d,i) { return colors(d.rank); });
http://jsfiddle.net/d4typ567/1/
Solution 2: It's also the case that d3 maintains a parent index variable for nested selections (usually denoted ii) that can be passed into most d3 .attr and .style functions, so you could do .attr("cy", function(d,i, ii) { return (height/tradeByCountry.length)*ii; }) instead, but offsetting the g element is only 1 operation whereas this is done for every circle. Remember to do the same for the color (fill) function.
Related
how to highlight a bar in stacked bar chart d3.js v4
I want to highlight a bar with April (value in x-axis) with a square box. but I am not getting a approach to do the same. tried getting the co-ordinates of the respected bar, but unable to find a solution for the same Unable to find the co-ordinates of the respective bar which I need to highlight. what should be the approach for highlighting a bar with a square box in stacked bar chart d3.js v4 createStackedBarChart(130,300,10,60,20,45,"manager-line-graph-2"); function createStackedBarChart(height,width,top,right,bottom,left,id){ var margin = {top: top, right: right, bottom: bottom, left: left }; //console.log("margin"+margin); var svg = d3.select("#"+id).append("svg"), width = width - margin.left - margin.right, height = height - margin.top - margin.bottom, g = svg.attr("width", "100%") .attr("height", height + margin.top + margin.bottom) .append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var x = d3.scaleBand() .rangeRound([0, width]) .padding(0.2) .align(5.0); var y = d3.scaleLinear() .rangeRound([height, 0]); var z = d3.scaleOrdinal() .range(["#0000FF", "#00FFFF", "#81F781", "#F3F781", "#FE2E2E"]); var data = [ {"Months": "Feb","Installation": 5,"Product": 10,"Payment": 15,"Billing": 20,"Outage": 25}, {"Months": "March","Installation": 6,"Product": 8,"Payment": 9,"Billing": 15,"Outage": 18}, {"Months": "April","Installation": 9,"Product": 12,"Payment": 24,"Billing": 17,"Outage": 14}, {"Months": "May","Installation": 9,"Product": 12,"Payment": 14,"Billing": 17,"Outage": 14}, {"Months": "June","Installation": 9,"Product": 12,"Payment": 15,"Billing": 11,"Outage": 10} ]; // fix pre-processing var keys = []; for (key in data[0]){ if (key != "Months") keys.push(key); } console.log("value of keys are " + keys); data.forEach(function(d){ d.total = 0; keys.forEach(function(k){ d.total += d[k]; }) }); //data.sort(function(a, b) { //return b.total - a.total; //}); x.domain(data.map(function(d) { return d.Months; })); y.domain([0, d3.max(data, function(d) { return d.total; })]).nice(); z.domain(keys); g.append("g") .selectAll("g") .data(d3.stack().keys(keys)(data)) .enter().append("g") .attr("fill", function(d) {return z(d.key);}) .selectAll("rect") .data(function(d) { return d; }) .enter().append("rect") .attr("x", function(d) { return x(d.data.Months); }) .attr("y", function(d) { return y(d[1]); }) .attr("height", function(d) { return y(d[0]) - y(d[1]); }) .attr("width", x.bandwidth()-5); g.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(5)); g.append("g") .attr("class", "axis") .call(d3.axisLeft(y).ticks(5, "s")) .append("text") .attr("x", 2) .attr("y", y(y.ticks(5).pop())) .attr("dy", "0.32em") .attr("fill", "#000") .attr("font-weight", "bold") .attr("text-anchor", "start") var legend = g.append("g") .attr("font-family", "sans-serif") .attr("font-size", 10) .attr("text-anchor", "end") .selectAll("g") .data(keys.slice().reverse()) .enter().append("g") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); //legend.append("rect") //.attr("x", width + 20) //.attr("width", 10) //.attr("height", 10) //.attr("fill", z); legend.append("circle") .attr("r",5) .attr("cx", width+30) .attr("cy", 0) .attr("fill",z); legend.append("text") .attr("x", width + 88) .attr("y", 3.5) .attr("dy", "0.12em") .text(function(d) { return`enter code here` d; }); }
One solution I have used previously is to create an invisible layer of bars for the same data. Then use the .on("mouseover", function...); to make the bars for that data visible again by changing the styles or the fill opacity. Here is a sample bl.ock with what I mean. It is a grouped bar chart with ordinal scale, but the same could be applied to your data with some tweaking. https://bl.ocks.org/Coola85/b05339b65a7f9b082ca210d307a3e469 Update May 5, 2018: upon further reading I see your issue is different than the solution I suggested. This link might give you a good starting point of how to use an if statement to selectively highlight particular data. so you could use the following style for your rect .style ("fill", function(d) { if (d.Months === "April") {return "red"} // <== Add these else { return "black" } })
misalignment in the ticks of axes and rectangles of bar graph in d3
I am using d3 js to draw a bar graph. I have x and y axis too. The x axis would hold the 'names' and y axis the 'marks'. I am using ordinal scale for x axis. In my json input dataset_rule_errors, I have 10 entries. My code is var svgd = d3.select("body") .append("svg") .attr("width", width) .attr("height", height); var x_domain = dataset_rule_errors.map(function(d) { return d.Rulename; }) var xScale = d3.scale.ordinal() .domain(dataset_rule_errors.map(function (d) { return d.Rulename; })) .rangeBands([padding_rule, wsvg]); var xaxeScale = d3.scale.ordinal() .domain(x_domain) .rangePoints([padding_rule, wsvg]); var xAxis = d3.svg.axis() .scale(xaxeScale) .tickValues(x_domain) .orient("bottom"); //drawing rectangles svgd.append("g") .selectAll("rect") //based on the data in the dataset[] array, append rectangles s.t. .data(dataset_rule_errors) // .enter() .append("rect") .attr("x", function (d, i) { return xScale(d.Rulename); // x position of rect as per i->0,1,2,3,... }) .attr("y", function (d) { return (h_rule - yScale(d.NumRuleFailed)); //y position of rect as per (h-value) to prevent inverted range }) .attr("width", xScale.rangeBand())//"10") //depending upon domain->no of inputs - with of band is decided acc. to fit in svg .attr("height", function (d) { return yScale(d.NumRuleFailed); //depending upon domain->value of inputs - with of band is decided acc. to fit in svg }) .attr("fill", function (d, i) { //colour based on values -> more errors - dark coloured bars if(i%2==0) return "rgb(" + 255 + "," + 255 + "," + 200 + ")"; else return "rgb(" + 0 + "," + 0 + "," + 200 + ")"; }) .attr("stroke", "black"); //drawing x axis with ticks svgd.append("g") .attr("class", "x axis") .attr("transform", "translate(" + 0 + "," + (h_rule) + ")") .call(xAxis) .selectAll("text") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", ".15em") .attr("text-anchor", "start") .attr("transform", function (d) { return "rotate(-90)" }) .selectAll(".tick text") .style("text-anchor", "start"); The problem I am facing is that my rectangles and the ticks of x-axis do not align with one another. The reason is because I have 10 bars and therefore, I should be having 11 ticks including the one at the beginning and the end. But I have only 10 ticks, which distribute evenly along the axis length, so they do not coincide with the rectangle beginnings just like in this question Unable to align ticks with d3.js. But the solution for this question did not work out for me. What can I do? dataset_rule_errors = data I retrieve from my database [{"Rulename":"A","NumRuleFailed":34321},{"Rulename":"B","NumRuleFailed":43},{"Rulename":"C","NumRuleFailed":45522}, {"Rulename":"D","NumRuleFailed":43643},{"Rulename":"E","NumRuleFailed":152},{"Rulename":"F","NumRuleFailed":152}]
I could not reproduce the issue you said you were having but I highly recommend using rangeRoundBands for a bar chart. You can achieve the bar chart with the following setup: var x = d3.scale.ordinal() .rangeRoundBands([0, width], .2); var y = d3.scale.linear() .range([height, 0]); // Finding domain of x (all our rulenames) x.domain(data.map(function(d) { return d.Rulename; })); // Finding domain of y (min and max values) y.domain([d3.min(data, function(d) { return d.NumRuleFailed; }), d3.max(data, function(d) { return d.NumRuleFailed; })]); var xAxis = d3.svg.axis() .scale(x) // no need yo specify ticks, x scale // will take care of that .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") And the following for axis and rect rendering: // Render xAxis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .selectAll("text") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", "-.50em") .attr("text-anchor", "start") .attr("transform", "rotate(-90)") .selectAll(".tick text") .style("text-anchor", "start") // Render yAxis svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("NumRuleFailed"); // Render rects svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.Rulename); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.NumRuleFailed); }) .attr("fill", function(d, i) { //colour based on values -> more errors - dark coloured bars return (i % 2) ? 'green' : 'red'; }) .attr("height", function(d) { return height - y(d.NumRuleFailed); }); Full Plnkr: https://plnkr.co/edit/jmtMGcRyT9hM5efwwTOb?p=preview
My D3.js code is not showing seconds in the time axis?
I am going to draw a line chart using d3.js. I am plotting time in the x axis and speed in the y axis of different vehicle ids. But one thing I am not getting seconds from my data. the code is rounding seconds to minute. So I am getting wrong chart. I am trying to draw something like http://bl.ocks.org/mbostock/3884955. Here is my code var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(x) .orient("bottom").ticks(d3.time.minutes,5).tickFormat(d3.time.format("%H:%M:%S")); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .interpolate("basis") .x(function(d) { return x(d.time); }) .y(function(d) { return y(d.speed); }); /* var points = popchart.selectAll(".point") .data(data) .enter().append("svg:circle") .attr("stroke", "black") .attr("fill", function(d, i) { return color(i) }) .attr("cx", function(d, i) { return x(d.time) }) .attr("cy", function(d, i) { return y(d.speed) }) .attr("r", function(d, i) { return 3 }); */ color.domain(d3.keys(data[0]).filter(function(key) { return key !== "time"; })); var ids = color.domain().map(function(name) { return { name: name, values: data.map(function(d) { return {time: d.time, speed: +d[name]}; }) }; }); x.domain([d3.min(data,function(d){return d.time}),d3.max(data,function(d){return d.time})]); y.domain([ d3.min(ids, function(c) { return d3.min(c.values, function(v) { return v.speed; }); }), d3.max(ids, function(c) { return d3.max(c.values, function(v) { return v.speed; }); }) ]); var svg = d3.select("#chart").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Speed (mph)"); var id = svg.selectAll(".id") .data(ids) .enter().append("g") .attr("class", "id"); id.append("path") .attr("class", "line") .attr("d", function(d) {return line(d.values); }) .style("stroke", function(d) { return color(d.name); }); id.append("text") .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) .attr("transform", function(d) { return "translate(" + x(d.value.time) + "," + y(d.value.speed) + ")"; }) .attr("x", 3) .attr("dy", ".35em") .text(function(d) { return d.name; }); Though I am picking time in %H:%M:%S format the second is not coming in the chart. Why is this happening?
The reason you are getting loop is only one, Your data is not sorted according to time values as i am suspecting.In Your code you add the following for sorting the data according to time. And use this sorted data to draw the line chart. Here is the code data=data.sort(function(a, b) { return d3.ascending(a.time,b.time); }); Then pass this sorted data to draw the line chart. You are done
I forked your plunker, it seems as though some of the tabs in your tsv were actually spaces. This was, for starters, causing massive problems in your plunk, so for testing I've changed it to a csv, and moved your script into script.js so that it's a little more testable. I'll try to have another look soon, but this should make it easier for others to debug also. Let us know if this is more like what you wanted Plunk: http://plnkr.co/edit/bxncPdugtRTaWTdztraC?p=preview d3.csv("data.csv", function(error, data) { ... }
How to update axis using d3.js
I'm trying to display different data on a graph. The user can change the displayed data clicking on a radio button. I'm using a "Bubble chart" to render the data. For each type of data I need to update the Yaxis (the domain is different). Here is what I've done for now: Chart initialisation var svg = d3.select("body .main-content").append("svg") .attr("class", "chart") .attr("width", width) .attr("height", height); Initialise the Axis initAxis(); function initAxis() { var y = d3.scale.linear().domain([0,1000]).range([height-margin, margin]).nice(), x = d3.scale.linear().domain([0,23]).range([margin,width-margin]), yAxis = d3.svg.axis().scale(y).orient("left"), xAxis = d3.svg.axis().scale(x).orient("bottom"); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + margin + ",0)") .call(yAxis); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (height - margin) + ")") .call(xAxis); } Update the chart function update(type) { // Get the data type = type || 'default'; var m = max[type], mi = min[type]+0.1, data = dataset[type], step = stp[type]; // Set the functions var y = d3.scale.linear().domain([mi,m]).range([height-margin, margin]).nice(); var o = d3.scale.linear().domain([0,m]).range([.5,1]); var r = d3.scale.linear().domain([0,Math.sqrt(m)]).range([0,30]); var x = d3.scale.linear().domain([0,23]).range([margin,width-margin]); var color = function (a, b) { for (var c = (a - 0) / (m - 0), d = [], e = 0; 3 > e; e++) d.push(Math.round(K[0][e] + c * (K[1][e] - K[0][e]))); d.push(b || 0.7); return "rgba(" + d.join(",") + ")" } //Attach the data to the graph var circle = svg.selectAll("circle").data(data); // Update existing element circle.attr("class", "update"); // Add new element circle.enter() .append("circle") .attr("class", "enter") .attr("stroke-width", 0) .attr("stroke", "black") .transition() .duration(750) .attr("y", 0) .style("fill-opacity", 1); // Apply attribute to new and updated element circle.attr("cx", function(d,i) {return x(d.h);}) .attr("cy", function(d,i) {return y(d.v);}) .attr("r", function(d,i) {return r(Math.sqrt(d.v));}) .style("fill", function(d,i) {return color(d.v);}) .style("opacity", function(d,i) {return o(d.v);}) .on("click", function(d,i){window.open(d.name,'_blank');}) .on("mouseover", function(d,i){d3.select(this).style("fill", "red").attr("stroke-width", 1);}) .on("mouseout", function(d,i){d3.select(this).style("fill", function(d,i) {return color(d.v);}).attr("stroke-width", 0);}) .append("title") .text(function(d) { return d.v+' '+ d.t+' (adjusted) - '+ d.d }) .transition() .duration(750) .attr("y", 0) .style("fill-opacity", 1); // Remove old elements circle.exit() .attr("class", "exit") .transition(750) .ease("linear") .attr("cy", 0) .style("opacity", 0.2) .remove(); // Update the Axis var xAxis = d3.svg.axis().scale(x).orient("bottom"); var yAxis = d3.svg.axis().scale(y).orient("left"); svg.selectAll("g .y.axis") .call(yAxis) svg.selectAll("g .x.axis") .call(xAxis); } The circles are updated correctly (transitions are not working tho) but the axis are not changing and I can't see why. I'm a bit lost, I've looked at many example but I cannot see what I'm doing wrong. I'm using d3.js version: v3.1.10 Maxime
It looks like you are using the wrong selector while updating the axes: svg.selectAll("g .y.axis") .call(yAxis); svg.selectAll("g .x.axis") .call(xAxis); maybe should read: svg.selectAll("g.y.axis") .call(yAxis); svg.selectAll("g.x.axis") .call(xAxis);
Why are you using "g .y.axis"? Try: svg.select(".y.axis") .call(yAxis); And similar for x axis.
Maximum width for column in bar chart
I'm looking for a way of limiting the column width in a chart, I'm sure this ought to be relatively easy but I cant find a way of doing it. I'm populating a chart from some dynamic data, where the number of columns can vary quite dramatically - between 1 and 20. e.g: sample of csv Location,Col1 "Your house",20 Location,Col1,Col2,Col3,Col4,Col5 "My House",12,5,23,1,5 This is working fine, and the col widths are dynamic, however when there is only one column in the data, I end up with one bar of width 756 (the whole chart), and I dont like the way this looks. What I'd like to do is only ever have a maximum column of width 100px irrespective of the number of columns of data. Below is my script for the chart Many thanks, <script> var margin = { top : 40, right : 80, bottom : 80, left : 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.linear().range([ 0, width ]); var y = d3.scale.linear().range([ height, 0 ]); var x0 = d3.scale.ordinal() .rangeRoundBands([0, width], .05); var x1 = d3.scale.ordinal(); var y = d3.scale.linear() .range([height, 0]); var chart = d3.select("body").append("svg") .attr("class","chart") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var legendChart = d3.select("body").append("svg") .attr("class","chart") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.csv("/sampledata.csv.txt", function(error, data) { // Use the first row of csv for header names var reasonNames = d3.keys(data[0]).filter(function(key) { return key !== "Location"; }); //console.log(reasonNames); data.forEach(function(d) { d.reasons = reasonNames.map(function(name) { return { name : name, value : +d[name] }; }); //console.log(d.reasons); }); x0.domain(data.map(function(d) {return d.Location; })); x1.domain(reasonNames).rangeRoundBands([0, x0.rangeBand()]); console.log(x0.rangeBand()); y.domain([0, d3.max(data, function(d) { return d3.max(d.reasons, function(d) { return d.value; }); })]); var maxVal = d3.max(data, function(d) { return d3.max(d.reasons, function(d) { return d.value; }); }); //console.log(maxVal); var xAxis = d3.svg.axis() .scale(x0) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") //.tickFormat(d3.format(".2s")); chart.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); chart.append("g") .attr("class", "y axis") .call(yAxis); var location = chart.selectAll(".name") .data(data) .enter().append("g") .attr("class", "g") .attr("transform", function(d) { return "translate(" + x0(d.Location) + ",0)"; }); location.selectAll("rect") .data(function(d) { return d.reasons; }) .enter().append("rect") .attr("width", x1.rangeBand()-2) .attr("x", function(d) { return x1(d.name); }) .attr("y", function(d) { return y(d.value); }) .attr("height", function(d) { return height - y(d.value); }) .style("fill", function(d,i) { return "#"+3+"9"+i; /*color(d.name);*/ }); chart.selectAll("text") .data(data) .enter().append("text") .attr("x", function(d) { return x1(d.name)+ x.rangeBand() / 2; }) .attr("y", function(d) { return y(d.value); }) .attr("dx", -3) // padding-right .attr("dy", ".35em") // vertical-align: middle .attr("text-anchor", "end") // text-align: right .text("String"); var legend = legendChart.selectAll(".legend") .data(reasonNames.slice().reverse()) .enter() .append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") //.attr("x", width - 18) .attr("x", 18) .attr("width", 18) .attr("height", 18) .style("fill", function(d, i) {/*console.log(i);*/return "#" + 3 + "9" + i; }); legend.append("text") //.attr("x", width - 24) .attr("x", 48) .attr("y", 9).attr("dy",".35em") //.style("text-anchor", "end") //.text(function(d,i) { return String.fromCharCode((65+i))+i; }); .text(function(d) { return d; }); }); </script>
The easiest way to achieve this is by changing the line .attr("width", x1.rangeBand()-2) to .attr("width", Math.min(x1.rangeBand()-2, 100)) You might also want to adjust the starting position and/or padding.
Code for adjusting starting position if anyone is stuck on it: .attr("x", function(d, i) { return x1(d.seriesName) + (x1.rangeBand() - 100)/2 ;}) P.S. : referring answer from Lars.
Setting an absolute maximum width for the columns doesn't allow proper rendering for different screen resolutions, div sizes, etc. In my case, I just wanted the columns not to look so large when the number of columns itself is small I found it easier and more straight-forward to play with the scale definition, by changing the maximum width (where all columns will fit), their inner and outer paddings. var w = 600 // var w = parseInt(d3.select(parentID).style('width'), 10) // retrieve the div width dynamically var inner_padding = 0.1 var outer_padding = 0.8 var xScale = d3.scale.ordinal().rangeRoundBands([0, w], inner_padding, outer_padding) When rendering the plot, I just ran a switch/if-else statement, which assigns different padding values. The lower the number of columns to plot, the greater the outer_padding (and eventually inner-padding) values I use. This way, I keep the plots responsive.
I am able to change the width of the bar using the above answer. But unfortunately, my X Axis labels are not aligned when there is a single bar in the chart and it uses the max width set. var tradeGroup = svg.selectAll("g.trade") .data(trades) .enter() .append("svg:g") .attr("class", "trade") .style("fill", function (d, i) { return self.color(self.color.domain()[i]); }) .style("stroke", function (d, i) { return d3.rgb(self.color(self.color.domain()[i])).darker(); }); var aWidth = Math.min.apply(null, [x.rangeBand(), 100]); // Add a rect for each date. var rect = tradeGroup.selectAll("rect") .data(Object) .enter() .append("svg:rect") .attr("x", function (d) { return x(d.x); }) .attr("y", function (d) { return y( (d.y || 0) + (d.y0 || 0)); }) .attr("height", function (d) { return y(d.y0 || 0) - y((d.y || 0) + (d.y0 || 0)); }) .attr("width", Math.min.apply(null, [x.rangeBand(), 100]));
For completeness the full answer would look like this: svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", (d) -> x1(d.name) + (x1.rangeBand() - d3.min([x1.rangeBand(), 100]))/2) .attr("width", d3.min([x1.rangeBand(), 100])) .attr("y", (d) -> y(d.grade) ) .attr("height", (d)-> height - y(d.value) ) (coffeescript syntax) Note this include the full answer, the 'width' and the 'x' settings. Also 'x' settings is accounting for a when 100 width is not the min value.
Thought I'd share that I came up with a slightly different answer to this. I didn't want to hard code in a maximum bar width because 1) it wasn't responsive to different screen sizes and 2) it also required playing with the x-coordinate attribute or accepting some irregular spacing. Instead, I just set a minimum number of bars, based on the point where the bars became too wide (in my case, I found that less than 12 bars made my chart look weird). I then adjusted the scaleBand's range attribute, proportionately, if there were less than that number of bars. So, for example, if the minimum was set to 12 and there were only 5 items in the data, rather than rendering each of them at 1/5th of the full width, I scaled the range down to 5/12ths of the original width. Basically, something like this: // x is a scaleBand() that was previously defined, and this would run on update var minBarSlots = 12; if (data.length < minBarSlots) { x.range([0, width*(data.length/minBarSlots)]) } else { x.range([0, width]) }`