Skip to content

Commit b57e0a7

Browse files
committed
Example gallery update
1 parent 2edf58e commit b57e0a7

46 files changed

Lines changed: 163 additions & 47 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/anu-examples/donutChart.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright : J.P. Morgan Chase & Co.
3+
4+
import * as anu from '@jpmorganchase/anu';
5+
import * as BABYLON from '@babylonjs/core';
6+
import * as d3 from 'd3';
7+
import 'manifold-3d'; //Required for Constructive Solid Geometry
8+
9+
//Create and export a function that takes a Babylon engine and returns a Babylon Scene
10+
export async function donutChart(engine){ //Mark this function as async
11+
12+
//Create an empty Scene
13+
const scene = new BABYLON.Scene(engine);
14+
//Add some lighting
15+
new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 10, -5), scene);
16+
//Add a camera that rotates around the origin and adjust its properties
17+
const camera = new BABYLON.ArcRotateCamera('Camera', 0, 0, 0, new BABYLON.Vector3(0, 0, 0), scene);
18+
camera.position = new BABYLON.Vector3(0, 1.25, -1);
19+
camera.wheelPrecision = 20;
20+
camera.minZ = 0;
21+
camera.attachControl(true);
22+
23+
//Create some fake data
24+
const data = [2, 3, 5, 7, 11, 13, 17, 19];
25+
26+
//Create our D3 pie generator which will calculate the required angles for our pie chart
27+
const pie = d3.pie();
28+
//Create arcs for our data
29+
const arcs = pie(data);
30+
31+
//Create a D3 scale function that will color each pie segment
32+
let scaleC = d3.scaleOrdinal(anu.ordinalChromatic('d310').toStandardMaterial());
33+
34+
//Create a Center of Transform TransformNode that serves the parent node for all our meshes that make up our chart
35+
let CoT = anu.create('cot', 'cot');
36+
//Select our CoT so that we have it as a Selection object
37+
let chart = anu.selectName('cot', scene);
38+
39+
//Create cylinder meshes as children of our CoT to form our pie segments based on the arcs we calculated
40+
let pieSegments = chart.bind('cylinder',
41+
{
42+
arc: (d,n,i) => (d.endAngle - d.startAngle) / (Math.PI * 2), //Babylon cylinders use percentages, so we have to convert this value
43+
diameter: 1,
44+
height: 0.1,
45+
enclose: true
46+
},
47+
arcs)
48+
.rotationY((d,n,i) => d.startAngle - Math.PI / 2); //Offset the rotation so that the segments "start" at 12 o'clock and not 3 o'clock
49+
50+
51+
//Initialize the CSG
52+
await BABYLON.InitializeCSG2Async();
53+
54+
//Create an "inner" cylinder that will be used to subtractively create the hole in the center of the pie to form a donut
55+
const inner = anu.create('cylinder', 'cylinder', { diameter: 0.5 });
56+
//Create the inner cylinder's CSG representation, which is used for calculations
57+
const innerCSG = BABYLON.CSG2.FromMesh(inner);
58+
59+
//Keep a list of all the new Meshes we are about to create
60+
const newMeshes = [];
61+
62+
//For each pie segment...
63+
pieSegments.run((d,n,i) => {
64+
//Create this segment's CSG representation
65+
const segmentCSG = BABYLON.CSG2.FromMesh(n);
66+
//Subtract the inner cylinder from the pie segment
67+
const donutSegmentCSG = segmentCSG.subtract(innerCSG);
68+
//Create the mesh from the CSG result
69+
const newMesh = donutSegmentCSG.toMesh(n.name, scene, { centerMesh: false }); //Disable centerMesh to keep our original pivot point
70+
//Store the reference so that we can rebuild our Selection
71+
newMeshes.push(newMesh);
72+
73+
//Cleanup, destroy the CSGs
74+
segmentCSG.dispose();
75+
donutSegmentCSG.dispose();
76+
})
77+
78+
//Because we created new meshes, we can create a new Anu Selection in case we want to further manipulate them
79+
const donutSegments = new anu.Selection(newMeshes); //Here we use the list of mesh references re created
80+
donutSegments.prop('parent', CoT) //Re-set the parent
81+
.metadata('data', (d,n,i) => arcs[i]) //Re-set the data
82+
.material((d,n,i) => scaleC(i)); //Set the material to color the segments
83+
84+
//Dispose of the original pie segments
85+
pieSegments.dispose();
86+
//Dispose of the inner cylinder and its CSG representation
87+
inner.dispose();
88+
innerCSG.dispose();
89+
90+
return scene;
91+
}

docs/anu-examples/layout.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -110,47 +110,47 @@ export function layout(engine) {
110110

111111
let advancedTexture = GUI.AdvancedDynamicTexture.CreateFullscreenUI('UI');
112112

113-
// let selectBox = new GUI.SelectionPanel('sp', [rowsGroup, curveGroup, marginXgroup, marginYgroup, layoutGroup]);
114-
// selectBox.width = 0.2;
115-
// selectBox.height = 1.5;
116-
// selectBox.scaleX = 0.6;
117-
// selectBox.scaleY = 0.6;
118-
// selectBox.background = '#FFFFFF';
119-
// selectBox.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
120-
// selectBox.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_CENTER;
121-
// selectBox.fontFamily = 'times new roman';
122-
// selectBox.fontSize = '14pt';
123-
124-
// let rect2 = GUI.Button.CreateSimpleButton('button1', 'add chart');
125-
// rect2.width = 0.2; // 0.2 = 20%
126-
// rect2.height = '40px';
127-
// rect2.cornerRadius = 20;
128-
// rect2.color = 'white';
129-
// rect2.thickness = 4;
130-
// rect2.background = 'blue';
131-
132-
// rect2.top = 200; //200 px
133-
// rect2.left = '10%';
134-
// rect2.onPointerClickObservable.add(() => {
135-
// addChart(scene);
136-
// });
137-
138-
// let rect1 = GUI.Button.CreateSimpleButton('button2', 'remove chart');
139-
// rect1.width = 0.2; // 0.2 = 20%
140-
// rect1.height = '40px';
141-
// rect1.cornerRadius = 20;
142-
// rect1.color = 'white';
143-
// rect1.thickness = 4;
144-
// rect1.background = 'blue';
145-
146-
// rect1.top = 250; //200 px
147-
// rect1.left = '10%';
148-
// rect1.onPointerClickObservable.add(() => {
149-
// removeChart(scene);
150-
// });
151-
// advancedTexture.addControl(rect1);
152-
// advancedTexture.addControl(rect2);
153-
// advancedTexture.addControl(selectBox);
113+
let selectBox = new GUI.SelectionPanel('sp', [rowsGroup, curveGroup, marginXgroup, marginYgroup, layoutGroup]);
114+
selectBox.width = 0.2;
115+
selectBox.height = 1.5;
116+
selectBox.scaleX = 0.6;
117+
selectBox.scaleY = 0.6;
118+
selectBox.background = '#FFFFFF';
119+
selectBox.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
120+
selectBox.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_CENTER;
121+
selectBox.fontFamily = 'times new roman';
122+
selectBox.fontSize = '14pt';
123+
124+
let rect2 = GUI.Button.CreateSimpleButton('button1', 'add chart');
125+
rect2.width = 0.2; // 0.2 = 20%
126+
rect2.height = '40px';
127+
rect2.cornerRadius = 20;
128+
rect2.color = 'white';
129+
rect2.thickness = 4;
130+
rect2.background = 'blue';
131+
132+
rect2.top = 200; //200 px
133+
rect2.left = '10%';
134+
rect2.onPointerClickObservable.add(() => {
135+
addChart(scene);
136+
});
137+
138+
let rect1 = GUI.Button.CreateSimpleButton('button2', 'remove chart');
139+
rect1.width = 0.2; // 0.2 = 20%
140+
rect1.height = '40px';
141+
rect1.cornerRadius = 20;
142+
rect1.color = 'white';
143+
rect1.thickness = 4;
144+
rect1.background = 'blue';
145+
146+
rect1.top = 250; //200 px
147+
rect1.left = '10%';
148+
rect1.onPointerClickObservable.add(() => {
149+
removeChart(scene);
150+
});
151+
advancedTexture.addControl(rect1);
152+
advancedTexture.addControl(rect2);
153+
advancedTexture.addControl(selectBox);
154154

155155
return scene;
156156
}

docs/anu-examples/pieChart.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function pieChart(engine){
3939
let pieSegments = chart.bind('cylinder',
4040
{
4141
arc: (d,n,i) => (d.endAngle - d.startAngle) / (Math.PI * 2), //Babylon cylinders use percentages, so we have to convert this value
42+
diameter: 1,
4243
height: 0.1,
4344
enclose: true //Only strictly necessary to set to true if the pie segments are separated from each other
4445
},

docs/anu-examples/prismMap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ export function prismMap(engine) {
3838
}, {});
3939

4040
//Create a D3 scale to map the airport counts with the height of the prisms
41-
let scaleY = d3.scaleLinear().domain([0, d3.max(Object.values(counts))]).range([0, 0.25]);
41+
let scaleY = d3.scaleLinear().domain([0, d3.max(Object.values(counts))]).range([0, -0.25]); //Negative range because our map is technically upside down
4242

4343
//Our geoJSON uses full state names but our airport data uses abbreviations
4444
const stateAbbreviations = { "Alabama": "AL", "Alaska": "AK", "Arizona": "AZ", "Arkansas": "AR", "California": "CA", "Colorado": "CO", "Connecticut": "CT", "Delaware": "DE", "District of Columbia": "DC", "Florida": "FL", "Georgia": "GA", "Hawaii": "HI", "Idaho": "ID", "Illinois": "IL", "Indiana": "IN", "Iowa": "IA", "Kansas": "KS", "Kentucky": "KY", "Louisiana": "LA", "Maine": "ME", "Maryland": "MD", "Massachusetts": "MA", "Michigan": "MI", "Minnesota": "MN", "Mississippi": "MS", "Missouri": "MO", "Montana": "MT", "Nebraska": "NE", "Nevada": "NV", "New Hampshire": "NH", "New Jersey": "NJ", "New Mexico": "NM", "New York": "NY", "North Carolina": "NC", "North Dakota": "ND", "Ohio": "OH", "Oklahoma": "OK", "Oregon": "OR", "Pennsylvania": "PA", "Rhode Island": "RI", "South Carolina": "SC", "South Dakota": "SD", "Tennessee": "TN", "Texas": "TX", "Utah": "UT", "Vermont": "VT", "Virginia": "VA", "Washington": "WA", "West Virginia": "WV", "Wisconsin": "WI", "Wyoming": "WY" };
4545

46-
//Assign each state a Material and therefore, their color, based on the number of airports
47-
states.scalingY((d) => -scaleY(counts[stateAbbreviations[d.NAME]]))
48-
46+
//Adjust the height of each state based on the number of airports
47+
states.scalingY((d) => scaleY(counts[stateAbbreviations[d.NAME]]))
48+
4949
return scene;
5050
}

docs/examples/donut_chart.md

Lines changed: 15 additions & 0 deletions

docs/examples/index.md

Lines changed: 2 additions & 1 deletion

docs/package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"d3": "^7.9.0",
2323
"d3-force-3d": "^3.0.5",
2424
"earcut": "^3.0.1",
25+
"manifold-3d": "^3.2.1",
2526
"vite-plugin-dynamic-import": "^1.5.0",
2627
"vitepress": "^1.0.1"
2728
}
44.5 KB
36.4 KB

0 commit comments

Comments
 (0)