我有此数据,
json:
const elements = chart.selectAll('g.cols.axis text');
// Get the values
const xValue = elements.attr('x');
const yValue = elements.attr('y');
elements.attr('x', null)
.attr('y', null)
.style("text-anchor", "end")
.attr('transform', `translate(${xValue},${yValue}) rotate(90)`);
我想做这样的事情:
var data = [{a:1,b:{amount: 10}, c:1},
{a:2,b:{amount: 20}, c:1},
{a:1,b:{amount: 30}, c:2}
];
有什么想法吗?
答案 0 :(得分:1)
您正在尝试对对象数组中的属性求和。
这将为您做到:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>three.js css2d - label</title>
<style>
body {
background-color: #000;
margin: 0;
overflow: hidden;
}
#info {
position: absolute;
top: 0px;
width: 100%;
color: #FFF;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
z-index: 1;
}
.label{
color: #FFF;
font-family: sans-serif;
padding: 2px;
background: rgba( 0, 0, 0, .6 );
}
a {
color: #000000;
}
</style>
</head>
<body>
<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - three.js css2d - label</div>
<script src="http://threejs.org/build/three.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/renderers/CSS2DRenderer.js"></script>
<script src="https://threejs.org/examples/js/effects/StereoEffect.js"></script>
<script>
var camera, scene, renderer, effect, labelRenderer;
var clock = new THREE.Clock();
var textureLoader = new THREE.TextureLoader();
var earth, moon;
init();
animate();
function init() {
var EARTH_RADIUS = 1;
var MOON_RADIUS = 0.27;
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 10, 5, 20 );
var controls = new THREE.OrbitControls( camera );
scene = new THREE.Scene();
var dirLight = new THREE.DirectionalLight( 0xffffff );
dirLight.position.set( 0, 0, 1 );
scene.add( dirLight );
var axesHelper = new THREE.AxesHelper( 5 );
scene.add( axesHelper );
//
var earthGeometry = new THREE.SphereBufferGeometry( EARTH_RADIUS, 16, 16 );
var earthMaterial = new THREE.MeshPhongMaterial( {
specular: 0x333333,
shininess: 5,
map: textureLoader.load('https://threejs.org/examples/textures/planets/earth_atmos_2048.jpg'),
specularMap: textureLoader.load('https://threejs.org/examples/textures/planets/earth_specular_2048.jpg'),
normalMap: textureLoader.load('https://threejs.org/examples/textures/planets/earth_normal_2048.jpg'),
normalScale: new THREE.Vector2( 0.85, 0.85 )
} );
earth = new THREE.Mesh( earthGeometry, earthMaterial );
scene.add( earth );
var moonGeometry = new THREE.SphereBufferGeometry( MOON_RADIUS, 16, 16 );
var moonMaterial = new THREE.MeshPhongMaterial( {
shininess: 5,
map: textureLoader.load('https://threejs.org/examples/textures/planets/moon_1024.jpg')
} );
moon = new THREE.Mesh( moonGeometry, moonMaterial );
scene.add( moon );
//
var earthDiv = document.createElement( 'div' );
earthDiv.className = 'label';
earthDiv.textContent = 'Earth';
earthDiv.style.marginTop = '-1em';
var earthLabel = new THREE.CSS2DObject( earthDiv );
earthLabel.position.set( 0, EARTH_RADIUS, 0 );
earth.add( earthLabel );
var moonDiv = document.createElement( 'div' );
moonDiv.className = 'label';
moonDiv.textContent = 'Moon';
moonDiv.style.marginTop = '-1em';
var moonLabel = new THREE.CSS2DObject( moonDiv );
moonLabel.position.set( 0, MOON_RADIUS, 0 );
moon.add( moonLabel );
//
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
labelRenderer = new THREE.CSS2DRenderer();
labelRenderer.setSize( window.innerWidth, window.innerHeight );
labelRenderer.domElement.style.position = 'absolute';
labelRenderer.domElement.style.top = 0;
document.body.appendChild( labelRenderer.domElement );
effect = new THREE.StereoEffect(renderer);
//effect.setEyeSeparation(1);
effect.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame( animate );
var elapsed = clock.getElapsedTime();
moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );
//renderer.render( scene, camera );
effect.render(scene, camera);
labelRenderer.render(scene, camera);
}
</script>
</body>
</html>
然后您可以使用let sum = [];
data.forEach(function(el){
if(sum[el.a] == undefined) sum[el.a] = [];
if(sum[el.a][el.c] == undefined) sum[el.a][el.c] = 0;
sum[el.a][el.c] += el.b.amount
});
访问a = 1
,c = 2
的总和。
此代码将与需要添加的较大数据集一起使用。
答案 1 :(得分:0)
如果我正确理解了您的问题,那么您在问如何对JSON对象执行分组和聚合。由于SQL只能在数据库领域中使用,因此您需要使用JavaScript函数,例如forEach(),map(),filter(),reduce()等。
为方便起见,我建议使用Lodash或Underscore之类的库。
以下是使用Lodash的示例:
let data = [
{a:1, b:{amount: 10}, c:1},
{a:2, b:{amount: 20}, c:1},
{a:1, b:{amount: 30}, c:2}
];
let results = _.sumBy(data, 'b.amount');
console.log(results);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
使用这些库可以消除编写自己的脚本的许多复杂性,尤其是在处理嵌套对象和数组时。
我建议尽可能使用纯JS,并仅在必要时使用库。