Learn how to evenly divide a circle based on the number of elements in an array using React, JavaScript, HTML, and CSS

Attempting to design a Polar/Radar Chart, my goal is to create distinct sections within the first circle based on the number of array items. Each item.name should be displayed in its own section with consistent text styling along the arc. The dimensions of the circle are set to 720px width and height.

Currently, as I iterate through the array and display the names, a separate circle is generated for each name due to mapping outside the container.

Any suggestions or solutions would be greatly appreciated. Thank you

    const items = [{ "id": 1, "name": "javascript" }, { "id": 2, "name": "HTML" }, { "id": 1, "name": "CSS" }]
    const length = items?.length ?? 0
    const width = 720
    const slice = width / length
     
    return (
            <div>
              { items?.map(item =>
              <div className={styles.circle} style={{width: `${slice}}}>
                <p>{item.name}</p>
              </div>
               )}
            </div>
    )

    //css
    .circle {
       height: 720px;
       border: solid 1px #fafafa;
       background: #fafafa;
       border-radius: 50%;
       padding: 20px;
       position: relative;
     }

Answer №1

If you want to create a pie chart in your web application, look no further than react-google-charts. This tool makes it easy to generate visually appealing charts with various customization options.

import React from "react";
import { Chart } from "react-google-charts";

const data = [
  ["Category", "Value"],
  ["A", 1],
  ["B", 2],
  ["C", 3]
];

const options = {
  legend: "none",
  pieSliceText: "label",
  title: "Chart Title",
  pieStartAngle: 90
};

export default function PieChart() {
  return (
    <Chart
      chartType="PieChart"
      data={data}
      options={options}
      width={"100%"}
      height={"400px"}
    />
  );
}

https://example.com/charts

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating fresh CSS styles through Selenium

I am attempting to showcase all the prices available on this page using Selenium and Chrome in order to capture a screenshot. By default, only 3 prices are visible. Is there a way to disable the slick-slider so that all 5 prices can be seen? I have tried r ...

Anyone have a solid example of STLLoader.js code to kickstart a project in three.js? Looking for a clean and reliable snippet

I am looking to add the STLLoader.js from three.js to my web application, but I am struggling to find clean and easy-to-understand code examples on how to get started. While the official three.js website offers a demonstration example for the STLLoader (h ...

Content overflowing into a different divison

I am facing a problem with an absolutely positioned DIV. It is placed beside an image, and below it, there is another DIV. Everything looks good in fullscreen mode, but when I resize my browser window, the text spills into the lower DIV. What I really want ...

Having trouble with ReactJS redirecting to another page after a successful statement?

Currently, I am working on developing a single-page application using React. My goal is to redirect users to another page based on the correctness of the username and password inputs. Despite trying various methods like using the <Redirect to.. tag and ...

Tips for minimizing the need to copy the entire dojo library each time you create a new Java EE package for deployment on GlassFish

Currently, I am diving into comet programming and utilizing the cometd implementation along with the JavaScript dojo library before deploying my war files to GlassFish. The issue I have encountered is that every time I create a new project, I find myself i ...

Exploring the functionality of Angular directives for bidirectional data binding with object references

In my custom Angular directive (v1.4.3), I am passing two bindings. The first binding is an Object with 2-way binding (model: '=') that can either be an Array or a single Object like [{something: 'foo'}, {something: 'moo'}, ...

When conducting tests using Selenium and the headless Google Chrome browser in Java, the dynamic JS content fails to load

Currently, I am in the process of testing a website that I have developed as part of a Spring Boot project using Selenium. While I can successfully test basic functionalities such as page loading and title verification, I am encountering difficulties when ...

An insightful guide on effectively binding form controls in Angular using reactive forms, exploring the nuances of formControlName and ngModel

Here is the code snippet: list.component.html <form nz-form [formGroup]="taskFormGroup" (submit)="saveFormData()"> <div nz-row *ngFor="let remark of checklist> <div nz-col nzXXl="12" *ngFor="let task of remark.tasks" styl ...

Strange Behavior When Encapsulating Image in Fragment Shader

Recently, I developed a basic fragment shader with THREE.js. The shader essentially takes every pixel's coordinate, modifies the x component by adding a value (looping back to 0 if it exceeds 1), and then retrieves the background image color at this n ...

Arranging JSON information based on category

I am currently populating an HTML table with data retrieved from a JSON file. It currently displays all the data in the order it appears in the JSON file, but I would like to organize it into different tables based on the "group" category in the file such ...

Looping through dates in a POST request using JavaScript/GAS - What's the best approach?

My task involves posting timesheet entries for the upcoming month into a system using an API. In order to do this, I require the following: User ID Assignable ID Dates (in the format YYYY-MMM-DD) Hours Initially, I obtained the user_id and assignable_id ...

Error encountered in Internet Explorer 11 - Access is forbidden - XMLHttpRequest

I am encountering a unique issue with IE11 and ajax. Most of the time, everything works as expected when I use the code below for my requests. However, I have noticed that when I try to use it in combination with a copy and paste function, an 'Access ...

Use Angular and JavaScript to fetch HTML from a mySQL database and dynamically render it on a webpage

I'm currently utilizing Angular for the front end and Node for the back end. The data is retrieved from a MySql database where it's manually stored in text format with HTML tags. For example: <ul> <li>item1</li> <li> ...

Sorting through an array of JavaScript objects and aggregating the step values for matching user names

Currently, I am delving into JavaScript and facing a certain challenge that may be considered easier for seasoned developers. My goal is to iterate through an array of objects, filter out objects with the same userName, and then aggregate their steps. The ...

Type into the asp:TextBox and then click on my button, where it triggers a JavaScript function

So here's my issue: <asp:TextBox runat='server' /> <button id='b2'>hi</button> <script> $('#b2').click(function(e){ e.preventDefault(); alert('you clicked the button'); }); </script ...

Troubleshooting Axios NPM connectivity issue in Express.js

I have created an express.js route with the following code snippet: - const path = require("path"); const express = require("express"); const hbs = require("hbs"); const weather = require("./weather"); const app = express(); app.get("/weather", (req, ...

How to position images in the center horizontally using CSS without using percentage (%) measurements

There seems to be some excess space on the right side of my gallery... The container for my images: .my-gallery figure { display: block; float: left; width: 150px; } Is it possible to always horizontally center images on different screen sizes wit ...

Issue with Ionic Grid: Row not occupying entire width of the container

Currently, I am working on creating a straightforward grid consisting of one row and seven columns. Each column holds a div with a single letter of text inside. My intention is for these columns to evenly space out across the page by default, but unfortu ...

In React Router, redirect when location.state is not defined

import React, { useState } from "react"; import { Redirect } from "react-router-dom"; function Update(data) { if(!data.location.state) return <Redirect to="/"/> const [name, setName] = useState(dat ...

What are the methods for establishing communication between two aspx pages with the help of JavaScript?

I have two aspx pages, one.aspx and two.aspx, and a JavaScript file named link.js. My goal is to establish communication between these two aspx pages using the JavaScript file as an intermediary. In the one.aspx file, I have a ModalPopupExtender with its ...