/
ChartGridRenderer.jsx
114 lines (102 loc) · 3.31 KB
/
ChartGridRenderer.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* ### ChartGrid Renderer
* Render a grid of N columns by N rows of the same kind of chart
* This is split up into two different sub-components, one for rendering a grid
* of bar (row) charts, and another for a grid of XY (line, dot, column) charts
*/
var React = require("react");
var PropTypes = React.PropTypes;
var update = require("react-addons-update");
var chartSizes = require("../../config/chart-sizes");
var clone = require("lodash/clone");
var assign = require("lodash/assign");
var each = require("lodash/each");
var gridDimensions = require("../../charts/cb-chart-grid/chart-grid-dimensions");
/* Chart grid types */
var ChartGridBars = require("./ChartGridBars.jsx");
var ChartGridXY = require("./ChartGridXY.jsx");
/**
* ### Component that decides which grid (small multiples) type to render and
* passes props to that renderer
* @property {boolean} editable - Allow the rendered component to interacted with and edited
* @property {object} displayConfig - Parsed visual display configuration for chart grid
* @property {object} chartProps - Properties used to draw this chart
* @instance
* @memberof renderers
*/
var ChartGridRenderer = React.createClass({
propTypes: {
editable: PropTypes.bool.isRequired,
displayConfig: PropTypes.shape({
margin: PropTypes.obj,
padding: PropTypes.obj
}).isRequired,
chartProps: PropTypes.shape({
chartSettings: PropTypes.array.isRequired,
data: PropTypes.array.isRequired,
scale: PropTypes.object.isRequired,
_grid: PropTypes.object.isRequired
}).isRequired,
showMetadata: PropTypes.bool,
metadata: PropTypes.object
},
_createMobileScale: function(_chartProps) {
var mobile = clone(_chartProps.mobile.scale, true);
var scale = assign({}, _chartProps.scale, mobile);
each(["prefix", "suffix"], function(text) {
if (!mobile.primaryScale[text] || mobile.primaryScale[text] === "") {
scale.primaryScale[text] = _chartProps.scale.primaryScale[text];
}
});
return scale;
},
render: function() {
var props = this.props;
var _chartProps = this.props.chartProps;
var gridTypeRenderer;
var dimensions = gridDimensions(props.width, {
metadata: props.metadata,
grid: _chartProps._grid,
data: _chartProps.data,
displayConfig: props.displayConfig,
showMetadata: props.showMetadata
});
var scale;
if (this.props.enableResponsive && _chartProps.hasOwnProperty("mobile") && this.props.svgSizeClass === "small") {
if (_chartProps.mobile.scale) {
scale = this._createMobileScale(_chartProps);
} else {
scale = _chartProps.scale;
}
} else {
scale = _chartProps.scale;
}
var chartProps = update(_chartProps, { $merge: { scale: scale }});
/* Pass a boolean that detects whether there is a title */
var hasTitle = (this.props.metadata.title.length > 0 && this.props.showMetadata);
/* Choose between grid of bars and grid of XY, and transfer all props to
* relevant component
*/
if (this.props.chartProps._grid.type == "bar") {
gridTypeRenderer = (
<ChartGridBars
{...this.props}
dimensions={dimensions}
scale={scale}
hasTitle={hasTitle}
/>
);
} else {
gridTypeRenderer = (
<ChartGridXY
{...this.props}
dimensions={dimensions}
scale={scale}
hasTitle={hasTitle}
/>
);
}
return gridTypeRenderer;
}
});
module.exports = ChartGridRenderer;