I am learning d3 and trying to implement a specific functionality.
Data is not getting plotted properly. I am not able to find where it is going wrong.
Link to jsfiddle https://jsfiddle.net/4nc1nc95/1/
$(function() {
var width = 355;
var height = 142;
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 70
};
var scope = {
graphData: {
"data": [{
"YEAR": "FY16Q1",
"SAVINGS": null,
"SPEND": null
}, {
"YEAR": "FY16Q2",
"SAVINGS": null,
"SPEND": null
}, {
"YEAR": "FY16Q3",
"SAVINGS": null,
"SPEND": null
}, {
"YEAR": "FY16Q4",
"SAVINGS": "0.023961",
"SPEND": "7419879.04"
}, {
"YEAR": "FY17Q1",
"SAVINGS": "0.00618",
"SPEND": "34923499.71732"
}]
}
};
var x_domain = d3.extent(scope.graphData.data, function(d) {
return d.YEAR;
});
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .0)
.domain(scope.graphData.data.map(function(d) {
return d.YEAR;
}));
var y = d3.scale.linear()
.domain([0, 0.03])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5, "%");
var area = d3.svg.area()
.x(function(d) {
return x(d.YEAR);
})
.y0(height)
.y1(function(d) {
return y(d.SAVINGS);
});
var sampleSVG = d3.select('#area')
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + 62)
.append("g")
.attr("transform", "translate(" + margin.left + "," + (margin.top + 24) + ")");
sampleSVG.append("path")
.datum(scope.graphData.data)
.attr("class", "area")
.attr("d", area);
sampleSVG.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
sampleSVG.append("g")
.attr("class", "y axis")
.call(yAxis);
if (scope.mode == "restore") {
sampleSVG.append("g")
.append("text")
.attr("y", "-8%")
.attr("x", "-2%")
.attr("class", "heading tableHeader")
.text("SAVING");
}
})
Instead of:
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .0)
Use:
var x = d3.scale.ordinal()
.rangePoints([0, width], .0)
Here is your fiddle: https://jsfiddle.net/gerardofurtado/b1mzyxLq/
Related
I have a problem with a bar chart and its line average.
The average line doesn't cover all the bars. The last bar is without average line.
I used d3js code.
var data = [{
"session": "1",
"score": 70
},
{
"session": "2",
"score": 30
},
{
"session": "3",
"score": 50
},
{
"session": "4",
"score": 60
},
{
"session": "5",
"score": 40
}
];
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 480 - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x2 = d3.scale.ordinal()
.rangeBands([0, width], 0);
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 + ")");
data.forEach(function(d) {
d.score = +d.score;
});
x.domain(data.map(function(d) {
return d.session;
}));
x2.domain(data.map(function(d) {
return d.session;
}));
y.domain([0, d3.max(data, function(d) {
return d.score;
})]);
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("score");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.session);
})
.attr("width", x.rangeBand())
.attr("y", function(d) {
return y(d.score);
})
.attr("height", function(d) {
return height - y(d.score);
});
var dataSum = d3.sum(data, function(d) {
return d.score;
});
var line = d3.svg.line()
.x(function(d, i) {
return x2(d.session) + i;
})
.y(function(d, i) {
return y(dataSum / data.length);
});
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("stroke-width", "2")
.attr("d", line);
<!DOCTYPE html>
<meta name="robots" content="noindex">
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
rect.bar {
fill: cyan;
}
path.line {
stroke: black;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
</body>
</html>
The existing code was appending a multi-segment path that stopped short because it was going from the start point of each entry in data to the next start point. This example replaces it with a single line that goes from the left most element's start position to the far right side of the graph.
var data = [{
"session": "1",
"score": 70
},
{
"session": "2",
"score": 30
},
{
"session": "3",
"score": 50
},
{
"session": "4",
"score": 60
},
{
"session": "5",
"score": 40
}
];
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 480 - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x2 = d3.scale.ordinal()
.rangeBands([0, width], 0);
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 + ")");
data.forEach(function(d) {
d.score = +d.score;
});
x.domain(data.map(function(d) {
return d.session;
}));
x2.domain(data.map(function(d) {
return d.session;
}));
y.domain([0, d3.max(data, function(d) {
return d.score;
})]);
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("score");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.session);
})
.attr("width", x.rangeBand())
.attr("y", function(d) {
return y(d.score);
})
.attr("height", function(d) {
return height - y(d.score);
});
var dataSum = d3.sum(data, function(d) {
return d.score;
});
svg.append("line")
.attr("x1", x2(1))
.attr("x2", width)
.attr("y1", y(dataSum / data.length))
.attr("y2", y(dataSum / data.length));
<!DOCTYPE html>
<meta name="robots" content="noindex">
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
rect.bar {
fill: cyan;
}
line {
stroke: black;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
</body>
</html>
I would like to show labels on the circles of the scatter plot when they are not colliding/overlapping. A simple example of scatter plot with labels is as follows:
var width = 500;
var height = 500;
var margin = {
top: 40,
right: 40,
bottom: 40,
left: 40
};
var x = d3.scale.linear().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var minX = _(data).orderBy('x').first().x;
var maxX = _(data).orderBy('x').last().x;
x.domain([minX - 500, maxX + 500]);
y.domain([0, 100]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3
.select("#d3")
.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 / 2 + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width / 2 + "," + 0 + ")")
.call(yAxis)
.append("text");
var gdots = svg.selectAll("g.dot")
.data(data)
.enter().append('g');
gdots.append("circle")
.attr("class", "dot")
.attr("r", function (d) {
return d.r;
})
.attr("cx", function (d) {
return x(d.x);
})
.attr("cy", function (d) {
return y(d.y);
})
.style("fill", function (d) {
return d.c;
});
gdots.append("text").text(function(d){
return d.name;
})
.attr("x", function (d) {
return x(d.x);
})
.attr("y", function (d) {
return y(d.y);
});
The example is available on fiddle:
https://jsfiddle.net/8e7qmzw8/1/
How can I apply collision detection in the given example to show the labels for non-collided circles?
Here's a brute force search approach:
gdots
// filter out those in a colliding state
.filter(function(d0, i){
var isCollide = false,
x0 = x(d0.x),
y0 = y(d0.y);
gdots.data().forEach(function(d1,j){
// if it has a collision with another, stop looking
if (!isCollide){
// if it's not itself
if (d0.name != d1.name){
var x1 = x(d1.x),
y1 = y(d1.y);
// if they overlap
isCollide = Math.pow((x1-x0),2) + Math.pow((y1-y0),2) <= Math.pow((d0.r+d1.r), 2);
}
}
});
return !isCollide;
})
.append("text").text(function(d){
return d.name;
});
Running code:
var data = [
{"x": -123, "y": 63, "r": 37, "c": "#50C2E3", "name": "A"},
{"x": 71, "y": 0, "r": 15, "c": "#50C2E3", "name": "B"},
{"x": 3845, "y": 77, "r": 15, "c": "#50C2E3", "name": "C"},
{"x": 3176, "y": 90, "r": 15, "c": "#50C2E3", "name": "D"},
{"x": -17, "y": 56, "r": 15, "c": "#50C2E3", "name": "D"},
{"x": 1357, "y": 58, "r": 15, "c": "#50C2E3", "name": "E"},
{"x": 7684, "y": 75, "r": 15, "c": "#50C2E3", "name": "F"}
];
var width = 500;
var height = 500;
var margin = {
top: 40,
right: 40,
bottom: 40,
left: 40
};
var x = d3.scale.linear().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var minX = _(data).orderBy('x').first().x;
var maxX = _(data).orderBy('x').last().x;
x.domain([minX - 500, maxX + 500]);
y.domain([0, 100]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3
.select("#d3")
.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 / 2 + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width / 2 + "," + 0 + ")")
.call(yAxis)
.append("text");
var gdots = svg.selectAll("g.dot")
.data(data)
.enter().append('g');
gdots.append("circle")
.attr("class", "dot")
.attr("r", function (d) {
return d.r;
})
.attr("cx", function (d) {
return x(d.x);
})
.attr("cy", function (d) {
return y(d.y);
})
.style("fill", function (d) {
return d.c;
});
gdots
.filter(function(d0, i){
var isCollide = false,
x0 = x(d0.x),
y0 = y(d0.y);
gdots.data().forEach(function(d1,j){
if (!isCollide){
if (d0.name != d1.name){
var x1 = x(d1.x),
y1 = y(d1.y);
isCollide = Math.pow((x1-x0),2) + Math.pow((y1-y0),2) <= Math.pow((d0.r+d1.r), 2);
}
}
});
return !isCollide;
})
.append("text").text(function(d){
return d.name;
})
.attr("x", function (d) {
return x(d.x);
})
.attr("y", function (d) {
return y(d.y);
});
.node {
cursor: pointer;
}
.dot {
opacity: .7;
cursor: pointer;
}
.axis path,
.axis line {
fill: none;
stroke: rgb(31, 119, 180);
shape-rendering: crispEdges;
}
text {
stroke: none;
fill: #666666;
font-size: .6em;
font-family: "Helvetica Neue"
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.min.js"></script>
<div id="d3"></div>
I have implement a bar chart to show statistic, but when I update the SVG with new data the old bar won't be remove.
Can someone help to clarify the problem.
I use updateStatisticData to update my chart.
var cwidth = 800, cheight = 900;
var margin = {top: 80, right: 10, bottom: 20, left: 150};
var dataset = {
"series": ["Fail","Pass"],
"colors": ["#e74c3c","#2ecc71"]
};
var width = cwidth - margin.left - margin.right,
height = cheight - margin.top - margin.bottom;
function getStatisticData() {
$(document).ready(function() {
$.get("/statistic-test-result", { "_": $.now() }, function(msg, status){
data = msg['result']; //[{"ip":"x.x.x.x", "pass": 3, "fail": 7, 'total':10}, {...}]
var yScale = d3.scale.ordinal().rangeRoundBands([0, height], .3);
var x = d3.scale.linear().range([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("top");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
var svg = d3.select("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('text')
.attr("transform", "translate(" + (width / 2) + ",-" + (margin.top / 2) + ")")
.style("text-anchor", "middle")
.text("Max test items")
yScale.domain(data.map(function(d) {
return d.ip;
}));
x.domain([0, 17]);
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
// add legend
var legend = svg.append("g")
.attr("class", "legend")
legend.selectAll('text')
.data(dataset["colors"])
.enter()
.append("rect")
.attr("x", width-margin.right - 25 - margin.left)
.attr("y", function(d, i){ return -(margin.top / 2) - i * 20})
.attr("width", 10)
.attr("height", 10)
.style("fill", function(d) {
return d;
})
legend.selectAll('text')
.data(dataset["series"])
.enter()
.append("text")
.attr("x", width-margin.right - margin.left)
.attr("y", function(d, i){ return -(margin.top / 2) + 9 - i * 20})
.text(function(d){return d});
var bars = svg.selectAll(".bar")
.data(data);
bars.enter().append('g').attr("class", "bar");
bars.append("rect")
.attr({
'fill':'#e74c3c',
'width':function(d) {
return x(d.fail);
},
'height': function(d) {
return yScale.rangeBand();
},
'y': function(d) {
return yScale(d.ip);
}
});
bars.append('text')
.text(function(d) {
if (d.fail > 0)
return d.fail;
})
.attr({
'fill':'#000',
'y': function(d) {
return yScale(d.ip) + yScale.rangeBand()/2;
},
'x': function(d) {
return 5;
}
});
bars.append("rect")
.attr({
'fill':'#2ecc71',
'width':function(d) {
return x(d.pass);
},
'height': function(d) {
return yScale.rangeBand();
},
'y': function(d) {
return yScale(d.ip);
},
'x':function(d) { return x(d.fail);
}
});
bars.append('text')
.text(function(d) {
if (d.pass > 0)
return d.pass;
})
.attr({
'fill':'#000',
'y': function(d) {
return yScale(d.ip) + yScale.rangeBand()/2;
},
'x': function(d) {
return 5 + x(d.fail);
}
});
return true;
});
});
}
function updateStatisticData() {
$.get("/statistic-test-result", { "_": $.now() }, function(msg, status){
data = msg['result']; //[{"ip":"x.x.x.x", "pass": 3, "fail": 7, 'total':10}, {...}]
var yScale = d3.scale.ordinal().rangeRoundBands([0, height], .3);
var x = d3.scale.linear().range([0, width]);
var svg = d3.select("svg")
var xAxis = d3.svg.axis()
.scale(x)
.orient("top");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
yScale.domain(data.map(function(d) {
return d.ip;
}));
x.domain([0, 17]);
svg.selectAll("g.y.axis")
.call(yAxis);
svg.selectAll("g.x.axis")
.call(xAxis);
var bars = svg.selectAll(".bar")
.data(data);
bars.exit().remove(); //remove old bars
bars.enter().append('g').attr("class", "bar");
bars.append("rect")
.attr({
'fill':'#e74c3c',
'width':function(d) {
return x(d.fail);
},
'height': function(d) {
return yScale.rangeBand();
},
'y': function(d) {
return yScale(d.ip);
}
});
bars.append('text')
.text(function(d) {
if (d.fail > 0)
return d.fail;
})
.attr({
'fill':'#000',
'y': function(d) {
return yScale(d.ip) + yScale.rangeBand()/2;
},
'x': function(d) {
return 5;
}
});
bars.append("rect")
.attr({
'fill':'#2ecc71',
'width':function(d) {
return x(d.pass);
},
'height': function(d) {
return yScale.rangeBand();
},
'y': function(d) {
return yScale(d.ip);
},
'x':function(d) { return x(d.fail);
}
});
bars.append('text')
.text(function(d) {
if (d.pass > 0)
return d.pass;
})
.attr({
'fill':'#000',
'y': function(d) {
return yScale(d.ip) + yScale.rangeBand()/2;
},
'x': function(d) {
return 5 + x(d.fail);
}
});
return true;
});
}
I want to append 1 image at the time to an DIV element by clicking on the bars in the bar chart, but I get only the first image. If I use enter() then I get all images exept the first one.
var data = [{"val": 5, "imgsrc": "http://placehold.it/300x100"},
{"val": 40, "imgsrc": "a1.png"},
{"val": 50, "imgsrc": "http://placehold.it/200x100"},
{"val": 60, "imgsrc": "http://placehold.it/150x100"},
{"val": 80, "imgsrc": "http://placehold.it/100x100"}];
var width = 500;
var height = 500;
var widthScale = d3.scale.linear()
.domain([0, 100])
.range([0, width]);
var color = d3.scale.linear()
.domain([0, 100])
.range(["red", "blue"]);
var axis = d3.svg.axis()
.scale(widthScale)
.ticks(5);
var container = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(20, 0)");
var bars = container.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("width", function(d){ return widthScale(d.val) })
.attr("height", 30)
.attr("fill", function(d){ return color(d.val) })
.attr("y", function(d, i){ return i * 50 });
var setEvents = bars
.on( 'click', function (d) {
d3.select("#tool").style("opacity", 1.0);
d3.select("#tool").html(d.imgsrc);
d3.select('#tool')
.data(data)
//.enter()
.append('img')
.attr('src', function(d, i) {return d.imgsrc;});
});
container.append("g")
.attr("transform", "translate(0, 400)")
.call(axis);
Thanx in advance.
Use an array variable images to hold selected image urls. Update the array on click of bars. You can create the image elements as shown below.
var images = [];
var setEvents = bars
.on('click', function(d) {
d3.select("#tool").style("opacity", 1.0);
// d3.select("#tool").html(d.imgsrc);
images.push(d.imgsrc);
d3.select('#tool')
.selectAll("img")
.data(images)
.enter()
.append('img')
.attr("width", 50)
.attr("height", 50)
.attr('src', function(d, i) {
return d
});
});
Working Code Snippet:
var data = [{
"val": 5,
"imgsrc": "http://www.iconsdb.com/icons/preview/gray/square-rounded-xxl.png"
}, {
"val": 40,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/005500/square-rounded-xxl.png"
}, {
"val": 50,
"imgsrc": "http://www.iconsdb.com/icons/preview/black/square-rounded-xxl.png"
}, {
"val": 60,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/550055/square-rounded-xxl.png"
}, {
"val": 80,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/990000/square-rounded-xxl.png"
}];
var width = 500;
var height = 500;
var widthScale = d3.scale.linear()
.domain([0, 100])
.range([0, width]);
var color = d3.scale.linear()
.domain([0, 100])
.range(["red", "blue"]);
var axis = d3.svg.axis()
.scale(widthScale)
.ticks(5);
var container = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(20, 0)");
var bars = container.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("width", function(d) {
return widthScale(d.val)
})
.attr("height", 30)
.attr("fill", function(d) {
return color(d.val)
})
.attr("y", function(d, i) {
return i * 50
});
var images = [];
var setEvents = bars
.on('click', function(d) {
d3.select("#tool").style("opacity", 1.0);
// d3.select("#tool").html(d.imgsrc);
images.push(d.imgsrc);
d3.select('#tool')
.selectAll("img")
.data(images)
.enter()
.append('img')
.attr("width", 50)
.attr("height", 50)
.attr('src', function(d, i) {
return d
});
});
container.append("g")
.attr("transform", "translate(0, 400)")
.call(axis);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="tool"></div>
Edit:
var data = [{
"val": 5,
"imgsrc": "http://www.iconsdb.com/icons/preview/gray/square-rounded-xxl.png"
}, {
"val": 40,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/005500/square-rounded-xxl.png"
}, {
"val": 50,
"imgsrc": "http://www.iconsdb.com/icons/preview/black/square-rounded-xxl.png"
}, {
"val": 60,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/550055/square-rounded-xxl.png"
}, {
"val": 80,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/990000/square-rounded-xxl.png"
}];
var width = 500;
var height = 500;
var widthScale = d3.scale.linear()
.domain([0, 100])
.range([0, width]);
var color = d3.scale.linear()
.domain([0, 100])
.range(["red", "blue"]);
var axis = d3.svg.axis()
.scale(widthScale)
.ticks(5);
var container = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(20, 0)");
var bars = container.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("width", function(d) {
return widthScale(d.val)
})
.attr("height", 30)
.attr("fill", function(d) {
return color(d.val)
})
.attr("y", function(d, i) {
return i * 50
});
var setEvents = bars
.on('click', function(d) {
d3.select("#tool").style("opacity", 1.0);
d3.select('#tool').selectAll("img").remove();
d3.select('#tool')
.append('img')
.attr("width", 50)
.attr("height", 50)
.attr('src',d.imgsrc);
});
container.append("g")
.attr("transform", "translate(0, 400)")
.call(axis);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="tool"></div>
I am new in D3 js.most of the examples in gallery load data from TSV files.
but I wanted to render line graph using json in mvc3 razor. below is code where i hard code my data.please let me know how to retrive dynamic data using json.
var data = [
{ "date": 0, "close": 0.3372 },
{ "date": 1, "close": 1.7 },
{ "date": 2, "close": 1.8 },
{ "date": 3, "close": 2.014 },
{ "date": 4, "close": 10.995 },
{ "date": 5, "close": 16.227 },
{ "date": 6, "close": 16.643 },
{ "date": 7, "close": 20.644 },
{ "date": 8, "close": 22.478 }
];
var margin = { top: 20, right: 20, bottom: 30, left: 50 },
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
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 line = d3.svg.line()
.x(function (d) { return x(d.date); })
.y(function (d) { return y(d.close); });
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 + ")");
console.log("here");
data.forEach(function (d) {
d.date = parseInt(d.date);
d.close = +d.close;
});
console.log("here2");
x.domain(d3.extent(data, function (d) { return d.date; }));
y.domain(d3.extent(data, function (d) { return d.close; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.text("Request")
.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("Probability");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);