"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var d3 = require("d3");
// noinspection TsLint
var d3_tip_1 = require("d3-tip");
var ScatterplotOptions_1 = require("./ScatterplotOptions");
var Scatterplot = /** @class */ (function () {
    function Scatterplot(options) {
        if (options === void 0) { options = new ScatterplotOptions_1.default(); }
        this.options = options;
        this.width = options.width;
        this.height = options.height;
    }
    Scatterplot.prototype.render = function () {
        var div = document.createElement("div");
        div.classList.add("lto-vis-scatterplot");
        return div;
    };
    Scatterplot.prototype.init = function (element) {
        var _this = this;
        this.options.data.then(function (data) {
            var margin = { top: 10, right: 10, bottom: 10, left: 10 };
            var outerWidth = _this.width;
            var outerHeight = _this.height;
            var width = outerWidth - margin.left - margin.right;
            var height = outerHeight - margin.top - margin.bottom - 20;
            var x = d3.scaleLinear().range([0, width]).nice();
            var y = d3.scaleLinear().range([height, 0]).nice();
            var xCat = "xcat";
            var yCat = "ycat";
            var rCat = "rcat";
            var colorCat = "type";
            // @ts-ignore
            var xMax = d3.max(data, function (d) { return d[xCat]; }) * 1.05;
            // @ts-ignore
            var xMin = d3.min(data, function (d) { return d[xCat]; });
            xMin = xMin > 0 ? 0 : xMin;
            // @ts-ignore
            var yMax = d3.max(data, function (d) { return d[yCat]; }) * 1.05;
            // @ts-ignore
            var yMin = d3.min(data, function (d) { return d[yCat]; });
            yMin = yMin > 0 ? 0 : yMin;
            x.domain([xMin, xMax]);
            y.domain([yMin, yMax]);
            var xAxis = d3.axisBottom(x).tickSize(-height);
            var yAxis = d3.axisLeft(y).tickSize(-width);
            var color = d3.scaleOrdinal(_this.options.color);
            // @ts-ignore
            var tooltip = d3_tip_1.default()
                .attr("class", "d3-tip")
                .offset([-10, 0])
                .html(function (d) { return xCat + ": " + d[xCat] + "<br>" + yCat + ": " + d[yCat]; });
            var svg = d3.select(element)
                .append("svg")
                .attr("width", outerWidth)
                .attr("height", outerHeight)
                .append("g")
                .attr("transform", "translate(20, 0)");
            svg.call(tooltip);
            svg.append("rect")
                .attr("width", width)
                .attr("height", height);
            svg.append("g")
                .classed("x axis", true)
                .attr("transform", "translate(0," + height + ")")
                .call(xAxis)
                .append("text")
                .classed("label", true)
                .attr("x", width)
                .attr("y", margin.bottom - 10)
                .style("text-anchor", "end")
                .text(xCat);
            svg.append("g")
                .classed("y axis", true)
                .call(yAxis)
                .append("text")
                .classed("label", true)
                .attr("transform", "rotate(-90)")
                .attr("y", 0)
                .attr("dy", ".71em")
                .style("text-anchor", "end")
                .text(yCat);
            var objects = svg.append("svg")
                .classed("objects", true)
                .attr("width", width)
                .attr("height", height);
            objects.append("svg:line")
                .classed("axisLine hAxisLine", true)
                .attr("x1", 0)
                .attr("y1", 0)
                .attr("x2", width)
                .attr("y2", 0)
                .attr("transform", "translate(0," + height + ")");
            objects.append("svg:line")
                .classed("axisLine vAxisLine", true)
                .attr("x1", 0)
                .attr("y1", 0)
                .attr("x2", 0)
                .attr("y2", height);
            objects.selectAll(".dot")
                .data(data)
                .enter().append("circle")
                .classed("dot", true)
                .attr("r", function (d) { return 6 * Math.sqrt(d[rCat] / Math.PI); })
                .attr("transform", function (d) { return "translate(" + x(d[xCat]) + "," + y(d[yCat]) + ")"; })
                .style("fill", function (d) { return color(d[colorCat]); })
                .on("mouseover", tooltip.show)
                .on("mouseout", tooltip.hide);
            var legend = svg.selectAll(".legend")
                .data(color.domain())
                .enter().append("g")
                .classed("legend", true)
                .attr("transform", function (d, i) { return "translate(" + (i * 75) + ", " + (height + 20) + ")"; });
            legend.append("circle")
                .attr("r", 3.5)
                .attr("cx", 20)
                .attr("fill", color);
            legend.append("text")
                .attr("x", 26)
                .attr("dy", ".35em")
                .text(function (d) { return d; });
        });
    };
    return Scatterplot;
}());
exports.default = Scatterplot;
//# sourceMappingURL=Scatterplot.js.map