Skip to content

Instantly share code, notes, and snippets.

@dragosbulugean
Forked from lgersman/README.md
Created May 7, 2017 09:49
Show Gist options
  • Select an option

  • Save dragosbulugean/0eb5fde55ba536e7ef0e040a0cbb1c7a to your computer and use it in GitHub Desktop.

Select an option

Save dragosbulugean/0eb5fde55ba536e7ef0e040a0cbb1c7a to your computer and use it in GitHub Desktop.

Revisions

  1. dragosbulugean revised this gist May 7, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,6 @@
    <body>
    </body>

    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://d3js.org/d3.v4.min.js"></script>
    <script src="app.js"></script>
    </html>
  2. @lgersman lgersman revised this gist Apr 8, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@ var states = [

    window.svg = d3.select("body")
    .append("svg")
    .attr("width", "100%")
    .attr("height", "100%");
    .attr("width", "960px")
    .attr("height", "500px");

    svg.selectAll( ".state")
    .data( states)
  3. @lgersman lgersman revised this gist Apr 5, 2013. 1 changed file with 1 addition and 14 deletions.
    15 changes: 1 addition & 14 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -9,23 +9,10 @@ var states = [

    window.svg = d3.select("body")
    .append("svg")
    //.attr("viewBox", "0 0 " + 1000 + " " + 1000 )
    //.attr("preserveAspectRatio", "xMinYMin")
    .attr("width", "100%")
    .attr("height", "100%");

    /*
    svg.append("circle")
    .style("stroke", "gray")
    .style("fill", "white")
    .attr("r", 40)
    .attr("cx", 150)
    .attr("cy", 150)
    .on("mouseover", function(){d3.select(this).style("fill", "aliceblue");})
    .on("mouseout", function(){d3.select(this).style("fill", "white");});
    */

    svg.selectAll( "state.node")
    svg.selectAll( ".state")
    .data( states)
    .enter()
    .append( "circle")
  4. @lgersman lgersman revised this gist Apr 4, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,6 @@
    * move the mouse to resize the selection frame
    * Release the mouse button to resize the selection frame

    (The circles are just for illustrating purposes)
    (The circles are just for illustrating purposes)

    See it live : http://bl.ocks.org/lgersman/5310854
  5. @lgersman lgersman revised this gist Apr 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion app.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ var states = [
    { x : 90, y : 400, label : "last" }
    ]

    window.svg = d3.select("#test")
    window.svg = d3.select("body")
    .append("svg")
    //.attr("viewBox", "0 0 " + 1000 + " " + 1000 )
    //.attr("preserveAspectRatio", "xMinYMin")
  6. @lgersman lgersman created this gist Apr 4, 2013.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    * Click into the drawing area to start the selection frame
    * move the mouse to resize the selection frame
    * Release the mouse button to resize the selection frame

    (The circles are just for illustrating purposes)
    12 changes: 12 additions & 0 deletions app.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    .selection {
    stroke : gray;
    stroke-width : 1px;
    stroke-dasharray: 4px;
    stroke-opacity : 0.5;
    fill : transparent;
    }

    .state {
    stroke : gray;
    fill : white;
    }
    99 changes: 99 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    var states = [
    { x : 43, y : 67, label : "first" },
    { x : 340, y : 150, label : "second" },
    { x : 300, y : 350, label : "third" },
    { x : 300, y : 350, label : "fourth" },
    { x : 50, y : 270, label : "fifth" },
    { x : 90, y : 400, label : "last" }
    ]

    window.svg = d3.select("#test")
    .append("svg")
    //.attr("viewBox", "0 0 " + 1000 + " " + 1000 )
    //.attr("preserveAspectRatio", "xMinYMin")
    .attr("width", "100%")
    .attr("height", "100%");

    /*
    svg.append("circle")
    .style("stroke", "gray")
    .style("fill", "white")
    .attr("r", 40)
    .attr("cx", 150)
    .attr("cy", 150)
    .on("mouseover", function(){d3.select(this).style("fill", "aliceblue");})
    .on("mouseout", function(){d3.select(this).style("fill", "white");});
    */

    svg.selectAll( "state.node")
    .data( states)
    .enter()
    .append( "circle")
    .attr({
    class : 'state',
    r : 40,
    cx : function( state) {
    return state.x;
    },
    cy : function( state) {
    return state.y;
    }
    })
    .on("mouseover", function(){d3.select(this).style("fill", "aliceblue");})
    .on("mouseout", function(){d3.select(this).style("fill", "white");});
    ;

    svg
    .on( "mousedown", function() {
    var p = d3.mouse( this);

    svg.append( "rect")
    .attr({
    rx : 6,
    ry : 6,
    class : "selection",
    x : p[0],
    y : p[1],
    width : 0,
    height : 0
    })
    })
    .on( "mousemove", function() {
    var s = svg.select( "rect.selection");

    if( !s.empty()) {
    var p = d3.mouse( this),

    d = {
    x : parseInt( s.attr( "x"), 10),
    y : parseInt( s.attr( "y"), 10),
    width : parseInt( s.attr( "width"), 10),
    height : parseInt( s.attr( "height"), 10)
    },
    move = {
    x : p[0] - d.x,
    y : p[1] - d.y
    }
    ;

    if( move.x < 1 || (move.x*2<d.width)) {
    d.x = p[0];
    d.width -= move.x;
    } else {
    d.width = move.x;
    }

    if( move.y < 1 || (move.y*2<d.height)) {
    d.y = p[1];
    d.height -= move.y;
    } else {
    d.height = move.y;
    }

    s.attr( d);
    //console.log( d);
    }
    })
    .on( "mouseup", function() {
    svg.select( ".selection").remove();
    });
    14 changes: 14 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>d3.js selection frame example</title>
    <link rel="stylesheet" href="app.css">
    </head>

    <body>
    </body>

    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="app.js"></script>
    </html>