PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/application/static/libs/adminlte/bower_components/jquery-sparkline/src/chart-bullet.js

https://bitbucket.org/tin_gimranoff/docstar
JavaScript | 131 lines | 116 code | 10 blank | 5 comment | 12 complexity | c33e68662e9e455f19005e9b55f6cf16 MD5 | raw file
  1    /**
  2     * Bullet charts
  3     */
  4    $.fn.sparkline.bullet = bullet = createClass($.fn.sparkline._base, {
  5        type: 'bullet',
  6
  7        init: function (el, values, options, width, height) {
  8            var min, max, vals;
  9            bullet._super.init.call(this, el, values, options, width, height);
 10
 11            // values: target, performance, range1, range2, range3
 12            this.values = values = normalizeValues(values);
 13            // target or performance could be null
 14            vals = values.slice();
 15            vals[0] = vals[0] === null ? vals[2] : vals[0];
 16            vals[1] = values[1] === null ? vals[2] : vals[1];
 17            min = Math.min.apply(Math, values);
 18            max = Math.max.apply(Math, values);
 19            if (options.get('base') === undefined) {
 20                min = min < 0 ? min : 0;
 21            } else {
 22                min = options.get('base');
 23            }
 24            this.min = min;
 25            this.max = max;
 26            this.range = max - min;
 27            this.shapes = {};
 28            this.valueShapes = {};
 29            this.regiondata = {};
 30            this.width = width = options.get('width') === 'auto' ? '4.0em' : width;
 31            this.target = this.$el.simpledraw(width, height, options.get('composite'));
 32            if (!values.length) {
 33                this.disabled = true;
 34            }
 35            this.initTarget();
 36        },
 37
 38        getRegion: function (el, x, y) {
 39            var shapeid = this.target.getShapeAt(el, x, y);
 40            return (shapeid !== undefined && this.shapes[shapeid] !== undefined) ? this.shapes[shapeid] : undefined;
 41        },
 42
 43        getCurrentRegionFields: function () {
 44            var currentRegion = this.currentRegion;
 45            return {
 46                fieldkey: currentRegion.substr(0, 1),
 47                value: this.values[currentRegion.substr(1)],
 48                region: currentRegion
 49            };
 50        },
 51
 52        changeHighlight: function (highlight) {
 53            var currentRegion = this.currentRegion,
 54                shapeid = this.valueShapes[currentRegion],
 55                shape;
 56            delete this.shapes[shapeid];
 57            switch (currentRegion.substr(0, 1)) {
 58                case 'r':
 59                    shape = this.renderRange(currentRegion.substr(1), highlight);
 60                    break;
 61                case 'p':
 62                    shape = this.renderPerformance(highlight);
 63                    break;
 64                case 't':
 65                    shape = this.renderTarget(highlight);
 66                    break;
 67            }
 68            this.valueShapes[currentRegion] = shape.id;
 69            this.shapes[shape.id] = currentRegion;
 70            this.target.replaceWithShape(shapeid, shape);
 71        },
 72
 73        renderRange: function (rn, highlight) {
 74            var rangeval = this.values[rn],
 75                rangewidth = Math.round(this.canvasWidth * ((rangeval - this.min) / this.range)),
 76                color = this.options.get('rangeColors')[rn - 2];
 77            if (highlight) {
 78                color = this.calcHighlightColor(color, this.options);
 79            }
 80            return this.target.drawRect(0, 0, rangewidth - 1, this.canvasHeight - 1, color, color);
 81        },
 82
 83        renderPerformance: function (highlight) {
 84            var perfval = this.values[1],
 85                perfwidth = Math.round(this.canvasWidth * ((perfval - this.min) / this.range)),
 86                color = this.options.get('performanceColor');
 87            if (highlight) {
 88                color = this.calcHighlightColor(color, this.options);
 89            }
 90            return this.target.drawRect(0, Math.round(this.canvasHeight * 0.3), perfwidth - 1,
 91                Math.round(this.canvasHeight * 0.4) - 1, color, color);
 92        },
 93
 94        renderTarget: function (highlight) {
 95            var targetval = this.values[0],
 96                x = Math.round(this.canvasWidth * ((targetval - this.min) / this.range) - (this.options.get('targetWidth') / 2)),
 97                targettop = Math.round(this.canvasHeight * 0.10),
 98                targetheight = this.canvasHeight - (targettop * 2),
 99                color = this.options.get('targetColor');
100            if (highlight) {
101                color = this.calcHighlightColor(color, this.options);
102            }
103            return this.target.drawRect(x, targettop, this.options.get('targetWidth') - 1, targetheight - 1, color, color);
104        },
105
106        render: function () {
107            var vlen = this.values.length,
108                target = this.target,
109                i, shape;
110            if (!bullet._super.render.call(this)) {
111                return;
112            }
113            for (i = 2; i < vlen; i++) {
114                shape = this.renderRange(i).append();
115                this.shapes[shape.id] = 'r' + i;
116                this.valueShapes['r' + i] = shape.id;
117            }
118            if (this.values[1] !== null) {
119                shape = this.renderPerformance().append();
120                this.shapes[shape.id] = 'p1';
121                this.valueShapes.p1 = shape.id;
122            }
123            if (this.values[0] !== null) {
124                shape = this.renderTarget().append();
125                this.shapes[shape.id] = 't0';
126                this.valueShapes.t0 = shape.id;
127            }
128            target.render();
129        }
130    });
131