def add_Chart(self, data_col_index=2, chart_title='My Chart', x_title="Size", y_title="Date", chart_location='D5'): refObj = openpyxl.chart.Reference(self.sheet, min_col=data_col_index, min_row=1, max_col=data_col_index, max_row=len(self.datas[0])) # data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7) chartObj = chart.LineChart() chartObj.style = 12 chartObj.title = chart_title chartObj.y_axis.title = x_title chartObj.y_axis.crossAx = 500 chartObj.y_axis.crossAx = 100 chartObj.x_axistitle = y_title chartObj.add_data(refObj, titles_from_data=True) dates = openpyxl.chart.Reference(self.sheet, min_col=1, min_row=2, max_row=len(self.datas[0])) chartObj.set_categories(dates) self.sheet.add_chart(chartObj, chart_location)
def make_chart(path): wb = load_workbook(path, data_only=True) ws = wb.active chart_val = chart.LineChart() chart_val.title = "주가_일별_등락패턴" chart_val.x_axis.title = "date" chart_val.y_axis.title = "stock" #data영역임. datas = chart.Reference(ws, min_col=1, min_row=2, max_col=ws.max_column, max_row=ws.max_row) chart_val.add_data(datas, from_rows=True, titles_from_data=True) #카테고리영역임. min_col,min_row는 시작범위 cell번호이고, max_col,max_row는 최대 범위 cell번호 이다. min_col = 2이므로 2열부터 시작이고, min_row = 1라서 1행부터 시작, max_col = 21 21열까지이고, max_row = 1 1행까지임. cats = chart.Reference(ws, min_col=2, min_row=1, max_col=21, max_row=1) chart_val.set_categories(cats) #어느 cell에 차트를 그릴건지 정함. ws.add_chart(chart_val, "A17") wb.save("./차트결과.xlsx") wb.close()
def create_chart(self): """Class method to accept all given chart parameters and generate a simple chart to the intended sheet and cell.""" try: chart_type = self.spin.get() if chart_type == "Line": self.chart = opc.LineChart() # Creates a line chart elif chart_type == "Bar": self.chart = opc.BarChart() # Creates a bar chart elif chart_type == "Area": self.chart = opc.AreaChart() # Creates an area chart elif chart_type == "Pie": self.chart = opc.PieChart() # Creates a pie chart else: self.chart = opc.DoughnutChart() # Creates a doughnut chart self.chart.title = self.chart_entry.get() # Sets the chart title self.chart.y_axis.title = self.y_axis.get( ) # Sets the y axis title self.chart.x_axis.title = self.x_axis.get( ) # Sets the x axis title destination_cell = self.destination_entry.get( ) # Sets the cell which the chart will be anchored chart_range = self.ws + "!" + self.range.get() data = opc.Reference(self.current_sheet, range_string=chart_range) self.chart.add_data(data) # Adds data from range to chart self.current_sheet.add_chart( self.chart, destination_cell) # Add the chart to the desired cell self.wb.save(self.edit_path) # Saves the file except: pass
sheet1['A2'] = "Groups C and D" # Unmerge sheet1.unmerge_cells('A2:A10') # Freeze Panes sheet1.freeze_panes = 'A2' # Freeze Row 1 sheet1.freeze_panes = 'B1' # Freeze Column A sheet1.freeze_panes = 'D5' # Freeze Cols A to C and Rows 1 to 4 sheet1.freeze_panes = 'A1' # Unfreeze all sheet1.freeze_panes = None # Unfreeze all # Charting for i in range(1, 11): # Create Data in Col L sheet1['L' + str(i)] = random.randint(1, 11) theRef = xlct.Reference(sheet1, min_col=12, min_row=1, max_col=12, max_row=10) theSeries = xlct.Series(theRef, title='Random Values') theChart = xlct.BarChart() theChart.title = 'Bar Chart' theChart.append(theSeries) sheet1.add_chart(theChart, 'M14') theChart = xlct.LineChart() theChart.title = 'Line Chart' theChart.append(theSeries) sheet1.add_chart(theChart, 'M30') workBook.save('sampleChart.xlsx')
cell = sheet.cell(row=i, column=1) cell.value = str(cell_to_datetime(cell)) ws1['B2'] = '0:00:00' dates = chart.Reference(ws1, min_col=2, min_row=2, max_row=sheet.max_row) vBat = chart.Reference(ws2, min_col=2, min_row=1, max_col=2, max_row=sheet.max_row) qBat = chart.Reference(ws2, min_col=3, min_row=1, max_col=3, max_row=sheet.max_row) c1 = chart.LineChart() c1.title = "SLA Discharge - 5.5A: V_BAT and Q_Count" c1.height = 10 # chart dimensions c1.width = 20 c1.x_axis.majorTimeUnit = "days" c1.x_axis = chart.axis.DateAxis() c1.x_axis.title = "Time" c1.x_axis.crosses = "min" c1.x_axis.majorTickMark = "out" c1.x_axis.number_format = 'd-HH-MM-SS' c1.add_data(vBat, titles_from_data=True) c1.set_categories(dates) c1.y_axis.title = "Battery Voltage" c1.y_axis.scaling.min = 10000 c1.y_axis.scaling.max = 14500 c1.y_axis.crossAx = 500