npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

divide-up-circle-in-circular-segments

v1.0.5

Published

Divide a circle into circular segments whose area is proportional to the data

Downloads

11

Readme

Divide a circle into circular segments whose area is proportional to the data
demo page


This library allows you to divide a circle into circular segments whose area is proportional to the data received in input. Here is an example:

step1

This means that the red circular segment (S1) has area equal to 30% of the area of ​​the circle, the blue circular segment (S2) also, while the blue circular segment (S3) has area equal to 40% of the total area of ​​the circle.

The computation is therefore made on areas, not as a proportion of the diameter. In this case the result would have been different:

propDiam

As you can see, in the firs case (library output) the first two circular segments have both value 30% but their heights are different, in the second case not.

⚙️ Install

yarn add divide-up-circle-in-circular-segments

or

npm install divide-up-circle-in-circular-segments --save

📷 Screenshots

demo

🐝 API

As seen before, you can create circular segments proportional by circle area.

computeCircularSegments(dataset, radius, center, options?)

The computeCircularSegments function accepts 4 parameters and an array of objects in which each object contains the useful information about each circular segment.

Parameters

| Argument | Type | Description | | -------------------- | --------- | -------------------------------------------------------------------------------------- | | dataset | number | array of objects, each object must contains a percentage property (number in [0, 1]) | | radius | number | circle radius | | center | Point | circle center | | options (optional) | Options | option objects |

Note: the sum of percentage values must be 1.

Where:

interface Point {
  x: number
  y: number
}
interface Options {
  orientation?: 'horizontal' | 'vertical'
}

options object is optional and the default is:

const defaultOptions: Options = {
  orientation: 'horizontal',
}

Returns

The returned array contains one object for each circular segment. Each object is composed of these properties:

| Property name | Type | Description | | ---------------------- | ---------- | ----------------------------------------------------------------------------------------- | | ...datum | / | the original dataset object info | | percentage | number | number in [0, 1] | | cumulativePercentage | number | cumulative percentage value | | height | number | circular segment height | | cumulativeHeight | number | cumulative circular segment height | | theta | number | angle subtended by the chord at the center of the circle related to the circlular segment | | path | string | path string useful to draw the circular segment | | center | Point | center point of the circular segment | | vertices | Vertices | coordinates of vertices of circular segment |

interface Vertices {
  topLeft: Point
  topRight: Point
  bottomLeft: Point
  bottomRight: Point
}

Example

I hope the following example can better explain the information written above.

import { computeCircularSegments } from 'divide-up-circle-in-circular-segments'

const dataset = [
  {percentage: 0.3, color: "#ff787a"},
  {percentage: 0.3, color: "#7f7ad9"},
  {percentage: 0.4, color: "#74dfc9"}
]
const radius = 125
const center = { x: radius, y: radius }
const circularSegments = computeCircularSegments(dataset, r, center)

// [
//   {
//     center: { x: 125, y: 42.5192806380935 },
//     color: '#ff787a',
//     cumulativeHeight: 85.038561276187,
//     cumulativePercentage: 0.3,
//     height: 85.038561276187,
//     path:
//       'M 125 0 L 125 0 A 125 125 2.4907848665483074 0 0 6.559789703315133 85.038561276187 L 243.44021029668488 85.038561276187 A 125 125 2.4907848665483074 0 0 125 0',
//     percentage: 0.3,
//     theta: 2.4907848665483074,
//     vertices: {
//       bottomLeft: { x: 6.559789703315133, y: 85.038561276187 },
//       bottomRight: { x: 243.44021029668488, y: 85.038561276187 },
//       topLeft: { x: 125, y: 0 },
//       topRight: { x: 125, y: 0 },
//     },
//   },
//   { ... },
//   { ... }

You can draw the circular segments using the information above and here is the result:

step1

In particular:

stepS1

stepS2

and so on...

In this case the options object is undefined and since the default orientation value is horizontal, the circular segments are drawn horizontally.

Setting orientation to vertical (computeCircularSegments(dataset, r, center, { orientation: 'vertical' })), you get:

vert

🙈 Demo page

A demo page is available.

License

MIT © Ilaria Venturini