React: d3.event๋Š” ReactJS + d3JS ๊ตฌ์„ฑ ์š”์†Œ์—์„œ null์ž…๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2016๋…„ 04์›” 28์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: facebook/react

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์—์„œ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ํ•„๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” 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์˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  3 ๋Œ“๊ธ€

์ฐธ์กฐํ•˜์‹  ์ฝ”๋“œ๋Š” ์ผ๋ฐ˜์ ์ธ d3 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋ณด์ด๋ฉฐ ์ด๋ฒคํŠธ๋Š” React์˜ ์ด๋ฒคํŠธ ์‹œ์Šคํ…œ์„ ๊ฑฐ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ React์™€ ๊ด€๋ จ์ด ์—†๋Š” d3 ์งˆ๋ฌธ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์ด ๋ฌธ์ œ๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋˜ ๋‹ค๋ฅธ ์ฐธ๊ณ  ์‚ฌํ•ญ์œผ๋กœ ref ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DOM ๋…ธ๋“œ๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•˜๋ฉฐ ID๋กœ ์„ ํƒํ•˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. d3.select("#viewport") ์™€ ๊ฐ™์€ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋™์ผํ•œ ID๋ฅผ ์„ ํƒํ•˜๋Š” ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ์™€์˜ ์ƒํ˜ธ ์šด์šฉ์„ฑ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋™์ผํ•œ ํŽ˜์ด์ง€์—์„œ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ref ํ•˜๋ฉด ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๋ถ„๋ช…ํžˆ ์—ฌ๊ธฐ์—์„œ ๋๋‚  ๊ฒƒ์ด๋ฏ€๋กœ ์ €์—๊ฒŒ ํšจ๊ณผ์ ์ธ ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. (chrome) ๋””๋ฒ„๊ฑฐ์˜ ์†Œ์Šค ์ฐฝ์„ ์—ฝ๋‹ˆ๋‹ค.
  2. ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ค๋Š” ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ค‘๋‹จ์  ์„ค์ •
  3. ๋ฌธ์ œ ์žฌํ˜„
  4. ์‹คํ–‰์€ ๋””๋ฒ„๊ทธ๋กœ ์ค‘๋‹จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. d3(ํŠนํžˆ d3_selection_onListener(listener, argumentz))๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ์•ž์œผ๋กœ ๋‚˜์•„๊ฐ€์‹ญ์‹œ์˜ค.
  5. ์ฝ˜์†”์— ์ž…๋ ฅํ•˜๊ณ  d3 ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ „์—ญ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

> window.theD3versionHostingTheEvent = d3;

  1. listener.apply(this, argumentz); ๊นŒ์ง€ ์•ž์œผ๋กœ ์ด๋™ํ•œ ๋‹ค์Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์œ„์น˜์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€
  2. ์ฝ˜์†”์— ๋‹ค์‹œ ์นจ์ž…ํ•˜์—ฌ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 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์˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰