It is also not difficult to draw a horizontal histogram, first inserting a G in SVG and then inserting a rect in G.
1.html Code
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "Utf-8" /> <title></title> </Head> <Body> <DivID= "Container"></Div> </Body> <Scripttype= "Text/javascript"src= "Js/d3.js" ></Script> <Scripttype= "Text/javascript"src= "Js/shuzhuangtu.js" ></Script></HTML>
2.js Code
vardata = [1,3,4,6,2,9,7,3,8],bar_height= 50, Bar_padding= 10, Svg_width= 500, Svg_height= (Bar_height +bar_padding) *data.length;varScale =d3.scale.linear (). Domain ([0, D3.max (data)]). Range ([0, Svg_width]);varSVG = D3.select ("#container"). Append ("SVG"). attr ("Width", Svg_width). attr ("Height", Svg_height)varBar = Svg.selectall ("G"). Data (data). Enter (). Append ("G"). attr ("Transform",function(d, I) {return"Translate (0," +i* (bar_height+bar_padding) + ")"}) Bar.append ("Rect"). attr ({"Width":function(d) {returnScale (d);},"Height": Bar_height}). Style ("Fill", "Cornflowerblue") Bar.append ("Text"). Text (function(d) {returnD;}). attr ({"X":function(d) {returnScale (d)},"Y": BAR_HEIGHT/2, "Text-anchor": "End"})
3. Operation effect
Draw a chart with D3 (5)--Horizontal histogram chart