|
| 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 xAxisTransform = require( './utils/x_axis_transform.js' ); |
| 25 | +var yAxisTransform = require( './utils/y_axis_transform.js' ); |
| 26 | +var renderMarks = require( './marks' ); |
| 27 | +var init = require( './init.js' ); |
| 28 | +var sync = require( './sync.js' ); |
| 29 | + |
| 30 | + |
| 31 | +// VARIABLES // |
| 32 | + |
| 33 | +var debug = logger( 'hist:render:svg:main' ); |
| 34 | + |
| 35 | + |
| 36 | +// MAIN // |
| 37 | + |
| 38 | +/** |
| 39 | +* Renders a virtual DOM tree. |
| 40 | +* |
| 41 | +* @private |
| 42 | +* @param {Object} state - state |
| 43 | +* @returns {VTree} virtual tree |
| 44 | +*/ |
| 45 | +function render( state ) { |
| 46 | + var annotations; |
| 47 | + var clipPath; |
| 48 | + var canvas; |
| 49 | + var title; |
| 50 | + var graph; |
| 51 | + var marks; |
| 52 | + var xAxis; |
| 53 | + var yAxis; |
| 54 | + var bkgd; |
| 55 | + var defs; |
| 56 | + var svg; |
| 57 | + |
| 58 | + svg = state.$.svg; |
| 59 | + |
| 60 | + // Lazily initialize... |
| 61 | + if ( !svg.canvas ) { |
| 62 | + debug( 'Initializing components...' ); |
| 63 | + init( state ); |
| 64 | + } |
| 65 | + debug( 'Syncing component states...' ); |
| 66 | + sync( state ); |
| 67 | + |
| 68 | + debug( 'Rendering individual components...' ); |
| 69 | + |
| 70 | + debug( 'Rendering annotations...' ); |
| 71 | + annotations = svg.annotations.render(); |
| 72 | + |
| 73 | + debug( 'Rendering clip-path...' ); |
| 74 | + clipPath = svg.clipPath.render(); |
| 75 | + |
| 76 | + debug( 'Rendering canvas...' ); |
| 77 | + canvas = svg.canvas.render(); |
| 78 | + |
| 79 | + debug( 'Rendering graph...' ); |
| 80 | + graph = svg.graph.render(); |
| 81 | + |
| 82 | + debug( 'Rendering title...' ); |
| 83 | + title = svg.title.render(); |
| 84 | + |
| 85 | + debug( 'Rendering x-axis...' ); |
| 86 | + xAxis = svg.xAxis.render(); |
| 87 | + |
| 88 | + debug( 'Rendering y-axis...' ); |
| 89 | + yAxis = svg.yAxis.render(); |
| 90 | + |
| 91 | + debug( 'Rendering background...' ); |
| 92 | + bkgd = svg.bkgd.render(); |
| 93 | + |
| 94 | + debug( 'Rendering definitions...' ); |
| 95 | + defs = svg.defs.render(); |
| 96 | + |
| 97 | + debug( 'Rendering marks...' ); |
| 98 | + marks = renderMarks( state ); |
| 99 | + |
| 100 | + debug( 'Updating rendered components...' ); |
| 101 | + |
| 102 | + debug( 'Updating title...' ); |
| 103 | + title.properties.attributes.x = state.paddingLeft + ( state.graphWidth/2 ); |
| 104 | + title.properties.attributes.y = state.paddingTop / 2; |
| 105 | + |
| 106 | + debug( 'Updating x-axis...' ); |
| 107 | + xAxis.properties.className += ' x'; |
| 108 | + xAxis.properties.attributes.transform = xAxisTransform( state.xAxisOrient, state.graphHeight ); // eslint-disable-line max-len |
| 109 | + |
| 110 | + debug( 'Updating y-axis...' ); |
| 111 | + yAxis.properties.className += ' y'; |
| 112 | + yAxis.properties.attributes.transform = yAxisTransform( state.yAxisOrient, state.graphWidth ); // eslint-disable-line max-len |
| 113 | + |
| 114 | + debug( 'Assembling virtual tree...' ); |
| 115 | + |
| 116 | + debug( 'Inserting clip-path into definitions...' ); |
| 117 | + defs.children.push( clipPath ); |
| 118 | + defs.count += clipPath.count; |
| 119 | + |
| 120 | + debug( 'Inserting background into graph...' ); |
| 121 | + graph.children.push( bkgd ); |
| 122 | + graph.count += bkgd.count; |
| 123 | + |
| 124 | + debug( 'Inserting marks into graph...' ); |
| 125 | + graph.children.push( marks ); |
| 126 | + graph.count += marks.count; |
| 127 | + |
| 128 | + debug( 'Inserting x-axis into graph...' ); |
| 129 | + graph.children.push( xAxis ); |
| 130 | + graph.count += xAxis.count; |
| 131 | + |
| 132 | + debug( 'Inserting y-axis into graph...' ); |
| 133 | + graph.children.push( yAxis ); |
| 134 | + graph.count += yAxis.count; |
| 135 | + |
| 136 | + debug( 'Inserting title into annotations...' ); |
| 137 | + annotations.children.push( title ); |
| 138 | + annotations.count += title.count; |
| 139 | + |
| 140 | + debug( 'Inserting definitions into canvas...' ); |
| 141 | + canvas.children.push( defs ); |
| 142 | + canvas.count += defs.count; |
| 143 | + |
| 144 | + debug( 'Inserting graph into canvas...' ); |
| 145 | + canvas.children.push( graph ); |
| 146 | + canvas.count += graph.count; |
| 147 | + |
| 148 | + debug( 'Inserting annotations into canvas...' ); |
| 149 | + canvas.children.push( annotations ); |
| 150 | + canvas.count += annotations.count; |
| 151 | + |
| 152 | + return canvas; |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +// EXPORTS // |
| 157 | + |
| 158 | +module.exports = render; |
0 commit comments