Skip to content

Instantly share code, notes, and snippets.

@dogobox
Forked from syntagmatic/README.md
Created April 29, 2016 14:45

Revisions

  1. @syntagmatic syntagmatic revised this gist Aug 16, 2015. 1 changed file with 0 additions and 0 deletions.
    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  2. @syntagmatic syntagmatic revised this gist Aug 6, 2015. 1 changed file with 18 additions and 8 deletions.
    26 changes: 18 additions & 8 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    stroke: #222;
    stroke-opacity: 0.4;
    pointer-events: none;
    stroke-width: 1.5px;
    stroke-width: 1px;
    }

    .axis .title {
    @@ -80,14 +80,14 @@
    description: "Nature of Injury",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,15); })
    .tickFormat(function(d) { return d.slice(0,12); })
    },
    {
    key: "CLASSIFICATION",
    description: "Classification",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,15); })
    .tickFormat(function(d) { return d.slice(0,12); })

    },
    {
    @@ -97,7 +97,7 @@
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d,i) {
    if (i % 3 != 0) return ""; // hide 2/3 tick labels
    return d.slice(0,15);
    return d.slice(0,12);
    })

    },
    @@ -106,7 +106,7 @@
    description: "Injured Body Part",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,15); })
    .tickFormat(function(d) { return d.slice(0,12); })
    },
    {
    key: "MINING_EQUIP",
    @@ -115,7 +115,7 @@
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d,i) {
    if (i % 2 != 0) return ""; // hide 1/2 tick labels
    return d.slice(0,15);
    return d.slice(0,12);
    })
    },
    {
    @@ -125,7 +125,7 @@
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d,i) {
    if (i % 2 != 0) return ""; // hide 1/2 tick labels
    return d.slice(0,15);
    return d.slice(0,12);
    })
    },
    {
    @@ -135,7 +135,17 @@
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d,i) {
    if (i % 5 != 0) return ""; // hide 4/5 tick labels
    return d.slice(0,15);
    return d.slice(0,12);
    })
    },
    {
    key: "OPERATOR_NAME",
    description: "Operator",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d,i) {
    if (i % 6 != 0) return ""; // hide 5/6 tick labels
    return d.slice(0,12);
    })
    },
    {
  3. @syntagmatic syntagmatic revised this gist Aug 6, 2015. 1 changed file with 27 additions and 12 deletions.
    39 changes: 27 additions & 12 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    .foreground path {
    fill: none;
    stroke: #222;
    stroke-opacity: 0.25;
    stroke-opacity: 0.4;
    pointer-events: none;
    stroke-width: 1.5px;
    }
    @@ -64,6 +64,13 @@
    extent: function (data) { return data.sort(); },
    within: function(d, extent, dim) { return extent[0] <= dim.scale(d) && dim.scale(d) <= extent[1]; },
    defaultScale: d3.scale.ordinal().rangePoints([0, height])
    },
    "Date": {
    key: "Date",
    coerce: function(d) { return new Date(d); },
    extent: d3.extent,
    within: function(d, extent) { return extent[0] <= d && d <= extent[1]; },
    defaultScale: d3.time.scale().range([0, height])
    }
    };

    @@ -122,17 +129,27 @@
    })
    },
    {
    key: "CAL_YR",
    description: "Year",
    type: types["String"]
    key: "CONTROLLER_NAME",
    description: "Controller",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d,i) {
    if (i % 5 != 0) return ""; // hide 4/5 tick labels
    return d.slice(0,15);
    })
    },
    {
    key: "ACCIDENT_DT",
    description: "Accident Date",
    type: types["Date"]
    },
    {
    key: "RETURN_TO_WORK_DT",
    description: "Return to Work",
    type: types["Date"]
    }
    ];

    var colordimension = dimensions.filter(function(d) { return d.key == "NATURE_INJURY"; })[0];

    var color = d3.scale.ordinal()
    .range(["#1b9e77","#d95f02","#7570b3","#e7298a","#66a61e","#e6ab02","#a6761d","#666666"]);

    var x = d3.scale.ordinal()
    .domain(dimensions.map(function(dim) { return dim.key; }))
    .rangePoints([0, width]);
    @@ -192,13 +209,11 @@
    dim.scale.domain(dim.domain);
    });

    color.domain(colordimension.scale.domain());

    foreground.selectAll("path")
    .data(data)
    .enter().append("path")
    .attr("d", draw)
    .style("stroke", function(d) { return color(d[colordimension.key]); });
    .style("stroke", function(d) { return "#6ac"; });

    axes.append("g")
    .attr("class", "axis")
  4. @syntagmatic syntagmatic revised this gist Aug 6, 2015. 1 changed file with 18 additions and 12 deletions.
    30 changes: 18 additions & 12 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    .foreground path {
    fill: none;
    stroke: #222;
    stroke-opacity: 0.38;
    stroke-opacity: 0.25;
    pointer-events: none;
    stroke-width: 1.5px;
    }
    @@ -46,7 +46,7 @@
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script>

    var margin = {top: 50, right: 80, bottom: 20, left: 180},
    var margin = {top: 50, right: 80, bottom: 20, left: 100},
    width = 960 - margin.left - margin.right,
    height = 340 - margin.top - margin.bottom;

    @@ -73,44 +73,53 @@
    description: "Nature of Injury",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    .tickFormat(function(d) { return d.slice(0,15); })
    },
    {
    key: "CLASSIFICATION",
    description: "Classification",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    .tickFormat(function(d) { return d.slice(0,15); })

    },
    {
    key: "INJURY_SOURCE",
    description: "Injury Source",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    .tickFormat(function(d,i) {
    if (i % 3 != 0) return ""; // hide 2/3 tick labels
    return d.slice(0,15);
    })

    },
    {
    key: "INJ_BODY_PART",
    description: "Injured Body Part",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    .tickFormat(function(d) { return d.slice(0,15); })
    },
    {
    key: "MINING_EQUIP",
    description: "Mining Equipment",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    .tickFormat(function(d,i) {
    if (i % 2 != 0) return ""; // hide 1/2 tick labels
    return d.slice(0,15);
    })
    },
    {
    key: "OCCUPATION",
    description: "Occupation",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    .tickFormat(function(d,i) {
    if (i % 2 != 0) return ""; // hide 1/2 tick labels
    return d.slice(0,15);
    })
    },
    {
    key: "CAL_YR",
    @@ -151,12 +160,9 @@
    .attr("class", "axis")
    .attr("transform", function(d) { return "translate(" + x(d.key) + ")"; });

    d3.dsv("|", "text/plain")("accidents.txt", function(error, rawdata) {
    d3.dsv("|", "text/plain")("accidents.txt", function(error, data) {
    if (error) throw error;

    // take subset of data
    data = d3.shuffle(rawdata).slice(0,100);

    data.forEach(function(d) {
    for (var k in d) {
    // truncate values
  5. @syntagmatic syntagmatic revised this gist Aug 5, 2015. 3 changed files with 759 additions and 1 deletion.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    http://www.msha.gov/OpenGovernmentData/OGIMSHA.asp
    501 changes: 501 additions & 0 deletions accidents.txt
    501 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    258 changes: 257 additions & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -1 +1,257 @@
    <!doctype html>
    <!DOCTYPE html>
    <meta charset="utf-8">
    <title>Infant Death</title>
    <style>
    svg {
    font: 8px sans-serif;
    }

    .foreground path {
    fill: none;
    stroke: #222;
    stroke-opacity: 0.38;
    pointer-events: none;
    stroke-width: 1.5px;
    }

    .axis .title {
    font-size: 8px;
    font-weight: bold;
    text-transform: uppercase;
    transform: rotate(-12deg) translate(-5px,-6px);
    }

    .axis line,
    .axis path {
    fill: none;
    stroke: #000;
    stroke-width: 1px;
    }

    .brush .extent {
    fill-opacity: .3;
    stroke: #fff;
    stroke-width: 1px;
    }

    pre {
    width: 900px;
    margin: 10px 30px;
    tab-size: 21;
    font-size: 12px;
    overflow: auto;
    }
    </style>
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script>

    var margin = {top: 50, right: 80, bottom: 20, left: 180},
    width = 960 - margin.left - margin.right,
    height = 340 - margin.top - margin.bottom;

    var types = {
    "Number": {
    key: "Number",
    coerce: function(d) { return +d; },
    extent: d3.extent,
    within: function(d, extent) { return extent[0] <= d && d <= extent[1]; },
    defaultScale: d3.scale.linear().range([height, 0])
    },
    "String": {
    key: "String",
    coerce: String,
    extent: function (data) { return data.sort(); },
    within: function(d, extent, dim) { return extent[0] <= dim.scale(d) && dim.scale(d) <= extent[1]; },
    defaultScale: d3.scale.ordinal().rangePoints([0, height])
    }
    };

    var dimensions = [
    {
    key: "NATURE_INJURY",
    description: "Nature of Injury",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    },
    {
    key: "CLASSIFICATION",
    description: "Classification",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })

    },
    {
    key: "INJURY_SOURCE",
    description: "Injury Source",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })

    },
    {
    key: "INJ_BODY_PART",
    description: "Injured Body Part",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    },
    {
    key: "MINING_EQUIP",
    description: "Mining Equipment",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    },
    {
    key: "OCCUPATION",
    description: "Occupation",
    type: types["String"],
    axis: d3.svg.axis().orient("left")
    .tickFormat(function(d) { return d.slice(0,18); })
    },
    {
    key: "CAL_YR",
    description: "Year",
    type: types["String"]
    },
    ];

    var colordimension = dimensions.filter(function(d) { return d.key == "NATURE_INJURY"; })[0];

    var color = d3.scale.ordinal()
    .range(["#1b9e77","#d95f02","#7570b3","#e7298a","#66a61e","#e6ab02","#a6761d","#666666"]);

    var x = d3.scale.ordinal()
    .domain(dimensions.map(function(dim) { return dim.key; }))
    .rangePoints([0, width]);

    var line = d3.svg.line()
    .defined(function(d) { return !isNaN(d[1]); });

    var yAxis = d3.svg.axis()
    .orient("left");

    var 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 + ")");

    var output = d3.select("body").append("pre");

    var foreground = svg.append("g")
    .attr("class", "foreground");

    var axes = svg.selectAll(".axis")
    .data(dimensions)
    .enter().append("g")
    .attr("class", "axis")
    .attr("transform", function(d) { return "translate(" + x(d.key) + ")"; });

    d3.dsv("|", "text/plain")("accidents.txt", function(error, rawdata) {
    if (error) throw error;

    // take subset of data
    data = d3.shuffle(rawdata).slice(0,100);

    data.forEach(function(d) {
    for (var k in d) {
    // truncate values
    d[k] = d[k].slice(0,20);
    };
    dimensions.forEach(function(p) {
    d[p.key] = p.type.coerce(d[p.key]);
    });
    });

    // type/dimension default setting happens here
    dimensions.forEach(function(dim) {
    if (!("domain" in dim)) {
    // detect domain using dimension type's extent function
    dim.domain = d3.functor(dim.type.extent)(data.map(function(d) { return d[dim.key]; }));

    // TODO - this line only works because the data encodes data with integers
    // Sorting/comparing should be defined at the type/dimension level
    dim.domain.sort(function(a,b) {
    return a - b;
    });
    }
    if (!("scale" in dim)) {
    // use type's default scale for dimension
    dim.scale = dim.type.defaultScale.copy();
    }
    dim.scale.domain(dim.domain);
    });

    color.domain(colordimension.scale.domain());

    foreground.selectAll("path")
    .data(data)
    .enter().append("path")
    .attr("d", draw)
    .style("stroke", function(d) { return color(d[colordimension.key]); });

    axes.append("g")
    .attr("class", "axis")
    .each(function(d) {
    var renderAxis = "axis" in d
    ? d.axis.scale(d.scale) // custom axis
    : yAxis.scale(d.scale); // default axis
    d3.select(this).call(renderAxis);
    })
    .append("text")
    .attr("class", "title")
    .attr("text-anchor", "start")
    .text(function(d) { return "description" in d ? d.description : d.key; });

    // Add and store a brush for each axis.
    axes.append("g")
    .attr("class", "brush")
    .each(function(d) {
    d3.select(this).call(d.brush = d3.svg.brush()
    .y(d.scale)
    .on("brushstart", brushstart)
    .on("brush", brush));
    })
    .selectAll("rect")
    .attr("x", -8)
    .attr("width", 16);

    output.text(d3.tsv.format(data));

    function draw(d) {
    return line(dimensions.map(function(dim) {
    return [x(dim.key), dim.scale(d[dim.key])];
    }));
    }

    function brushstart() {
    d3.event.sourceEvent.stopPropagation();
    }

    // Handles a brush event, toggling the display of foreground lines.
    function brush() {
    var actives = dimensions.filter(function(p) { return !p.brush.empty(); }),
    extents = actives.map(function(p) { return p.brush.extent(); });

    var selected = [];

    d3.selectAll(".foreground path").style("display", function(d) {
    if (actives.every(function(dim, i) {
    // test if point is within extents for each active brush
    return dim.type.within(d[dim.key], extents[i], dim);
    })) {
    selected.push(d);
    return null;
    }
    return "none";
    });

    output.text(d3.tsv.format(selected));
    }
    });

    </script>
  6. @syntagmatic syntagmatic created this gist Aug 5, 2015.
    1 change: 1 addition & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <!doctype html>