[dba-Tech] Super JavaScript graphing tools

Jim Lawrence accessd at shaw.ca
Fri Feb 7 23:39:52 CST 2014


Hi All:

Creating and retrieving data is one thing but then how do you represent it. First assumption is that the data must be displayed in a browser. To that end here is a simple example taken from the new D3 graphic display engine. It has numerous very powerful functions.

http://d3js.org 

This system can retrieve and display data from graphic storage, CVS files and JASON files. Check out the following simple example: (This is one of dozens of working examples.) 

<store the text file listed below in an index.html file and run it...>

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  margin: 0;
}

.map {
  position: relative;
  overflow: hidden;
}

.layer {
  position: absolute;
}

.tile {
  pointer-events: none;
  position: absolute;
  width: 256px;
  height: 256px;
}

.info {
  position: absolute;
  bottom: 10px;
  left: 10px;
}

</style>

<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.tile.v0.min.js"></script>
<script>

var width = Math.max(960, window.innerWidth),
    height = Math.max(500, window.innerHeight),
    prefix = prefixMatch(["webkit", "ms", "Moz", "O"]);

var tile = d3.geo.tile()
    .size([width, height]);

var projection = d3.geo.mercator();

var zoom = d3.behavior.zoom()
    .scale(1 << 12)
    .scaleExtent([1 << 9, 1 << 23])
    .translate([width / 2, height / 2])
    .on("zoom", zoomed);

var map = d3.select("body").append("div")
    .attr("class", "map")
    .style("width", width + "px")
    .style("height", height + "px")
    .call(zoom)
    .on("mousemove", mousemoved);

var layer = map.append("div")
    .attr("class", "layer");

var info = map.append("div")
    .attr("class", "info");

zoomed();

function zoomed() {
  var tiles = tile
      .scale(zoom.scale())
      .translate(zoom.translate())
      ();

  projection
      .scale(zoom.scale() / 2 / Math.PI)
      .translate(zoom.translate());

  var image = layer
      .style(prefix + "transform", matrix3d(tiles.scale, tiles.translate))
    .selectAll(".tile")
      .data(tiles, function(d) { return d; });

  image.exit()
      .remove();

  image.enter().append("img")
      .attr("class", "tile")
      .attr("src", function(d) { return "http://" + ["a", "b", "c", "d"][Math.random() * 4 | 0] + ".tiles.mapbox.com/v3/examples.map-vyofok3q/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
      .style("left", function(d) { return (d[0] << 8) + "px"; })
      .style("top", function(d) { return (d[1] << 8) + "px"; });
}

function mousemoved() {
  info.text(formatLocation(projection.invert(d3.mouse(this)), zoom.scale()));
}

function matrix3d(scale, translate) {
  var k = scale / 256, r = scale % 1 ? Number : Math.round;
  return "matrix3d(" + [k, 0, 0, 0, 0, k, 0, 0, 0, 0, k, 0, r(translate[0] * scale), r(translate[1] * scale), 0, 1 ] + ")";
}

function prefixMatch(p) {
  var i = -1, n = p.length, s = document.body.style;
  while (++i < n) if (p[i] + "Transform" in s) return "-" + p[i].toLowerCase() + "-";
  return "";
}

function formatLocation(p, k) {
  var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f");
  return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " "
       + (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E");
}

</script>

</store the text file lists above in a index.html file and run it...> 

Note: Use the scroll wheel on your mouse to zoom in and out. (I would tend to place the JS scripts in the header or in its own js library and the style code in it own css library but for simplicity sake this example is excellent.)

Jim



More information about the dba-Tech mailing list