I'm trying to create a simple line graph using nvd3.js. I got a simple one working which looks like so:
When I manually set my height and width, it all gets cut off.
My code looks like so:
var min_date = data[0].values[0].x
var max_date = data[0].values[0].x
var xScale = d3.time.scale()
.domain([min_date, max_date]);
var width = nv.utils.windowSize().width - 40,
height = nv.utils.windowSize().height - 40;
nv.addGraph(function() {
var chart = nv.models.lineChart()
.useInteractiveGuideline(true)
.height(height)
.width(width)
;
chart.xAxis
.scale(xScale)
.axisLabel('Day')
.tickFormat(function(d){
return d3.time.format('%x')(new Date(d))
})
.tickValues(data[0].values.map(function(d){
return d.x;
}))
;
chart.xScale(d3.time.scale());
chart.yAxis
.axisLabel('Followers')
.tickValues(data[0].values.map(function(d){
return d.y;
}))
.ticks(1)
;
d3.select('#chart svg')
.datum(data)
.transition().duration(500)
.call(chart)
;
nv.utils.windowResize(chart.update);
return chart;
});
I can't seem to figure out what's going on. Any ideas?
Update
Here's a jsfiddle of the top graph, before I try to resize it. http://jsfiddle.net/yw7Jn/1/
Dates can be ignored. They got messed up when I made the fiddle but work fine for me.
If you change the dimensions for the chart, you also need to change the dimensions for the enclosing SVG accordingly:
var chart = nv.models.lineChart()
.useInteractiveGuideline(true)
.height(800)
// ...
d3.select('#chart svg')
.attr("height", 800)
.datum(chartData)
...
Complete demo here.
Related
I need to add circules/nodes to each point on a line chart of nvd3.
I have my codes here - https://jsfiddle.net/sujit77/7ns2g4a1/12/
Also I need to have these circles with different colors depending on value at that point.
I tried to follow "http://www.d3noob.org/2013/01/change-line-chart-into-scatter-plot.html". But messed up my code between D3 and nvd3 functions.
nv.addGraph(function() {
var chart = nv.models.lineChart()
.x(function(d) {
return d3.time.format("%Y-%m-%d %H:%M:%S").parse(d['key']);
})
.y(function(d){ return d.value; })
var format = d3.time.format("%m %d %Y");
chart.yAxis
.tickFormat(d3.format(',.2f'));
chart.xAxis
.tickFormat(function(d) {
return d3.time.format('%Y %b %d')(new Date(d))
});
d3.select('#line-charts').append('svg')
.attr('height', 250)
.attr('width', 400)
.datum(dataValue)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
I found similar question - Draw a circle at a point on an NVD3 line chart, but its unaswered yet.
I am quite new in D3 and nvd3. I am trying to plot a graph with date axis and data axis - https://jsfiddle.net/sujit77/7ns2g4a1/6/.
var dataValue = [{'key':'Line', 'values':[{"key":"2016-04-04 0:0:0","value":1},{"key":"2016-04-05 0:0:0","value":1},{"key":"2016-04-07 0:0:0","value":1},{"key":"2016-04-08 0:0:0","value":1},{"key":"2016-04-11 0:0:0","value":1},{"key":"2016-04-13 0:0:0","value":1},{"key":"2016-04-14 0:0:0","value":0.5},{"key":"2016-04-19 0:0:0","value":1},{"key":"2016-04-20 0:0:0","value":1},{"key":"2016-04-22 0:0:0","value":1},{"key":"2016-04-25 0:0:0","value":1},{"key":"2016-04-26 0:0:0","value":1},{"key":"2016-04-27 0:0:0","value":1},{"key":"2016-04-28 0:0:0","value":1},{"key":"2016-04-29 0:0:0","value":1},{"key":"2016-05-03 0:0:0","value":1},{"key":"2016-05-04 0:0:0","value":0},{"key":"2016-05-06 0:0:0","value":1},{"key":"2016-05-09 0:0:0","value":1},{"key":"2016-05-10 0:0:0","value":1},{"key":"2016-05-11 0:0:0","value":1},{"key":"2016-05-12 0:0:0","value":1},{"key":"2016-05-13 0:0:0","value":1},{"key":"2016-05-16 0:0:0","value":1},{"key":"2016-05-17 0:0:0","value":1},{"key":"2016-05-19 0:0:0","value":1},{"key":"2016-05-24 0:0:0","value":0},{"key":"2016-05-25 0:0:0","value":0.5},{"key":"2016-05-26 0:0:0","value":1}]}];
nv.addGraph(function() {
var chart = nv.models.lineWithFocusChart()
//.showControls(false)
//.showMaxMin(false);
.x(function(d) {return d3.time.format("%Y-%m-%d %H:%M:%S").parse(d['key']);})
var format = d3.time.format("%m %d %Y");
chart.yAxis
.tickFormat(d3.format(',.2f'));
chart.xAxis
.tickFormat(function(d) { return d3.time.format('%Y %b %d')(new Date(d)) });
d3.select('#line-charts').append('svg')
.attr('height', 250)
.attr('width', 400)
.datum(dataValue)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
I am not able to plot the graph which should have been generated using the sample data. I am getting following error in browser's console window -
Error: attribute transform: Trailing garbage, "translate(0,NaN)".
Along with that I am trying to hide the bottom scale and min-max value in X-axis using ".showControls(false)" and ".showMaxMin(false)". But those are not working.
You forgot to do the y accessor.
Do something like:
chart.y(function (d) { return d.value; });
I'm trying to build an interactive population pyramid. I want to update the width of the bars.
Something is not working with my selections though. Female and male bars charts are updated to the same width, because they receive the same datum. I suspect that it has to do with the way I select or join the data. I've tried several combinations, now I'm stuck reading the doc on select and selectAll.
Any comments on what goes wrong here are highly appreciated.
The code (here is the fiddle):
var pyramidCanvas = d3.select("body")
.append("svg")
.attr("width",200)
.attr("height",100);
var pyramidHeight = 100;
var pyramidWidth = 100;
var pyramidBarsRight, pyramidBarsLeft;
function setupPyramid(){
var data = [300,20,40,50,10];
var pyramidHeight = 100;
var pyramidWidth = 100;
var xScale = d3.scale.linear()
.domain([0,d3.max(data,function(d){return d;})])
.range([0,pyramidWidth]);
var barHeight = pyramidHeight/data.length;
console.log(barHeight);
pyramidBarsLeft = pyramidCanvas.selectAll("g-female")
.data(data)
.enter()
.append("g")
.attr("transform",function(d,i){return "translate(0,"+ (i * 10)+ ")" ;});
pyramidBarsLeft.append("rect")
.attr("class","female-bar")
.attr("x",function(d){return pyramidWidth-xScale(d);})
.attr("y",function(d,i){
return i*10;
})
.attr("width",function(d){console.log(d);return xScale(d);})
.attr("height",10)
.style("fill","pink");
pyramidBarsRight = pyramidCanvas.selectAll("g-male")
.data(data)
.enter()
.append("g")
.attr("transform",function(d,i){return "translate(0,"+ (i * 10)+ ")" ;});
pyramidBarsRight.append("rect")
.attr("class","male-bar")
.attr("x",pyramidWidth)
.attr("y",function(d,i){
return i*10;
})
.attr("width",function(d){console.log(d);return xScale(d);})
.attr("height",10)
.style("fill","steelblue");
}
function updatePyramid(data){
var femData = data.slice(0,5);
var malData = data.slice(5,9);
console.log(femData);
console.log(malData);
console.log(pyramidBarsRight.selectAll("rect"));
var xScale = d3.scale.linear()
.domain([0,d3.max(data,function(d){return d;})])
.range([0,pyramidWidth]);
pyramidBarsLeft
.selectAll(".female-bar")
.data(femData)
.transition()
.duration(500)
.attr("x",function(d){return pyramidWidth-xScale(d);})
.attr("width",function(d){console.log(d);return xScale(d);})
pyramidBarsRight
.selectAll(".male-bar")
.data(malData)
.transition()
.duration(500)
.attr("width",function(d){console.log(d);return xScale(d);});
}
setupPyramid();
updatePyramid([20,10,50,100,600,30,22,54,91,19,10]);
You shouldn't save the result of an enter selection and use it like that. It works fine it you select from the canvas, see here. The reason is that elements in the enter selection merge into the update selection once DOM elements have been appended.
I have problem adding text in my histogram. I can do this in more simple example.
I try to do this:
// try to add bar value
var barnum = g.selectAll('text')
.data(layout)
.enter()
.append("text")
.attr('y',-10)
.attr('x',10)
.attr("text-anchor", "middle")
.style("fill","black")
.text('testtest')
.style("pointer-events", "none")
;
barnum.transition();
I can't see any text in my figure. The code include definition is here:
var dateFormat = d3.time.format("%Y-%m-%d");
var g;
var data;
var margin = {top: 30, right: 30, bottom: 80, left: 80},
width = 500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var cx = 10;
var numberBins = 5;
var dispatch = d3.dispatch(chart, "hover");
function chart(container) {
g = container;
update();
}
chart.update = update;
function update() {
// create hist layout
var hist = d3.layout.histogram()
.value(function(d) { return d.selectvar })
.range([d3.min(data, function(d){ return d.selectvar }) , d3.max(data, function(d){ return d.selectvar }) ])
.bins(numberBins);
var layout = hist(data);
var maxLength = d3.max(layout, function(d) { return d.length });
var widthScale = d3.scale.linear()
.domain([0, maxLength])
.range([0, width])
var yScale = d3.scale.ordinal()
.domain(d3.range(numberBins))
.rangeBands([height, 0], 0)
var colorScale = d3.scale.category20();
// create svg
var rects = g.selectAll("rect")
.data(layout)
rects.enter().append("rect")
rects .transition()
.duration(500)
.attr({
y: function(d,i) {
return yScale(i)
},
x: 50,
height: yScale.rangeBand(),
width: function(d,i) {
return widthScale(d.length)
},
fill: function(d, i) { return colorScale(i) }
});
rects.exit().transition().remove();
// try to add bar value
var barnum = g.selectAll('text')
.data(layout)
.enter()
.append("text")
.attr('y',-10)
.attr('x',10)
.attr("text-anchor", "middle")
.style("fill","black")
.text('testtest')
.style("pointer-events", "none")
;
barnum.transition();
is there something wrong with my way to create svg element? I found out some successful case use append('g') from the beginning. New to d3.js! thank you.
You're using d3.dispatch, which is documented on a page titled Internals. That doesn't mean you shouldn't use it, but rather, it shouldn't be your first choice.
You're correct that there's "something wrong with my way to create svg element" -- you're not creating one! Try:
var svg = d3.select("body").append("svg");
var g = svg.append("g");
At this point, you need to have a good understanding of the DOM, the SVG standard, CSS selectors, and D3's selection API to make things work. You don't tell D3 to put labels on your bars and that's it. You have to instruct it what elements to create, and where, keeping track of translates and offsets and stuff like that. You're best off copying and studying one of Mike Bostock's many examples.
D3 is not learned quickly. You need to invest time learning it before you can make any chart you like.
How can I invert the xAxis for the NVD3 MultiBar chart? The data is the same (except I had to convert an {x: ... , y: ... } object for the values property to a paired array for the stacked area chart. The data is displaying correctly on the lineChart as well.
FYI, the date data is ordered from latest to earliest in the array but that shouldn't matter for a time scale, right?
// Stacked Area Chart
var renderStackedAreaChart = function(data){
var newData = convertData(data);
nv.addGraph(function() {
var chart = nv.models.stackedAreaChart()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; })
.clipEdge(true);
chart.xAxis
.tickFormat(function(d) { return d3.time.format('%b')(new Date(d)); });
chart.yAxis
.tickFormat(d3.format('$,.2f'));
d3.select('#graph svg')
.datum(newData)
.transition().duration(500).call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
};
// Stacked MultiBar Chart
var renderStackedMultiBar = function(data) {
nv.addGraph(function() {
var chart = nv.models.multiBarChart();
chart.xAxis
.tickFormat(function(d) { return d3.time.format('%b')(new Date(d)); });
chart.yAxis
.tickFormat(d3.format('$,.2f'));
d3.select('#graph svg')
.datum(data)
.transition().duration(500).call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
}
This isn't supported by NVD3 -- you would have to modify the source code. A quick and dirty workaround is (as you've suggested in the comments) to use an ordinal scale instead of a time scale. This way, the order of the values you specify as domain matters and you can simply pass it in in reverse order.
Note that you'll lose the advantages of a time scale this way though, e.g. automatic label format depending on the time range shown.