def convert_QData_pixels(self, data): xMin = self.Stats['yAxis']['min'] xMax = self.Stats['yAxis']['max'] newData = [] for i in range(len(data)): y1 = canv_utils.Point2Pixel(xMin, xMax, 0, self.height, data[i][0]) y2 = canv_utils.Point2Pixel(xMin, xMax, 0, self.height, data[i][1]) newData.append([y1, y2]) return newData
def convert_to_pixels_2d(self, data, sourceRange, targetRange): newData = [] for i in range(len(data)): y1 = canv_utils.Point2Pixel(sourceRange[0], sourceRange[1], targetRange[0], targetRange[1], data[i][0]) y2 = canv_utils.Point2Pixel(sourceRange[0], sourceRange[1], targetRange[0], targetRange[1], data[i][1]) newData.append([y1, y2]) return newData
def convert_xAxis_pixels(self, data): xMin = self.Stats['xAxis']['min'] xMax = self.Stats['xAxis']['max'] newData = [] for i in range(len(data)): newData.append(canv_utils.Point2Pixel(xMin, xMax, 0, self.width, data[i])) return newData
def convert_to_pixels_1d(self, data, sourceRange, targetRange): newData = [] for i in range(len(data)): newData.append( canv_utils.Point2Pixel(sourceRange[0], sourceRange[1], targetRange[0], targetRange[1], data[i])) return newData
def DrawHGrid(self, grid): min_v = self.Stats['yAxis']['min'] max_v = self.Stats['yAxis']['max'] rows = list(range(0, max_v + 1, 20)) w, h = canv_utils.GetFontWidhHeight('120', self.canv._fontname, self.canv._fontsize) for rowV in rows: pos_y = canv_utils.Point2Pixel(min_v, max_v, 0, self.height, rowV) pos = [-h * 3, pos_y] if grid: self.canv.line(-(h / 3), pos[1], self.width, pos[1]) self.canv.drawString(pos[0], pos[1] - (h / 3), str(rowV))
def DrawNormalRange(self, txt="Normal Range"): rangeLow = canv_utils.Point2Pixel(self.Stats['yAxis']['min'], self.Stats['yAxis']['max'], 0, self.height, self.NormalRange[0]) rangeHigh = canv_utils.Point2Pixel(self.Stats['yAxis']['min'], self.Stats['yAxis']['max'], 0, self.height, self.NormalRange[1]) self.canv.saveState() clr = np.array([3, 125, 80, 125]) / 255 self.canv.setStrokeColor(Color(*clr)) w, h = canv_utils.GetFontWidhHeight(txt, self.canv._fontname, self.canv._fontsize) self.canv.setLineWidth(2) # small lines self.canv.line(0, rangeLow, self.width + h, rangeLow) self.canv.line(0, rangeHigh, self.width + h, rangeHigh) diff = abs(rangeLow - rangeHigh) fontSize = min( 10, max(5, int(diff * 0.12)) ) # Calculating Suitable Font Size between 7-12 according to Range Difference self.canv.setFontSize(fontSize) w, h = canv_utils.GetFontWidhHeight(txt, self.canv._fontname, self.canv._fontsize) y = rangeLow + (diff - w) / 2 canv_utils.WriteText(self.canv, txt, x=self.width + h, y=y, rot=-90) self.canv.setFontSize(8) w, h = canv_utils.GetFontWidhHeight(txt, self.canv._fontname, self.canv._fontsize) canv_utils.WriteText(self.canv, self.NormalRange[0], self.width + (h * 4), rangeLow - (h / 2)) canv_utils.WriteText(self.canv, self.NormalRange[1], self.width + (h * 4), rangeHigh - (h / 2)) self.canv.restoreState()
def DrawHGrid(self, grid): rows = self.Stats['yAxis']['pos'] minV = self.Stats['yAxis']['min'] maxV = self.Stats['yAxis']['max'] w, h = canv_utils.GetFontWidhHeight('12', self.canv._fontname, self.canv._fontsize) for rowV in rows: posY = canv_utils.Point2Pixel(minV, maxV, 0, self.height, rowV) pos = [-h * 3, posY] if grid: self.canv.line(-(h / 3), pos[1], self.width, pos[1]) # yVal = round(canv_utils.Point2Pixel(0, self.height, min(self.dataY), max(self.dataY), pos[1])) # vPos.append(pos) self.canv.drawString(pos[0], pos[1] - (h / 3), str(rowV))
def DrawHGrid(self, grid=True): rows = self.Stats['yAxis']['major'] minV = self.Stats['yAxis']['min'] maxV = self.Stats['yAxis']['max'] w, h = canv_utils.GetFontWidhHeight('12', self.canv._fontname, self.canv._fontsize) gridW = 0 if grid: gridW = self.width for rowV in rows: posY = canv_utils.Point2Pixel(minV, maxV, 0, self.height, rowV) pos = [-h*3, posY] self.canv.line(-(h/3), pos[1], gridW, pos[1]) strLabel = str(rowV) if self.yAxisDataFormator is not None: strLabel = self.yAxisDataFormator(rowV) self.canv.drawString(pos[0], pos[1] - (h/3), strLabel)
def DrawVGrid(self, grid=True): cols = self.Stats['xAxis']['major'] minTime = self.Stats['xAxis']['min'] maxTime = self.Stats['xAxis']['max'] w, h = canv_utils.GetFontWidhHeight('12', self.canv._fontname, self.canv._fontsize) gridH = 0 if grid: gridH = self.height y = 227 for col in cols: posX = canv_utils.Point2Pixel(minTime, maxTime, 0, self.width, col) pos = [posX, -(h * 2)] self.canv.line(pos[0], -(h / 3), pos[0], gridH) strLabel = str(col) if self.xAxisDataFormator is not None: strLabel = self.xAxisDataFormator(col) # canv_utils.WriteText(self.canv, strLabel, pos[0], pos[1], 0) canv_utils.WriteCenteredText(self.canv, strLabel, pos[0], pos[1])
def DrawVGrid(self, grid=False): cols = self.Stats['xAxis']['pos'] minTime = datetime.fromtimestamp(self.Stats['xAxis']['min']) maxTime = datetime.fromtimestamp(self.Stats['xAxis']['max']) w, h = canv_utils.GetFontWidhHeight('12', self.canv._fontname, self.canv._fontsize) for col in cols: if col == 24: timeD = time.max else: timeD = time(hour=col, second=0, microsecond=0) newT = datetime.combine(minTime.date(), timeD) posX = canv_utils.Point2Pixel(minTime.timestamp(), maxTime.timestamp(), 0, self.width, newT.timestamp()) # print(newT, posX, self.width) pos = [posX, -(h * 2)] if grid: self.canv.line(pos[0], -(h / 3), pos[0], self.height) labelStr = newT.strftime('%I %P') self.canv.drawString(pos[0] - (h * 1.2), pos[1], labelStr)
def DrawVGrid(self, grid=False): cols_vals = list(range(0, len(self.data.category), 1)) min_v = self.Stats['xAxis']['min'] max_v = self.Stats['xAxis']['max'] w, h = canv_utils.GetFontWidhHeight('12', self.canv._fontname, self.canv._fontsize) pixel_pos = [] for colV in cols_vals: pos_x = canv_utils.Point2Pixel(min_v, max_v, 0, self.width, colV) pos = [pos_x, -(h * 2)] if grid: self.canv.line(pos[0], -(h / 3), pos[0], self.height) label_str = self.data.iloc[colV].category pixel_pos.append(pos + [label_str]) pixel_pos.append([self.width, 0, '']) for i in range(len(pixel_pos) - 1): pos = pixel_pos[i] str_w, str_h = canv_utils.GetFontWidhHeight( pos[2], self.canv._fontname, self.canv._fontsize) diff = pixel_pos[i + 1][0] - pos[0] self.canv.drawString(pos[0] + (diff - str_w) / 2, pos[1], pos[2])
def draw(self): """ Draw the shape, text, etc """ self.Figure(grid=True) area = self.Stats['xAxis']['major_size'] padding = 0.07 center_space = 0.07 padding_size = padding * area center_size = center_space * area bar_size = (area - (padding_size * 2 + center_size)) / 2 self.canv.saveState() self.canv.setFontSize(8) self.canv.setFillColorRGB(0.2, 0.2, 0.2, 1.0) for i in range(len(self.data)): data_row = self.data.iloc[i] actual = canv_utils.Point2Pixel(0, self.Stats['yAxis']['max'], 0, self.height, max(data_row.actual, 0.2)) target = canv_utils.Point2Pixel(0, self.Stats['yAxis']['max'], 0, self.height, max(data_row.target, 0.2)) xy = (padding_size + area * i, 0) wh = (bar_size, actual) xy2 = (padding_size + center_size + bar_size + area * i, 0) wh2 = (bar_size, target) # print("XY: ", xy) # print("WH: ", wh) # print("bar_size: ", bar_size, "padding_size: ", padding_size, "center_size", center_size) # Actual Bar w_txt, h_txt = canv_utils.GetFontWidhHeight( f"A: {data_row.actual}%", self.canv._fontname, self.canv._fontsize) canv_utils.DrawRectangle(self.canv, xy, wh, fill=1, color=self.Colors[i], stroke=0) canv_utils.WriteText(self.canv, f"A: {data_row.actual}%", xy[0] + wh[0] / 2 + w_txt / 2, wh[1] / 2, rot=0) # Target Bar w_txt, h_txt = canv_utils.GetFontWidhHeight( f"T: {data_row.target}%", self.canv._fontname, self.canv._fontsize) canv_utils.DrawRectangle(self.canv, xy2, wh2, fill=1, color=self.Colors[i], stroke=0) canv_utils.WriteText(self.canv, f"T: {data_row.target}%", xy2[0] + wh2[0] / 2 + w_txt / 2, wh2[1] / 2, rot=0) self.canv.restoreState()