Gravity Bubbles

The gravity bubbles it's a bubble chart based on d3js library

by Leonardo Flores

HTML

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://rawgit.com/lflores/gravity-bubbles/master/src/js/custom-tooltip.js"></script>
<script src="https://cdn.rawgit.com/lflores/gravity-bubbles/master/dist/js/gravity-bubbles.min.js"></script>
<div id="vis"></div>

CSS

body{
  font-family: "Helvetica";
}
/**
Gravity Bubbles
*/
#vis{
  height: 250px;
  width: auto;
  border: 1px #ccc solid;
}
.map-container {
    /*border: 1px #716262 solid;*/
    /*position: absolute;*/
}

.bubble {
    stroke-width: 2px;
    opacity: .9;
}

    .bubble:hover {
        stroke: #2a2a2a;
        stroke-width: 3px;
    }

.group {
    stroke: #dddddd;
    /*stroke-dasharray: 5,2,3,2;*/
    stroke-width: 0px;
    fill: transparent;
}

rect.group.multiple {
    stroke: #dddddd;
    /*stroke-dasharray: 10,2;*/
    stroke-width: 1px;
    opacity: 1;
    fill: transparent;
}

.group-text {
    font-size: 0.6em;
    stroke: #000;
}

.legend-circle {
    stroke: #8e8e8e;
    stroke-dasharray: 7,3;
    fill: transparent;
}

.legend-text {
    font-size: 0.6em;
    stroke: #000;
}

.tooltip {
	position: absolute;
	top: 100px;
	left: 100px;
  -moz-border-radius:5px;
	border-radius: 5px;
  border: 2px solid #000;
	/* background: #222222; */
  background: #fff;
	opacity: .9!important;
	/* color: #eeeeee; */
  color: black;
	padding: 10px;
	width: 300px;
	font-size: 12px;
	z-index: 10;
}

.tooltip .title {
	font-size: 13px;
}

.tooltip .name {
  font-weight:bold;
}

JavaScript

var loaded_data;
var totalLines;

var chart = new GravityBubbles({
  id: "vis",
  width: 450,
  height: 200,
  sizeById: "size",
  colorById: "perc",
  data: {
    tooltip: function(d) {
      return "<b>Name:</b>{name}<br><b>Size:</b> {size}<br><b>Size of Total:</b> {perc}%";
    },
    label:{
    template:"{name}",
    autofit:true
    }
  }
});

chart.config({
  colors: ["#FFFFCC", "#C2E699", "#78C679", "#31A354", "#006837", "#006837"],
  points: [0, 3, 7, 20, 50, 100]
});

d3.json("https://rawgit.com/lflores/gravity-bubbles/master/examples/flare/js/flare.json", function(data) {
  rollup(data);
  totalLines = data.size;
  perc(data);
  chart.data(data.children);
  loaded_data = data;
});

function rollup(node) {
  node['size'] = node['children'].reduce(function(result, item) {
    return result + (item['children'] ? rollup(item) : item['size']);
  }, 0);
  return node['size'];
}

function perc(node) {
  node['perc'] = node['children'].reduce(function(result, item) {
    item['perc'] = (item['size'] / totalLines) * 100;
    return result + (item['children'] ? perc(item) : (item['size'] / totalLines) * 100);
  }, 0);
  return (node['size'] / totalLines) * 100;
}