Okay
  Public Ticket #1423040
Data Vis with D3.JS
Closed

Comments

  •  2
    7872825 started the conversation

    Hi, 

    Really like your themes. Great functionality. 

    I wanted to add a couple of data visualisations using d3.js https://d3js.org/

    What is the best way to do this?

    Many thanks.

  • [deleted] replied

    Hi there,

    This javascript library could be implemented by shortcodes for example (taken here):

    // Example shortcode
    function d3_sunburst_shortcode( $atts ) {
        wp_enqueue_script( 'd3', 'https://d3js.org/d3.v4.min.js' );
        
        ?>
        <script>    
        var width = 960,
            height = 700,
            radius = Math.min(width, height) / 2,
            color = d3.scale.category20c();
        
        var svg = d3.select("body").append("svg")
            .attr("width", width)
            .attr("height", height)
          .append("g")
            .attr("transform", "translate(" + width / 2 + "," + height * .52 + ")");
        
        var partition = d3.layout.partition()
            .sort(null)
            .size([2 * Math.PI, radius * radius])
            .value(function(d) { return 1; });
        
        var arc = d3.svg.arc()
            .startAngle(function(d) { return d.x; })
            .endAngle(function(d) { return d.x + d.dx; })
            .innerRadius(function(d) { return Math.sqrt(d.y); })
            .outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });
        
        d3.json("flare.json", function(error, root) {
          if (error) throw error;
        
          var path = svg.datum(root).selectAll("path")
              .data(partition.nodes)
            .enter().append("path")
              .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
              .attr("d", arc)
              .style("stroke", "#fff")
              .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
              .style("fill-rule", "evenodd")
              .each(stash);
        
          d3.selectAll("input").on("change", function change() {
            var value = this.value === "count"
                ? function() { return 1; }
                : function(d) { return d.size; };
        
            path
                .data(partition.value(value).nodes)
              .transition()
                .duration(1500)
                .attrTween("d", arcTween);
          });
        });
        
        // Stash the old values for transition.
        function stash(d) {
          d.x0 = d.x;
          d.dx0 = d.dx;
        }
        
        // Interpolate the arcs in data space.
        function arcTween(a) {
          var i = d3.interpolate({x: a.x0, dx: a.dx0}, a);
          return function(t) {
            var b = i(t);
            a.x0 = b.x;
            a.dx0 = b.dx;
            return arc(b);
          };
        }
        
        d3.select(self.frameElement).style("height", height + "px");
        
        </script>
        <?php
    }
    add_shortcode( 'd3_sunburst', 'd3_sunburst_shortcode', 100 );

    You can figure out other ways what fits best for you.

  •  2
    7872825 replied

    Hi Arlind, 


    Apologies for the really really slow response. 

    Thanks so much for the suggestion. 

    Will give it a go.

    Kind regards, 

    V

  •  2
    7872825 replied

    Hi Arlind, 

    For adding shortcode, i saw in the Calcium theme that Vision helper was recommended  https://documentation.laborator.co/kb/calcium/how-to-add-shortcodes/

    I couldn't see this for Kalium. Is this an extra plugin which is not included with Kalium? Is there anything you would recommend that works well with Kalium for installing shortcode?

    Also,  your example below has the js and json together. If you are working with a set of d3 files (short code which refers to other js which itself refers to other js files, then do they all need to be concatenated into the one file or can you keep them in separate files?  In which case, where do they need to be placed to be called correctly?

    Thanks Arlind. 

    Kind regards, 

    V

  • [deleted] replied

    Hi V,

    Calcium uses a specific plugin to manage shortcodes, which now is quite old and deprecated. The shortcode I gave is can be implemented simply, just put that code in PHP and everytime you use 

    [d3_sunburst]

    It will show the JS code. 

    As for other files, I am not sure how many types of D3 documents you have or how can they be included?

    Have you checked for plugins that provide D3 functionality:

    https://wordpress.org/plugins/wp-d3/

    https://premium.wpmudev.org/blog/beyond-pie-charts-add-mind-blowing-visualizations-to-wordpress/