Skip to content

Commit 0ed7788

Browse files
committed
refactor: lift D3Chart mixin into impl class
1 parent 39f9a0c commit 0ed7788

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

packages/d3chart/src/Axes.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { axisLeft, axisBottom } from 'd3-axis';
22

3-
import { Margin } from './Margin';
3+
import { D3Chart } from './D3Chart';
44

55
const scaleFuncMap = {
66
left: axisLeft,
@@ -62,8 +62,7 @@ class AxesImpl {
6262

6363
let axis = this.group[direction];
6464
if (!axis) {
65-
// TODO: make the dependency on D3Chart explicit.
66-
axis = this.group[direction] = this.that[direction].append('g');
65+
axis = this.group[direction] = this.that.d3chart[direction].append('g');
6766
axis.append('text')
6867
.attr('fill', '#000')
6968
.style('font-size', '11pt')
@@ -109,7 +108,7 @@ class AxesImpl {
109108
}
110109
}
111110

112-
export const Axes = Base => class extends Margin(Base) {
111+
export const Axes = Base => class extends D3Chart(Base) {
113112
constructor () {
114113
super(...arguments);
115114
this.axes = new AxesImpl(this);

packages/d3chart/src/Crosshairs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Margin } from './Margin';
1+
import { D3Chart } from './D3Chart';
22

33
class CrosshairsImpl {
44
constructor (that) {
@@ -13,13 +13,13 @@ class CrosshairsImpl {
1313
const that = this.that;
1414

1515
const plotBounds = that.margin.bounds('plot');
16-
this.target = that.plot.append('rect', ':first-child')
16+
this.target = that.d3chart.plot.append('rect', ':first-child')
1717
.classed('crosshairs-target', true)
1818
.attr('width', plotBounds.width)
1919
.attr('height', plotBounds.height)
2020
.style('opacity', 0.0);
2121

22-
const g = that.plot.append('g')
22+
const g = that.d3chart.plot.append('g')
2323
.classed('crosshairs', true)
2424
.style('pointer-events', 'none');
2525

@@ -83,7 +83,7 @@ class CrosshairsImpl {
8383
}
8484
}
8585

86-
export const Crosshairs = Base => class extends Margin(Base) {
86+
export const Crosshairs = Base => class extends D3Chart(Base) {
8787
constructor () {
8888
super(...arguments);
8989
if (!this.crosshairs) {

packages/d3chart/src/D3Chart.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import 'd3-transition';
66

77
import { Margin } from './Margin';
88

9-
export const D3Chart = Base => class extends Margin(InitSize(Base)) {
10-
constructor () {
11-
super(...arguments);
9+
class D3ChartImpl {
10+
constructor (that) {
11+
this.that = that;
1212

13-
this.svg = select(this.el)
13+
this.svg = select(this.that.el)
1414
.append('svg')
1515
.attr('xmlns', 'http://www.w3.org/2000/svg');
1616

@@ -32,18 +32,27 @@ export const D3Chart = Base => class extends Margin(InitSize(Base)) {
3232
.classed('plot', true);
3333
}
3434

35-
initD3Chart () {
36-
this.svg.attr('width', this.width)
37-
.attr('height', this.height);
35+
init () {
36+
this.svg.attr('width', this.that.width)
37+
.attr('height', this.that.height);
3838

39-
const margin = this.margin.get();
39+
const margin = this.that.margin.get();
4040

4141
this.left.attr('transform', `translate(0,${margin.top})`);
42-
this.bottom.attr('transform', `translate(${margin.left},${this.height - margin.bottom})`);
43-
this.right.attr('transform', `translate(${this.width - margin.right},${margin.top})`);
42+
this.bottom.attr('transform', `translate(${margin.left},${this.that.height - margin.bottom})`);
43+
this.right.attr('transform', `translate(${this.that.width - margin.right},${margin.top})`);
4444
this.top.attr('transform', `translate(${margin.left},0)`);
4545
this.plot.attr('transform', `translate(${margin.left},${margin.top})`);
4646
}
47+
}
48+
49+
export const D3Chart = Base => class extends Margin(InitSize(Base)) {
50+
constructor () {
51+
super(...arguments);
52+
if (!this.d3chart) {
53+
this.d3chart = new D3ChartImpl(this);
54+
}
55+
}
4756
};
4857

4958
export class Swatches extends D3Chart(VisComponent) {
@@ -52,44 +61,44 @@ export class Swatches extends D3Chart(VisComponent) {
5261

5362
this.width = options.width;
5463
this.height = options.height;
55-
this.margin.set(options.margin)
56-
.initD3Chart();
64+
this.margin.set(options.margin);
65+
this.d3chart.init();
5766

5867
const margin = this.margin.get();
5968

60-
this.left.append('rect')
69+
this.d3chart.left.append('rect')
6170
.attr('x', 0)
6271
.attr('y', 0)
6372
.attr('width', margin.left)
6473
.attr('height', this.height - margin.bottom - margin.top)
6574
.style('stroke', 'black')
6675
.style('fill', 'red');
6776

68-
this.bottom.append('rect')
77+
this.d3chart.bottom.append('rect')
6978
.attr('x', 0)
7079
.attr('y', 0)
7180
.attr('width', this.width - margin.left - margin.right)
7281
.attr('height', margin.bottom)
7382
.style('stroke', 'black')
7483
.style('fill', 'green');
7584

76-
this.right.append('rect')
85+
this.d3chart.right.append('rect')
7786
.attr('x', 0)
7887
.attr('y', 0)
7988
.attr('width', margin.right)
8089
.attr('height', this.height - margin.bottom - margin.top)
8190
.style('stroke', 'black')
8291
.style('fill', 'cyan');
8392

84-
this.top.append('rect')
93+
this.d3chart.top.append('rect')
8594
.attr('x', 0)
8695
.attr('y', 0)
8796
.attr('width', this.width - margin.left - margin.right)
8897
.attr('height', margin.top)
8998
.style('stroke', 'black')
9099
.style('fill', 'yellow');
91100

92-
this.plot.append('rect')
101+
this.d3chart.plot.append('rect')
93102
.attr('x', 0)
94103
.attr('y', 0)
95104
.attr('width', this.width - margin.left - margin.right)

0 commit comments

Comments
 (0)