Blame view
static/ueditor/third-party/highcharts/modules/annotations.src.js
8.18 KB
831eac332 add file |
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
(function (Highcharts, HighchartsAdapter) { var UNDEFINED, ALIGN_FACTOR, ALLOWED_SHAPES, Chart = Highcharts.Chart, extend = Highcharts.extend, each = Highcharts.each; ALLOWED_SHAPES = ["path", "rect", "circle"]; ALIGN_FACTOR = { top: 0, left: 0, center: 0.5, middle: 0.5, bottom: 1, right: 1 }; // Highcharts helper methods var inArray = HighchartsAdapter.inArray, merge = Highcharts.merge; function defaultOptions(shapeType) { var shapeOptions, options; options = { xAxis: 0, yAxis: 0, title: { style: {}, text: "", x: 0, y: 0 }, shape: { params: { stroke: "#000000", fill: "transparent", strokeWidth: 2 } } }; shapeOptions = { circle: { params: { x: 0, y: 0 } } }; if (shapeOptions[shapeType]) { options.shape = merge(options.shape, shapeOptions[shapeType]); } return options; } function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } function isNumber(n) { return typeof n === 'number'; } function defined(obj) { return obj !== UNDEFINED && obj !== null; } function translatePath(d, xAxis, yAxis, xOffset, yOffset) { var len = d.length, i = 0; while (i < len) { if (typeof d[i] === 'number' && typeof d[i + 1] === 'number') { d[i] = xAxis.toPixels(d[i]) - xOffset; d[i + 1] = yAxis.toPixels(d[i + 1]) - yOffset; i += 2; } else { i += 1; } } return d; } // Define annotation prototype var Annotation = function () { this.init.apply(this, arguments); }; Annotation.prototype = { /* * Initialize the annotation */ init: function (chart, options) { var shapeType = options.shape && options.shape.type; this.chart = chart; this.options = merge({}, defaultOptions(shapeType), options); }, /* * Render the annotation */ render: function (redraw) { var annotation = this, chart = this.chart, renderer = annotation.chart.renderer, group = annotation.group, title = annotation.title, shape = annotation.shape, options = annotation.options, titleOptions = options.title, shapeOptions = options.shape; if (!group) { group = annotation.group = renderer.g(); } if (!shape && shapeOptions && inArray(shapeOptions.type, ALLOWED_SHAPES) !== -1) { shape = annotation.shape = renderer[options.shape.type](shapeOptions.params); shape.add(group); } if (!title && titleOptions) { title = annotation.title = renderer.label(titleOptions); title.add(group); } group.add(chart.annotations.group); // link annotations to point or series annotation.linkObjects(); if (redraw !== false) { annotation.redraw(); } }, /* * Redraw the annotation title or shape after options update */ redraw: function () { var options = this.options, chart = this.chart, group = this.group, title = this.title, shape = this.shape, linkedTo = this.linkedObject, xAxis = chart.xAxis[options.xAxis], yAxis = chart.yAxis[options.yAxis], width = options.width, height = options.height, anchorY = ALIGN_FACTOR[options.anchorY], anchorX = ALIGN_FACTOR[options.anchorX], resetBBox = false, shapeParams, linkType, series, param, bbox, x, y; if (linkedTo) { linkType = (linkedTo instanceof Highcharts.Point) ? 'point' : (linkedTo instanceof Highcharts.Series) ? 'series' : null; if (linkType === 'point') { options.xValue = linkedTo.x; options.yValue = linkedTo.y; series = linkedTo.series; } else if (linkType === 'series') { series = linkedTo; } if (group.visibility !== series.group.visibility) { group.attr({ visibility: series.group.visibility }); } } // Based on given options find annotation pixel position x = (defined(options.xValue) ? xAxis.toPixels(options.xValue + xAxis.minPointOffset) - xAxis.minPixelPadding : options.x); y = defined(options.yValue) ? yAxis.toPixels(options.yValue) : options.y; if (isNaN(x) || isNaN(y) || !isNumber(x) || !isNumber(y)) { return; } if (title) { title.attr(options.title); title.css(options.title.style); resetBBox = true; } if (shape) { shapeParams = extend({}, options.shape.params); if (options.units === 'values') { for (param in shapeParams) { if (inArray(param, ['width', 'x']) > -1) { shapeParams[param] = xAxis.translate(shapeParams[param]); } else if (inArray(param, ['height', 'y']) > -1) { shapeParams[param] = yAxis.translate(shapeParams[param]); } } if (shapeParams.width) { shapeParams.width -= xAxis.toPixels(0) - xAxis.left; } if (shapeParams.x) { shapeParams.x += xAxis.minPixelPadding; } if (options.shape.type === 'path') { translatePath(shapeParams.d, xAxis, yAxis, x, y); } } // move the center of the circle to shape x/y if (options.shape.type === 'circle') { shapeParams.x += shapeParams.r; shapeParams.y += shapeParams.r; } resetBBox = true; shape.attr(shapeParams); } group.bBox = null; // If annotation width or height is not defined in options use bounding box size if (!isNumber(width)) { bbox = group.getBBox(); width = bbox.width; } if (!isNumber(height)) { // get bbox only if it wasn't set before if (!bbox) { bbox = group.getBBox(); } height = bbox.height; } // Calculate anchor point if (!isNumber(anchorX)) { anchorX = ALIGN_FACTOR.center; } if (!isNumber(anchorY)) { anchorY = ALIGN_FACTOR.center; } // Translate group according to its dimension and anchor point x = x - width * anchorX; y = y - height * anchorY; if (chart.animation && defined(group.translateX) && defined(group.translateY)) { group.animate({ translateX: x, translateY: y }); } else { group.translate(x, y); } }, /* * Destroy the annotation */ destroy: function () { var annotation = this, chart = this.chart, allItems = chart.annotations.allItems, index = allItems.indexOf(annotation); if (index > -1) { allItems.splice(index, 1); } each(['title', 'shape', 'group'], function (element) { if (annotation[element]) { annotation[element].destroy(); annotation[element] = null; } }); annotation.group = annotation.title = annotation.shape = annotation.chart = annotation.options = null; }, /* * Update the annotation with a given options */ update: function (options, redraw) { extend(this.options, options); // update link to point or series this.linkObjects(); this.render(redraw); }, linkObjects: function () { var annotation = this, chart = annotation.chart, linkedTo = annotation.linkedObject, linkedId = linkedTo && (linkedTo.id || linkedTo.options.id), options = annotation.options, id = options.linkedTo; if (!defined(id)) { annotation.linkedObject = null; } else if (!defined(linkedTo) || id !== linkedId) { annotation.linkedObject = chart.get(id); } } }; // Add annotations methods to chart prototype extend(Chart.prototype, { annotations: { /* * Unified method for adding annotations to the chart */ add: function (options, redraw) { var annotations = this.allItems, chart = this.chart, item, len; if (!isArray(options)) { options = [options]; } len = options.length; while (len--) { item = new Annotation(chart, options[len]); annotations.push(item); item.render(redraw); } }, /** * Redraw all annotations, method used in chart events */ redraw: function () { each(this.allItems, function (annotation) { annotation.redraw(); }); } } }); // Initialize on chart load Chart.prototype.callbacks.push(function (chart) { var options = chart.options.annotations, group; group = chart.renderer.g("annotations"); group.attr({ zIndex: 7 }); group.add(); // initialize empty array for annotations chart.annotations.allItems = []; // link chart object to annotations chart.annotations.chart = chart; // link annotations group element to the chart chart.annotations.group = group; if (isArray(options) && options.length > 0) { chart.annotations.add(chart.options.annotations); } // update annotations after chart redraw Highcharts.addEvent(chart, 'redraw', function () { chart.annotations.redraw(); }); }); }(Highcharts, HighchartsAdapter)); |