ReactJS, d3JS ๋ฐ ES6 ์ ์ฌ์ฉํ์ฌ ์กฐ์ง๋๋ฅผ ๋ง๋ค๊ณ ์์ต๋๋ค. ์ฐจํธ๋ฅผ ๋ง๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค. ํ์ง๋ง ์ฐจํธ์ ํ๋/์ถ์ ๋ฐ ๋๊ธฐ ๋์์ ์ถ๊ฐํ๊ณ ์ถ์ผ๋ฏ๋ก d3.behavior.zoom ์ ์ฌ์ฉ ํ๋ํ๋ ๋ฐฉ๋ฒ์ ๋ณผ ์ ์์ง๋ง, d3.event์ null์ ๋๋ค.
ํ๋/์ถ์ ๋์์ div, svg ๋ฐ g์ ์ฐ๊ฒฐํ๋ ค๊ณ ์๋ํ์ง๋ง ์๋ฌด ๊ฒ๋ ๋์์ด ๋์ง ์์ต๋๋ค. ํ๋๋ ๋์์ด ํธ์ถ๋์ง๋ง ์ด๋ฒคํธ ๋ null์ ๋๋ค.
๋ด ASP.NET ํ๋ก์ ํธ์์ ๋ง์ stackoverflow ๋ต๋ณ์ด d3.event๊ฐ null์ธ ์ด์ ์ ๊ด๋ จ๋ ๋ฌธ์ ๋ก ์ธ๊ธ๋ systemJS ๊ตฌ์ฑ ์ด์ธ์ d3.js์ ๋ํ ์ฐธ์กฐ๊ฐ ์๋์ง ํ์ธํ์ต๋๋ค.
๋ด ์ฝ๋๋ ์ด์ ๋ฒ์ ์ ReactJS๋ฅผ ์ฌ์ฉํ๋ https://github.com/Swizec/candidate-bucket-chart/blob/169779e110c8825f4545c71ae5845ff7bad4f1f4/src/BubbleChart.jsx ์ ๊ฐ์ ์์ ์ ๋งค์ฐ ์ ์ฌํฉ๋๋ค.
์ด๊ฒ์ ๋ด systemJS ๊ตฌ์ฑ์ ๋๋ค.
<script>
System.defaultJSExtensions = true;
System.config({
map: {
'rx': "https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.7/rx.all.min.js",
'react': 'https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.js',
'react-dom': 'https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.js',
'd3': 'https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.js'
}
});
System.import('../app/app.js');
</script>
import React, { Component } from 'react';
import * as Rx from 'rx';
import * as d3 from 'd3';
import Person from './person';
class OrgChart extends Component {
constructor(props) {
super(props);
this.state = { persons: [] };
}
componentWillMount() {
// Just read a JSON file
var source = Rx.Observable.fromPromise($.getJSON("/js/org-persons.json"));
source.subscribe(
function(chart) {
var tree = d3.layout.tree()
.nodeSize([110, 50])
.separation(function() {
return 1;
});
var nodes = tree.nodes(chart[0]).reverse();
var links = tree.links(nodes);
var p = [];
nodes.forEach(function(node) {
fs.push((<Person x={node.x} y={node.y}></Person>));
}.bind(this));
this.setState({ person: p });
}.bind(this)
);
}
componentDidMount() {
var rootX = document.body.clientWidth / 4;
var rootY = 300;
// A standard setup with d3 to use it's zoom/drag behavior
var zoom = d3.behavior.zoom()
.scaleExtent([1, 10])
.on("zoom", this.zoomed)
.translate([rootX, rootY]);
// Attach zoom behavior to the first group in the svg.
d3.select("#viewport")
.call(zoom);
}
// When I drag the svg, this function gets called but the event is null. d3 is a global object.
zoomed() {
d3.select("#viewport").attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// renders an org chart
render() {
return (
<div id='treeContainer'><svg id='fullTree' width='100%'>
<g id='viewport'>
{ this.state.persons }
</g>
</svg></div>
);
}
}
export default OrgChart;
์ฐธ์กฐํ์ ์ฝ๋๋ ์ผ๋ฐ์ ์ธ d3 ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋ณด์ด๋ฉฐ ์ด๋ฒคํธ๋ React์ ์ด๋ฒคํธ ์์คํ ์ ๊ฑฐ์น์ง ์์ต๋๋ค. ์ด๊ฒ์ React์ ๊ด๋ จ์ด ์๋ d3 ์ง๋ฌธ์ฒ๋ผ ๋ณด์ ๋๋ค. ์ด๋ฌํ ์ด์ ๋ก ์ด ๋ฌธ์ ๋ฅผ ๋ง๋ฌด๋ฆฌํ๊ฒ ์ต๋๋ค.
๋ ๋ค๋ฅธ ์ฐธ๊ณ ์ฌํญ์ผ๋ก ref
๋ฅผ ์ฌ์ฉํ์ฌ DOM ๋
ธ๋๋ฅผ ์ฐธ์กฐํด์ผ ํ๋ฉฐ ID๋ก ์ ํํ์ง ์์์ผ ํฉ๋๋ค. d3.select("#viewport")
์ ๊ฐ์ ๊ฒ์ ์ฌ์ฉํ๋ฉด ๋์ผํ ID๋ฅผ ์ ํํ๋ ๋ค๋ฅธ ๊ตฌ์ฑ ์์์์ ์ํธ ์ด์ฉ์ฑ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฉฐ ๊ตฌ์ฑ ์์๊ฐ ๋์ผํ ํ์ด์ง์์ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉ๋๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์์ต๋๋ค. ref
ํ๋ฉด ์ด๋ฌํ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋ฉ๋๋ค.
์ด ๋ฌธ์ ๊ฐ ์๋ ๋ค๋ฅธ ์ฌ๋๋ค์ ๋ถ๋ช ํ ์ฌ๊ธฐ์์ ๋๋ ๊ฒ์ด๋ฏ๋ก ์ ์๊ฒ ํจ๊ณผ์ ์ธ ์ ๊ทผ ๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
> window.theD3versionHostingTheEvent = d3;
listener.apply(this, argumentz);
๊น์ง ์์ผ๋ก ์ด๋ํ ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ์์น์ ๋๋ฌํ ๋๊น์ง window.theD3versionHostingTheEvent == d3
๊ฒฐ๊ณผ๊ฐ false
์ด๋ฉด d3์ ๋ ์ธ์คํด์ค๋ฅผ ํ์คํ ์คํํ๊ณ ์๋ ๊ฒ์
๋๋ค. ์ ๊ฒฝ์ฐ์๋ ๋ด๊ฐ ์ฌ์ฉํ๊ณ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค ํ๋์ d3๊ฐ ๋ฒ๋ค๋ก ํฌํจ๋์ด ์๋ ๊ฒ์ผ๋ก ๋ํ๋ฌ์ต๋๋ค.
๊ฐ์ ธ์ค๊ธฐ/๋ด๋ณด๋ด๊ธฐ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ d3์์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ํ๋๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐ ๋ฌธ์ ๊ฐ ์๋ Babel์ ์ฌ์ฉํ๊ณ ์์ ๊ฐ๋ฅ์ฑ์ด ํฝ๋๋ค.
์ด๊ฒ์ ์ฐธ์กฐํ์ญ์์ค : https://github.com/d3/d3/issues/2733#issuecomment -190743489
๋ค์์ ์ฌ์ฉํ์ฌ d3์ ๊ฐ์ ธ์ค๋ ๋ฐฉ์์ ๋ณ๊ฒฝํ๊ณ ์ถ์ ๊ฒ์ ๋๋ค.
import * as d3 from 'd3';
import {event as currentEvent} from 'd3';
๊ทธ๋ฆฌ๊ณ d3.event
์ ๋ชจ๋ ํญ๋ชฉ์ currentEvent
.
์ด๊ฒ์ d3.js v3์ ๋ฌธ์ ๋ฅผ ์์ ํฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๊ฐ์ ธ์ค๊ธฐ/๋ด๋ณด๋ด๊ธฐ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ d3์์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ํ๋๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐ ๋ฌธ์ ๊ฐ ์๋ Babel์ ์ฌ์ฉํ๊ณ ์์ ๊ฐ๋ฅ์ฑ์ด ํฝ๋๋ค.
์ด๊ฒ์ ์ฐธ์กฐํ์ญ์์ค : https://github.com/d3/d3/issues/2733#issuecomment -190743489
๋ค์์ ์ฌ์ฉํ์ฌ d3์ ๊ฐ์ ธ์ค๋ ๋ฐฉ์์ ๋ณ๊ฒฝํ๊ณ ์ถ์ ๊ฒ์ ๋๋ค.
๊ทธ๋ฆฌ๊ณ
d3.event
์ ๋ชจ๋ ํญ๋ชฉ์currentEvent
.์ด๊ฒ์ d3.js v3์ ๋ฌธ์ ๋ฅผ ์์ ํฉ๋๋ค.