1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BaseStruct } from './BaseStruct';
17
18export class HeapStruct extends BaseStruct {
19  static hoverHeapStruct: HeapStruct | undefined;
20  startTime: number | undefined;
21  endTime: number | undefined;
22  dur: number | undefined;
23  eventType: string | undefined;
24  heapsize: number | undefined;
25  density: number | undefined;
26  maxHeapSize: number = 0;
27  minHeapSize: number = 0;
28
29  static draw(heapCtx: CanvasRenderingContext2D, heapData: HeapStruct): void {
30    if (heapData.frame) {
31      let width = heapData.frame.width || 0;
32      heapCtx.fillStyle = '#2db3aa';
33      heapCtx.strokeStyle = '#2db3aa';
34      if (heapData.startTime === HeapStruct.hoverHeapStruct?.startTime) {
35        heapCtx.lineWidth = 1;
36        heapCtx.globalAlpha = 0.6;
37        let drawHeight: number = Math.ceil(
38          ((heapData.heapsize || 0) * (heapData.frame.height || 0)) / heapData.maxHeapSize
39        );
40        heapCtx.fillRect(heapData.frame.x, heapData.frame.y + heapData.frame.height - drawHeight, width, drawHeight);
41        heapCtx.beginPath();
42        heapCtx.arc(heapData.frame.x, heapData.frame.y + heapData.frame.height - drawHeight, 3, 0, 2 * Math.PI, true);
43        heapCtx.fill();
44        heapCtx.globalAlpha = 1.0;
45        heapCtx.stroke();
46        heapCtx.beginPath();
47        heapCtx.moveTo(heapData.frame.x + 3, heapData.frame.y + heapData.frame.height - drawHeight);
48        heapCtx.lineWidth = 3;
49        heapCtx.lineTo(heapData.frame.x + width, heapData.frame.y + heapData.frame.height - drawHeight);
50        heapCtx.stroke();
51      } else {
52        heapCtx.globalAlpha = 0.6;
53        heapCtx.lineWidth = 1;
54        let drawHeight: number = Math.ceil(
55          ((heapData.heapsize || 0) * (heapData.frame.height || 0)) / heapData.maxHeapSize
56        );
57        heapCtx.fillRect(heapData.frame.x, heapData.frame.y + heapData.frame.height - drawHeight, width, drawHeight);
58      }
59    }
60    heapCtx.globalAlpha = 1.0;
61    heapCtx.lineWidth = 1;
62  }
63}
64