-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathaxis.jsx
182 lines (146 loc) · 4.49 KB
/
axis.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"use strict";
import {
default as React,
Component,
PropTypes
} from 'react';
import D3Axis from 'd3-axis';
import D3Selection from 'd3-selection'
import ReactFauxDOM from 'react-faux-dom';
import {scale} from '../utils/scale';
export default class Axis extends Component {
constructor (props) {
super(props);
}
static defaultProps = {
range: null,
rangeRoundBands: null,
domain: null,
tickFormat: null,
tickOrient: null
}
static PropTypes = {
showAxis: PropTypes.bool,
type: PropTypes.string,
orient: PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
tickOrient: PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
}
_mkTickAxis () {
const {
type,
tickOrient,
tickFormat,
tickPadding,
tickSizeInner,
tickSizeOuter,
ticks,
tickValues
} = this.props;
var func = D3Axis;
if(tickOrient === 'left') {
func = func.axisLeft(this._mkScale(this.props));
}else if (tickOrient === 'right') {
func = func.axisRight(this._mkScale(this.props))
}else if (tickOrient === 'top') {
func = func.axisTop(this._mkScale(this.props))
}else if (tickOrient === 'bottom') {
func = func.axisBottom(this._mkScale(this.props))
}
if(tickFormat)
func.tickFormat(tickFormat);
if(tickPadding)
func.tickPadding(tickPadding);
if(tickSizeOuter)
func.tickSizeOuter(tickSizeOuter);
if(tickSizeInner)
func.tickSizeInner(tickSizeInner);
if(tickValues)
func.tickValues(tickValues);
if(ticks)
func.ticks.apply(null, ticks);
return func;
}
_mkScale () {
var newScale
if(this.props.scale === 'ordinal')
newScale = 'band'
else
newScale = this.props.scale
var func = scale(Object.assign({}, this.props, {scale: newScale}));
return func;
}
render () {
const {
showAxis,
gridAxisClassName,
axisClassName,
type,
style,
axisStyling, //styling object that holds user defined css classes for different axis elements
gridStyleClassName //css class to style grids on chart
} = this.props;
var axisGroup = ReactFauxDOM.createElement('g');
if(type === 'x')
var axisClasses = `${axisClassName} axis x`
else if(type === 'y')
var axisClasses = `${axisClassName} axis y`
else if(type === 'gridx' || type === 'gridy')
var axisClasses = `${gridAxisClassName} grid-axis`
axisGroup.setAttribute('class', axisClasses);
var axisDom = D3Selection.select(axisGroup);
axisDom.call(this._mkTickAxis());
if(!showAxis) {
axisDom.selectAll(".grid-axis .tick text")
.style("opacity", "0")
if(type === 'gridx' || type === 'gridy') {
// hide domain in grids
axisDom.selectAll(".grid-axis .domain")
.style("opacity", "0")
}
}
// apply user defined axis path style (path refers to x-axis line)if provided or else default
if(axisStyling && axisStyling.pathClassName) {
var axisPath = axisDom.selectAll('.axis path')
axisPath.attr("class", axisStyling.pathClassName);
}
else
axisDom.selectAll('.axis path')
.style('fill', 'none')
.style('stroke', '#000')
.style('shape-rendering', 'crispEdges')
.style('display','none')
// apply user defined style for axis tick line if provided or else default
if(axisStyling && axisStyling.ticksClassName) {
var axisLine = axisDom.selectAll('.axis line')
axisLine.attr("class", axisStyling.ticksClassName);
}
else
axisDom.selectAll('.axis line')
.style('fill', 'none')
.style('stroke', '#000')
.style('shape-rendering', 'crispEdges');
// apply user defined style for grid axes if provided or else default
if(gridStyleClassName) {
var grids = axisDom.selectAll('.grid-axis line')
grids.attr("class", gridStyleClassName);
}
else
axisDom.selectAll('.grid-axis line')
.style('opacity', .2)
.style('fill', 'none')
.style('stroke', '#000')
.style('stroke-width', '1.5px')
// Axis tick labels style
var axisText = axisDom.selectAll('.axis text')
if(style) {
for(var key in style) {
axisText.style(key, style[key]);
}
}
// user defined style for axis labels
else if(axisStyling && axisStyling.textClassName) {
axisText.attr("class", axisStyling.textClassName);
}
return axisDom.node().toReact();
}
}