polarCreator.js 5.1 KB
Newer Older
L
lang 已提交
1 2 3 4 5 6 7 8 9 10 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 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
// 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');

    // 依赖 PolarModel, AxisModel 做预处理
    require('./PolarModel');
    require('./AxisModel');

    /**
     * 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();
        var size = Math.min(width, height);
        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);
        axis.onBand = axisModel.get('boundaryGap');
        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();
                if (! isRadiusCategory) {
                    radiusAxis.scale.setExtentFromData(data.map(function (dataItem) { return dataItem.getRadius(); }), true);
                }
                if (! isAngleCategory) {
                    angleAxis.scale.setExtentFromData(data.map(function (dataItem) { return dataItem.getAngle(); }), true);
                }
            }
        });
    }

    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);

                polar.resize(polarModel, api);
                polarList.push(polar);

                polarModel.coordinateSystem = polar;
            });

            setPolarAxisFromSeries(polarList, ecModel, api);

            return polarList;
        }
    };

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