D3JS Simple JSON SVG barchart vertical with Axis

Size
2,699 Kb
Views
36,432

How do I make an d3js simple json svg barchart vertical with axis?

From tutorial http://bost.ocks.org/mike/bar/2/. What is a d3js simple json svg barchart vertical with axis? How do you make a d3js simple json svg barchart vertical with axis? This script and codes were developed by Dzulfikar Adi Putra on 06 September 2022, Tuesday.

D3JS Simple JSON SVG barchart vertical with Axis Previews

D3JS Simple JSON SVG barchart vertical with Axis - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>D3JS Simple JSON SVG barchart vertical with Axis</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h2>D3JS Simple JSON SVG barchart Horizontal with Axis</h2>
<i>using simple JSON data</i><br/>
taken from <a href="https://bost.ocks.org/mike/bar/3/">https://bost.ocks.org/mike/bar/3/</a> <br/><br/>
<div id="chart"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://d3js.org/d3.v3.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

D3JS Simple JSON SVG barchart vertical with Axis - Script Codes CSS Codes

#chart rect{ fill: #4aaeea;
}
#chart text{ fill: white; font: 10px sans-serif; text-anchor: end;
}
.axis text{ font: 10px sans-serif;
}
.axis path, .axis line{ fill: none; stroke : #fff; shape-rendering: crispEdges;
}
body{ background: #1a1a1a; color : #eaeaea; padding : 10px;
}

D3JS Simple JSON SVG barchart vertical with Axis - Script Codes JS Codes

var margin ={top:20, right:30, bottom:30, left:40}, width=960-margin.left - margin.right, height=500-margin.top-margin.bottom;
// scale to ordinal because x axis is not numerical
var x = d3.scale.ordinal().rangeRoundBands([0, width], .1);
//scale to numerical value by height
var y = d3.scale.linear().range([height, 0]);
var chart = d3.select("#chart") .append("svg") //append svg element inside #chart .attr("width", width+(2*margin.left)+margin.right) //set width .attr("height", height+margin.top+margin.bottom); //set height
var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); //orient bottom because x-axis will appear below the bars
var yAxis = d3.svg.axis() .scale(y) .orient("left");
d3.json("https://codepen.io/superpikar/pen/kcJDf.js", function(error, data){ x.domain(data.map(function(d){ return d.letter})); y.domain([0, d3.max(data, function(d){return d.frequency})]); var bar = chart.selectAll("g") .data(data) .enter() .append("g") .attr("transform", function(d, i){ return "translate("+x(d.letter)+", 0)"; }); bar.append("rect") .attr("y", function(d) { return y(d.frequency); }) .attr("x", function(d,i){ return x.rangeBand()+(margin.left/4); }) .attr("height", function(d) { return height - y(d.frequency); }) .attr("width", x.rangeBand()); //set width base on range on ordinal data bar.append("text") .attr("x", x.rangeBand()+margin.left ) .attr("y", function(d) { return y(d.frequency) -10; }) .attr("dy", ".75em") .text(function(d) { return d.frequency; }); chart.append("g") .attr("class", "x axis") .attr("transform", "translate("+margin.left+","+ height+")") .call(xAxis); chart.append("g") .attr("class", "y axis") .attr("transform", "translate("+margin.left+",0)") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Frequency");
});
function type(d) { d.letter = +d.letter; // coerce to number return d; }
D3JS Simple JSON SVG barchart vertical with Axis - Script Codes
D3JS Simple JSON SVG barchart vertical with Axis - Script Codes
Home Page Home
Developer Dzulfikar Adi Putra
Username superpikar
Uploaded September 06, 2022
Rating 3
Size 2,699 Kb
Views 36,432
Do you need developer help for D3JS Simple JSON SVG barchart vertical with Axis?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Dzulfikar Adi Putra (superpikar) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!