Unneeded white space before the 1st bar in D3 bar chart - javascript
I am trying to populate a data set into D3's Bar chart data. I am using this example from the d3: http://bl.ocks.org/mbostock/3885304
Here is my sample data:
var data = [{"name":"0","members_count":4},{"name":"1","members_count":1},{"name":"2","members_count":6},{"name":"3","members_count":1},{"name":"4","members_count":1},{"name":"5","members_count":2},{"name":"6","members_count":4},{"name":"7","members_count":3},{"name":"8","members_count":5},{"name":"9","members_count":1},{"name":"10","members_count":2},{"name":"11","members_count":3},{"name":"12","members_count":2},{"name":"13","members_count":0},{"name":"14","members_count":5},{"name":"15","members_count":2},{"name":"16","members_count":2},{"name":"17","members_count":4},{"name":"18","members_count":2},{"name":"19","members_count":4},{"name":"20","members_count":3},{"name":"21","members_count":5},{"name":"22","members_count":8},{"name":"23","members_count":3},{"name":"24","members_count":2},{"name":"25","members_count":3},{"name":"26","members_count":4},{"name":"27","members_count":4},{"name":"28","members_count":1},{"name":"29","members_count":1},{"name":"30","members_count":3},{"name":"31","members_count":5},{"name":"32","members_count":4},{"name":"33","members_count":3},{"name":"34","members_count":6},{"name":"35","members_count":2},{"name":"36","members_count":2},{"name":"37","members_count":7},{"name":"38","members_count":2},{"name":"39","members_count":4},{"name":"40","members_count":2},{"name":"41","members_count":1},{"name":"42","members_count":8},{"name":"43","members_count":10},{"name":"44","members_count":1},{"name":"45","members_count":5},{"name":"46","members_count":2},{"name":"47","members_count":5},{"name":"48","members_count":3},{"name":"49","members_count":1},{"name":"50","members_count":9},{"name":"51","members_count":3},{"name":"52","members_count":2},{"name":"53","members_count":8},{"name":"54","members_count":2},{"name":"55","members_count":3},{"name":"56","members_count":2},{"name":"57","members_count":2},{"name":"58","members_count":5},{"name":"59","members_count":1},{"name":"60","members_count":2},{"name":"61","members_count":4},{"name":"62","members_count":2},{"name":"63","members_count":2},{"name":"64","members_count":6},{"name":"65","members_count":6},{"name":"66","members_count":1},{"name":"67","members_count":1},{"name":"68","members_count":3},{"name":"69","members_count":5},{"name":"70","members_count":3},{"name":"71","members_count":4},{"name":"72","members_count":4},{"name":"73","members_count":1},{"name":"74","members_count":3},{"name":"75","members_count":2},{"name":"76","members_count":4},{"name":"77","members_count":5},{"name":"78","members_count":2},{"name":"79","members_count":1},{"name":"80","members_count":2},{"name":"81","members_count":1},{"name":"82","members_count":4},{"name":"83","members_count":3},{"name":"84","members_count":1},{"name":"85","members_count":4},{"name":"86","members_count":1},{"name":"87","members_count":3},{"name":"88","members_count":1},{"name":"89","members_count":8},{"name":"90","members_count":6},{"name":"91","members_count":6},{"name":"92","members_count":3},{"name":"93","members_count":4},{"name":"94","members_count":5},{"name":"95","members_count":2},{"name":"96","members_count":3},{"name":"97","members_count":2},{"name":"98","members_count":3},{"name":"99","members_count":6},{"name":"100","members_count":3},{"name":"101","members_count":1},{"name":"102","members_count":0},{"name":"103","members_count":5},{"name":"104","members_count":2},{"name":"105","members_count":1},{"name":"106","members_count":7},{"name":"107","members_count":1},{"name":"108","members_count":1},{"name":"109","members_count":7},{"name":"110","members_count":5},{"name":"111","members_count":5},{"name":"112","members_count":2},{"name":"113","members_count":6},{"name":"114","members_count":1},{"name":"115","members_count":2},{"name":"116","members_count":1},{"name":"117","members_count":1},{"name":"118","members_count":10},{"name":"119","members_count":2},{"name":"120","members_count":6},{"name":"121","members_count":1},{"name":"122","members_count":1},{"name":"123","members_count":2},{"name":"124","members_count":4},{"name":"125","members_count":5},{"name":"126","members_count":1},{"name":"127","members_count":1},{"name":"128","members_count":1},{"name":"129","members_count":3},{"name":"130","members_count":3},{"name":"131","members_count":3},{"name":"132","members_count":4},{"name":"133","members_count":3},{"name":"134","members_count":1},{"name":"135","members_count":5},{"name":"136","members_count":5},{"name":"137","members_count":1},{"name":"138","members_count":2},{"name":"139","members_count":5},{"name":"140","members_count":1},{"name":"141","members_count":1},{"name":"142","members_count":2},{"name":"143","members_count":2},{"name":"144","members_count":4},{"name":"145","members_count":4},{"name":"146","members_count":3},{"name":"147","members_count":5},{"name":"148","members_count":2},{"name":"149","members_count":1},{"name":"150","members_count":2},{"name":"151","members_count":3},{"name":"152","members_count":0},{"name":"153","members_count":3},{"name":"154","members_count":1},{"name":"155","members_count":2},{"name":"156","members_count":2},{"name":"157","members_count":1},{"name":"158","members_count":5},{"name":"159","members_count":4},{"name":"160","members_count":4},{"name":"161","members_count":5},{"name":"162","members_count":4},{"name":"163","members_count":2},{"name":"164","members_count":4},{"name":"165","members_count":2},{"name":"166","members_count":2},{"name":"167","members_count":1},{"name":"168","members_count":1},{"name":"169","members_count":10},{"name":"170","members_count":4},{"name":"171","members_count":2},{"name":"172","members_count":2},{"name":"173","members_count":1},{"name":"174","members_count":5},{"name":"175","members_count":1},{"name":"176","members_count":1},{"name":"177","members_count":5},{"name":"178","members_count":3},{"name":"179","members_count":1},{"name":"180","members_count":5},{"name":"181","members_count":8},{"name":"182","members_count":4},{"name":"183","members_count":2},{"name":"184","members_count":1},{"name":"185","members_count":3},{"name":"186","members_count":1},{"name":"187","members_count":3},{"name":"188","members_count":1},{"name":"189","members_count":1},{"name":"190","members_count":6},{"name":"191","members_count":4},{"name":"192","members_count":5},{"name":"193","members_count":1},{"name":"194","members_count":4},{"name":"195","members_count":2},{"name":"196","members_count":2},{"name":"197","members_count":2},{"name":"198","members_count":0},{"name":"199","members_count":3},{"name":"200","members_count":3},{"name":"201","members_count":1},{"name":"202","members_count":1},{"name":"203","members_count":1},{"name":"204","members_count":5},{"name":"205","members_count":1},{"name":"206","members_count":1},{"name":"207","members_count":1},{"name":"208","members_count":1},{"name":"209","members_count":1},{"name":"210","members_count":5},{"name":"211","members_count":2},{"name":"212","members_count":1},{"name":"213","members_count":5},{"name":"214","members_count":1},{"name":"215","members_count":3},{"name":"216","members_count":1},{"name":"217","members_count":1},{"name":"218","members_count":4},{"name":"219","members_count":2},{"name":"220","members_count":1},{"name":"221","members_count":3},{"name":"222","members_count":1},{"name":"223","members_count":3},{"name":"224","members_count":1},{"name":"225","members_count":2},{"name":"226","members_count":1},{"name":"227","members_count":1},{"name":"228","members_count":11},{"name":"229","members_count":9},{"name":"230","members_count":9},{"name":"231","members_count":9},{"name":"232","members_count":10},{"name":"233","members_count":10},{"name":"234","members_count":10},{"name":"235","members_count":9},{"name":"236","members_count":10},{"name":"237","members_count":10},{"name":"238","members_count":10},{"name":"239","members_count":10},{"name":"240","members_count":9},{"name":"241","members_count":9},{"name":"242","members_count":9},{"name":"243","members_count":10},{"name":"244","members_count":10},{"name":"245","members_count":9},{"name":"246","members_count":2},{"name":"247","members_count":1},{"name":"248","members_count":2},{"name":"249","members_count":2},{"name":"250","members_count":1},{"name":"251","members_count":2},{"name":"252","members_count":1},{"name":"253","members_count":5},{"name":"254","members_count":1},{"name":"255","members_count":1},{"name":"256","members_count":1},{"name":"257","members_count":1},{"name":"258","members_count":1},{"name":"259","members_count":1},{"name":"260","members_count":2},{"name":"261","members_count":1},{"name":"262","members_count":1},{"name":"263","members_count":2},{"name":"264","members_count":1},{"name":"265","members_count":1},{"name":"266","members_count":1},{"name":"267","members_count":1},{"name":"268","members_count":2},{"name":"269","members_count":2},{"name":"270","members_count":1},{"name":"271","members_count":1},{"name":"272","members_count":0},{"name":"273","members_count":2},{"name":"274","members_count":1},{"name":"275","members_count":2},{"name":"276","members_count":2},{"name":"277","members_count":1},{"name":"278","members_count":3},{"name":"279","members_count":1},{"name":"280","members_count":1},{"name":"281","members_count":1},{"name":"282","members_count":1},{"name":"283","members_count":1},{"name":"284","members_count":1},{"name":"285","members_count":1},{"name":"286","members_count":1},{"name":"287","members_count":1},{"name":"288","members_count":1},{"name":"289","members_count":1},{"name":"290","members_count":1},{"name":"291","members_count":1},{"name":"292","members_count":1},{"name":"293","members_count":1},{"name":"294","members_count":1},{"name":"295","members_count":1},{"name":"296","members_count":1},{"name":"297","members_count":2},{"name":"298","members_count":2},{"name":"299","members_count":1},{"name":"300","members_count":1},{"name":"301","members_count":1},{"name":"302","members_count":1},{"name":"303","members_count":1},{"name":"304","members_count":1},{"name":"305","members_count":1},{"name":"306","members_count":1},{"name":"307","members_count":1},{"name":"308","members_count":1},{"name":"309","members_count":1},{"name":"310","members_count":1},{"name":"311","members_count":1},{"name":"312","members_count":1},{"name":"313","members_count":0},{"name":"314","members_count":1}]
This is chart's drawing code.
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = (960 - margin.left - margin.right) * data.length/50,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.domain([0,1])
.rangeRoundBands([0, width], 0.1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
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)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(data.map(function(d) { return d.name; }));
y.domain([0, d3.max(data, function(d) { return d.members_count; })]);
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("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("fill","green")
.attr("x", function(d) { return x(d.name); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.members_count); })
.attr("height", function(d) { return height - y(d.members_count); });
This issue I am getting is that I have some white space which looks ugly. This space come before the 1st bar and also after the last bar of the chart. I have tried tweaking the x value of the bar, But I think that is not a good way to do.
This space does not come when the data set is small. But when dataset is large then this space comes up. How can I remove this space from the start and from end.
The problem is with this line in scale:
.rangeRoundBands([0, width], 0.1);
When there is not specified the third parameter outerPadding it adds before and after the chart extra space produced by the padding parameter for each bar. You have set the padding to 0.1. To solve this problem set the outerPadding parameter:
var x = d3.scale.ordinal()
.domain([0,1])
.rangeRoundBands([0, width], 0.1, 0);
Here is updated your code: http://jsfiddle.net/ojntup1a/
Related
how do i clear my visualization in order to make space for the updated visualization using svg?
here's my d3.js function to visualize a bar graph: function barGraph(data1) { // console(data1.count); var i = 0; data1.forEach(function(d){ while(i>0){ d.avspeed= +d.avspeed; d.duration = +d.duration; i--; } }) //console.log(data1.avspeed); // console.log(data1.avspeed); var margin = {top: 40, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; //console.log(data1.avspeed); console.log("hey1"); var formatPercent = d3.format(""); console.log("hey2"); var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); console.log("hey"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickFormat(formatPercent); console.log("hey3"); var tip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { return "<strong>Avg Speed:</strong> <span style='color:red'>" + d.avspeed + "</span>"; }) var svg = d3.select("#rightside").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.call(tip); console.log("heyllo "); x.domain(data1.map(function(d) { return d.tripid; })); y.domain([0, d3.max(data1, function(d) { return d.avspeed; })]); 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("Avg Speed"); svg.selectAll(".bar") .data(data1) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.tripid); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.avspeed); }) .attr("height", function(d) { return height - y(d.avspeed); }) .on('mouseover', tip.show) .on('mouseout', tip.hide) console.log(d.avspeed); console.log("hello234"); function type(d) { d.avspeed = +d.avspeed; return d; } } It displays a graph based on the selected region on the map dynamically. If i select new region, another graph is being created below the old graph. I want the old graph to clear and new graph to be in place of old graph. How do I achieve that. I am new to d3.js Thanks in advance.
You can clear away anything inside of the parent container with .html("") while creating your svg: var svg = d3.select("#rightside").html("").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 + ")"); remove also works but I prefer this slightly terser way of doing it.
Before appending the new avg, just call d3.select('svg').remove(); That will remove the first svg on the page (I'm assuming you only have one). If you don't have an svg, it won't fail, so you can always call it.
D3.js: Changing the tick values on x-axis based on the screen width
I am making a responsive D3.js Bar Chart, by setting the height to clientWidth. Though the chart resizes after reloading in a new size, when the size is small the x-axis values overlap. I need to avoid this, how can i do it? Script: clientw = document.body.clientWidth; var margin = {top: 40, right: 20, bottom: 30, left: 40}, width = clientw - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(10) .tickFormat(d3.format("s")); var tip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { return "<strong>Hola:</strong> <span style='color:red'>" + d.hola+ "°C"+"</span>"; }) 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.call(tip); d3.tsv("data.tsv", type, function(error, data) { x.domain(data.map(function(d) { return d.hermanos; })); y.domain([0, 45]); 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("Hola"); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("width", x) .attr("fill","grey") .attr("class", "bar") .attr("x", function(d) { return x(d.hermanos); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.hola); }) .attr("height", function(d) { return height- y(d.hola); }) .on('mouseover', tip.show) .on('mouseout', tip.hide) }); function type(d) { d.hola= +d.hola; return d; }
Plotting a line graph along with Bar graph in d3js. Issue with Date
I'm learning d3js using various examples found online. I've been trying to plot a chart with dual Y axis and an X-axis. The Y axis on the left side would plot a bar chart against the X-axis and the Y-axis on the right side would plot a line chart against X-axis. The Bar graph plots as exactly as required but the line graph does not. The X-axis is date (2015-10-15 04:10). Following this example. The code I wrote var margin = {top: 50, right: 50, bottom: 100, left: 50}, width = 900 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var parseDate = d3.time.format("%m/%d/%Y %H:%M:%S").parse; var x = d3.scale.ordinal().rangeRoundBands([0, width], .05); var yTxnVol = d3.scale.linear().range([height, 0]); var yResTime = d3.scale.linear().range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") var yAxis = d3.svg.axis() .scale(yTxnVol) .orient("left") var yAxis2 = d3.svg.axis() .scale(yResTime) .orient("right") .ticks(10); var svg = d3.selectAll("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 + ")"); d3.csv("../res/data.csv", function(error, data) { data.forEach(function(d) { d.AVRG_RESP_TIME = +d.AVRG_RESP_TIME; d.TXN_VOL = +d.TXN_VOL; }); x.domain(data.map(function(d) { return d.TYM; })); yTxnVol.domain([0, d3.max(data, function(d) { return d.TXN_VOL+50; })]); yResTime.domain([0, d3.max(data, function(d) { return d.AVRG_RESP_TIME+50; })]); var minDate = d3.min(data, function(d){return d.TYM}); var maxDate = d3.max(data, function(d){ return d.TYM}); var xScale = d3.time.scale().range([0,width]);//.domain([minDate, maxDate]); xScale.domain(d3.extent(data, function(d) { return new Date(d.TYM); })); 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", "-.55em") .attr("transform", "rotate(-90)" ); svg.append("g") .attr("class", "y axis") .call(yAxis) svg.append("g") .attr("class","y axis") .attr("transform","translate("+width+ ", 0)") .call(yAxis2) svg.selectAll("bar") .data(data) .enter().append("rect") .attr("class", "yhover") .attr("x", function(d) { return x(d.TYM); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return yTxnVol(d.TXN_VOL); }) .attr("height", function(d) { return height - yTxnVol(d.TXN_VOL); }) var line = d3.svg.line() .x(function(d) { return xScale(new Date(d.TYM));}) .y(function(d) { return d.AVRG_RESP_TIME; }); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line); }); The Output Trying to make this to a meaningful line graph. Got NaN error while formatting the dates. Could someone help me to make this a proper line graph ? The csv data sample TYM, AVRG_RESP_TIME, TXN_VOL 2015-10-15 04:00:00, 12, 170 2015-10-15 04:10:00, 18, 220 2015-10-15 04:20:00, 28, 251 2015-10-15 05:00:00, 19, 100
First, fix your csv file. It is improperly formatted and should not have spaces after the comma. Second, You are trying to mix an ordinal scale and a time scale for you xAxis. This isn't going to work. For your use case, just stick with time. Here's a reworking of your code with explanatory comments: <!DOCTYPE html> <html> <head> <script data-require="d3#3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script> </head> <body> <script> var margin = { top: 50, right: 50, bottom: 100, left: 50 }, width = 900 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse; // x scale should be time and only time var x = d3.time.scale().range([0, width]); var yTxnVol = d3.scale.linear().range([height, 0]); var yResTime = d3.scale.linear().range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") var yAxis = d3.svg.axis() .scale(yTxnVol) .orient("left") var yAxis2 = d3.svg.axis() .scale(yResTime) .orient("right") .ticks(10); var svg = d3.selectAll("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 + ")"); //d3.csv("data.csv", function(error, data) { var data = [{"TYM":"2015-10-15 04:00:00","AVRG_RESP_TIME":"12","TXN_VOL":"170"},{"TYM":"2015-10-15 04:10:00","AVRG_RESP_TIME":"18","TXN_VOL":"220"},{"TYM":"2015-10-15 04:20:00","AVRG_RESP_TIME":"28","TXN_VOL":"251"},{"TYM":"2015-10-15 05:00:00","AVRG_RESP_TIME":"19","TXN_VOL":"100"}]; // just make TYM a date and keep it as a date data.forEach(function(d) { d.TYM = parseDate(d.TYM); d.AVRG_RESP_TIME = +d.AVRG_RESP_TIME; d.TXN_VOL = +d.TXN_VOL; }); // get our min and max date in milliseconds // set a padding around our domain of 15% var minDate = d3.min(data, function(d){ return d.TYM; }).getTime(); var maxDate = d3.max(data, function(d){ return d.TYM; }).getTime(); var padDate = (maxDate - minDate) * .15; x.domain([new Date(minDate - padDate), new Date(maxDate + padDate)]); yTxnVol.domain([0, d3.max(data, function(d) { return d.TXN_VOL + 50; })]); yResTime.domain([0, d3.max(data, function(d) { return d.AVRG_RESP_TIME + 50; })]); // set an intelligent bar width var barWidth = (width / x.ticks().length) - 20; 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", "-.55em") .attr("transform", "rotate(-90)"); svg.append("g") .attr("class", "y axis") .call(yAxis) svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + width + ", 0)") .call(yAxis2) svg.selectAll("bar") .data(data) .enter().append("rect") .attr("class", "yhover") .attr("x", function(d) { // center bar on time return x(d.TYM) - (barWidth / 2); }) .attr("width", barWidth) .attr("y", function(d) { return yTxnVol(d.TXN_VOL); }) .attr("height", function(d) { return height - yTxnVol(d.TXN_VOL); }) .style("fill","orange"); var line = d3.svg.line() .x(function(d) { return x(d.TYM); }) .y(function(d) { return d.AVRG_RESP_TIME; }); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line) .style("fill","none") .style("stroke","steelblue") .style("stoke-width","3px"); // }); </script> </body> </html>
The issue with the line graph filling with black was due to improper css. The new css property. .line { fill: none; stroke: darkgreen; stroke-width: 2.5px; } For the dates I formatted it to (%Y-%m-%d %H:%M) format and it worked.
what scale to use for representing years only on x axis in d3 js
I have been developing an area chart for year(x axis) vs Revenue (y axis) in D3 Js.The data is as: localData=[ {"Revenue":"4.5","Year":"2011"}, {"Revenue":"5.5","Year":"2010"}, {"Revenue":"7.0","Year":"2012"}, {"Revenue":"6.5","Year":"2013"} ] I want year at x axis and revenue at y axis for an area chart.Currently I am using time scale for x axis but i dont know how to use it as I have not date format I have only Years to represent. My Current code is: var margin = { top: 20, right: 20, bottom: 30, left: 50 }, width = 500 - margin.left - margin.right, height = 300 - margin.top - margin.bottom; var parseDate = d3.time.format("%Y").parse; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var area = d3.svg.area() .x(function (d) { return x(d.Year); }) .y0(height) .y1(function (d) { return y(d.Revenue); }); $("#chartArea").html(""); var svg = d3.select("#chartArea").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 + ")"); x.domain(d3.extent(localData, function (d) { return d.Year; })); y.domain([0, d3.max(localData, function (d) { return d.Revenue; })]); svg.append("path") .datum(localData) .attr("class", "area") .attr("d", area) .attr("fill",color); 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("Revenue (M)"); Currently I am getting on my X axis as .011,.012,013,.014 I need as 2011,2012,2013,2014 I am new to D3 js so dnt know much about how to use scales??Please Help anyone..Thanks in advance.
Just add tick Format to your x Axis definition: var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .tickFormat(d3.time.format("%Y")); // <-- format
In v4, it'd be something like this: d3.axisBottom(x).ticks(d3.timeYear.every(1))
huan feng is right. The time scale is treating your year(implicitly converted to an int) as timestamps. To force the scale to operate on the year, create Date objects specifying the year in it. Basically change the following line: x.domain(d3.extent(localData, function (d) { return d.Year; })); to x.domain(d3.extent(localData, function (d) { return new Date(parseInt(d.Year),0); })); You may then use Klaujesi's solution to change the tickFormat.
How do I create a column chart with negative values?
I am trying to essentially rotate this horizontal bar chart into a vertical bar chart, but can't figure out how to do so. I can create a normal column chart, but once I try to put in the negative values and compute the y and height, all hell breaks loose. Here's my fiddle. (At least I was able to create the y-axis (I think).) What am I doing wrong here? var data = [{"letter":"A",'frequency':10},{"letter":"B","frequency":-5},{"letter":"C","frequency":7}]; var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 750 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.ordinal().rangeRoundBands([0, width], .1); var y = d3.scale.linear().range([height, 0]); 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 + ")"); var x0 = Math.max(-d3.min(data), d3.max(data)); x.domain(data.map(function(d) { return d.letter; })); y.domain([d3.min(data, function(d) { return d.frequency; }), d3.max(data, function(d) { return d.frequency; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis); svg.selectAll(".bar") .data(data).enter().append("rect") .attr("class", "bar") .attr("x", function(d, i) { return x(d.letter); }) .attr("y", function(d, i) { return x(Math.min(0, d.frequency));}) .attr("width", x.rangeBand()) .attr("height", function(d) { return Math.abs(x(d.frequency) - x(0)); });
Looks like there are two problems here: The typos: .attr("y", function(d, i) { return x(...);}) should now be .attr("y", function(d, i) { return y(...);}). Same is true for the scales in your height attribute. The change from a 0 base on the X axis to a 0 base on the Y axis. With a zero-based bar on the X axis, the x attribute of the bar is x(0). With a 0 based bar on the Y axis, the y attribute of the bar is not y(0), but y(value) (because the "base" of the bar is no longer the leading edge of the rectangle) - so in this code you need to use Math.max(0, value) (which will give y(value) for positive values) instead of Math.min(0, value): svg.selectAll(".bar") // ...snip... .attr("y", function(d, i) { return y(Math.max(0, d.frequency));}) .attr("height", function(d) { return Math.abs(y(d.frequency) - y(0)); }); See updated fiddle: http://jsfiddle.net/pYZn8/5/