JS 封裝類

qingyezhu發表於2014-08-26
function HighchartsObj(id, type) {
    var that = this;
    this.options = {
        chart : {
            renderTo : id,
            type : type,
            style : {
                cursor : 'pointer'
            },
            events : {
                click : function(e) {
                    // console.log(that.extra);
                    var params = that.extra.split(',');
                    if (null != params && 5 === params.length) {
                        toIndiPage(params[0], params[1], params[2], params[3],
                                params[4]);
                    }
                }
            }
        },
        credits : {
            // 隱藏版權
            enabled : false
        },
        title : {
            // 隱藏標題
            text : null
        },
        legend : {
            // 隱藏圖列
            enabled : false
        },
        tooltip : {
            pointFormat : '{series.name}:<b>{point.y}</b>'
        },
        xAxis : {
            labels : {
                // X座標隱藏
                enabled : false
            },
            categories : []
        },
        yAxis : {
            title : {
                // 隱藏Y軸文字
                text : null
            }
        },
        series : []
    };
    this.highcharts = null;
    this.extra = null;
}
HighchartsObj.prototype = {
    setCategories : function(categories) {
        if (!this.isArrayType(categories)) {
            alert('not array');
            return;
        }
        this.options.xAxis.categories = categories;
    },
    setSeries : function(series) {
        if (!this.isArrayType(series)) {
            alert('not array');
            return;
        }
        var data = [];
        for ( var i = series.length - 1; i >= 0; i--) {
            for ( var j = 0, len = series[i]['data'].length; j < len; j++) {
                var val = series[i]['data'][j];
                if ('' === val || null === val) {
                    val = null;
                } else {
                    val = Number(val);
                }
                data.push(val);
            }
            var minVal = Math.min.apply(null, data);
            var maxVal = Math.max.apply(null, data);

            // console.log(minVal + '==========' + maxVal);
            series[i]['data'] = $.map(data, function(val, key) {
                // console.log(val + '--------' + key);
                if (val === minVal) {
                    return {
                        y : val,
                        marker : {
                            radius : 3
                        }
                    };
                } else if (val === maxVal) {
                    return {
                        y : val,
                        marker : {
                            radius : 5
                        }
                    };
                }
                return val;
            });
        }
        this.options.series = series;
    },
    createObj : function() {
        this.highcharts = new Highcharts.Chart(this.options);
    },
    isArrayType : isType('Array'),
    isNumberType : isType('Number'),
    isStringType : isType('String')
}

// 判斷型別
function isType(type) {
    return function(obj) {
        return Object.prototype.toString.call(obj) === '[object ' + type + ']';
    }
}

 

相關文章