Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>D3 example</title>
    
<style>
</style>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<script>
var data = [
    { Request: 1, AvgRequest: 4123.18 },
    { Request: 2, AvgRequest: 5221.16 },
    { Request: 3, AvgRequest: 32.42 },
    { Request: 4, AvgRequest: 22.13 },
    { Request: 5, AvgRequest: 413.21 },
    { Request: 6, AvgRequest: 112.19 }
];
var margin = { top: 40, right: 40, bottom: 35, left: 85 },
    width = 450,
    height = 250;
    plotAreaWidth  = width - margin.left - margin.right,
    plotAreaHeight = height - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
    .rangeRoundBands([0, plotAreaWidth], .1);
var y = d3.scale.linear()
    .range([plotAreaHeight, 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)
        .attr("height", height)
    .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
data.forEach(function (d) {
    d.Request    = d.Request;
    d.AvgRequest = +d.AvgRequest;
});
x.domain(data.map(function (d) { 
    return d.Request; 
}));
y.domain([0, d3.max(data, function (d) { 
    return d.AvgRequest; 
}) ]);
// xAxis
svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + plotAreaHeight + ")")
    .call(xAxis);
// xAxis label
svg.append("text")
    .attr("transform", "translate(" + (plotAreaWidth / 2) + " ," + (plotAreaHeight + margin.bottom) + ")")
    .style("text-anchor", "middle")
    .text("Numbers of request");
// yAxis
svg.append("g")
    .attr("class", "y axis")
    .call(yAxis);
// yAxis label
svg.append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 0 - margin.left)
    .attr("x", 0 - (plotAreaHeight / 2))
    .attr("dy", "1em")
    .style("text-anchor", "middle")
    .text("avg request");
// Title
svg.append("text")
    .attr("x", (plotAreaWidth / 2))
    .attr("y", 0 - (margin.top / 2))
    .attr("text-anchor", "middle")
    .style("font-size", "16px")
    .style("text-decoration", "underline")
    .text("Avg");
svg.selectAll(".bar")
    .data(data)
    .enter()
    .append("rect")
        .attr("class", "bar")
        .attr("x", function (d) { return x(d.Request); })
        .attr("width", x.rangeBand())
        .attr("y", function (d) { return y(d.AvgRequest); })
        .attr("height", function (d) { return plotAreaHeight - y(d.AvgRequest); });
var text = svg.selectAll("text1")
    .data(data)
    .enter()
    .append("text")
    .attr("class", function (d) { return "label " + d.Request; })
    .attr("x", function (d, i) {
        //return x(i) + x.rangeBand() / 2;
        return x(d.Request);
    })
    .attr("y", function (d, i) {
        //return y(d.AvgRequest) + 25;
        return y(d.AvgRequest) - 5;
    })
    .text(function (d) { return d.AvgRequest; })
        .attr("font-size", "15px")
        .style("stroke", "black");
</script>
</body>
</html>
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
antojurkovicpro
0viewers