How To Draw A Circle Using D3 And Reactjs
Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
D3 lets united states of america add graphics to a front end-terminate web app easily.
Vue is a popular front web framework.
They work great together. In this article, we'll look at how to add together graphics to a Vue app with D3.
Circle Chart
Nosotros can create a circle chart with D3 in our React app.
For instance, we can write:
import React, { useEffect } from "react"; import * as d3 from "d3"; consign default function App() { useEffect(() => { const width = 400; const height = 400; const data = [10, 28, 35]; const colors = ["greenish", "lightblue", "yellow"]; const svg = d3 .select("torso") .suspend("svg") .attr("width", width) .attr("height", superlative); const yard = svg .selectAll("1000") .information(data) .enter() .append("k") .attr("transform", part (d, i) { render "translate(0,0)"; }); one thousand.append("circle") .attr("cx", function (d, i) { render i * 75 + 50; }) .attr("cy", role (d, i) { return 75; }) .attr("r", role (d) { return d * 1.5; }) .attr("fill", function (d, i) { return colors[i]; }); g.append("text") .attr("10", function (d, i) { render i * 75 + 25; }) .attr("y", eighty) .attr("stroke", "teal") .attr("font-size", "10px") .attr("font-family", "sans-serif") .text((d) => { return d; }); }, []); return ( <div className="App"> <div id="svgcontainer"></div> </div> ); }
We create the svg
by selecting the body
so add the svg
to information technology.
And we as well set up the width
and superlative
of the SVG.
This is done with:
const svg = d3 .select("trunk") .append("svg") .attr("width", width) .attr("height", superlative);
So we create the group with the data by writing:
const g = svg .selectAll("g") .data(data) .enter() .append("g") .attr("transform", function(d, i) { return "interpret(0,0)"; });
data
has the information.
Next, we add the circles by writing:
g.append("circle") .attr("cx", function(d, i) { return i * 75 + 50; }) .attr("cy", role(d, i) { return 75; }) .attr("r", function(d) { return d * 1.5; }) .attr("fill", function(d, i) { render colors[i]; });
We add the cx
and cy
attributes by call attr
.
r
has the radius, and fill up
has the background color for each circumvolve.
Then nosotros add the text that goes with the circles by writing:
g.append("text") .attr("x", office(d, i) { return i * 75 + 25; }) .attr("y", 80) .attr("stroke", "teal") .attr("font-size", "10px") .attr("font-family", "sans-serif") .text((d) => { render d; });
We telephone call append
with the 'text'
argument.
And then we prepare the x
and y
attributes of the position of the text.
Then we prepare the color of the text with the stroke
.
font-size
has the font size and font-family unit
has the font family.
The text
method takes a callback that returns the text.
Conclusion
We tin can add a circle chart into our React app with D3 hands.
The post Calculation Graphics to a React App with D3 — Circle Chart appeared kickoff on The Web Dev.
Source: https://dev.to/aumayeung/adding-graphics-to-a-react-app-with-d3-circle-chart-436h
Posted by: johnsonwhowerromed56.blogspot.com
0 Response to "How To Draw A Circle Using D3 And Reactjs"
Post a Comment