javascript - using scalajs and d3 to create a histogram -


i'm trying use scala-js facade on d3 create histogram 1000 integers have. i've used 1 of examples create kinda working model, there style aspects can't work out. enter image description here

ideally i'd justify entire histogram left against y-axis or @ least center entire thing , move y-axis on it's close. i'd have histogram more of density plot percentages on y-axis, haven't found anyway yet.

val margin = js.dictionary("top" -> 30, "right" -> 30, "bottom" -> 30, "left" -> 30) val width = 2500 - margin("left") - margin("right") val height = 500 - margin("top") - margin("bottom")  val x = d3.scale.identity().range(js.array(values.min, values.max))  val data = d3.layout.histogram().bins(x.ticks(100))(values)  val mm: js.function2[bin[double], double, double] = { (x: bin[double], y: double) => x.y }  val m = d3.max(data, mm)  val y = d3.scale.linear().domain(js.array(0, m)).range(js.array(height, 0))  val xaxis = d3.svg.axis().scale(x).orient("bottom") val yaxis = d3.svg.axis().scale(y).tickvalues(js.array(20, 40, 60, 80)).orient("right")  dom.document.getelementbyid("scalajsshoutout").textcontent = data.join(", ")  val 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") + ")")  val ff = (d: bin[double]) => "translate(" + x(d.x) + "," + y(d.y) + ")"  val bar = svg.selectall(".bar")   .data(data)   .enter().append("g")   .attr("class", "bar")   .attr("transform", ff)  val rect = (d: bin[double]) => height - y(d.y)  bar.append("rect")   .attr("x", 1)   .attr("width", x(data(0).dx) - 1)   .attr("height", rect)  bar.append("text")   .attr("dy", ".75em")   .attr("y", 6)   .attr("x", x(data(0).dx) / 2)   .attr("text-anchor", "middle")   .text((d: bin[double]) => formatpercent(d.y))  svg.append("g")   .attr("class", "x axis")   .attr("transform", "translate(0," + height + ")")   .call(xaxis)  svg.append("g")   .attr("class", "y axis")   .attr("transform", "translate(" + 0 + ", 0)")   .call(yaxis) 

does have insight how can achieve effect? help, sorry code hacky right now.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -