def calculateDatasets(self, scene, axes, datasets): """ Builds the datasets for this renderer. Each renderer will need to subclass and implemenent this method, otherwise, no data will be shown in the chart. :param scene | <XChartScene> axes | [< datasets | [<XChartDataset>, ..] """ items = self.calculateDatasetItems(scene, datasets) if not items: scene.clear() return rect = self.buildData('axis_rect') half_size = self.maximumBarSize() / 2.0 for dataset, item in items.items(): path = QPainterPath() subpaths = [] for value in dataset.values(): pos = self.pointAt(axes, value) radius = min(rect.bottom() - pos.y(), 8) subpath = QPainterPath() # create a vertical bar graph if self.orientation() == Qt.Vertical: subpath.moveTo(pos.x() - half_size, rect.bottom()) subpath.lineTo(pos.x() - half_size, pos.y() + radius) subpath.quadTo(pos.x() - half_size, pos.y(), pos.x() - half_size + radius, pos.y()) subpath.lineTo(pos.x() + half_size - radius, pos.y()) subpath.quadTo(pos.x() + half_size, pos.y(), pos.x() + half_size, pos.y() + radius) subpath.lineTo(pos.x() + half_size, rect.bottom()) subpath.lineTo(pos.x() - half_size, rect.bottom()) # create a horizontal bar graph else: subpath.moveTo(rect.left(), pos.y() - half_size) subpath.lineTo(pos.x(), pos.y() - half_size) subpath.lineTo(pos.x(), pos.y() + half_size) subpath.lineTo(rect.left(), pos.y() + half_size) subpath.lineTo(rect.left(), pos.y() - half_size) path.addPath(subpath) subpaths.append(subpath) item.setPath(path) item.setBuildData('subpaths', subpaths)
def rebuild(self): """ Rebuilds the item based on the current points. """ scene = self.scene() if not scene: return self._subpaths = [] grid = scene.gridRect() typ = self.chartType() hruler = scene.horizontalRuler() vruler = scene.verticalRuler() path = QPainterPath() area = QPainterPath() self._buildData.clear() self._buildData['path_area'] = area self.setPos(0, 0) # draw a line item if typ == XChartScene.Type.Line: first = True pos = None home = None self._ellipses = [] points = self.points() if (self.orientation() == Qt.Horizontal): points.sort(hruler.compareValues, key=lambda x: x[0]) else: points.sort(vruler.compareValues, key=lambda y: y[1]) points.reverse() for x, y in self.points(): pos = scene.mapFromChart(x, y) if first: home = QPointF(pos.x(), grid.bottom()) area.moveTo(home) area.lineTo(pos) path.moveTo(pos) self._ellipses.append(pos) first = False else: path.lineTo(pos) area.lineTo(pos) self._ellipses.append(pos) if pos and home: area.lineTo(pos.x(), grid.bottom()) area.lineTo(home) # draw a bar item elif typ == XChartScene.Type.Bar: barsize = self.barSize() horiz = self.orientation() == Qt.Horizontal for x, y in self.points(): pos = scene.mapFromChart(x, y) subpath = QPainterPath() if horiz: r = min(grid.bottom() - pos.y(), 8) subpath.moveTo(pos.x() - barsize / 2.0, grid.bottom()) subpath.lineTo(pos.x() - barsize / 2.0, pos.y() + r) subpath.quadTo(pos.x() - barsize / 2.0, pos.y(), pos.x() - barsize / 2.0 + r, pos.y()) subpath.lineTo(pos.x() + barsize / 2.0 - r, pos.y()) subpath.quadTo(pos.x() + barsize / 2.0, pos.y(), pos.x() + barsize / 2.0, pos.y() + r) subpath.lineTo(pos.x() + barsize / 2.0, grid.bottom()) subpath.lineTo(pos.x() - barsize / 2.0, grid.bottom()) else: subpath.moveTo(grid.left(), pos.y() - barsize / 2.0) subpath.lineTo(pos.x(), pos.y() - barsize / 2.0) subpath.lineTo(pos.x(), pos.y() + barsize / 2.0) subpath.lineTo(grid.left(), pos.y() + barsize / 2.0) subpath.lineTo(grid.left(), pos.y() - barsize / 2.0) path.addPath(subpath) self._subpaths.append((x, y, subpath)) # draw a pie chart elif typ == XChartScene.Type.Pie: if self.orientation() == Qt.Horizontal: key_index = 0 value_index = 1 value_ruler = self.verticalRuler() else: key_index = 1 value_index = 0 value_ruler = self.horizontalRuler() pie_values = {} for point in self.points(): key = point[key_index] value = point[value_index] pie_values.setdefault(key, []) pie_values[key].append(value) for key, values in pie_values.items(): pie_values[key] = value_ruler.calcTotal(values) total = max(1, value_ruler.calcTotal(pie_values.values())) # calculate drawing parameters center = self.pieCenter() radius = self.radius() diameter = radius * 2 angle = 0 bound = QRectF(-radius, -radius, diameter, diameter) for key, value in sorted(pie_values.items(), key=lambda x: x[1]): # calculate the percentage perc = float(value) / total # calculate the angle as the perc * 360 item_angle = perc * 360 self.setPos(center) sub_path = QPainterPath() sub_path.arcTo(bound, angle, item_angle) sub_path.lineTo(0, 0) path.addPath(sub_path) self._subpaths.append((key, value, sub_path)) angle += item_angle self.setPath(path) self._dirty = False
def rebuildMonth( self ): """ Rebuilds the current item in month mode. """ scene = self.scene() if ( not scene ): return start_date = self.dateStart() end_date = self.dateEnd() min_date = scene.minimumDate() max_date = scene.maximumDate() # make sure our item is visible if ( not (min_date <= end_date and start_date <= max_date)): self.hide() self.setPath(QPainterPath()) return # make sure we have valid range information if ( start_date < min_date ): start_date = min_date start_inrange = False else: start_inrange = True if ( max_date < end_date ): end_date = max_date end_inrange = False else: end_inrange = True start_rect = scene.dateRect(start_date) end_rect = scene.dateRect(end_date) if ( not (start_rect.isValid() and end_rect.isValid()) ): self.hide() return # rebuild an all day path path = QPainterPath() self.setPos(0, 0) pad = 2 offset = 18 height = 16 min_left = 10 max_right = scene.width() - 16 delta_h = start_rect.height() # draw the all day event if ( self.isAllDay() ): top = start_rect.top() left = start_rect.left() + 3 first = start_inrange while ( top <= end_rect.top() ): sub_path = QPainterPath() # calculate the end position if ( end_rect.top() - 2 <= top and end_inrange ): at_end = True right = end_rect.right() - pad else: at_end = False right = max_right if ( first ): sub_path.moveTo(left, top + offset) text_left = left + 4 else: sub_path.moveTo(left + height / 2, top + offset) text_left = left + height / 2 + 2 if ( at_end ): sub_path.lineTo(right, top + offset) sub_path.lineTo(right, top + offset + height) else: sub_path.lineTo(right - height / 2, top + offset) sub_path.lineTo(right, top + offset + height / 2) sub_path.lineTo(right - height / 2, top + offset + height) if ( first ): sub_path.lineTo(left, top + offset + height) sub_path.lineTo(left, top + offset) else: sub_path.lineTo(left + height / 2, top + offset + height) sub_path.lineTo(left, top + offset + height / 2) sub_path.lineTo(left + height / 2, top + offset) path.addPath(sub_path) data = (text_left, top + offset + 1, right, height, Qt.AlignLeft | Qt.AlignVCenter, self.title()) self._textData.append(data) left = min_left top += delta_h first = False else: text = '%s: (%s)' % (self.timeStart().toString('h:mm ap'), self.title()) font = scene.font() left = start_rect.left() + 2 * pad top = start_rect.top() + offset path.addText(left, top + height / 2, font, text) # setup the path for this item self.setPath(path) self.show() # make sure there are no collisions while ( self.collidingItems() ): self.setPos(self.pos().x(), self.pos().y() + height + 2) # hide the item if out of the visible scope if ( delta_h - offset <= self.pos().y() + height ): self.hide() break
def calculateDatasets(self, scene, axes, datasets): """ Builds the datasets for this renderer. Each renderer will need to subclass and implemenent this method, otherwise, no data will be shown in the chart. :param scene | <XChartScene> axes | [< datasets | [<XChartDataset>, ..] """ items = self.calculateDatasetItems(scene, datasets) if not items: scene.clear() return xaxis = scene.chart().horizontalAxis() yaxis = scene.chart().verticalAxis() data_axis = None all_values = [] # determine if we're mapping data aginst the sets themselves, in # which case, create a pie chart of the dataset vs. its if isinstance(xaxis, XDatasetAxis): per_dataset = False data_axis = yaxis total = 1 elif isinstance(yaxis, XDatasetAxis): per_dataset = False total = 1 data_axis = xaxis else: per_dataset = True total = len(items) if not per_dataset: all_values = [dataset.sum(data_axis) \ for dataset in datasets] # generate the build information rect = self.buildData('grid_rect') rect.setX(rect.x() + 10) rect.setY(rect.y() + 10) rect.setWidth(rect.width() - 20) rect.setHeight(rect.height() - 20) if rect.width() > rect.height(): radius = min(rect.width() / 2.0, rect.height() / 2.0) x = rect.left() y = rect.top() + radius deltax = min(radius * 2, rect.width() / float(total + 1)) deltay = 0 else: radius = min(rect.height() / 2.0, rect.width() / 2.0) x = rect.left() + radius y = rect.top() deltax = 0 deltay = min(radius * 2, rect.height() / float(total + 1)) # initialize the first pie chart angle = 0 cx = x + deltax cy = y + deltay x += deltax y += deltay self.setBuildData('center', QPointF(cx, cy)) self.setBuildData('radius', radius) for dataset in datasets: item = items.get(dataset) if not item: continue item.setBuildData('center', QPointF(cx, cy)) item.setBuildData('radius', radius) subpaths = [] bound = QRectF(cx-radius, cy-radius, radius * 2, radius * 2) path = QPainterPath() if per_dataset: data_values = dataset.values(yaxis.name()) andle = 0 for value in dataset.values(): perc = yaxis.percentOfTotal(value.get(yaxis.name()), data_values) # calculate the angle as the perc item_angle = perc * 360 subpath = QPainterPath() subpath.moveTo(cx, cy) subpath.arcTo(bound, angle, item_angle) subpath.lineTo(cx, cy) path.addPath(subpath) subpaths.append((value.get(xaxis.name()), subpath)) angle += item_angle cx = x + deltax cy = y + deltay x += deltax y += deltay else: value = dataset.sum(data_axis) perc = data_axis.percentOfTotal(value, all_values) # calculate the angle as the perc item_angle = perc * 360 subpath = QPainterPath() subpath.moveTo(cx, cy) subpath.arcTo(bound, angle, item_angle) subpath.lineTo(cx, cy) path.addPath(subpath) subpaths.append((value, subpath)) angle += item_angle item.setPath(path) item.setBuildData('subpaths', subpaths)
def calculateDatasets(self, scene, axes, datasets): """ Builds the datasets for this renderer. Each renderer will need to subclass and implemenent this method, otherwise, no data will be shown in the chart. :param scene | <XChartScene> axes | [< datasets | [<XChartDataset>, ..] """ items = self.calculateDatasetItems(scene, datasets) if not items: scene.clear() return xaxis = scene.chart().horizontalAxis() yaxis = scene.chart().verticalAxis() data_axis = None all_values = [] # determine if we're mapping data aginst the sets themselves, in # which case, create a pie chart of the dataset vs. its if isinstance(xaxis, XDatasetAxis): per_dataset = False data_axis = yaxis total = 1 elif isinstance(yaxis, XDatasetAxis): per_dataset = False total = 1 data_axis = xaxis else: per_dataset = True total = len(items) if not per_dataset: all_values = [dataset.sum(data_axis) \ for dataset in datasets] # generate the build information rect = self.buildData('grid_rect') rect.setX(rect.x() + 10) rect.setY(rect.y() + 10) rect.setWidth(rect.width() - 20) rect.setHeight(rect.height() - 20) if rect.width() > rect.height(): radius = min(rect.width() / 2.0, rect.height() / 2.0) x = rect.left() y = rect.top() + radius deltax = min(radius * 2, rect.width() / float(total + 1)) deltay = 0 else: radius = min(rect.height() / 2.0, rect.width() / 2.0) x = rect.left() + radius y = rect.top() deltax = 0 deltay = min(radius * 2, rect.height() / float(total + 1)) # initialize the first pie chart angle = 0 cx = x + deltax cy = y + deltay x += deltax y += deltay self.setBuildData('center', QPointF(cx, cy)) self.setBuildData('radius', radius) for dataset in datasets: item = items.get(dataset) if not item: continue item.setBuildData('center', QPointF(cx, cy)) item.setBuildData('radius', radius) subpaths = [] bound = QRectF(cx - radius, cy - radius, radius * 2, radius * 2) path = QPainterPath() if per_dataset: data_values = dataset.values(yaxis.name()) andle = 0 for value in dataset.values(): perc = yaxis.percentOfTotal(value.get(yaxis.name()), data_values) # calculate the angle as the perc item_angle = perc * 360 subpath = QPainterPath() subpath.moveTo(cx, cy) subpath.arcTo(bound, angle, item_angle) subpath.lineTo(cx, cy) path.addPath(subpath) subpaths.append((value.get(xaxis.name()), subpath)) angle += item_angle cx = x + deltax cy = y + deltay x += deltax y += deltay else: value = dataset.sum(data_axis) perc = data_axis.percentOfTotal(value, all_values) # calculate the angle as the perc item_angle = perc * 360 subpath = QPainterPath() subpath.moveTo(cx, cy) subpath.arcTo(bound, angle, item_angle) subpath.lineTo(cx, cy) path.addPath(subpath) subpaths.append((value, subpath)) angle += item_angle item.setPath(path) item.setBuildData('subpaths', subpaths)
def rebuild( self ): """ Rebuilds the item based on the current points. """ scene = self.scene() if not scene: return self._subpaths = [] grid = scene.gridRect() typ = self.chartType() hruler = scene.horizontalRuler() vruler = scene.verticalRuler() path = QPainterPath() area = QPainterPath() self._buildData.clear() self._buildData['path_area'] = area self.setPos(0, 0) # draw a line item if typ == XChartScene.Type.Line: first = True pos = None home = None self._ellipses = [] points = self.points() if ( self.orientation() == Qt.Horizontal ): points.sort(hruler.compareValues, key = lambda x: x[0]) else: points.sort(vruler.compareValues, key = lambda y: y[1]) points.reverse() for x, y in self.points(): pos = scene.mapFromChart(x, y) if first: home = QPointF(pos.x(), grid.bottom()) area.moveTo(home) area.lineTo(pos) path.moveTo(pos) self._ellipses.append(pos) first = False else: path.lineTo(pos) area.lineTo(pos) self._ellipses.append(pos) if pos and home: area.lineTo(pos.x(), grid.bottom()) area.lineTo(home) # draw a bar item elif typ == XChartScene.Type.Bar: barsize = self.barSize() horiz = self.orientation() == Qt.Horizontal for x, y in self.points(): pos = scene.mapFromChart(x, y) subpath = QPainterPath() if horiz: r = min(grid.bottom() - pos.y(), 8) subpath.moveTo(pos.x() - barsize / 2.0, grid.bottom()) subpath.lineTo(pos.x() - barsize / 2.0, pos.y() + r) subpath.quadTo(pos.x() - barsize / 2.0, pos.y(), pos.x() - barsize / 2.0 + r, pos.y()) subpath.lineTo(pos.x() + barsize / 2.0 - r, pos.y()) subpath.quadTo(pos.x() + barsize / 2.0, pos.y(), pos.x() + barsize / 2.0, pos.y() + r) subpath.lineTo(pos.x() + barsize / 2.0, grid.bottom()) subpath.lineTo(pos.x() - barsize / 2.0, grid.bottom()) else: subpath.moveTo(grid.left(), pos.y() - barsize / 2.0) subpath.lineTo(pos.x(), pos.y() - barsize / 2.0) subpath.lineTo(pos.x(), pos.y() + barsize / 2.0) subpath.lineTo(grid.left(), pos.y() + barsize / 2.0) subpath.lineTo(grid.left(), pos.y() - barsize / 2.0) path.addPath(subpath) self._subpaths.append((x, y, subpath)) # draw a pie chart elif typ == XChartScene.Type.Pie: if self.orientation() == Qt.Horizontal: key_index = 0 value_index = 1 value_ruler = self.verticalRuler() else: key_index = 1 value_index = 0 value_ruler = self.horizontalRuler() pie_values = {} for point in self.points(): key = point[key_index] value = point[value_index] pie_values.setdefault(key, []) pie_values[key].append(value) for key, values in pie_values.items(): pie_values[key] = value_ruler.calcTotal(values) total = max(1, value_ruler.calcTotal(pie_values.values())) # calculate drawing parameters center = self.pieCenter() radius = self.radius() diameter = radius * 2 angle = 0 bound = QRectF(-radius, -radius, diameter, diameter) for key, value in sorted(pie_values.items(), key = lambda x: x[1]): # calculate the percentage perc = float(value) / total # calculate the angle as the perc * 360 item_angle = perc * 360 self.setPos(center) sub_path = QPainterPath() sub_path.arcTo(bound, angle, item_angle) sub_path.lineTo(0, 0) path.addPath(sub_path) self._subpaths.append((key, value, sub_path)) angle += item_angle self.setPath(path) self._dirty = False
def rebuildMonth(self): """ Rebuilds the current item in month mode. """ scene = self.scene() if (not scene): return start_date = self.dateStart() end_date = self.dateEnd() min_date = scene.minimumDate() max_date = scene.maximumDate() # make sure our item is visible if (not (min_date <= end_date and start_date <= max_date)): self.hide() self.setPath(QPainterPath()) return # make sure we have valid range information if (start_date < min_date): start_date = min_date start_inrange = False else: start_inrange = True if (max_date < end_date): end_date = max_date end_inrange = False else: end_inrange = True start_rect = scene.dateRect(start_date) end_rect = scene.dateRect(end_date) if (not (start_rect.isValid() and end_rect.isValid())): self.hide() return # rebuild an all day path path = QPainterPath() self.setPos(0, 0) pad = 2 offset = 18 height = 16 min_left = 10 max_right = scene.width() - 16 delta_h = start_rect.height() # draw the all day event if (self.isAllDay()): top = start_rect.top() left = start_rect.left() + 3 first = start_inrange while (top <= end_rect.top()): sub_path = QPainterPath() # calculate the end position if (end_rect.top() - 2 <= top and end_inrange): at_end = True right = end_rect.right() - pad else: at_end = False right = max_right if (first): sub_path.moveTo(left, top + offset) text_left = left + 4 else: sub_path.moveTo(left + height / 2, top + offset) text_left = left + height / 2 + 2 if (at_end): sub_path.lineTo(right, top + offset) sub_path.lineTo(right, top + offset + height) else: sub_path.lineTo(right - height / 2, top + offset) sub_path.lineTo(right, top + offset + height / 2) sub_path.lineTo(right - height / 2, top + offset + height) if (first): sub_path.lineTo(left, top + offset + height) sub_path.lineTo(left, top + offset) else: sub_path.lineTo(left + height / 2, top + offset + height) sub_path.lineTo(left, top + offset + height / 2) sub_path.lineTo(left + height / 2, top + offset) path.addPath(sub_path) data = (text_left, top + offset + 1, right, height, Qt.AlignLeft | Qt.AlignVCenter, self.title()) self._textData.append(data) left = min_left top += delta_h first = False else: text = '%s: (%s)' % (self.timeStart().toString('h:mm ap'), self.title()) font = scene.font() left = start_rect.left() + 2 * pad top = start_rect.top() + offset path.addText(left, top + height / 2, font, text) # setup the path for this item self.setPath(path) self.show() # make sure there are no collisions while (self.collidingItems()): self.setPos(self.pos().x(), self.pos().y() + height + 2) # hide the item if out of the visible scope if (delta_h - offset <= self.pos().y() + height): self.hide() break