-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaxis-chart.class.js
More file actions
151 lines (137 loc) · 4.61 KB
/
axis-chart.class.js
File metadata and controls
151 lines (137 loc) · 4.61 KB
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
import { CapMixin, ColorMixin, MarginMixin, transition } from 'dc'
import { axisBottom, axisLeft, axisRight, axisTop, extent as d3Extent, scaleLinear } from 'd3'
// https://gist.github.com/ialarmedalien/ebca7eeb67c6d0b0a7809c46ef160cd0
// Adapted into a class implementation by shawn@geodav.tech
/**
* Separate axis implementation.
*
* Examples:
* - {@link https://bl.ocks.org/ialarmedalien/0a4bf25ffc0fb96ae569a20f91957bc1 eslint on dc.js source}
* @class AxisChart
* @example
* // create an axis under #chart-container1 element using the default global chart group
* var chart1 = new AxisChart('#chart-container1');
* // create an axis under #chart-container2 element using chart group A
* var chart2 = new AxisChart('#chart-container2', 'chartGroupA');
* @param {String|node|d3.selection} parent - Any valid
* {@link https://github.com/d3/d3-selection d3 single selector} specifying
* a dom block element such as a div; or a dom element or d3 selection.
* @param {String} [chartGroup] - The name of the chart group this chart instance should be placed in.
* Interaction with a chart will only trigger events and redraws within the chart's group.
*/
export class AxisChart extends CapMixin(ColorMixin(MarginMixin)) {
_g
_scale
_elastic
_type = 'axisBottom'
_theAxis = axisBottom()
_axisData
validAxisTypes = ['axisBottom', 'axisTop', 'axisLeft', 'axisRight']
axisCap
calculateAxisScale() {
if (!this._scale || this._elastic) {
var extent = d3Extent(this._axisData, (d, i) => this.cappedValueAccessor(d, i))
if (extent[0] > 0) {
extent[0] = 0
}
if (extent[1] < 0) {
extent[1] = 0
}
this._scale = scaleLinear()
.domain(extent)
.range([0, this._type === 'axisBottom' || this._type === 'axisTop' ? this.effectiveWidth() : this.effectiveHeight()])
}
this._theAxis.scale(this._scale)
}
drawAxis() {
var axisG = this._g.select('g.axis')
this.calculateAxisScale()
if (axisG.empty()) {
axisG = this._g.append('g').attr('class', 'axis')
}
transition(axisG, this.transitionDuration(), this.transitionDelay()).call(this._theAxis)
}
_doRender() {
this.resetSvg()
this._g = this.svg().append('g').attr('transform', `translate(${this.margins().left}, ${this.margins().top})`)
this.drawChart()
return this
}
/**
* Gets or sets the axis type. The axis type can be any valid
* {@link https://github.com/d3/d3-axis d3 axis}. The default is
* axisBottom (a bottom axis).
* @see {@link https://github.com/d3/d3-axis d3 axis}
* @param {d3.type} [type]
* @returns {string|dc.axisChart} no args: type string; args: axis chart
*/
type(type) {
if (!arguments.length) {
return this._type
}
if (this.validAxisTypes.indexOf(type) !== -1) {
const axis = { axisBottom, axisTop, axisRight, axisLeft }[type]
this._theAxis = axis()
this._type = type
} else {
console.error(type + ' is not a valid d3 axis type')
}
return this
}
/**
* Gets or sets the axis scale. The axis scale can be any d3
* {@link https://github.com/d3/d3-scale quantitative scale}.
* @see {@link https://github.com/d3/d3-scale quantitative scale}
* @param {d3.scale} [scale] any value d3 scale
* @returns {d3.scale|dc.axisChart} no args: chart scale; args: axis chart
*/
scale(scale) {
if (!arguments.length) {
return this._scale
}
this._scale = scale
return this
}
/**
* Get or set the elasticity on the axis. If this attribute is set to true,
* then the axis will rescale to auto-fit the data range when filtered.
* @param {Boolean} [elastic] any valid boolean
* @returns {Boolean|dc.axisChart} no args: boolean; args: axis chart
*/
elastic(elastic) {
if (!arguments.length) {
return this._elastic
}
this._elastic = elastic
return this
}
/**
* Get the axis for the axis chart instance.
* See the {@link https://github.com/d3/d3-axis d3 axis object}
* documention for more information.
* @see {@link https://github.com/d3/d3-axis d3.axis}
* @example
* // customize axis tick format
* chart.axis().tickFormat(function (v) {return v + '%';});
* // customize axis tick values
* chart.axis().tickValues([0, 100, 200, 300]);
* @returns {d3.axis} d3 axis
*/
axis() {
return this._theAxis
}
drawChart() {
this._axisData = this.data()
this.drawAxis()
}
_doRedraw() {
this.drawChart()
return this
}
constructor(parent, chartGroup) {
super(parent, chartGroup)
this._minHeight = 0
this._defaultHeightCalc = () => 24
this.anchor(parent, chartGroup)
}
}