●python(3.8.2)コード
import openpyxl
from openpyxl.chart import BarChart, LineChart, Reference
from openpyxl.chart.plotarea import DataTable
wb = openpyxl.load_workbook("./sample.xlsx")
sh = wb['list']
labels = Reference(sh, min_col=1, max_col=1, min_row=3, max_row=sh.max_row)
data1 = Reference(sh, min_col=2, max_col=6, min_row=2, max_row=sh.max_row)
data2 = Reference(sh, min_col=5, max_col=6, min_row=2, max_row=sh.max_row)
chart = BarChart()
chart.type = "col"
chart.grouping = "stacked"
chart.overlap = 100
chart.title = "メインタイトル"
chart.y_axis.title = "数量"
chart.legend.position = 'b'
chart.add_data(data1, titles_from_data=True)
chart.set_categories(labels)
chart2 = LineChart()
chart2.add_data(data2, titles_from_data=True)
chart.height = 10
chart.width = 15
datatable
Python(3.8.2)コード
chart.plot_area.dTable = DataTable()
chart.plot_area.dTable.showHorzBorder = True
chart.plot_area.dTable.showVertBorder = True
chart.plot_area.dTable.showOutline = True
chart.plot_area.dTable.showKeys = True
import openpyxl
from openpyxl.chart import BarChart, LineChart, Reference
from openpyxl.chart.plotarea import DataTable
wb = openpyxl.load_workbook("./sample.xlsx")
sh = wb['list']
labels = Reference(sh, min_col=1, max_col=1, min_row=3, max_row=sh.max_row)
data1 = Reference(sh, min_col=2, max_col=6, min_row=2, max_row=sh.max_row)
data2 = Reference(sh, min_col=5, max_col=6, min_row=2, max_row=sh.max_row)
chart = BarChart()
chart.type = "col"
chart.grouping = "stacked"
chart.overlap = 100
chart.title = "メインタイトル"
chart.y_axis.title = "数量"
chart.legend.position = 'b'
chart.add_data(data1, titles_from_data=True)
chart.set_categories(labels)
chart2 = LineChart()
chart2.add_data(data2, titles_from_data=True)
chart.height = 10
chart.width = 15
# datatable
chart.plot_area.dTable = DataTable()
chart.plot_area.dTable.showHorzBorder = True
chart.plot_area.dTable.showVertBorder = True
chart.plot_area.dTable.showOutline = True
chart.plot_area.dTable.showKeys = True
sh.add_chart(chart, "I2")
sh.add_chart(chart2, "I20")
wb.save("./sample.xlsx")
sh.add_chart(chart, "I2")
sh.add_chart(chart2, "I20")
グラフ作成時の入力データ
wb.save("./sample.xlsx")
title
date 100-10 200-10 300-10 gokei base
9/16 15 48 41 104 50
9/17 15 48 46 109 50
9/18 15 50 50 115 50
9/19 15 50 49 114 50
9/20 15 49 49 113 50
●グラフ作成時の入力データ
title
date 100-10 200-10 300-10 gokei base
9/16 15 48 41 104 50
9/17 15 48 46 109 50
9/18 15 50 50 115 50
9/19 15 50 49 114 50
9/20 15 49 49 113 50
※※"100-10","200-10","300-10"を積上げ棒グラフ
※"gokei","base"を折れ線グラフ
※最終的には"gokei"の折れ線グラフを非表示にし、データラベルの設定を実施したい
●確認したこと
1.グラフ単体で作成できることを確認
2.グラフが複数作成できることを確認
確認したこと
- グラフ単体で作成できることを確認
- グラフが複数作成できることを確認