9951 explained code solutions for 126 technologies


javascript-d3How can I use d3.js to create a visualization in Qiita?


Qiita is a popular Japanese technical blog and online community for developers. You can use d3.js to create visualizations for your blog posts or articles on Qiita. Here is an example of how to do this:

// Create a SVG element
var svg = d3.select("body")
            .append("svg")
            .attr("width", 500)
            .attr("height", 500);

// Create a circle
svg.append("circle")
   .attr("cx", 250)
   .attr("cy", 250)
   .attr("r", 100)
   .style("fill", "red");

The above code will create a SVG element with a 500px by 500px size, and a circle with a radius of 100px and a red fill color.

Code explanation

  • d3.select("body"): Selects the HTML body element.
  • .append("svg"): Appends an SVG element to the body element.
  • .attr("width", 500): Sets the width of the SVG element to 500px.
  • .attr("height", 500): Sets the height of the SVG element to 500px.
  • .append("circle"): Appends a circle element to the SVG element.
  • .attr("cx", 250): Sets the x-coordinate of the circle's center point to 250px.
  • .attr("cy", 250): Sets the y-coordinate of the circle's center point to 250px.
  • .attr("r", 100): Sets the radius of the circle to 100px.
  • .style("fill", "red"): Sets the fill color of the circle to red.

You can find more information about d3.js and how to use it to create visualizations on the d3.js website.

Edit this code on GitHub