def populate_fixture(self, workbook_, worksheet_): chart_data = BubbleChartData() series_1 = chart_data.add_series('Series 1') for pt in ((1, 1.1, 10), (2, 2.2, 20)): series_1.add_data_point(*pt) series_2 = chart_data.add_series('Series 2') for pt in ((3, 3.3, 30), (4, 4.4, 40)): series_2.add_data_point(*pt) workbook_writer = BubbleWorkbookWriter(chart_data) expected_calls = [ call.write_column(1, 0, [1, 2], ANY), call.write(0, 1, 'Series 1'), call.write_column(1, 1, [1.1, 2.2], ANY), call.write(0, 2, 'Size'), call.write_column(1, 2, [10, 20], ANY), call.write_column(5, 0, [3, 4], ANY), call.write(4, 1, 'Series 2'), call.write_column(5, 1, [3.3, 4.4], ANY), call.write(4, 2, 'Size'), call.write_column(5, 2, [30, 40], ANY), ] return workbook_writer, workbook_, worksheet_, expected_calls
def replace_bubble_data(shape: GraphicFrame, data: BubbleData): chart_data = BubbleChartData() for name, points in data.series.items(): series_data = chart_data.add_series(name) for point in points: series_data.add_data_point(point.x, point.y, point.size) shape.chart.replace_data(chart_data)
def when_I_replace_its_data_with_3_series_of_three_bubble_pts_each(context): chart_data = BubbleChartData() for idx in range(3): series_title = 'New Series %d' % (idx + 1) series = chart_data.add_series(series_title) for jdx in range(3): x, y, size = idx * 3 + jdx, idx * 2 + jdx, idx + jdx series.add_data_point(x, y, size) context.chart.replace_data(chart_data)
def when_I_replace_its_data_with_3_series_of_three_bubble_pts_each(context): chart_data = BubbleChartData() for idx in range(3): series_title = "New Series %d" % (idx + 1) series = chart_data.add_series(series_title) for jdx in range(3): x, y, size = idx * 3 + jdx, idx * 2 + jdx, idx + jdx series.add_data_point(x, y, size) context.chart.replace_data(chart_data)
def bubbleSize_fixture(self, request): cht_numfmt, ser_numfmt, expected_numfmt = request.param expected_xml = xml( 'c:bubbleSize/c:numRef/(c:f"Sheet1!$C$2:$C$1",c:numCache/(c:form' 'atCode"%s",c:ptCount{val=0}))' % expected_numfmt) chart_number_format = () if cht_numfmt is None else (cht_numfmt, ) series_number_format = () if ser_numfmt is None else (ser_numfmt, ) chart_data = BubbleChartData(*chart_number_format) series_data = chart_data.add_series(None, *series_number_format) xml_writer = _BubbleSeriesXmlWriter(series_data) return xml_writer, expected_xml
def rewrite_fixture(self): ser_xml, expected_xml = snippet_seq("rewrite-ser")[4:6] chart_data = BubbleChartData() series_data = chart_data.add_series("Series 1") series_data.add_data_point(1, 2, 10) series_data.add_data_point(3, 4, 20) rewriter = _BubbleSeriesXmlRewriter(chart_data) ser = parse_xml(ser_xml) return rewriter, ser, series_data, expected_xml
def rewrite_fixture(self): ser_xml, expected_xml = snippet_seq('rewrite-ser')[4:6] chart_data = BubbleChartData() series_data = chart_data.add_series('Series 1') series_data.add_data_point(1, 2, 10) series_data.add_data_point(3, 4, 20) rewriter = _BubbleSeriesXmlRewriter(chart_data) ser = parse_xml(ser_xml) return rewriter, ser, series_data, expected_xml
def bubbleSize_fixture(self, request): cht_numfmt, ser_numfmt, expected_numfmt = request.param expected_xml = xml( 'c:bubbleSize/c:numRef/(c:f"Sheet1!$C$2:$C$1",c:numCache/(c:form' 'atCode"%s",c:ptCount{val=0}))' % expected_numfmt ) chart_number_format = () if cht_numfmt is None else (cht_numfmt,) series_number_format = () if ser_numfmt is None else (ser_numfmt,) chart_data = BubbleChartData(*chart_number_format) series_data = chart_data.add_series(None, *series_number_format) xml_writer = _BubbleSeriesXmlWriter(series_data) return xml_writer, expected_xml
def __create_xyzdata(dfs): chart_data = None seriesNum = 1 for df in dfs: colNames = df.columns.tolist() name = 'Series ' + str(seriesNum) if hasattr(df, 'name') and df.name != "": name = df.name if len(colNames) > 1 and len(colNames) < 4: if len(colNames) == 2 and chart_data is None: chart_data = XyChartData() elif len(colNames) == 3 and chart_data is None: chart_data = BubbleChartData() series = chart_data.add_series(name) for index, row in df.iterrows(): data = [] for colName in colNames: data.append(row[colName]) if len(colNames) == 2: series.add_data_point(data[0], data[1]) else: series.add_data_point(data[0], data[1], data[2]) seriesNum += 1 return chart_data
def plot_PriorityvsRisk_slide1(): slide = prs.slides.add_slide(prs.slide_layouts[3]) #Declare the chart chart_data = BubbleChartData() #annotate the points series_1 = chart_data.add_series(' Category of KRI ') series_1.add_data_point(7.0, 3.5, 2) series_1.add_data_point(15.0, 3.5, 2) series_1.add_data_point(5.0, 2.2, 2) series_1.add_data_point(10.0, 2.2, 2) series_1.add_data_point(15.0, 2.2, 2) x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) chart = slide.shapes.add_chart(XL_CHART_TYPE.BUBBLE, x, y, cx, cy, chart_data).chart category_axis = chart.category_axis value_axis = chart.value_axis value_axis.axis_title.text_frame.text = 'Priority' category_axis.axis_title.text_frame.text = 'Risk' chart.has_legend = True value_axis.has_major_gridlines = False value_axis.major_tick_mark = XL_TICK_MARK.NONE plot = chart.plots[0] # print plot.has_data_labels # print plot.has_data_labels plot.has_data_labels = True # assert plot.data_labels.font.fill.fore_color.type == MSO_COLOR_TYPE.RGB # print plot.data_labels.font.color.rgb tick_labels = value_axis.tick_labels tick_labels.number_format = '0' tick_labels.font.bold = True tick_labels.font.size = Pt(1) tick_labels = category_axis.tick_labels tick_labels.number_format = '0' tick_labels.font.bold = True tick_labels.font.size = Pt(1) prs.save('chart-01.pptx') pass
def make_bubble_chart_data(ser_count, point_count): """ Return an |BubbleChartData| object populated with *ser_count* series, each having *point_count* data points. """ points = ( (1.1, 11.1, 10.0), (2.1, 12.1, 20.0), (3.1, 13.1, 30.0), (1.2, 11.2, 40.0), (2.2, 12.2, 50.0), (3.2, 13.2, 60.0), ) chart_data = BubbleChartData() for i in range(ser_count): series_label = 'Series %d' % (i+1) series = chart_data.add_series(series_label) for j in range(point_count): point_idx = (i * point_count) + j x, y, size = points[point_idx] series.add_data_point(x, y, size) return chart_data
def when_I_add_a_bubble_chart_having_2_series_of_3_pts(context, bubble_type): chart_type = getattr(XL_CHART_TYPE, bubble_type) data = ( ('Series 1', ((-0.1, 0.5, 1.0), (16.2, 0.0, 2.0), (8.0, -0.2, 3.0))), ('Series 2', ((12.4, 0.8, 4.0), (-7.5, 0.5, 5.0), (5.1, -0.5, 6.0))), ) chart_data = BubbleChartData() for series_data in data: series_label, points = series_data series = chart_data.add_series(series_label) for point in points: x, y, size = point series.add_data_point(x, y, size) context.chart = context.slide.shapes.add_chart(chart_type, Inches(1), Inches(1), Inches(8), Inches(5), chart_data).chart
def when_I_add_a_bubble_chart_having_2_series_of_3_pts(context, bubble_type): chart_type = getattr(XL_CHART_TYPE, bubble_type) data = ( ("Series 1", ((-0.1, 0.5, 1.0), (16.2, 0.0, 2.0), (8.0, -0.2, 3.0))), ("Series 2", ((12.4, 0.8, 4.0), (-7.5, 0.5, 5.0), (5.1, -0.5, 6.0))), ) chart_data = BubbleChartData() for series_data in data: series_label, points = series_data series = chart_data.add_series(series_label) for point in points: x, y, size = point series.add_data_point(x, y, size) context.chart = context.slide.shapes.add_chart( chart_type, Inches(1), Inches(1), Inches(8), Inches(5), chart_data ).chart
def make_bubble_chart_data(ser_count, point_count): """ Return an |BubbleChartData| object populated with *ser_count* series, each having *point_count* data points. """ points = ( (1.1, 11.1, 10.0), (2.1, 12.1, 20.0), (3.1, 13.1, 30.0), (1.2, 11.2, 40.0), (2.2, 12.2, 50.0), (3.2, 13.2, 60.0), ) chart_data = BubbleChartData() for i in range(ser_count): series_label = "Series %d" % (i + 1) series = chart_data.add_series(series_label) for j in range(point_count): point_idx = (i * point_count) + j x, y, size = points[point_idx] series.add_data_point(x, y, size) return chart_data
def df_to_chartdata(df, datatype, number_format=None): ''' 根据给定的图表数据类型生成相应的数据 Chartdata:一般的数据 XyChartData: 散点图数据 BubbleChartData:气泡图数据 ''' if isinstance(df, pd.Series): df = pd.DataFrame(df) df.fillna(0, inplace=True) datatype = datatype.lower() if datatype == 'chartdata': chart_data = ChartData() chart_data.categories = ['%s' % (c) for c in list(df.index)] for col_name in df.columns: chart_data.add_series('%s' % (col_name), list(df[col_name]), number_format) return chart_data if datatype == 'xychartdata': chart_data = XyChartData() if not isinstance(df, list): df = [df] for d in df: series_name = '%s' % (d.columns[0]) + ' vs ' + '%s' % ( d.columns[1]) series_ = chart_data.add_series(series_name) for i in range(len(d)): series_.add_data_point(d.iloc[i, 0], d.iloc[i, 1]) return chart_data if datatype == 'bubblechartdata': chart_data = BubbleChartData() if not isinstance(df, list): df = [df] for d in df: series_name = '%s' % (d.columns[0]) + ' vs ' + '%s' % ( d.columns[1]) series_ = chart_data.add_series(series_name) for i in range(len(d)): series_.add_data_point(d.iloc[i, 0], d.iloc[i, 1], d.iloc[i, 2]) return chart_data
def add_bubble_chart(self, slide, x, y, cx, cy, *series, unit='cm'): ''' Add bubble chart into given slide. Args: slide: Slide Given slide x: x aixs location of chart y: float y aixs location of chart cx: float Total width of chart cy: float Total height of chart series: list List of data series, one of each has 2 element: 'series name', 'point list (x, y, size)' eg: *series = *[('Series 1', [(1.3, 3.6, 6.6), (2.3, 1.5, 12)]), ('Series 2', [(-0.5, 1.6, 15), (0.4, 2.9, 3)])] unit: str Must 'cm' or 'inches' Return: bubble_chart: Bubble chart ''' x, y, cx, cy = self._convert_nums(x, y, cx, cy, unit=unit) chart_data = self._add_series_datapoints(BubbleChartData(), *series) graphic_frame = slide.shapes.add_chart(XL_CHART_TYPE.BUBBLE, x, y, cx, cy, chart_data) return graphic_frame.chart
def given_a_BubbleChartData_object_with_number_format(context, strval): params = {} if strval != 'None': params['number_format'] = int(strval) context.chart_data = BubbleChartData(**params)
def add_series_fixture(self, request, BubbleSeriesData_, series_data_): chart_data = BubbleChartData() name = 'Series Name' return chart_data, name, BubbleSeriesData_, series_data_
from pptx import Presentation from pptx.chart.data import XyChartData, BubbleChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches # create presentation with 1 slide ------ prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[5]) # define chart data --------------------- chart_data = BubbleChartData() series_1 = chart_data.add_series('Series 1') series_1.add_data_point(0.7, 2.7, 10) series_1.add_data_point(1.8, 3.2, 4) series_1.add_data_point(2.6, 0.8, 8) # add chart to slide -------------------- x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) chart = slide.shapes.add_chart(XL_CHART_TYPE.BUBBLE, x, y, cx, cy, chart_data).chart prs.save('BubbleChart.pptx')