Polar.js 2.9 KB
Newer Older
L
lang 已提交
1 2 3
/**
 * @module echarts/coord/polar/Polar
 */
L
lang 已提交
4 5 6 7
define(function(require) {

    'use strict';

L
lang 已提交
8 9
    var RadiusAxis = require('./RadiusAxis');
    var AngleAxis = require('./AngleAxis');
L
lang 已提交
10

L
lang 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    /**
     * @alias {module:echarts/coord/polar/Polar}
     * @constructor
     * @param {string} name
     */
    var Polar = function (name) {

        /**
         * @type {string}
         */
        this.name = name || '';

        /**
         * x of polar center
         * @type {number}
         */
        this.cx = 0;

        /**
         * y of polar center
         * @type {number}
         */
        this.cy = 0;

        /**
         * @type {module:echarts/coord/polar/RadiusAxis}
         * @private
         */
        this._radiusAxis = new RadiusAxis();

        /**
         * @type {module:echarts/coord/polar/AngleAxis}
         * @private
         */
        this._angleAxis = new AngleAxis();
L
lang 已提交
46 47
    };

L
lang 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    Polar.prototype = {

        constructor: Polar,

        type: 'polar',

        /**
         * @return {module:echarts/coord/polar/AngleAxis}
         */
        getAngleAxis: function () {
            return this._angleAxis;
        },

        /**
         * @return {module:echarts/coord/polar/RadiusAxis}
         */
        getRadiusAxis: function () {
            return this._radiusAxis;
        },

        /**
         * Convert series data to a list of coorindates
         * @param {module:echarts/data/List} data
         * @return {Array}
         *  Return list of coordinates. For example:
         *  `[[10, 10], [20, 20], [30, 30]]`
         */
        dataToCoords: function (data) {
            return data.map(function (dataItem) {
                return this.dataToCoord([dataItem.getRadius(), dataItem.getAngle()]);
            }, this);
        },

        /**
         * Convert a single data item to coordinate.
         * Parameter data is an array which the first element is radius and the second is angle
         * @param {Array.<number>} data
         * @return {Array.<number>}
         */
        dataToCoord: function (data) {
            var radius = this._radiusAxis.dataToRadius(data[0]);
            var angle = this._angleAxis.dataToAngle(data[1]);

            var x = Math.cos(angle) * radius + this.cx;
            var y = Math.sin(angle) * radius + this.cy;

            return [x, y];
        },

        /**
         * Convert a coord to data
         * @param {Array.<number>} coord
         * @return {Array.<number>}
         */
        coordToData: function (coord) {
            var dx = coord[0] - this.cx;
            var dy = coord[1] - this.cy;

            var radius = Math.sqrt(dx * dx + dy * dy);
            dx /= radius;
            dy /= radius;

            var angle = Math.atan2(dy, dx);

            return [
                this._radiusAxis.radiusToData(radius),
                this._angleAxis.angleToData(angle)
            ];
        }
    }

L
lang 已提交
119 120
    return Polar;
});