Advertisement
Guest User

Untitled

a guest
May 14th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 6.98 KB | None | 0 0
  1. package example
  2.  
  3. import d3v4._
  4. //import org.w3c.dom.events.EventTarget
  5. //
  6. import scala.scalajs.js
  7. import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
  8. import scala.scalajs.js.Dynamic.global
  9. import d3selection._
  10.  
  11. object ScalaJSExample {
  12.   /********************************************
  13.     ************* Def new traits **************
  14.     *******************************************/
  15.   @js.native trait ChordGroupJson extends js.Object {
  16.     val index:Int= js.native
  17.     val startAngle: Double = js.native
  18.     val endAngle: Double= js.native
  19.     val value:Int = js.native
  20.   }
  21.   @JSExportTopLevel("myproject")
  22.   protected def getInstance(): this.type = this
  23.  
  24.   /********************************************
  25.     ************* Def Groups ******************
  26.     *******************************************/
  27.   def groupTicks(d: ChordGroup, step: Double): js.Array[js.Dictionary[Double]] = {
  28.     val k: Double = (d.endAngle - d.startAngle) / d.value
  29.     d3.range(0, d.value, step).map((v: Double) => js.Dictionary("value" -> v, "angle" -> (v * k + d.startAngle)))
  30.   }
  31.   def groupLabelData(d:ChordGroup): js.Array[js.Dictionary[Double]] = {
  32.     val angleMean = (d.endAngle - d.startAngle) / 2
  33.     js.Array(js.Dictionary("index" -> d.index.toDouble, "angle" -> (d.startAngle + angleMean)))
  34.   }
  35.  
  36.   /********************************************
  37.     ************ Extra Functions **************
  38.     *******************************************/
  39.   def mouseover(d:js.Any) = {
  40. //    global.console.log(d.asInstanceOf[ChordGroupJson])
  41.     // Change opacity chords
  42.     d3selection.select("svg").selectAll("path.chord").style("opacity", "0.1")
  43.  
  44.     val id = d.asInstanceOf[ChordGroupJson].index
  45.       d3selection.select("svg").selectAll("g").select("#chordgroup"+ id).style("opacity", "1")
  46.  
  47.     //Change opacity ribbons
  48.     d3selection.select("svg").selectAll("g.ribbons").selectAll("path").style("opacity", "0.1")
  49.  
  50.  
  51.     //d3selection.select("#ribbonID"=).style("opacity", "0.1")
  52.     //d3selection.select("svg").selectAll("g.ribbons").selectAll("path.ribbons").style("opacity", "0.1")
  53.     //d3selection.select("")
  54.   }
  55.  
  56.   def mouseout(d:js.Any, opacityDefault: Double) = {
  57.     d3selection.select("svg").selectAll("g").selectAll("path.ribbons").selectAll("path").style("opacity", opacityDefault.toString)
  58.     d3selection.select("svg").selectAll("g").selectAll("path.chord").style("opacity", opacityDefault.toString)
  59.     d3selection.select("svg").selectAll("g.ribbons").selectAll("path").style("opacity", opacityDefault.toString)
  60.   }
  61.  
  62.   @JSExport
  63.   def main(args: Array[String]): Unit = {
  64.     /*******************************************
  65.      ************* Global Var ******************
  66.       *******************************************/
  67.     val w = 980
  68.     val h = 800
  69.     val r1 = h/2
  70.     val r0 = r1 - 100
  71.     val innerRadius = r0
  72.     val outerRadius = r0+20
  73.     val formatValue = d3.formatPrefix(",.0", 1e3)
  74.     val opacityDefault = 0.8
  75.  
  76.  
  77.     val matrix = js.Array[js.Array[Double]](
  78.       js.Array(11975,  5871, 8916, 2868),
  79.       js.Array(1951, 10048, 2060, 6171),
  80.       js.Array(8010, 16145, 8090, 8045),
  81.       js.Array(1013,   990,  940, 6907)
  82.     )
  83.  
  84.     val color = d3.scaleOrdinal[Int, String]().domain(d3.range(4)).
  85.       range(js.Array("#c7b570","#c6cdc7","#335c64","#768935","#507282","#5c4a56","#aa7455","#574109","#837722","#73342d","#0a5564","#9c8f57","#7895a4","#4a5456","#b0a690","#0a3542"))
  86.  
  87.     var fill = d3.scaleOrdinal()
  88.       .domain(d3.range(4))
  89.       .range(js.Array("#000000", "#FFDD89", "#957244", "#F26223"))
  90.  
  91.     /*******************************************
  92.       ******* Create ribons-svg-chord-arcs *****
  93.       *******************************************/
  94.     val ribbon = d3.ribbon().radius(innerRadius)
  95.  
  96.     val chord = d3.chord().padAngle(.02).sortSubgroups(d3.descending)
  97.  
  98.     var arc = d3.arc()
  99.       .innerRadius(innerRadius)
  100.       .outerRadius(outerRadius)
  101.  
  102.     var svg = d3.select("svg")
  103.       .attr("width", w)
  104.       .attr("height", h)
  105.       .append("svg:g")
  106.       .attr("id", "circle")
  107.       .style("fill","None")
  108.       .attr("transform", "translate(" + w / 2 + "," + h / 2 + ")")
  109.       .datum(chord(matrix))
  110.  
  111.     svg.append("circle")
  112.       .attr("r", r0 + 20)
  113.  
  114.     var g = svg
  115.       .attr("class", "groups")
  116.       .selectAll("g")
  117.       .data((c: ChordArray) => c.groups)
  118.       .on("mouseout", (d:js.Any) => {mouseover(d); if(false) return })
  119.       .enter().append("g")
  120.  
  121.     g.append("svg:path")
  122.       .style("stroke", "black")
  123.       .style("opacity",opacityDefault.toString)
  124.       .style("fill", (d: ChordGroup) => color(d.index))
  125.       .attr("id",(d:ChordGroup) => "chordgroup" + d.index)
  126.       .attr("d", (x: ChordGroup) => arc(x))
  127.  
  128.     /*******************************************
  129.       ********** Draw cords & ribbons **********
  130.       *******************************************/
  131.     var chordPaths = svg.selectAll("path.chord") //outerArcs
  132.       .data((c: ChordArray) => c.groups)
  133.       .enter().append("svg:path")
  134.       .attr("class", "chord")
  135.       .style("stroke", (d: ChordGroup) => d3.rgb(color(d.index)).darker())
  136.       .style("fill", (d: ChordGroup) => color(d.index))
  137.       .style("opacity", (d:ChordGroup)  => opacityDefault)
  138.       .attr("id",(d:ChordGroup) => "chordgroup" + d.index)
  139.       .attr("d", (x: ChordGroup) => arc(x))
  140.       .on("mouseover", (d: js.Any) => {mouseover(d); if(false) {return}})
  141.       .on("mouseout", (d:js.Any) => {mouseout(d,opacityDefault) ; if(false) {return}})
  142.  
  143.     var ribbons = svg.append("g").attr("class", "ribbons").selectAll("path")
  144.       .data((c: ChordArray) => c) //ribbons
  145.       .enter().append("svg:path")
  146.       .attr("d", (d: Chord) => ribbon(d))
  147.       .style("fill", (d: Chord) => color(d.target.index))
  148.       .attr("id",(d:Chord) => "ribbonID" + d.source.index+d.target.index)
  149.       .style("opacity", (d:Chord)  => opacityDefault)
  150.       .style("stroke", (d: Chord) => d3.rgb(color(d.target.index)).darker())
  151. //      .on("mouseover", (d: js.Any) => {mouseover(d); if(false) {return}})
  152. //      .on("mouseout", (d:js.Any) => {mouseout(d, opacityDefault); if(false) {return}})
  153.  
  154.  
  155.     /*******************************************
  156.       ************** Draw Ticks ****************
  157.       *******************************************/
  158.     var groupTick = g.selectAll(".group-tick").data((d: ChordGroup) => groupTicks(d, 1e3))
  159.       .enter().append("g").attr("class", "group-tick")
  160.       .attr("transform", (d: js.Dictionary[Double]) =>  "rotate(" + (d("angle") * 180 / Math.PI - 90) + ") translate(" + outerRadius + ",0)")
  161.  
  162.     groupTick.append("line").attr("x2", 6)
  163.  
  164.     groupTick.filter((d: js.Dictionary[Double]) => d("value") % 5e3 == 0).append("text")
  165.       .attr("x", 8)
  166.       .attr("dy", ".35em")
  167.       .attr("transform", (d: js.Dictionary[Double]) => if(d("angle") > Math.PI) "rotate(180) translate(-16)" else null)
  168.       .style("text-anchor", (d: js.Dictionary[Double]) => if(d("angle") > Math.PI) "end" else null)
  169.       .text((d: js.Dictionary[Double]) => formatValue(d("value")))
  170.  
  171.   }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement