Installations
npm install d3-scale-cluster
Developer
schnerd
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
Typescript Support
No
Node Version
12.6.0
NPM Version
6.9.0
Statistics
132 Stars
101 Commits
10 Forks
9 Watching
11 Branches
5 Contributors
Updated on 13 Oct 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
502,829
Last day
14.7%
273
Compared to previous day
Last week
10.4%
1,428
Compared to previous week
Last month
-17.8%
6,023
Compared to previous month
Last year
-40.2%
84,777
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
d3-scale-cluster
A custom D3 scale powered by a 1-dimensional clustering algorithm. Similar to quantile scales, the cluster scale maps a continuous input domain to a discrete range. The number of values in the output range determines the number of clusters that will be computed from the domain. The graphic below demonstrates how cluster compares to D3's quantile and quantize scales:
You can also check out the "Choropleth with d3-scale-cluster" block for an interactive comparison of cluster, quantile, and quantize scales.
Clusters are computed using a 1-dimensional clustering algorithm with an O(kn log(n))
runtime (where k
is the number of clusters desired). This should be fast enough for the majority of data sets, but it's worth doing your own performance testing.
For more details on this project and the underlying clustering algorithm, please read this blog post on Medium: "Using clustering to create a new D3.js color scale"
For more direct access to the ckmeans algorithm (not as a D3 scale), check out ckmeans or its native sibling ckmeans-native.
Getting Started
Using npm
Install the npm package
npm install --save d3-scale-cluster
Load the scale into your project
1// Using ES6 imports 2import scaleCluster from 'd3-scale-cluster'; 3 4// Or, using require 5var scaleCluster = require('d3-scale-cluster');
Using a <script>
tag
Include the following script tag on your page after D3
1<script src="https://unpkg.com/d3-scale-cluster@1.3.1/dist/d3-scale-cluster.min.js"></script>
Reference the scale directly from the d3 object
1var scale = d3.scaleCluster();
Example Usage
This scale largely has the same API as d3.scaleQuantile (however we use clusters()
instead of quantiles()
)
1var scale = d3 2 .scaleCluster() 3 .domain([1, 2, 4, 5, 12, 43, 52, 123, 234, 1244]) 4 .range(['#E5D6EA', '#C798D3', '#9E58AF', '#7F3391', '#581F66', '#30003A']); 5 6var clusters = scale.clusters(); // [12, 43, 123, 234, 1244] 7var color = scale(52); // '#9E58AF' 8var extent = scale.invertExtent('#9E58AF'); // [43, 123]
API
d3.scaleCluster()
Constructs a new cluster scale with an empty domain and an empty range. The cluster scale is invalid until both a domain and range are specified.
cluster(value)
Given a value in the input domain, returns the corresponding value in the output range.
cluster.invertExtent(value)
Returns the extent of values in the domain [x0, x1] for the corresponding value in the range: the inverse of cluster. This method is useful for interaction, say to determine the value in the domain that corresponds to the pixel location under the mouse.
cluster.domain([domain])
If domain is specified, sets the domain of the quantile scale to the specified set of discrete numeric values. The array must not be empty, and must contain at least one numeric value; NaN, null and undefined values are ignored and not considered part of the sample population. If the elements in the given array are not numbers, they will be coerced to numbers. A copy of the input array is sorted and stored internally. If domain is not specified, returns the scale’s current domain.
cluster.range([range])
If range is specified, sets the discrete values in the range. The array must not be empty, and may contain any type of value. The number of values in (the cardinality, or length, of) the range array determines the number of clusters that are computed. If range is not specified, returns the current range.
cluster.clusters()
Returns the cluster thresholds. If the range contains n discrete values, the returned array will contain n - 1 thresholds. Values less than the first threshold are considered in the first cluster; values greater than or equal to the first threshold but less than the second threshold are in the second cluster, and so on.
cluster.copy()
Returns an exact copy of this scale. Changes to this scale will not affect the returned scale, and vice versa.
cluster.import()
Updates the scale with the result of a cluster.export() call. Useful for offloading computation into a webworker.
cluster.export()
Exports the internals of the scale as an object, for use with cluster.import(). Useful for offloading computation into a webworker.
Using in a Web Worker
For data sets of significant size, you may want to offload computation into a Web Worker so that it does not block the main thread. You can use cluster.import() and cluster.export() as follows:
worker.js
1const scale = scaleCluster().domain(domain).range(range); 2self.postMessage({scale: scale.export()});
Main thread
1worker.onmessage = function (event) { 2 const scale = scaleCluster().import(event.data.scale); 3};
Thanks
Thanks to Haizhou Wang and Mingzhou Song for developing the original Ckmeans 1D clustering algorithm, and Tom MacWright for his previous work in bringing these techniques to the web.
Links & Resources
- Using clustering to create a new D3.js color scale - Medium post describing this project
- Choropleth with d3-scale-cluster - Interactive block comparing cluster, quantile, and quantize scales
- Interactive d3-scale-cluster demo - Paste in your data to see clusters. By @tomshanleynz
Contributing
npm install
npm run test # run tests
npm run build # build distributable file
Publishing
- Build distributable file for browser:
npm run build
- Update CHANGELOG.md with changes in next version bump
- Update slated version in unpkg url in README.md
- Create new npm version:
npm version [major|minor|patch]
- Push to github with new version tag:
git push origin --tags
- Publish to npm:
npm publish
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/schnerd/d3-scale-cluster/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/schnerd/d3-scale-cluster/nodejs.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/nodejs.yml:28
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
Found 1/17 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Reason
21 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
2.5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More