Skip to content

Commit 7f45950

Browse files
committed
Add rendering logic
This is a work in progress.
1 parent 987d772 commit 7f45950

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var logger = require( 'debug' );
24+
var h = require( 'virtual-dom/h' );
25+
26+
27+
// VARIABLES //
28+
29+
var debug = logger( 'rects:render:rects' );
30+
var ELEMENT = 'rect';
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Renders data as a rectangles.
37+
*
38+
* @private
39+
* @param {Object} state - state
40+
* @returns {Array<VTree>} array of virtual trees
41+
*/
42+
function render( state ) {
43+
var lineOpacity;
44+
var faceOpacity;
45+
var faceColor;
46+
var lineColor;
47+
var label;
48+
var props;
49+
var xPos;
50+
var yPos;
51+
var data;
52+
var out;
53+
var pos;
54+
var x0;
55+
var x1;
56+
var y;
57+
var i;
58+
var j;
59+
60+
debug( 'Rendering rectangles...' );
61+
62+
label = state.label;
63+
lineOpacity = state.lineOpacity;
64+
faceOpacity = state.faceOpacity;
65+
lineColor = state.lineColor;
66+
faceColor = state.faceColor;
67+
xPos = state.xPos;
68+
yPos = state.yPos;
69+
data = state.data;
70+
71+
out = new Array( data.length/3 );
72+
for ( i = 0; i < data.length; i += 3 ) {
73+
j = i / 3;
74+
75+
x0 = data[ i ];
76+
x1 = data[ i+1 ];
77+
y = data[ i+2 ];
78+
79+
pos = xPos( x0 );
80+
81+
debug( 'Rendering datum %d...', j );
82+
props = {
83+
'namespace': 'http://www.w3.org/2000/svg',
84+
'property': 'column',
85+
'className': 'column',
86+
'attributes': {
87+
'x': pos,
88+
'width': xPos( x1 ) - pos,
89+
'height': yPos( y ),
90+
'stroke': lineColor( x0, x1, y, j ),
91+
'stroke-opacity': lineOpacity( x0, x1, y, j ),
92+
'fill': faceColor( x0, x1, y, j ),
93+
'fill-opacity': faceOpacity( x0, x1, y, j ),
94+
'data-label': label( x0, x1, y, j )
95+
}
96+
};
97+
debug( 'Generating a virtual DOM tree (%s) with properties: %s.', ELEMENT, JSON.stringify( props ) );
98+
out[ j ] = h( ELEMENT, props, [] );
99+
}
100+
return out;
101+
}
102+
103+
104+
// EXPORTS //
105+
106+
module.exports = render;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var logger = require( 'debug' );
24+
var h = require( 'virtual-dom/h' );
25+
var columns = require( './columns.js' );
26+
var bars = require( './bars.js' );
27+
28+
29+
// VARIABLES //
30+
31+
var debug = logger( 'rects:render' );
32+
var ELEMENT = 'g';
33+
var RENDER = {
34+
'vertical': columns,
35+
'horizontal': bars
36+
};
37+
38+
39+
// MAIN //
40+
41+
/**
42+
* Renders a virtual DOM tree.
43+
*
44+
* @private
45+
* @returns {VTree} virtual DOM tree
46+
*/
47+
function render() {
48+
/* eslint-disable no-invalid-this */
49+
var children;
50+
var props;
51+
var vtree;
52+
var f;
53+
54+
debug( 'Rendering...' );
55+
56+
props = {
57+
'namespace': 'http://www.w3.org/2000/svg',
58+
'property': 'rects',
59+
'className': 'rects'
60+
};
61+
f = RENDER[ this.orientation ];
62+
children = f( this );
63+
64+
debug( 'Generating a virtual DOM tree (%s) with properties: %s.', ELEMENT, JSON.stringify( props ) );
65+
vtree = h( ELEMENT, props, children );
66+
67+
// Announce that a new tree has been rendered:
68+
this.emit( '_render', vtree );
69+
70+
return vtree;
71+
}
72+
73+
74+
// EXPORTS //
75+
76+
module.exports = render;

0 commit comments

Comments
 (0)