This is an example of how to use the d3 framework without Scalable Vector Graphics. The bubbles are CSS styled div
elements.
Try it on bl.ocks.org
It is also an example of how to write your JavaScript using CoffeeScript. The following CoffeeScript was compiled into JavaScript, and results in the graphic above.
colors = new d3.scale.category10()
class SimpleBubble
constructor: (@data, @id, @canvas) ->
@el = null
@x = 0
@y = 0
@radius = 0
@boxSize = 0
@isDragging = false
@isSelected = false
@tooltip = null
@init()
init: ->
# Elements that make up the bubbles display
@el = $("<div class='bubble' id='bubble-" + @id + "'></div>")
@elFill = $("<div class='bubbleFill'></div>")
@el.append(@elFill)
# Attach mouse interaction to root element
# Note use of $.proxy to maintain context
@el.on('mouseover', @showToolTip)
@el.on('mouseout', @hideToolTip)
# Set CSS of Elements
@radius = @data
@boxSize = @data * 2
@elFill.css(
width: @boxSize
height: @boxSize
left: -@boxSize / 2
top: -@boxSize / 2
"background-color": colors(@data))
showToolTip: =>
@tooltip = $("<div class='tooltip'></div>")
@tooltip.html("<div class='tooltipFill'><p>" + @data + "</p></div>")
@tooltip.css(
left: @x + @radius*0.5,
top: @y + @radius*0.5)
@canvas.append(@tooltip)
hideToolTip: =>
$(".tooltip").remove()
move: ->
@el.css(
top: this.y,
left:this.x )
class SimpleVis
constructor: (canvas, @data) ->
@width = 800
@height = 400
@canvas = $(canvas)
@force = null
@bubbles = []
@centers = [
{x: 200, y:200}
{x: 400, y:200}
{x: 600, y:200}
]
@bin = d3.scale.ordinal().range([0,1,2])
@init()
bubbleCharge: (d) ->
-Math.pow(d.radius,1) * 8
makeBubbles: (data, i, canvas) ->
b = new SimpleBubble(data, i, canvas)
b.x = b.boxSize + (20 * (i+1))
b.y = b.boxSize + (10 * (i+1))
# Store bubble reference
@bubbles.push b
@canvas.append b.el
setBubbleLocation: (bubble, alpha) ->
center = @centers[this.bin(bubble.id)]
bubble.y = bubble.y + (center.y - bubble.y) * (0.115) * alpha
bubble.x = bubble.x + (center.x - bubble.x) * (0.115) * alpha
[bubble.x,bubble.y]
updateBubbleLocation: (bubble, alpha) ->
@setBubbleLocation(bubble, alpha)
bubble.move()
tickHandler: (event) =>
@updateBubbleLocation(bubble, event.alpha) for bubble in @bubbles
init: ->
# Initialize root visualization element
@canvas.css(
width: @width
height: @height
"background-color": "#eee"
position: "relative")
# Create Bubbles
@makeBubbles(d, i, @canvas) for d, i in @data
# Setup force layout
@force = d3.layout.force()
.nodes(@bubbles)
.gravity(0)
.charge(@bubbleCharge)
.friction(0.87)
.size([@width, @height])
.on('tick', @tickHandler)
@force.start()
# Create the visualization
$(document).ready () ->
vis = new SimpleVis("#canvas", [12,33,20,40,60,10,25,44,13,23,14,25,8])
$("#move").on "click", (e) ->
vis.bin.range(vis.bin.range().reverse())
vis.force.resume()
false
The original code was taken from http://vallandingham.me/d3_without_svg.html