Skip to content

Commit 697677e

Browse files
committed
Refactor legend to component
1 parent b7926e0 commit 697677e

7 files changed

Lines changed: 420 additions & 64 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ yarn-error.log*
2323

2424
# idea
2525
.idea
26+
27+
# testing
28+
/coverage

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.3",
44
"description": "A light weight React based mutation lollipop plot.",
55
"author": "Riza Nugraha",
6-
"license": "GPLv3",
6+
"license": "GPL-3.0-or-later",
77
"repository": "/react-mutation-plot",
88
"main": "dist/index.js",
99
"module": "dist/index.es.js",
@@ -51,6 +51,8 @@
5151
"@svgr/rollup": "^2.4.1",
5252
"babel-eslint": "^10.0.1",
5353
"cross-env": "^5.1.4",
54+
"enzyme": "^3.8.0",
55+
"enzyme-adapter-react-16": "^1.9.1",
5456
"eslint": "^5.0.1",
5557
"eslint-config-standard": "^11.0.0",
5658
"eslint-config-standard-react": "^6.0.0",

src/components/Legend.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as PropTypes from 'prop-types'
2+
import * as React from 'react'
3+
4+
const renderDomainLegendItems = (uniqueDomains) => {
5+
return uniqueDomains.map((d, idx) => {
6+
return (
7+
<React.Fragment>
8+
<rect
9+
fill={d.color}
10+
x={770}
11+
y={32 + (15 * idx) + 'px'}
12+
width={10}
13+
height={10}
14+
/>
15+
<text
16+
x={785}
17+
y={40 + (15 * idx) + 'px'}
18+
style={{
19+
fontSize: '11px',
20+
fontFamily: 'arial'
21+
}}
22+
>
23+
{d.label}
24+
</text>
25+
</React.Fragment>)
26+
})
27+
}
28+
29+
const pfamDomainsLegend = (domains) => {
30+
const uniqueDomains = Array.from(new Set(domains.map(d => d.label)))
31+
.map(label => ({
32+
label: label,
33+
color: domains.find(d => d.label === label).color
34+
}))
35+
return uniqueDomains.length ? (
36+
<g>
37+
<text x='770'
38+
y='20'
39+
style={{
40+
fontSize: '12px',
41+
fontWeight: 'bold',
42+
fontFamily: 'arial',
43+
fill: '#333'
44+
}}
45+
>
46+
Pfam domains:
47+
</text>
48+
{renderDomainLegendItems(uniqueDomains)}
49+
</g>
50+
) : ''
51+
}
52+
53+
const legend = (props) => {
54+
const {domains} = props
55+
return domains ? pfamDomainsLegend(domains) : ''
56+
}
57+
58+
legend.propTypes = {
59+
domains: PropTypes.array
60+
}
61+
export default legend

src/components/Legend.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import {shallow} from 'enzyme'
3+
import Legend from './Legend'
4+
5+
export const mockDomains = [
6+
{
7+
'startCodon': 57,
8+
'endCodon': 167,
9+
'label': 'Recep_L_domain',
10+
'color': '#2dcf00',
11+
'tooltip': {
12+
'header': 'Recep_L_domain',
13+
'body': 'Recep_L_domain (57 - 167)'
14+
}
15+
},
16+
{
17+
'startCodon': 185,
18+
'endCodon': 338,
19+
'label': 'Furin-like',
20+
'color': '#ff5353',
21+
'tooltip': {
22+
'header': 'Furin-like'
23+
}
24+
}
25+
]
26+
27+
describe('Legend', () => {
28+
it('should not render anything if theres is no domains', () => {
29+
const wrapper = shallow(<Legend domains={[]} />)
30+
expect(wrapper).toEqual({})
31+
})
32+
it('should render legend', () => {
33+
const wrapper = shallow(<Legend domains={mockDomains} />)
34+
expect(wrapper.find('g').length).toBe(1)
35+
expect(wrapper.find('rect').length).toBe(2)
36+
})
37+
})

src/index.js

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Lollipop, {lollipopSpec} from './components/Lollipop'
66
import Domain, {domainSpec} from './components/Domain'
77
import SVGAxis from './components/SVGAxis'
88
import Tooltip from './components/Tooltip'
9+
import Legend from './components/Legend'
910

1011
const LOLLIPOP_ID_CLASS_PREFIX = 'lollipop-'
1112
const DOMAIN_ID_CLASS_PREFIX = 'domain-'
@@ -193,57 +194,8 @@ class LollipopPlot extends React.Component {
193194
return (this.props.lollipops.find(lollipop => (lollipop.count > this.yMax())) ? '>= ' : '') + this.yMax()
194195
}
195196

196-
renderDomainLegendItems = (uniqueDomains) => {
197-
return uniqueDomains.map((d, idx) => {
198-
return (
199-
<React.Fragment>
200-
<rect
201-
fill={d.color}
202-
x={770}
203-
y={32 + (15 * idx) + 'px'}
204-
width={10}
205-
height={10}
206-
/>
207-
<text
208-
x={785}
209-
y={40 + (15 * idx) + 'px'}
210-
style={{
211-
fontSize: '11px',
212-
fontFamily: 'arial'
213-
}}
214-
>
215-
{d.label}
216-
</text>
217-
</React.Fragment>)
218-
})
219-
}
220-
221-
pfamDomainsLegend = () => {
222-
const {domains} = this.props
223-
const uniqueDomains = Array.from(new Set(domains.map(d => d.label)))
224-
.map(label => ({
225-
label: label,
226-
color: domains.find(d => d.label === label).color
227-
}))
228-
return (
229-
<g>
230-
<text x='770'
231-
y='20'
232-
style={{
233-
fontSize: '12px',
234-
fontWeight: 'bold',
235-
fontFamily: 'arial',
236-
fill: '#333'
237-
}}
238-
>
239-
Pfam domains:
240-
</text>
241-
{this.renderDomainLegendItems(uniqueDomains)}
242-
</g>
243-
)
244-
}
245-
246197
render() {
198+
const {domains} = this.props
247199
return (
248200
<React.Fragment>
249201
<svg xmlns='http://www.w3.org/2000/svg' width={this.svgWidth() + 200} height={this.svgHeight()}
@@ -267,7 +219,7 @@ class LollipopPlot extends React.Component {
267219
/>
268220
{this.lollipops()}
269221
{this.domains()}
270-
{this.pfamDomainsLegend()}
222+
<Legend domains={domains} />
271223
<SVGAxis
272224
key='horz'
273225
x={this.geneX()}

src/setupTests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { configure } from 'enzyme'
2+
import Adapter from 'enzyme-adapter-react-16'
3+
4+
configure({ adapter: new Adapter() })

0 commit comments

Comments
 (0)