BarView.ts 25.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

S
sushuang 已提交
20
import {__DEV__} from '../../config';
S
sushuang 已提交
21
import * as zrUtil from 'zrender/src/core/util';
22
import {Rect, Sector, getECData, updateProps, initProps, setHoverStyle} from '../../util/graphic';
S
sushuang 已提交
23
import {setLabel} from './helper';
24 25
import {getBarItemStyle} from './barItemStyle';
import Path, { PathProps } from 'zrender/src/graphic/Path';
26
import Group from 'zrender/src/container/Group';
27
import {throttle} from '../../util/throttle';
28
import {createClipPath} from '../helper/createClipPathFromCoordSys';
29
import Sausage from '../../util/shape/sausage';
30 31 32 33
import ChartView from '../../view/Chart';
import List from '../../data/List';
import GlobalModel from '../../model/Global';
import ExtensionAPI from '../../ExtensionAPI';
34
import { StageHandlerProgressParams, ZRElementEvent } from '../../util/types';
35 36 37 38 39
import BarSeriesModel, { BarSeriesOption, BarDataItemOption } from './BarSeries';
import type Axis2D from '../../coord/cartesian/Axis2D';
import type Cartesian2D from '../../coord/cartesian/Cartesian2D';
import type { RectLike } from 'zrender/src/core/BoundingRect';
import type Model from '../../model/Model';
40
import { isCoordinateSystemType } from '../../coord/CoordinateSystem';
41 42 43 44 45 46 47 48

const BAR_BORDER_WIDTH_QUERY = ['itemStyle', 'borderWidth'] as const;
const _eventPos = [0, 0];

const mathMax = Math.max;
const mathMin = Math.min;

type CoordSysOfBar = BarSeriesModel['coordinateSystem'];
49 50
type RectShape = Rect['shape']
type SectorShape = Sector['shape']
51 52 53 54

type SectorLayout = SectorShape;
type RectLayout = RectShape;

55
type BarPossiblePath = Sector | Rect | Sausage
P
pissang 已提交
56

L
lang 已提交
57

58
function getClipArea(coord: CoordSysOfBar, data: List) {
59
    if (isCoordinateSystemType<Cartesian2D>(coord, 'cartesian2d')) {
60
        var coordSysClipArea = coord.getArea && coord.getArea();
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        var baseAxis = coord.getBaseAxis();
        // When boundaryGap is false or using time axis. bar may exceed the grid.
        // We should not clip this part.
        // See test/bar2.html
        if (baseAxis.type !== 'category' || !baseAxis.onBand) {
            var expandWidth = data.getLayout('bandWidth');
            if (baseAxis.isHorizontal()) {
                coordSysClipArea.x -= expandWidth;
                coordSysClipArea.width += expandWidth * 2;
            }
            else {
                coordSysClipArea.y -= expandWidth;
                coordSysClipArea.height += expandWidth * 2;
            }
        }
    }

    return coordSysClipArea;
}

L
lang 已提交
81

82 83 84 85 86 87 88 89
class BarView extends ChartView {
    static type = 'bar' as const
    type = BarView.type

    _data: List

    _isLargeDraw: boolean

90
    _backgroundGroup: Group
91

92
    _backgroundEls: (Rect | Sector)[]
L
lang 已提交
93

94
    render(seriesModel: BarSeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
95 96
        this._updateDrawMode(seriesModel);

S
sushuang 已提交
97
        var coordinateSystemType = seriesModel.get('coordinateSystem');
L
lang 已提交
98

S
sushuang 已提交
99 100 101
        if (coordinateSystemType === 'cartesian2d'
            || coordinateSystemType === 'polar'
        ) {
102 103 104
            this._isLargeDraw
                ? this._renderLarge(seriesModel, ecModel, api)
                : this._renderNormal(seriesModel, ecModel, api);
S
sushuang 已提交
105 106 107 108
        }
        else if (__DEV__) {
            console.warn('Only cartesian2d and polar supported for bar.');
        }
L
lang 已提交
109

S
sushuang 已提交
110
        return this.group;
111
    }
L
lang 已提交
112

113
    incrementalPrepareRender(seriesModel: BarSeriesModel) {
114 115
        this._clear();
        this._updateDrawMode(seriesModel);
116
    }
117

118 119
    incrementalRender(
        params: StageHandlerProgressParams, seriesModel: BarSeriesModel) {
120 121
        // Do not support progressive in normal mode.
        this._incrementalRenderLarge(params, seriesModel);
122
    }
123

124
    _updateDrawMode(seriesModel: BarSeriesModel) {
125
        var isLargeDraw = seriesModel.pipelineContext.large;
126
        if (this._isLargeDraw == null || isLargeDraw !== this._isLargeDraw) {
127 128 129
            this._isLargeDraw = isLargeDraw;
            this._clear();
        }
130
    }
L
lang 已提交
131

132
    _renderNormal(seriesModel: BarSeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
S
sushuang 已提交
133 134 135
        var group = this.group;
        var data = seriesModel.getData();
        var oldData = this._data;
1
100pah 已提交
136

S
sushuang 已提交
137 138
        var coord = seriesModel.coordinateSystem;
        var baseAxis = coord.getBaseAxis();
139
        var isHorizontalOrRadial: boolean;
L
lang 已提交
140

S
sushuang 已提交
141
        if (coord.type === 'cartesian2d') {
142
            isHorizontalOrRadial = (baseAxis as Axis2D).isHorizontal();
S
sushuang 已提交
143 144 145 146
        }
        else if (coord.type === 'polar') {
            isHorizontalOrRadial = baseAxis.dim === 'angle';
        }
O
Ovilia 已提交
147

S
sushuang 已提交
148
        var animationModel = seriesModel.isAnimationEnabled() ? seriesModel : null;
O
Ovilia 已提交
149

150
        var needsClip = seriesModel.get('clip', true);
151
        var coordSysClipArea = getClipArea(coord, data);
152 153 154 155 156
        // If there is clipPath created in large mode. Remove it.
        group.removeClipPath();
        // We don't use clipPath in normal mode because we needs a perfect animation
        // And don't want the label are clipped.

O
Ovilia 已提交
157 158
        var roundCap = seriesModel.get('roundCap', true);

159 160 161
        var drawBackground = seriesModel.get('showBackground', true);
        var backgroundModel = seriesModel.getModel('backgroundStyle');

162 163
        var bgEls: BarView['_backgroundEls'] = [];
        var oldBgEls = this._backgroundEls;
164

S
sushuang 已提交
165 166
        data.diff(oldData)
            .add(function (dataIndex) {
167 168 169 170
                var itemModel = data.getItemModel(dataIndex);
                var layout = getLayout[coord.type](data, dataIndex, itemModel);

                if (drawBackground) {
171 172 173 174
                    var bgEl = createBackgroundEl(
                        coord, isHorizontalOrRadial, layout
                    );
                    bgEl.useStyle(getBarItemStyle(backgroundModel));
175 176 177
                    bgEls[dataIndex] = bgEl;
                }

178
                // If dataZoom in filteMode: 'empty', the baseValue can be set as NaN in "axisProxy".
S
sushuang 已提交
179 180 181
                if (!data.hasValue(dataIndex)) {
                    return;
                }
182

P
pissang 已提交
183 184 185 186 187 188 189 190 191 192
                if (needsClip) {
                    // Clip will modify the layout params.
                    // And return a boolean to determine if the shape are fully clipped.
                    var isClipped = clip[coord.type](coordSysClipArea, layout);
                    if (isClipped) {
                        group.remove(el);
                        return;
                    }
                }

S
sushuang 已提交
193
                var el = elementCreator[coord.type](
194
                    dataIndex, layout, isHorizontalOrRadial, animationModel, false, roundCap
S
sushuang 已提交
195 196 197 198 199 200 201 202 203 204
                );
                data.setItemGraphicEl(dataIndex, el);
                group.add(el);

                updateStyle(
                    el, data, dataIndex, itemModel, layout,
                    seriesModel, isHorizontalOrRadial, coord.type === 'polar'
                );
            })
            .update(function (newIndex, oldIndex) {
205 206
                var itemModel = data.getItemModel(newIndex);
                var layout = getLayout[coord.type](data, newIndex, itemModel);
S
sushuang 已提交
207

208 209
                if (drawBackground) {
                    var bgEl = oldBgEls[oldIndex];
210
                    bgEl.useStyle(getBarItemStyle(backgroundModel));
211 212 213
                    bgEls[newIndex] = bgEl;

                    var shape = createBackgroundShape(isHorizontalOrRadial, layout, coord);
214 215
                    updateProps(
                        bgEl as Path, { shape: shape }, animationModel, newIndex
216
                    );
217 218
                }

P
pissang 已提交
219
                var el = oldData.getItemGraphicEl(oldIndex) as BarPossiblePath;
S
sushuang 已提交
220 221 222 223
                if (!data.hasValue(newIndex)) {
                    group.remove(el);
                    return;
                }
L
lang 已提交
224

P
pissang 已提交
225 226 227 228 229 230 231 232
                if (needsClip) {
                    var isClipped = clip[coord.type](coordSysClipArea, layout);
                    if (isClipped) {
                        group.remove(el);
                        return;
                    }
                }

S
sushuang 已提交
233
                if (el) {
234
                    updateProps(el as Path, {
235 236
                        shape: layout
                    }, animationModel, newIndex);
S
sushuang 已提交
237 238 239
                }
                else {
                    el = elementCreator[coord.type](
240
                        newIndex, layout, isHorizontalOrRadial, animationModel, true, roundCap
P
pah100 已提交
241
                    );
S
sushuang 已提交
242
                }
1
100pah 已提交
243

S
sushuang 已提交
244 245 246 247 248 249 250 251 252 253 254 255
                data.setItemGraphicEl(newIndex, el);
                // Add back
                group.add(el);

                updateStyle(
                    el, data, newIndex, itemModel, layout,
                    seriesModel, isHorizontalOrRadial, coord.type === 'polar'
                );
            })
            .remove(function (dataIndex) {
                var el = oldData.getItemGraphicEl(dataIndex);
                if (coord.type === 'cartesian2d') {
256
                    el && removeRect(dataIndex, animationModel, el as Rect);
S
sushuang 已提交
257 258
                }
                else {
259
                    el && removeSector(dataIndex, animationModel, el as Sector);
S
sushuang 已提交
260 261 262 263
                }
            })
            .execute();

264 265 266 267 268 269 270 271 272
        var bgGroup = this._backgroundGroup || (this._backgroundGroup = new Group());
        bgGroup.removeAll();

        for (var i = 0; i < bgEls.length; ++i) {
            bgGroup.add(bgEls[i]);
        }
        group.add(bgGroup);
        this._backgroundEls = bgEls;

S
sushuang 已提交
273
        this._data = data;
274
    }
S
sushuang 已提交
275

276
    _renderLarge(seriesModel: BarSeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
277 278
        this._clear();
        createLarge(seriesModel, this.group);
279 280

        // Use clipPath in large mode.
281
        var clipPath = seriesModel.get('clip', true)
282 283 284 285 286 287
            ? createClipPath(seriesModel.coordinateSystem, false, seriesModel)
            : null;
        if (clipPath) {
            this.group.setClipPath(clipPath);
        }
        else {
288
            this.group.removeClipPath();
289
        }
290
    }
291

292
    _incrementalRenderLarge(params: StageHandlerProgressParams, seriesModel: BarSeriesModel) {
293
        this._removeBackground();
294
        createLarge(seriesModel, this.group, true);
295
    }
296

297
    remove(ecModel?: GlobalModel) {
298
        this._clear(ecModel);
299
    }
300

301
    _clear(ecModel?: GlobalModel) {
S
sushuang 已提交
302 303
        var group = this.group;
        var data = this._data;
S
sushuang 已提交
304
        if (ecModel && ecModel.get('animation') && data && !this._isLargeDraw) {
305 306 307
            this._removeBackground();
            this._backgroundEls = [];

308
            data.eachItemGraphicEl(function (el: Sector | Rect) {
S
sushuang 已提交
309
                if (el.type === 'sector') {
310
                    removeSector(getECData(el).dataIndex, ecModel, el as (Sector));
S
sushuang 已提交
311 312
                }
                else {
313
                    removeRect(getECData(el).dataIndex, ecModel, el as (Rect));
S
sushuang 已提交
314 315
                }
            });
L
lang 已提交
316
        }
S
sushuang 已提交
317 318 319
        else {
            group.removeAll();
        }
320
        this._data = null;
321
    }
322

323
    _removeBackground() {
324 325
        this.group.remove(this._backgroundGroup);
        this._backgroundGroup = null;
S
sushuang 已提交
326
    }
327
}
328

329 330 331 332 333 334
interface Clipper {
    (coordSysBoundingRect: RectLike, layout: RectLayout | SectorLayout): boolean
}
var clip: {
    [key in 'cartesian2d' | 'polar']: Clipper
} = {
335
    cartesian2d(coordSysBoundingRect: RectLike, layout: Rect['shape']) {
P
pissang 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        var signWidth = layout.width < 0 ? -1 : 1;
        var signHeight = layout.height < 0 ? -1 : 1;
        // Needs positive width and height
        if (signWidth < 0) {
            layout.x += layout.width;
            layout.width = -layout.width;
        }
        if (signHeight < 0) {
            layout.y += layout.height;
            layout.height = -layout.height;
        }

        var x = mathMax(layout.x, coordSysBoundingRect.x);
        var x2 = mathMin(layout.x + layout.width, coordSysBoundingRect.x + coordSysBoundingRect.width);
        var y = mathMax(layout.y, coordSysBoundingRect.y);
        var y2 = mathMin(layout.y + layout.height, coordSysBoundingRect.y + coordSysBoundingRect.height);

        layout.x = x;
        layout.y = y;
        layout.width = x2 - x;
        layout.height = y2 - y;

358
        var clipped = layout.width < 0 || layout.height < 0;
P
pissang 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372

        // Reverse back
        if (signWidth < 0) {
            layout.x += layout.width;
            layout.width = -layout.width;
        }
        if (signHeight < 0) {
            layout.y += layout.height;
            layout.height = -layout.height;
        }

        return clipped;
    },

373
    polar() {
P
pissang 已提交
374 375 376 377
        return false;
    }
};

378 379 380 381
interface ElementCreator {
    (
        dataIndex: number, layout: RectLayout | SectorLayout, isHorizontalOrRadial: boolean,
        animationModel: BarSeriesModel, isUpdate: boolean, roundCap?: boolean
P
pissang 已提交
382
    ): BarPossiblePath
383 384 385 386 387
}

var elementCreator: {
    [key in 'polar' | 'cartesian2d']: ElementCreator
} = {
P
pah100 已提交
388

389 390
    cartesian2d(
        dataIndex, layout: RectLayout, isHorizontal,
S
sushuang 已提交
391 392
        animationModel, isUpdate
    ) {
393
        var rect = new Rect({
394 395 396 397 398
            shape: zrUtil.extend({}, layout),
            z2: 1
        });

        rect.name = 'item';
S
sushuang 已提交
399 400 401 402

        // Animation
        if (animationModel) {
            var rectShape = rect.shape;
403 404
            var animateProperty = isHorizontal ? 'height' : 'width' as 'width' | 'height';
            var animateTarget = {} as RectShape;
S
sushuang 已提交
405 406
            rectShape[animateProperty] = 0;
            animateTarget[animateProperty] = layout[animateProperty];
407
            (isUpdate ? updateProps : initProps)(rect, {
S
sushuang 已提交
408 409 410
                shape: animateTarget
            }, animationModel, dataIndex);
        }
1
100pah 已提交
411

S
sushuang 已提交
412 413
        return rect;
    },
1
100pah 已提交
414

415 416
    polar(
        dataIndex: number, layout: SectorLayout, isRadial: boolean,
417
        animationModel, isUpdate, roundCap
S
sushuang 已提交
418
    ) {
S
sushuang 已提交
419 420 421 422 423
        // Keep the same logic with bar in catesion: use end value to control
        // direction. Notice that if clockwise is true (by default), the sector
        // will always draw clockwisely, no matter whether endAngle is greater
        // or less than startAngle.
        var clockwise = layout.startAngle < layout.endAngle;
424

425
        var ShapeClass = (!isRadial && roundCap) ? Sausage : Sector;
426 427

        var sector = new ShapeClass({
428 429
            shape: zrUtil.defaults({clockwise: clockwise}, layout),
            z2: 1
S
sushuang 已提交
430
        });
S
sushuang 已提交
431

432 433
        sector.name = 'item';

S
sushuang 已提交
434 435 436
        // Animation
        if (animationModel) {
            var sectorShape = sector.shape;
437 438
            var animateProperty = isRadial ? 'r' : 'endAngle' as 'r' | 'endAngle';
            var animateTarget = {} as SectorShape;
S
sushuang 已提交
439 440
            sectorShape[animateProperty] = isRadial ? 0 : layout.startAngle;
            animateTarget[animateProperty] = layout[animateProperty];
441
            (isUpdate ? updateProps : initProps)(sector, {
S
sushuang 已提交
442 443 444
                shape: animateTarget
            }, animationModel, dataIndex);
        }
O
Ovilia 已提交
445

S
sushuang 已提交
446 447 448 449
        return sector;
    }
};

450 451 452
function removeRect(
    dataIndex: number,
    animationModel: BarSeriesModel | GlobalModel,
453
    el: Rect
454
) {
S
sushuang 已提交
455 456
    // Not show text when animating
    el.style.text = null;
457
    updateProps(el, {
S
sushuang 已提交
458 459
        shape: {
            width: 0
O
Ovilia 已提交
460
        }
S
sushuang 已提交
461 462 463 464 465
    }, animationModel, dataIndex, function () {
        el.parent && el.parent.remove(el);
    });
}

466 467 468
function removeSector(
    dataIndex: number,
    animationModel: BarSeriesModel | GlobalModel,
469
    el: Sector
470
) {
S
sushuang 已提交
471 472
    // Not show text when animating
    el.style.text = null;
473
    updateProps(el, {
S
sushuang 已提交
474 475 476 477 478 479 480 481
        shape: {
            r: el.shape.r0
        }
    }, animationModel, dataIndex, function () {
        el.parent && el.parent.remove(el);
    });
}

482 483 484 485 486 487 488 489
interface GetLayout {
    (data: List, dataIndex: number, itemModel: Model<BarDataItemOption>): RectLayout | SectorLayout
}
var getLayout: {
    [key in 'cartesian2d' | 'polar']: GetLayout
} = {
    cartesian2d(data, dataIndex, itemModel): RectLayout {
        var layout = data.getItemLayout(dataIndex) as RectLayout;
S
sushuang 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502
        var fixedLineWidth = getLineWidth(itemModel, layout);

        // fix layout with lineWidth
        var signX = layout.width > 0 ? 1 : -1;
        var signY = layout.height > 0 ? 1 : -1;
        return {
            x: layout.x + signX * fixedLineWidth / 2,
            y: layout.y + signY * fixedLineWidth / 2,
            width: layout.width - signX * fixedLineWidth,
            height: layout.height - signY * fixedLineWidth
        };
    },

503
    polar(data, dataIndex, itemModel): SectorLayout {
S
sushuang 已提交
504 505 506 507 508 509 510 511
        var layout = data.getItemLayout(dataIndex);
        return {
            cx: layout.cx,
            cy: layout.cy,
            r0: layout.r0,
            r: layout.r,
            startAngle: layout.startAngle,
            endAngle: layout.endAngle
512
        } as SectorLayout;
1
100pah 已提交
513
    }
S
sushuang 已提交
514 515
};

516
function isZeroOnPolar(layout: SectorLayout) {
517 518 519 520 521
    return layout.startAngle != null
        && layout.endAngle != null
        && layout.startAngle === layout.endAngle;
}

S
sushuang 已提交
522
function updateStyle(
P
pissang 已提交
523
    el: BarPossiblePath,
524 525 526 527 528 529
    data: List, dataIndex: number,
    itemModel: Model<BarDataItemOption>,
    layout: RectLayout | SectorLayout,
    seriesModel: BarSeriesModel,
    isHorizontal: boolean,
    isPolar: boolean
S
sushuang 已提交
530 531 532
) {
    var color = data.getItemVisual(dataIndex, 'color');
    var opacity = data.getItemVisual(dataIndex, 'opacity');
Z
zhangyi 已提交
533
    var stroke = data.getVisual('borderColor');
534
    var itemStyleModel = itemModel.getModel('itemStyle');
535
    var hoverStyle = getBarItemStyle(itemModel.getModel(['emphasis', 'itemStyle']));
S
sushuang 已提交
536 537 538

    if (!isPolar) {
        el.setShape('r', itemStyleModel.get('barBorderRadius') || 0);
O
Ovilia 已提交
539 540
    }

S
sushuang 已提交
541 542
    el.useStyle(zrUtil.defaults(
        {
543 544
            stroke: isZeroOnPolar(layout as SectorLayout) ? 'none' : stroke,
            fill: isZeroOnPolar(layout as SectorLayout) ? 'none' : color,
Z
zhangyi 已提交
545
            opacity: opacity
P
pah100 已提交
546
        },
547
        getBarItemStyle(itemStyleModel)
S
sushuang 已提交
548
    ));
1
100pah 已提交
549

S
sushuang 已提交
550 551
    var cursorStyle = itemModel.getShallow('cursor');
    cursorStyle && el.attr('cursor', cursorStyle);
1
100pah 已提交
552

S
sushuang 已提交
553
    if (!isPolar) {
554 555 556 557
        var labelPositionOutside = isHorizontal
            ? ((layout as RectLayout).height > 0 ? 'bottom' : 'top')
            : ((layout as RectLayout).width > 0 ? 'left' : 'right');

S
sushuang 已提交
558 559 560 561
        setLabel(
            el.style, hoverStyle, itemModel, color,
            seriesModel, dataIndex, labelPositionOutside
        );
1
100pah 已提交
562
    }
563
    if (isZeroOnPolar(layout as SectorLayout)) {
Z
zhangyi 已提交
564 565
        hoverStyle.fill = hoverStyle.stroke = 'none';
    }
566
    setHoverStyle(el, hoverStyle);
S
sushuang 已提交
567
}
1
tweak  
100pah 已提交
568

S
sushuang 已提交
569
// In case width or height are too small.
570 571 572 573
function getLineWidth(
    itemModel: Model<BarSeriesOption>,
    rawLayout: RectLayout
) {
S
sushuang 已提交
574
    var lineWidth = itemModel.get(BAR_BORDER_WIDTH_QUERY) || 0;
575 576 577 578
    // width or height may be NaN for empty data
    var width = isNaN(rawLayout.width) ? Number.MAX_VALUE : Math.abs(rawLayout.width);
    var height = isNaN(rawLayout.height) ? Number.MAX_VALUE : Math.abs(rawLayout.height);
    return Math.min(lineWidth, width, height);
S
sushuang 已提交
579
}
580

581 582 583 584 585 586 587 588
class LagePathShape {
    points: ArrayLike<number>
}
interface LargePathProps extends PathProps {
    shape?: LagePathShape
}
class LargePath extends Path {
    type = 'largeBar'
589

590
    shape: LagePathShape
591

592 593 594 595
    __startPoint: number[]
    __baseDimIdx: number
    __largeDataIndices: ArrayLike<number>
    __barWidth: number
596

597 598 599
    constructor(opts?: LargePathProps) {
        super(opts, null, new LagePathShape());
    }
600

601
    buildPath(ctx: CanvasRenderingContext2D, shape: LagePathShape) {
602 603
        // Drawing lines is more efficient than drawing
        // a whole line or drawing rects.
604 605 606
        const points = shape.points;
        const startPoint = this.__startPoint;
        const baseDimIdx = this.__baseDimIdx;
607 608

        for (var i = 0; i < points.length; i += 2) {
609
            startPoint[baseDimIdx] = points[i + baseDimIdx];
610 611 612 613
            ctx.moveTo(startPoint[0], startPoint[1]);
            ctx.lineTo(points[i], points[i + 1]);
        }
    }
614
}
615

616 617 618 619 620
function createLarge(
    seriesModel: BarSeriesModel,
    group: Group,
    incremental?: boolean
) {
621 622 623
    // TODO support polar
    var data = seriesModel.getData();
    var startPoint = [];
624 625
    var baseDimIdx = data.getLayout('valueAxisHorizontal') ? 1 : 0;
    startPoint[1 - baseDimIdx] = data.getLayout('valueAxisStart');
626

627 628 629 630 631 632 633
    var largeDataIndices = data.getLayout('largeDataIndices');
    var barWidth = data.getLayout('barWidth');

    var backgroundModel = seriesModel.getModel('backgroundStyle');
    var drawBackground = seriesModel.get('showBackground', true);

    if (drawBackground) {
634 635
        const points = data.getLayout('largeBackgroundPoints');
        const backgroundStartPoint: number[] = [];
636 637
        backgroundStartPoint[1 - baseDimIdx] = data.getLayout('backgroundStart');

638
        const bgEl = new LargePath({
639 640 641 642 643
            shape: {points: points},
            incremental: !!incremental,
            silent: true,
            z2: 0
        });
644 645 646 647
        bgEl.__startPoint = backgroundStartPoint;
        bgEl.__baseDimIdx = baseDimIdx;
        bgEl.__largeDataIndices = largeDataIndices;
        bgEl.__barWidth = barWidth;
648 649 650 651
        setLargeBackgroundStyle(bgEl, backgroundModel, data);
        group.add(bgEl);
    }

652 653
    var el = new LargePath({
        shape: {points: data.getLayout('largePoints')},
654
        incremental: !!incremental
655
    });
656 657 658 659
    el.__startPoint = startPoint;
    el.__baseDimIdx = baseDimIdx;
    el.__largeDataIndices = largeDataIndices;
    el.__barWidth = barWidth;
660 661
    group.add(el);
    setLargeStyle(el, seriesModel, data);
662 663

    // Enable tooltip and user mouse/touch event handlers.
664
    getECData(el).seriesIndex = seriesModel.seriesIndex;
665 666 667 668 669 670 671 672

    if (!seriesModel.get('silent')) {
        el.on('mousedown', largePathUpdateDataIndex);
        el.on('mousemove', largePathUpdateDataIndex);
    }
}

// Use throttle to avoid frequently traverse to find dataIndex.
673
var largePathUpdateDataIndex = throttle(function (this: LargePath, event: ZRElementEvent) {
674 675
    var largePath = this;
    var dataIndex = largePathFindDataIndex(largePath, event.offsetX, event.offsetY);
676
    getECData(largePath).dataIndex = dataIndex >= 0 ? dataIndex : null;
677 678
}, 30, false);

679
function largePathFindDataIndex(largePath: LargePath, x: number, y: number) {
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    var baseDimIdx = largePath.__baseDimIdx;
    var valueDimIdx = 1 - baseDimIdx;
    var points = largePath.shape.points;
    var largeDataIndices = largePath.__largeDataIndices;
    var barWidthHalf = Math.abs(largePath.__barWidth / 2);
    var startValueVal = largePath.__startPoint[valueDimIdx];

    _eventPos[0] = x;
    _eventPos[1] = y;
    var pointerBaseVal = _eventPos[baseDimIdx];
    var pointerValueVal = _eventPos[1 - baseDimIdx];
    var baseLowerBound = pointerBaseVal - barWidthHalf;
    var baseUpperBound = pointerBaseVal + barWidthHalf;

    for (var i = 0, len = points.length / 2; i < len; i++) {
        var ii = i * 2;
        var barBaseVal = points[ii + baseDimIdx];
        var barValueVal = points[ii + valueDimIdx];
        if (
            barBaseVal >= baseLowerBound && barBaseVal <= baseUpperBound
            && (
                startValueVal <= barValueVal
                    ? (pointerValueVal >= startValueVal && pointerValueVal <= barValueVal)
                    : (pointerValueVal >= barValueVal && pointerValueVal <= startValueVal)
            )
        ) {
            return largeDataIndices[i];
        }
    }

    return -1;
711 712
}

713 714 715 716 717
function setLargeStyle(
    el: LargePath,
    seriesModel: BarSeriesModel,
    data: List
) {
718 719 720 721 722 723 724 725 726
    var borderColor = data.getVisual('borderColor') || data.getVisual('color');
    var itemStyle = seriesModel.getModel('itemStyle').getItemStyle(['color', 'borderColor']);

    el.useStyle(itemStyle);
    el.style.fill = null;
    el.style.stroke = borderColor;
    el.style.lineWidth = data.getLayout('barWidth');
}

727 728 729 730 731
function setLargeBackgroundStyle(
    el: LargePath,
    backgroundModel: Model<BarSeriesOption['backgroundStyle']>,
    data: List
) {
732 733 734 735 736 737
    var borderColor = backgroundModel.get('borderColor') || backgroundModel.get('color');
    var itemStyle = backgroundModel.getItemStyle(['color', 'borderColor']);

    el.useStyle(itemStyle);
    el.style.fill = null;
    el.style.stroke = borderColor;
738
    el.style.lineWidth = data.getLayout('barWidth') as number;
739 740
}

741 742 743 744 745
function createBackgroundShape(
    isHorizontalOrRadial: boolean,
    layout: SectorLayout | RectLayout,
    coord: CoordSysOfBar
): SectorShape | RectShape {
746
    if (isCoordinateSystemType<Cartesian2D>(coord, 'cartesian2d')) {
747 748 749 750 751 752 753 754
        const rectShape = layout as RectShape;
        const coordLayout = coord.getArea();
        return {
            x: isHorizontalOrRadial ? rectShape.x : coordLayout.x,
            y: isHorizontalOrRadial ? coordLayout.y : rectShape.y,
            width: isHorizontalOrRadial ? rectShape.width : coordLayout.width,
            height: isHorizontalOrRadial ? coordLayout.height : rectShape.height
        } as RectShape;
755 756
    }
    else {
757 758
        const coordLayout = coord.getArea();
        const sectorShape = layout as SectorShape;
759 760 761
        return {
            cx: coordLayout.cx,
            cy: coordLayout.cy,
762 763 764 765 766
            r0: isHorizontalOrRadial ? coordLayout.r0 : sectorShape.r0,
            r: isHorizontalOrRadial ? coordLayout.r : sectorShape.r,
            startAngle: isHorizontalOrRadial ? sectorShape.startAngle : 0,
            endAngle: isHorizontalOrRadial ? sectorShape.endAngle : Math.PI * 2
        } as SectorShape;
767 768 769
    }
}

770 771 772 773
function createBackgroundEl(
    coord: CoordSysOfBar,
    isHorizontalOrRadial: boolean,
    layout: SectorLayout | RectLayout
774 775
): Rect | Sector {
    var ElementClz = coord.type === 'polar' ? Sector : Rect;
776
    return new ElementClz({
777
        shape: createBackgroundShape(isHorizontalOrRadial, layout, coord) as any,
778 779 780 781
        silent: true,
        z2: 0
    });
}
782 783 784 785

ChartView.registerClass(BarView);

export default BarView;