GaugeSeries.js 18 KB
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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
/**
 * (c) 2010-2018 Torstein Honsi
 *
 * License: www.highcharts.com/license
 */
'use strict';
import H from '../parts/Globals.js';
import '../parts/Utilities.js';
import '../parts/Options.js';
import '../parts/Point.js';
import '../parts/Series.js';
import '../parts/Interaction.js';
var each = H.each,
    isNumber = H.isNumber,
    merge = H.merge,
    noop = H.noop,
    pick = H.pick,
    pInt = H.pInt,
    Series = H.Series,
    seriesType = H.seriesType,
    TrackerMixin = H.TrackerMixin;


/**
 * Gauges are circular plots displaying one or more values with a dial pointing
 * to values along the perimeter.
 *
 * @sample       highcharts/demo/gauge-speedometer/ Gauge chart
 * @extends      {plotOptions.line}
 * @excluding    animationLimit,boostThreshold,connectEnds,connectNulls,
 *               cropThreshold,dashStyle,findNearestPointBy,getExtremesFromAll,
 *               marker,negativeColor,pointPlacement,shadow,softThreshold,
 *               stacking,states,step,threshold,turboThreshold,xAxis,zoneAxis,
 *               zones
 * @product      highcharts
 * @optionparent plotOptions.gauge
 */
seriesType('gauge', 'line', {

    /**
     * When this option is `true`, the dial will wrap around the axes. For
     * instance, in a full-range gauge going from 0 to 360, a value of 400
     * will point to 40\. When `wrap` is `false`, the dial stops at 360.
     *
     * @type      {Boolean}
     * @see       [overshoot](#plotOptions.gauge.overshoot)
     * @default   true
     * @since     3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.wrap
     */

    /**
     * Data labels for the gauge. For gauges, the data labels are enabled
     * by default and shown in a bordered box below the point.
     *
     * @type    {Object}
     * @extends plotOptions.series.dataLabels
     * @since   2.3.0
     * @product highcharts
     */
    dataLabels: {

        /**
         * Enable or disable the data labels.
         *
         * @since   2.3.0
         * @product highcharts highmaps
         */
        enabled: true,

        defer: false,

        /**
         * The y position offset of the label relative to the center of the
         * gauge.
         *
         * @since   2.3.0
         * @product highcharts highmaps
         */
        y: 15,

        /**
         * The border radius in pixels for the gauge's data label.
         *
         * @since   2.3.0
         * @product highcharts highmaps
         */
        borderRadius: 3,

        crop: false,

        /**
         * The vertical alignment of the data label.
         *
         * @product highcharts highmaps
         */
        verticalAlign: 'top',

        /**
         * The Z index of the data labels. A value of 2 display them behind
         * the dial.
         *
         * @since   2.1.5
         * @product highcharts highmaps
         */
        zIndex: 2,
        
        // Presentational

        /**
         * The border width in pixels for the gauge data label.
         *
         * @since   2.3.0
         * @product highcharts highmaps
         */
        borderWidth: 1,

        /**
         * The border color for the data label.
         *
         * @type    {Color}
         * @default #cccccc
         * @since   2.3.0
         * @product highcharts highmaps
         */
        borderColor: '#cccccc'
        
    },

    /**
     * Options for the dial or arrow pointer of the gauge.
     *
     * In styled mode, the dial is styled with the
     * `.highcharts-gauge-series .highcharts-dial` rule.
     *
     * @type    {Object}
     * @sample  {highcharts} highcharts/css/gauge/ Styled mode
     * @since   2.3.0
     * @product highcharts
     */
    dial: {},

    /**
     * The length of the dial's base part, relative to the total radius
     * or length of the dial.
     *
     * @type      {String}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   70%
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.baseLength
     */

    /**
     * The pixel width of the base of the gauge dial. The base is the part
     * closest to the pivot, defined by baseLength.
     *
     * @type      {Number}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   3
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.baseWidth
     */

    /**
     * The radius or length of the dial, in percentages relative to the
     * radius of the gauge itself.
     *
     * @type      {String}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   80%
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.radius
     */

    /**
     * The length of the dial's rear end, the part that extends out on the
     * other side of the pivot. Relative to the dial's length.
     *
     * @type      {String}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   10%
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.rearLength
     */

    /**
     * The width of the top of the dial, closest to the perimeter. The pivot
     * narrows in from the base to the top.
     *
     * @type      {Number}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   1
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.topWidth
     */

    

    /**
     * The background or fill color of the gauge's dial.
     *
     * @type      {Color}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   #000000
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.backgroundColor
     */

    /**
     * The border color or stroke of the gauge's dial. By default, the
     * borderWidth is 0, so this must be set in addition to a custom border
     * color.
     *
     * @type      {Color}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   #cccccc
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.borderColor
     */

    /**
     * The width of the gauge dial border in pixels.
     *
     * @type      {Number}
     * @sample    {highcharts} highcharts/plotoptions/gauge-dial/
     *            Dial options demonstrated
     * @default   0
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.dial.borderWidth
     */

    

    /**
     * Allow the dial to overshoot the end of the perimeter axis by this
     * many degrees. Say if the gauge axis goes from 0 to 60, a value of
     * 100, or 1000, will show 5 degrees beyond the end of the axis when this
     * option is set to 5.
     *
     * @type      {Number}
     * @see       [wrap](#plotOptions.gauge.wrap)
     * @sample    {highcharts} highcharts/plotoptions/gauge-overshoot/
     *            Allow 5 degrees overshoot
     * @default   0
     * @since     3.0.10
     * @product   highcharts
     * @apioption plotOptions.gauge.overshoot
     */

    /**
     * Options for the pivot or the center point of the gauge.
     *
     * In styled mode, the pivot is styled with the
     * `.highcharts-gauge-series .highcharts-pivot` rule.
     *
     * @type    {Object}
     * @sample  {highcharts} highcharts/css/gauge/ Styled mode
     * @since   2.3.0
     * @product highcharts
     */
    pivot: {},

    /**
     * The pixel radius of the pivot.
     *
     * @type      {Number}
     * @sample    {highcharts} highcharts/plotoptions/gauge-pivot/
     *            Pivot options demonstrated
     * @default   5
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.pivot.radius
     */

    

    /**
     * The border or stroke width of the pivot.
     *
     * @type      {Number}
     * @sample    {highcharts} highcharts/plotoptions/gauge-pivot/
     *            Pivot options demonstrated
     * @default   0
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.pivot.borderWidth
     */

    /**
     * The border or stroke color of the pivot. In able to change this,
     * the borderWidth must also be set to something other than the default
     * 0.
     *
     * @type      {Color}
     * @sample    {highcharts} highcharts/plotoptions/gauge-pivot/
     *            Pivot options demonstrated
     * @default   #cccccc
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.pivot.borderColor
        */

    /**
     * The background color or fill of the pivot.
     *
     * @type      {Color}
     * @sample    {highcharts} highcharts/plotoptions/gauge-pivot/
     *            Pivot options demonstrated
     * @default   #000000
     * @since     2.3.0
     * @product   highcharts
     * @apioption plotOptions.gauge.pivot.backgroundColor
     */
    


    tooltip: {
        headerFormat: ''
    },

    /**
     * Whether to display this particular series or series type in the
     * legend. Defaults to false for gauge series.
     *
     * @since   2.3.0
     * @product highcharts
     */
    showInLegend: false



// Prototype members
}, {
    // chart.angular will be set to true when a gauge series is present,
    // and this will be used on the axes
    angular: true,
    directTouch: true, // #5063
    drawGraph: noop,
    fixedBox: true,
    forceDL: true,
    noSharedTooltip: true,
    trackerGroups: ['group', 'dataLabelsGroup'],

    /**
     * Calculate paths etc
     */
    translate: function () {

        var series = this,
            yAxis = series.yAxis,
            options = series.options,
            center = yAxis.center;

        series.generatePoints();

        each(series.points, function (point) {

            var dialOptions = merge(options.dial, point.dial),
                radius = (pInt(pick(dialOptions.radius, 80)) * center[2]) /
                    200,
                baseLength = (pInt(pick(dialOptions.baseLength, 70)) * radius) /
                    100,
                rearLength = (pInt(pick(dialOptions.rearLength, 10)) * radius) /
                    100,
                baseWidth = dialOptions.baseWidth || 3,
                topWidth = dialOptions.topWidth || 1,
                overshoot = options.overshoot,
                rotation = yAxis.startAngleRad +
                    yAxis.translate(point.y, null, null, null, true);

            // Handle the wrap and overshoot options
            if (isNumber(overshoot)) {
                overshoot = overshoot / 180 * Math.PI;
                rotation = Math.max(
                    yAxis.startAngleRad - overshoot,
                    Math.min(yAxis.endAngleRad + overshoot, rotation)
                );

            } else if (options.wrap === false) {
                rotation = Math.max(
                    yAxis.startAngleRad,
                    Math.min(yAxis.endAngleRad, rotation)
                );
            }

            rotation = rotation * 180 / Math.PI;

            point.shapeType = 'path';
            point.shapeArgs = {
                d: dialOptions.path || [
                    'M',
                    -rearLength, -baseWidth / 2,
                    'L',
                    baseLength, -baseWidth / 2,
                    radius, -topWidth / 2,
                    radius, topWidth / 2,
                    baseLength, baseWidth / 2,
                    -rearLength, baseWidth / 2,
                    'z'
                ],
                translateX: center[0],
                translateY: center[1],
                rotation: rotation
            };

            // Positions for data label
            point.plotX = center[0];
            point.plotY = center[1];
        });
    },

    /**
     * Draw the points where each point is one needle
     */
    drawPoints: function () {

        var series = this,
            center = series.yAxis.center,
            pivot = series.pivot,
            options = series.options,
            pivotOptions = options.pivot,
            renderer = series.chart.renderer;

        each(series.points, function (point) {

            var graphic = point.graphic,
                shapeArgs = point.shapeArgs,
                d = shapeArgs.d,
                dialOptions = merge(options.dial, point.dial); // #1233

            if (graphic) {
                graphic.animate(shapeArgs);
                shapeArgs.d = d; // animate alters it
            } else {
                point.graphic = renderer[point.shapeType](shapeArgs)
                    .attr({
                        // required by VML when animation is false
                        rotation: shapeArgs.rotation,
                        zIndex: 1
                    })
                    .addClass('highcharts-dial')
                    .add(series.group);

                
                // Presentational attributes
                point.graphic.attr({
                    stroke: dialOptions.borderColor || 'none',
                    'stroke-width': dialOptions.borderWidth || 0,
                    fill: dialOptions.backgroundColor ||
                        '#000000'
                });
                
            }
        });

        // Add or move the pivot
        if (pivot) {
            pivot.animate({ // #1235
                translateX: center[0],
                translateY: center[1]
            });
        } else {
            series.pivot = renderer.circle(0, 0, pick(pivotOptions.radius, 5))
                .attr({
                    zIndex: 2
                })
                .addClass('highcharts-pivot')
                .translate(center[0], center[1])
                .add(series.group);

            
            // Presentational attributes
            series.pivot.attr({
                'stroke-width': pivotOptions.borderWidth || 0,
                stroke: pivotOptions.borderColor ||
                    '#cccccc',
                fill: pivotOptions.backgroundColor ||
                    '#000000'
            });
            
        }
    },

    /**
     * Animate the arrow up from startAngle
     */
    animate: function (init) {
        var series = this;

        if (!init) {
            each(series.points, function (point) {
                var graphic = point.graphic;

                if (graphic) {
                    // start value
                    graphic.attr({
                        rotation: series.yAxis.startAngleRad * 180 / Math.PI
                    });

                    // animate
                    graphic.animate({
                        rotation: point.shapeArgs.rotation
                    }, series.options.animation);
                }
            });

            // delete this function to allow it only once
            series.animate = null;
        }
    },

    render: function () {
        this.group = this.plotGroup(
            'group',
            'series',
            this.visible ? 'visible' : 'hidden',
            this.options.zIndex,
            this.chart.seriesGroup
        );
        Series.prototype.render.call(this);
        this.group.clip(this.chart.clipRect);
    },

    /**
     * Extend the basic setData method by running processData and generatePoints
     * immediately, in order to access the points from the legend.
     */
    setData: function (data, redraw) {
        Series.prototype.setData.call(this, data, false);
        this.processData();
        this.generatePoints();
        if (pick(redraw, true)) {
            this.chart.redraw();
        }
    },

    /**
     * If the tracking module is loaded, add the point tracker
     */
    drawTracker: TrackerMixin && TrackerMixin.drawTrackerPoint

// Point members
}, {
    /**
     * Don't do any hover colors or anything
     */
    setState: function (state) {
        this.state = state;
    }
});

/**
 * A `gauge` series. If the [type](#series.gauge.type) option is not
 * specified, it is inherited from [chart.type](#chart.type).
 *
 * @type      {Object}
 * @extends   series,plotOptions.gauge
 * @excluding animationLimit,boostThreshold,connectEnds,connectNulls,
 *            cropThreshold,dashStyle,dataParser,dataURL,findNearestPointBy,
 *            getExtremesFromAll,marker,negativeColor,pointPlacement,shadow,
 *            softThreshold,stack,stacking,states,step,threshold,
 *            turboThreshold,zoneAxis,zones
 * @product   highcharts
 * @apioption series.gauge
 */

/**
 * An array of data points for the series. For the `gauge` series type,
 * points can be given in the following ways:
 *
 * 1.  An array of numerical values. In this case, the numerical values
 * will be interpreted as `y` options. Example:
 *
 *  ```js
 *  data: [0, 5, 3, 5]
 *  ```
 *
 * 2.  An array of objects with named values. The following snippet shows only a
 * few settings, see the complete options set below. If the total number of data
 * points exceeds the series' [turboThreshold](#series.gauge.turboThreshold),
 * this option is not available.
 *
 *  ```js
 *     data: [{
 *     y: 6,
 *     name: "Point2",
 *     color: "#00FF00"
 * }, {
 *     y: 8,
 *     name: "Point1",
 *     color: "#FF00FF"
 * }]</pre>
 *
 * The typical gauge only contains a single data value.
 *
 * @type      {Array<Object|Number>}
 * @extends   series.line.data
 * @excluding drilldown,marker,x
 * @sample    {highcharts} highcharts/chart/reflow-true/
 *            Numerical values
 * @sample    {highcharts} highcharts/series/data-array-of-arrays/
 *            Arrays of numeric x and y
 * @sample    {highcharts} highcharts/series/data-array-of-arrays-datetime/
 *            Arrays of datetime x and y
 * @sample    {highcharts} highcharts/series/data-array-of-name-value/
 *            Arrays of point.name and y
 * @sample    {highcharts} highcharts/series/data-array-of-objects/
 *            Config objects
 * @product   highcharts
 * @apioption series.gauge.data
 */