polarCreator.js 5.6 KB
Newer Older
L
lang 已提交
1 2 3 4 5 6 7
// TODO Axis scale
define(function (require) {

    var Polar = require('./Polar');
    var IntervalScale = require('../../scale/Interval');
    var OrdinalScale = require('../../scale/Ordinal');
    var numberUtil = require('../../util/number');
L
lang 已提交
8
    var zrUtil = require('zrender/core/util');
L
lang 已提交
9

L
lang 已提交
10
    // 依赖 PolarModel 做预处理
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 46 47 48 49 50 51
    require('./PolarModel');

    /**
     * Retrieve angle axis or radius axis belongs to the given polar
     * @param {string} axisType
     * @param {number} polarIndex
     * @param {module:echarts/model/Global} ecModel
     * @param {module:echarts/ExtensionAPI} api
     * @return {module:echarts/coord/polar/AxisModel}
     * @inner
     */
    function retrieveAxisModelForPolar(axisType, polarIndex, ecModel, api) {
        var axisModel;
        ecModel.eachComponent(axisType, function (model) {
            if (model.get('polarIndex') === polarIndex) {
                if (axisModel) {
                    api.log('Polar ' + polarIndex + ' has more than one ' + axisType);
                    return;
                }
                axisModel = model;
            }
        });
        return axisModel;
    }

    /**
     * Resize methods bound to the polar
     * @param {module:echarts/coord/polar/PolarModel} polarModel
     * @param {module:echarts/ExtensionAPI} api
     */
    function resizePolar(polarModel, api) {
        var center = polarModel.get('center');
        var radius = polarModel.get('radius');
        var width = api.getWidth();
        var height = api.getHeight();
        var parsePercent = numberUtil.parsePercent;

        this.cx = parsePercent(center[0], width);
        this.cy = parsePercent(center[1], height);

        var radiusAxis = this.getRadiusAxis();
L
lang 已提交
52
        var size = Math.min(width, height) / 2;
L
lang 已提交
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
        radiusAxis.setExtent(
            parsePercent(radius[0], size),
            parsePercent(radius[1], size)
        );
    }

    /**
     * @param {module:echarts/coord/cartesian/AxisModel} axisModel
     * @return {module:echarts/scale/*}
     * @inner
     */
    function createScaleByModel(axisModel) {
        var axisType = axisModel.get('type');
        if (axisType) {
            return axisType === 'value'
                ? new IntervalScale()
                : new OrdinalScale(axisModel.get('data'));
        }
    };

    /**
     * Set common axis properties
     * @param {module:echarts/coord/polar/AngleAxis|module:echarts/coord/polar/RadiusAxis}
     * @param {module:echarts/coord/polar/AxisModel}
     * @inner
     */
    function setAxis(axis, axisModel) {
        axis.type = axisModel.get('type');
        axis.scale = createScaleByModel(axisModel);
L
lang 已提交
82
        axis.onBand = axisModel.get('boundaryGap') && axis.type === 'category';
L
lang 已提交
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
        axis.inverse = axisModel.get('inverse');

        // Inject axis instance
        axisModel.axis = axis;
    }

    /**
     * Set polar axis scale from series data
     */
    function setPolarAxisFromSeries(polarList, ecModel, api) {
        ecModel.eachSeries(function (seriesModel) {
            if (seriesModel.get('coordinateSystem') === 'polar') {
                var polarIndex = seriesModel.get('polarIndex') || 0;

                var polar = polarList[polarIndex];
                if (! polar) {
                    api.log('Polar configuration not exist for series ' + seriesModel.name + '.');
                    return;
                }
                // Inject polar instance
                seriesModel.coordinateSystem = polar;

                var radiusAxis = polar.getRadiusAxis();
                var angleAxis = polar.getAngleAxis();
                var isRadiusCategory = radiusAxis.type === 'category';
                var isAngleCategory = angleAxis.type === 'category';

                var data = seriesModel.getData();
L
lang 已提交
111 112 113
                var valueMapper = function (a) {
                    return a;
                }
L
lang 已提交
114
                if (! isRadiusCategory) {
L
lang 已提交
115
                    radiusAxis.scale.setExtentFromData(data.mapRadius(valueMapper, true), true);
L
lang 已提交
116 117
                }
                if (! isAngleCategory) {
L
lang 已提交
118
                    angleAxis.scale.setExtentFromData(data.mapAngle(valueMapper, true), true);
L
lang 已提交
119 120 121
                }
            }
        });
L
lang 已提交
122 123 124 125 126 127

        zrUtil.each(polarList, function (polar) {
            // PENDING
            polar.getAngleAxis().scale.niceExtent(12);
            polar.getRadiusAxis().scale.niceExtent(5);
        });
L
lang 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    }

    var polarCreator = {

        create: function (ecModel, api) {
            var polarList = [];
            ecModel.eachComponent('polar', function (polarModel, idx) {
                var polar = new Polar(idx);
                // Inject resize method
                polar.resize = resizePolar;

                var radiusAxis = polar.getRadiusAxis();
                var angleAxis = polar.getAngleAxis();

                var radiusAxisModel = retrieveAxisModelForPolar('radiusAxis', idx, ecModel, api);
                var angleAxisModel = retrieveAxisModelForPolar('angleAxis', idx, ecModel, api);

                setAxis(radiusAxis, radiusAxisModel);
                setAxis(angleAxis, angleAxisModel);

L
lang 已提交
148
                if (angleAxis.type === 'category' && ! angleAxis.onBand) {
L
lang 已提交
149
                    var angle = 360 - 360 / (angleAxis.scale.count() + 1);
L
lang 已提交
150 151 152
                    angleAxis.setExtent(0, angle);
                }

L
lang 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166
                polar.resize(polarModel, api);
                polarList.push(polar);

                polarModel.coordinateSystem = polar;
            });

            setPolarAxisFromSeries(polarList, ecModel, api);

            return polarList;
        }
    };

    require('../../CoordinateSystem').register('polar', polarCreator);
});