Gathering detailed insights and metrics for d3-hexjson-oldjs
Gathering detailed insights and metrics for d3-hexjson-oldjs
npm install d3-hexjson-oldjs
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
1,030
Last Day
1
Last Week
7
Last Month
25
Last Year
254
47 Stars
36 Commits
12 Forks
5 Watching
4 Branches
3 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
d3-hexjson-oldjs@1.1.0
Unpacked Size
364.15 kB
Size
127.60 kB
File Count
21
NPM Version
6.10.2
Node Version
10.16.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
40%
7
Compared to previous week
Last month
78.6%
25
Compared to previous month
Last year
20.4%
254
Compared to previous year
This D3 plugin makes it easy to generate hexmaps from data that uses the Open Data Institute's HexJSON format. The plugin provides a single function that takes a HexJSON object and adds the properties needed to render it as a hexmap of a given size. The renderHexJSON
function calculates the hex size necessary to render the hexmap within an svg of the specified dimensions, and generates the position and the points for each hex. This data is returned as an array of hexes that you can render directly with D3 as polygons, by using the x and y coordinates to position each hex, and the points property as the points attribute of its polygon. Hex coordinates are rendered according to the coordinate system specified by the layout property of the HexJSON provided.
If you use NPM, npm install d3-hexjson
. Otherwise, download the latest release and use d3-hexjson.min.js, which can be found in the build directory. The module is designed to be used in conjunction with the core D3 library (see the example below).
# d3.renderHexJSON(hexjson, width, height)
Returns an array of hexes from a HexJSON object, adding to each hex the properties needed to render them as a hexmap with D3. The function calculates the appropriate size to render the hexes within an svg of the given width and height. The central pixel coordinates of each hex are stored in x and y properties, while the vertices property holds an array of coordinate pairs, which specify the position of each vertex relative to x and y. The points property contains the vertex coordinates as a string that can be inserted directly into the points attribute of a polygon. The key used to reference each hex within the HexJSON object is stored within each hex in the key property. As these properties are added to the hexes stored in the source HexJSON, the hexes and their properties can be accessed either through the returned array or through the original HexJSON object (if you need to merge additional data based on the key, for example).
The full list of properties that the function adds to each hex is as follows:
Note that while the absolute row numbers of each hex are represented internally in the rc property from top to bottom, the r property of the HexJSON data represents row numbers from bottom to top. This follows the row numbering convention used in the example HexJSON implementation provided by the ODI in their hexmap of UK Parliamentary constituencies.
# d3.getGridForHexJSON(hexjson)
Returns a new hexjson object representing the complete grid for a hexjson dataset. The returned grid has the same layout as the hexjson provided, and the same number of columns and rows needed to represent the data. The main purpose of this function is to produce the hex data needed to display a background grid for the given dataset (see the example with background grid below), but it also provides a convenient way to generate hex grids for other uses.
# d3.getBoundarySegmentsForHexJSON(hexjson, width, height, field))
Provides an array of line segments corresponding to the single edges between hexes in the provided hexjson which do not have the same value in their field property. The segments returned are in the form {x1, x2, y1, y2}
as used to make simple SVG line segments. Unlike for renderHexJSON, these coordinates are absolute and do not need to be transformed.
No line segments are added for external borders (where a hex has no adjacent hex). Line segments should be consistently directed with respect to the comparison of the field values of the two hexes.
These line segments are not ordered so as to create contiguous polylines.
This function has all the side-effects of adding properties to each hex as renderHexJSON.
# d3.getBoundaryDotsForHexJSON(hexjson, width, height, field))
Provides an array of points along the edges between hexes in the provided hexjson which do not have the same value in their field property. The points returned are in the form {x, y}
and may be used (for example) as the centre of SVG circles. Unlike for renderHexJSON, these coordinates are absolute and do not need to be transformed.
Each edge has five points - one at each end, and three evenly distributed in between. Where two or more edges meet at a vertex, each edge will contribute its own point in the array returned by this function. The points for each edge are included in sequence, but the edges' points are not ordered so as to create contiguous polylines.
This function has all the side-effects of adding properties to each hex as renderHexJSON.
The following example shows the most common usage. See the code in action in this block by Henry Lau.
Given this HexJSON stored in the file example.hexjson:
1{ 2 "layout":"odd-r", 3 "hexes":{ 4 "Q0R0":{"q":0,"r":0}, 5 "Q1R0":{"q":1,"r":0}, 6 "Q2R0":{"q":2,"r":0}, 7 "Q3R0":{"q":3,"r":0}, 8 "Q0R1":{"q":0,"r":1}, 9 "Q1R1":{"q":1,"r":1}, 10 "Q2R1":{"q":2,"r":1}, 11 "Q3R1":{"q":3,"r":1}, 12 "Q0R2":{"q":0,"r":2}, 13 "Q1R2":{"q":1,"r":2}, 14 "Q2R2":{"q":2,"r":2}, 15 "Q3R2":{"q":3,"r":2}, 16 "Q0R3":{"q":0,"r":3}, 17 "Q1R3":{"q":1,"r":3}, 18 "Q2R3":{"q":2,"r":3}, 19 "Q3R3":{"q":3,"r":3} 20 } 21}
A hexmap of the data can be rendered in the following way:
1<html> 2<head> 3<style> 4#vis { 5 margin: 0; 6 padding: 0; 7 text-align: center; 8 font-family: sans-serif; 9 font-size: 10pt; 10} 11</style> 12</head> 13<body> 14<div id="vis"></div> 15<script src="d3.min.js"></script> 16<script src="d3-hexjson.min.js"></script> 17<script> 18 19d3.json("example.hexjson", function(error, hexjson) { 20 21 // Set the size and margins of the svg 22 var margin = {top: 10, right: 10, bottom: 10, left: 10}, 23 width = 500 - margin.left - margin.right, 24 height = 420 - margin.top - margin.bottom; 25 26 // Create the svg element 27 var svg = d3 28 .select("#vis") 29 .append("svg") 30 .attr("width", width + margin.left + margin.right) 31 .attr("height", height + margin.top + margin.bottom) 32 .append("g") 33 .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 34 35 // Render the hexes 36 var hexes = d3.renderHexJSON(hexjson, width, height); 37 38 // Bind the hexes to g elements of the svg and position them 39 var hexmap = svg 40 .selectAll("g") 41 .data(hexes) 42 .enter() 43 .append("g") 44 .attr("transform", function(hex) { 45 return "translate(" + hex.x + "," + hex.y + ")"; 46 }); 47 48 // Draw the polygons around each hex's centre 49 hexmap 50 .append("polygon") 51 .attr("points", function(hex) {return hex.points;}) 52 .attr("stroke", "white") 53 .attr("stroke-width", "2") 54 .attr("fill", "#b0e8f0"); 55 56 // Add the hex codes as labels 57 hexmap 58 .append("text") 59 .append("tspan") 60 .attr("text-anchor", "middle") 61 .text(function(hex) {return hex.key;}); 62}); 63 64</script> 65</body> 66</html>
The example will look like this:
The following example shows how to render hexjson data as a hexmap with a background grid. Use the getGridForHexJSON
function to generate a hexjson object representing the background grid for the source data, then render the background hexes before rendering the data hexes on top. See it in action in this block.
Given this HexJSON stored in the file example-grid.hexjson:
1{ 2 "layout":"odd-r", 3 "hexes": { 4 "Q0R0":{"q":0,"r":0}, 5 "Q1R1":{"q":1,"r":1}, 6 "Q1R2":{"q":1,"r":2}, 7 "Q2R3":{"q":2,"r":3} 8 } 9}
A hexmap of the data with a background grid can be rendered in the following way:
1<html> 2<head> 3<style> 4#vis { 5 margin: 0; 6 padding: 0; 7 text-align: center; 8 font-family: sans-serif; 9 font-size: 10pt; 10} 11</style> 12</head> 13<body> 14<div id="vis"></div> 15<script src="d3.min.js"></script> 16<script src="d3-hexjson.min.js"></script> 17<script> 18 19d3.json("example-grid.hexjson", function(error, hexjson) { 20 21 // Set the size and margins of the svg 22 var margin = {top: 10, right: 10, bottom: 10, left: 10}, 23 width = 500 - margin.left - margin.right, 24 height = 420 - margin.top - margin.bottom; 25 26 // Create the svg element 27 var svg = d3 28 .select("#vis") 29 .append("svg") 30 .attr("width", width + margin.left + margin.right) 31 .attr("height", height + margin.top + margin.bottom) 32 .append("g") 33 .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 34 35 // Create the grid hexes and render them 36 var grid = d3.getGridForHexJSON(hexjson); 37 var gridHexes = d3.renderHexJSON(grid, width, height); 38 39 // Render the data hexes 40 var hexes = d3.renderHexJSON(hexjson, width, height); 41 42 // Draw the background grid BEFORE the data 43 44 // Bind the grid hexes to g.grid elements of the svg and position them 45 var hexgrid = svg 46 .selectAll("g.grid") 47 .data(gridHexes) 48 .enter() 49 .append("g") 50 .attr("transform", function(hex) { 51 return "translate(" + hex.x + "," + hex.y + ")"; 52 }); 53 54 // Draw the polygons around each grid hex's centre 55 hexgrid 56 .append("polygon") 57 .attr("points", function(hex) {return hex.points;}) 58 .attr("stroke", "#b0b0b0") 59 .attr("stroke-width", "1") 60 .attr("fill", "#f0f0f0"); 61 62 // Bind the data hexes to g.data elements of the svg and position them 63 var hexmap = svg 64 .selectAll("g.data") 65 .data(hexes) 66 .enter() 67 .append("g") 68 .attr("transform", function(hex) { 69 return "translate(" + hex.x + "," + hex.y + ")"; 70 }); 71 72 // Draw the polygons around each data hex's centre 73 hexmap 74 .append("polygon") 75 .attr("points", function(hex) {return hex.points;}) 76 .attr("stroke", "white") 77 .attr("stroke-width", "2") 78 .attr("fill", "#b0e8f0"); 79 80 // Add the codes for the data hexes as labels 81 hexmap 82 .append("text") 83 .append("tspan") 84 .attr("text-anchor", "middle") 85 .text(function(hex) {return hex.key;}); 86}); 87 88</script> 89</body> 90</html>
The example will look like this:
These examples can also be found in the examples directory.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
Found 3/18 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-27
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