1 /* 2 * Copyright (c) 2021 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 16 package ohos.devtools.views.charts; 17 18 import com.intellij.ui.JBColor; 19 import ohos.devtools.views.charts.model.ChartDataModel; 20 import ohos.devtools.views.layout.chartview.ProfilerChartsView; 21 22 import java.awt.Graphics; 23 import java.awt.event.MouseEvent; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import static ohos.devtools.views.charts.model.ChartType.RECT; 28 import static ohos.devtools.views.charts.utils.ChartUtils.multiply; 29 30 /** 31 * RectChart chart 32 * 33 * @since 2021/5/19 16:39 34 */ 35 public class RectChart extends ProfilerChart { 36 /** 37 * thread unspecified 38 */ 39 private static final int THREAD_UNSPECIFIED = 0; 40 41 /** 42 * thread running 43 */ 44 private static final int THREAD_RUNNING = 1; 45 46 /** 47 * thread sleeping 48 */ 49 private static final int THREAD_SLEEPING = 2; 50 51 /** 52 * thread stopped 53 */ 54 private static final int THREAD_STOPPED = 3; 55 56 /** 57 * thread waiting 58 */ 59 private static final int THREAD_WAITING = 4; 60 61 private int threadId; 62 63 /** 64 * Constructor 65 * 66 * @param bottomPanel ProfilerChartsView 67 * @param name chart name 68 */ RectChart(ProfilerChartsView bottomPanel, String name)69 public RectChart(ProfilerChartsView bottomPanel, String name) { 70 super(bottomPanel, name); 71 chartType = RECT; 72 top = 0; 73 } 74 setThreadId(int threadId)75 public void setThreadId(int threadId) { 76 this.threadId = threadId; 77 } 78 79 /** 80 * Init legends 81 */ 82 @Override initLegends()83 protected void initLegends() { 84 } 85 86 /** 87 * Build legends of chart 88 * 89 * @param lastModels Data on the far right side of the panel 90 * @see "The legend shows the y value corresponding to the rightmost X axis, not the mouse hover position" 91 */ 92 @Override buildLegends(List<ChartDataModel> lastModels)93 protected void buildLegends(List<ChartDataModel> lastModels) { 94 } 95 96 /** 97 * Paint chart 98 * 99 * @param graphics Graphics 100 */ 101 @Override paintChart(Graphics graphics)102 protected void paintChart(Graphics graphics) { 103 if (dataMap == null || dataMap.size() == 0) { 104 return; 105 } 106 int[] timeArray = dataMap.keySet().stream().mapToInt(Integer::valueOf).toArray(); 107 int pointX = 0; 108 int lastValue = 0; 109 int endX = 0; 110 for (int time : timeArray) { 111 List<ChartDataModel> chartDataModelList = dataMap.get(time); 112 ArrayList<RectDataModel> splitList = new ArrayList<RectDataModel>(); 113 if (chartDataModelList != null && !chartDataModelList.isEmpty()) { 114 for (ChartDataModel chartDataModel : chartDataModelList) { 115 if (chartDataModel.getIndex() == threadId) { 116 int currentValue = chartDataModel.getValue(); 117 pointX = startXCoordinate + multiply(pixelPerX, time - startTime); 118 endX = startXCoordinate + multiply(pixelPerX, timeArray[timeArray.length - 1] - startTime); 119 if (currentValue != lastValue) { 120 splitList.add(new RectDataModel(pointX, currentValue)); 121 lastValue = currentValue; 122 } 123 } 124 } 125 for (int index = 0; index < splitList.size(); index++) { 126 switch (splitList.get(index).getThreadStatus()) { 127 case THREAD_RUNNING: 128 graphics.setColor(JBColor.GREEN); 129 break; 130 case THREAD_SLEEPING: 131 graphics.setColor(JBColor.background().darker()); 132 break; 133 case THREAD_WAITING: 134 graphics.setColor(JBColor.YELLOW); 135 break; 136 default: 137 graphics.setColor(null); 138 } 139 graphics.fillRect(splitList.get(index).getPointX(), 0, endX, yZero); 140 } 141 } 142 } 143 } 144 145 /** 146 * Build tooltip content 147 * 148 * @param showKey Key to show 149 * @param actualKey The actual value of the key in the data map 150 * @param newChart Is it a new chart 151 */ 152 @Override buildTooltip(int showKey, int actualKey, boolean newChart)153 protected void buildTooltip(int showKey, int actualKey, boolean newChart) { 154 } 155 156 /** 157 * User defined events 158 * 159 * @param event MouseEvent 160 */ 161 @Override leftMouseClickEvent(MouseEvent event)162 protected void leftMouseClickEvent(MouseEvent event) { 163 } 164 165 /** 166 * User defined events 167 * 168 * @param event MouseEvent 169 */ 170 @Override rightMouseClickEvent(MouseEvent event)171 protected void rightMouseClickEvent(MouseEvent event) { 172 } 173 174 /** 175 * User defined events 176 * 177 * @param event MouseEvent 178 */ 179 @Override mouseDraggedEvent(MouseEvent event)180 protected void mouseDraggedEvent(MouseEvent event) { 181 } 182 183 /** 184 * User defined events 185 * 186 * @param event MouseEvent 187 */ 188 @Override mouseReleaseEvent(MouseEvent event)189 protected void mouseReleaseEvent(MouseEvent event) { 190 } 191 192 @Override drawYAxis(Graphics graphics)193 protected void drawYAxis(Graphics graphics) { 194 } 195 196 private class RectDataModel { 197 private int pointX; 198 private int threadStatus; 199 200 /** 201 * Constructor 202 * 203 * @param pointX pointX 204 * @param threadStatus threadStatus 205 */ RectDataModel(int pointX, int threadStatus)206 RectDataModel(int pointX, int threadStatus) { 207 this.pointX = pointX; 208 this.threadStatus = threadStatus; 209 } 210 getPointX()211 public int getPointX() { 212 return pointX; 213 } 214 setPointX(int pointX)215 public void setPointX(int pointX) { 216 this.pointX = pointX; 217 } 218 getThreadStatus()219 public int getThreadStatus() { 220 return threadStatus; 221 } 222 setThreadStatus(int threadStatus)223 public void setThreadStatus(int threadStatus) { 224 this.threadStatus = threadStatus; 225 } 226 } 227 } 228