Вход Регистрация
Файл: protected/extensions/widgets/highcharts/assets/modules/map.src.js
Строк: 535
<?php
/**
 * @license Map plugin v0.1 for Highcharts
 *
 * (c) 2011-2013 Torstein Hønsi
 *
 * License: www.highcharts.com/license
 */

/* 
 * See www.highcharts.com/studies/world-map.htm for use case.
 *
 * To do:
 * - Optimize long variable names and alias adapter methods and Highcharts namespace variables
 * - Zoom and pan GUI
 */
(function (Highcharts) {
    var 
UNDEFINED,
        
Axis Highcharts.Axis,
        
each Highcharts.each,
        
extend Highcharts.extend,
        
merge Highcharts.merge,
        
pick Highcharts.pick,
        
numberFormat Highcharts.numberFormat,
        
plotOptions Highcharts.getOptions().plotOptions,
        
Color Highcharts.Color,
        
noop = function () {};
    
    
/**
     * Utility for reading SVG paths directly.
     * 
     * @todo This is moved to the Data plugin. Make sure it is deleted here.
     */
    
Highcharts.pathToArray = function (path) {
        var 
i;

        
// Move letters apart
        
path path.replace(/([A-Za-z])/g' $1 ');
        
// Trim
        
path path.replace(/^s*/, "").replace(/s*$/, "");
        
        
// Split on spaces and commas
        
path path.split(/[ ,]+/);
        
        for (
0path.lengthi++) {
            if (!/[
a-zA-Z]/.test(path[i])) {
                
path[i] = parseFloat(path[i]);
            }
        }
        return 
path;
    };
    
    
/**
     * Extend the Axis object with methods specific to maps
     */
    
Highcharts.wrap(Axis.prototype'init', function (proceedchartuserOptions) {
        
        if (
chart.options.chart.type === 'map') {
            
extend(this, {
                
                
/**
                 * Override to use the extreme coordinates from the SVG shape, not the
                 * data values
                 */
                
getSeriesExtremes: function () {
                    var 
isXAxis this.isXAxis,
                        
dataMin Number.MAX_VALUE,
                        
dataMax Number.MIN_VALUE;
                    
each(this.series, function (series) {
                        
dataMin Math.min(dataMinseries[isXAxis 'minX' 'minY']);
                        
dataMax Math.max(dataMaxseries[isXAxis 'maxX' 'maxY']);
                    });
                    
this.dataMin dataMin;
                    
this.dataMax dataMax;
                },
                
                
/**
                 * Override axis translation to make sure the aspect ratio is always kept
                 */
                
setAxisTranslation: function () {
                    var 
chart this.chart,
                        
mapRatio,
                        
plotRatio chart.plotWidth chart.plotHeight,
                        
isXAxis this.isXAxis,
                        
adjustedAxisLength,
                        
xAxis chart.xAxis[0],
                        
padAxis;
                    
                    
// Run the parent method
                    
Axis.prototype.setAxisTranslation.call(this);
                    
                    
// On Y axis, handle both
                    
if (!isXAxis && xAxis.transA !== UNDEFINED) {
                        
                        
// Use the same translation for both axes
                        
this.transA xAxis.transA Math.min(this.transAxAxis.transA);
                        
                        
mapRatio = (xAxis.max xAxis.min) / (this.max this.min);
                        
                        
// What axis to pad to put the map in the middle
                        
padAxis mapRatio plotRatio this xAxis;
                        
                        
// Pad it
                        
adjustedAxisLength = (padAxis.max padAxis.min) * padAxis.transA;
                        
padAxis.minPixelPadding = (padAxis.len adjustedAxisLength) / 2;
                    }
                    
                }
            });
        }    
        
        return 
proceed.call(thischartuserOptions);
    });
    
    
/**
     * Extend the default options with map options
     */
    
plotOptions.map merge(
        
plotOptions.scatter
        {
            
animationfalse// makes the complex shapes slow
            
minOpacity0.2,
            
nullColor'#F8F8F8',
            
borderColor'silver',
            
borderWidth1,
            
markernull,
            
stickyTrackingfalse,
            
tooltip: {
                
followPointertrue,
                
headerFormat'<span style="font-size:10px">{point.key}</span><br/>',
                
pointFormat'{series.name}: {point.y}<br/>'
            
}
        }
    );
    
    
/**
     * Add the series type
     */
    
Highcharts.seriesTypes.map Highcharts.extendClass(Highcharts.seriesTypes.scatter, {
        
type'map',
        
pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
            
stroke'borderColor',
            
'stroke-width''borderWidth',
            
fill'color'
        
},
        
colorKey'y',
        
trackerGroups: ['group''markerGroup'],
        
getSymbolnoop,
        
getExtremesFromAlltrue,
        
init: function (chart) {
            var 
series this,
                
valueDecimals chart.options.legend.valueDecimals,
                
legendItems = [],
                
name,
                
from,
                
to,
                
fromLabel,
                
toLabel,
                
colorRange,
                
gradientColor,
                
grad,
                
tmpLabel,
                
horizontal chart.options.legend.layout === 'horizontal';

            
            
Highcharts.Series.prototype.init.apply(thisarguments);
            
colorRange series.options.colorRange;

            if (
series.options.valueRanges) {
                
each(series.options.valueRanges, function (range) {
                    
from range.from;
                    
to range.to;
                    
                    
// Assemble the default name. This can be overridden by legend.options.labelFormatter
                    
name '';
                    if (
from === UNDEFINED) {
                        
name '< ';
                    } else if (
to === UNDEFINED) {
                        
name '> ';
                    }
                    if (
from !== UNDEFINED) {
                        
name += numberFormat(fromvalueDecimals);
                    }
                    if (
from !== UNDEFINED && to !== UNDEFINED) {
                        
name += ' - ';
                    }
                    if (
to !== UNDEFINED) {
                        
name += numberFormat(tovalueDecimals);
                    }
                    
                    
// Add a mock object to the legend items
                    
legendItems.push(Highcharts.extend({
                        
chartseries.chart,
                        
namename,
                        
options: {},
                        
drawLegendSymbolHighcharts.seriesTypes.area.prototype.drawLegendSymbol,
                        
visibletrue,
                        
setState: function () {},
                        
setVisible: function () {}
                    }, 
range));
                });
                
series.legendItems legendItems;

            } else if (
colorRange) {

                
from colorRange.from;
                
to colorRange.to;
                
fromLabel colorRange.fromLabel;
                
toLabel colorRange.toLabel;

                
// Flips linearGradient variables and label text.
                
grad horizontal ? [0010] : [0100]; 
                if (!
horizontal) {
                    
tmpLabel fromLabel;
                    
fromLabel toLabel;
                    
toLabel tmpLabel;
                } 

                
// Creates color gradient.
                
gradientColor = {
                    
linearGradient: { x1grad[0], y1grad[1], x2grad[2], y2grad[3] },
                    
stops
                    [
                        [
0from],
                        [
1to]
                    ]
                };

                
// Add a mock object to the legend items.
                
legendItems = [{
                    
chartseries.chart,
                    
options: {},
                    
fromLabelfromLabel,
                    
toLabeltoLabel,
                    
colorgradientColor,
                    
drawLegendSymbolthis.drawLegendSymbol,
                    
visibletrue,
                    
setState: function () {},
                    
setVisible: function () {}
                }];

                
series.legendItems legendItems;
            }
        },

        
/**
         * Gets the series' symbol in the legend and extended legend with more information.
         * 
         * @param {Object} legend The legend object
         * @param {Object} item The series (this) or point
         */
        
drawLegendSymbol: function (legenditem) {
            
            var 
spacing legend.options.symbolPadding,
                
padding pick(legend.options.padding8),
                
positionY,
                
positionX,
                
gradientSize this.chart.renderer.fontMetrics(legend.options.itemStyle.fontSize).h,
                
horizontal legend.options.layout === 'horizontal',
                
box1,
                
box2,
                
box3,
                
rectangleLength pick(legend.options.rectangleLength200);

            
// Set local variables based on option.
            
if (horizontal) {
                
positionY = -(spacing 2);
                
positionX 0;
            } else {
                
positionY = -rectangleLength legend.baseline - (spacing 2);
                
positionX padding gradientSize;
            }

            
// Creates the from text.
            
item.fromText this.chart.renderer.text(
                    
item.fromLabel,    // Text.
                    
positionX,        // Lower left x.
                    
positionY        // Lower left y.
                
).attr({
                    
zIndex2
                
}).add(item.legendGroup);
            
box1 item.fromText.getBBox();

            
// Creates legend symbol.
            // Ternary changes variables based on option.
            
item.legendSymbol this.chart.renderer.rect(
                
horizontal box1.box1.width spacing box1.gradientSize spacing,        // Upper left x.
                
box1.y,                                                                                // Upper left y.
                
horizontal rectangleLength gradientSize,                                            // Width.
                
horizontal gradientSize rectangleLength,                                        // Height.
                
2                                                                                    // Corner radius.
            
).attr({
                
zIndex1
            
}).add(item.legendGroup);
            
box2 item.legendSymbol.getBBox();

            
// Creates the to text.
            // Vertical coordinate changed based on option.
            
item.toText this.chart.renderer.text(
                    
item.toLabel,
                    
box2.box2.width spacing,
                    
horizontal positionY box2.box2.height spacing
                
).attr({
                    
zIndex2
                
}).add(item.legendGroup);
            
box3 item.toText.getBBox();

            
// Changes legend box settings based on option.
            
if (horizontal) {
                
legend.offsetWidth box1.width box2.width box3.width + (spacing 2) + padding;
                
legend.itemY gradientSize padding;
            } else {
                
legend.offsetWidth Math.max(box1.widthbox3.width) + (spacing) + box2.width padding;
                
legend.itemY box2.height padding;
                
legend.itemX spacing;
            }
        },

        
/**
         * Get the bounding box of all paths in the map combined.
         */
        
getBox: function () {
            var 
chart this.chart,
                
maxX = -Math.pow(231), 
                
minX =  Math.pow(231) - 1
                
maxY = -Math.pow(231), 
                
minY =  Math.pow(231) - 1,
                
xyRatio,
                
ratioCorrection,
                
plotWidth chart.plotWidth
                
plotHeight chart.plotHeight,
                
pad;
            
            
            
// Find the bounding box
            
each(this.options.data, function (point) {
                var 
path point.path,
                    
path.length,
                    
even false// while loop reads from the end
                    
                
while (i--) {
                    if (
typeof path[i] === 'number') {
                        if (
even) { // even = x
                            
maxX Math.max(maxXpath[i]);
                            
minX Math.min(minXpath[i]);
                        } else { 
// odd = Y
                            
maxY Math.max(maxYpath[i]);
                            
minY Math.min(minYpath[i]);
                        }
                        
even = !even;
                    }
                }
            });
            
this.minY minY;
            
this.maxY maxY;
            
this.minX minX;
            
this.maxX maxX;
            
        },
        
        
        
        
/**
         * Translate the path so that it automatically fits into the plot area box
         * @param {Object} path
         */
        
translatePath: function (path) {
            
            var 
series this,
                
chart series.chart,
                
even false// while loop reads from the end
                
xAxis series.xAxis,
                
yAxis series.yAxis;
                
            
// Preserve the original
            
path = [].concat(path);
                
            
// Do the translation
            
path.length;
            while (
i--) {
                if (
typeof path[i] === 'number') {
                    if (
even) { // even = x
                        
path[i] = Math.round(xAxis.translate(path[i]));
                    } else { 
// odd = Y
                        
path[i] = Math.round(yAxis.len yAxis.translate(path[i]));
                    }
                    
even = !even;
                }
            }
            return 
path;
        },
        
        
setData: function () {
            
Highcharts.Series.prototype.setData.apply(thisarguments);
            
this.getBox();
        },
        
        
/**
         * Add the path option for data points. Find the max value for color calculation.
         */
        
translate: function () {
            var 
series this,
                
options series.options,
                
dataMin Number.MAX_VALUE,
                
dataMax Number.MIN_VALUE,
                
opacity,
                
minOpacity options.minOpacity,
                
path,
                
color;
    
            
series.generatePoints();
    
            
each(series.data, function (point) {
                
                
point.shapeType 'path';
                
point.shapeArgs = {
                    
dseries.translatePath(point.path)
                };
                
                
// TODO: do point colors in drawPoints instead of point.init
                
if (typeof point.=== 'number') {
                    if (
point.dataMax) {
                        
dataMax point.y;
                    } else if (
point.dataMin) {
                        
dataMin point.y;
                    }
                }
            });
            
            
series.translateColors(dataMindataMax);
        },
        
        
/**
         * In choropleth maps, the color is a result of the value, so this needs translation tood
         */
        
translateColors: function (dataMindataMax) {
            
            var 
seriesOptions this.options,
                
valueRanges seriesOptions.valueRanges,
                
colorRange seriesOptions.colorRange,
                
colorKey this.colorKey;
            
            
each(this.data, function (point) {
                var 
value point[colorKey],
                    
rgba = [],
                    
range,
                    
from,
                    
to,
                    
i,
                    
pos;

                if (
valueRanges) {
                    
valueRanges.length;
                    while (
i--) {
                        
range valueRanges[i];
                        
from range.from;
                        
to range.to;
                        if ((
from === UNDEFINED || value >= from) && (to === UNDEFINED || value <= to)) {
                            
point.options.color range.color;
                            break;
                        }
                            
                    }
                } else if (
colorRange && value !== undefined) {
                    
from Color(colorRange.from);
                    
to Color(colorRange.to);
                    
pos = (dataMax value) / (dataMax dataMin);
                    
4;
                    while (
i--) {
                        
rgba[i] = Math.round(
                            
to.rgba[i] + (from.rgba[i] - to.rgba[i]) * pos
                        
);
                    }
                    
point.options.color 'rgba(' rgba.join(',') + ')';
                }
            });
        },
        
        
drawGraphnoop,
        
        
/**
         * We need the points' bounding boxes in order to draw the data labels, so 
         * we skip it now and call if from drawPoints instead.
         */
        
drawDataLabelsnoop,
        
        
/** 
         * Use the drawPoints method of column, that is able to handle simple shapeArgs.
         * Extend it by assigning the tooltip position.
         */
        
drawPoints: function () {
            var 
series this,
                
chart series.chart,
                
saturation,
                
bBox,
                
colorKey series.colorKey;
            
            
// Make points pass test in drawing
            
each(series.data, function (point) {
                
point.plotY 1// pass null test in column.drawPoints
                
if (point[colorKey] === null) {
                    
point[colorKey] = 0;
                    
point.isNull true;
                }
            });
            
            
// Draw them
            
Highcharts.seriesTypes.column.prototype.drawPoints.apply(series);
            
            
each(series.data, function (point) {
                
                
bBox point.graphic.getBBox();
                
// for tooltip
                
point.tooltipPos = [
                    
bBox.bBox.width 2,
                    
bBox.bBox.height 2
                
];
                
// for data labels
                
point.plotX point.tooltipPos[0];
                
point.plotY point.tooltipPos[1]; 
                
                
// Reset escaped null points
                
if (point.isNull) {
                    
point[colorKey] = null;
                }
            });

            
// Now draw the data labels
            
Highcharts.Series.prototype.drawDataLabels.call(series);
            
        }
    });
    
    
/**
     * A wrapper for Chart with all the default values for a Map
     */
    
Highcharts.Map = function (optionscallback) {
        
        var 
hiddenAxis = {
            
endOnTickfalse,
            
gridLineWidth0,
            
labels: {
                
enabledfalse
            
},
            
lineWidth0,
            
minPadding0,
            
maxPadding0,
            
startOnTickfalse,
            
tickWidth0,
            
titlenull
        
};
        
        
// Don't merge the data
        
seriesOptions options.series;
        
options.series null;
        
        
options merge({
            
chart: {
                
type'map'
            
},
            
xAxishiddenAxis,
            
yAxismerge(hiddenAxis, { reversedtrue })    
        },
        
options// user's options
    
        
// forced options
            
chart: {
                
invertedfalse
            
}
        });
    
        
options.series seriesOptions;
    
    
        return new 
Highcharts.Chart(optionscallback);
    };
}(
Highcharts));
?>
Онлайн: 3
Реклама