"""
openpyxl_chart_demos.py
(openpyxl三:图表相关操作:创建图表、使用轴、图表布局)
使用:
创建图表:
openpyxl.chart.Reference(), Chart.add_data(), Worksheet.add_chart();
使用轴:
Chart.title / style / x_axis.title / y_axis.title,
Chart.x_axis.scaling.min,
Chart.x_axis.Scaling.max,
openpyxl.chart.Series(), Chart.series.appedn(Series), Chart.append(Series),
Chart.x_axis.scaling.logBase
Chart.x_axis.scaling.orientation
Chart.y_axis.crosses, Chart.y_axis.majorGridlines
Chart1 + Chart2
图表布局:
openpyxl.chart.layout.Layout(): 为手动布局对象ManualLayout(),
openpyxl.chart.layout.ManualLayout():为Lyaout()类的参数,
Chart.layout,
Chart.legend.position;
"""
import openpyxl
from openpyxl import Workbook, load_workbook
from openpyxl.chart import (AreaChart,
AreaChart3D,
BarChart,
BubbleChart,
LineChart,
LineChart3D,
ScatterChart,
PieChart,
PieChart3D,
ProjectedPieChart,
StockChart,
Reference,
Series)
from copy import deepcopy
from openpyxl.chart.axis import DateAxis
from openpyxl.chart.series import DataPoint
from openpyxl.chart.layout import (Layout,
ManualLayout
)
wb = Workbook()
wb_filename = r'openpyxl_cases\openpyxl_chart_demos_case1.xlsx'
ws_cjtb = wb.create_sheet('创建图表', index=0)
for i in range(10):
ws_cjtb.append([i])
chart = BarChart()
values = Reference(ws_cjtb, min_col=1, min_row=1, max_col=1, max_row=10)
chart.add_data(values)
ws_cjtb.add_chart(chart)
ws_zxzxdz = wb.create_sheet('轴线最小大值', index=0)
ws_zxzxdz.append(['X', '1/X'])
for x in range(-10, 11):
if x:
ws_zxzxdz.append([x, 1.0 / x])
chart1 = ScatterChart()
chart1.title = "Full Axes"
chart1.x_axis.title = 'x'
chart1.y_axis.title = '1/x'
chart1.legend = None
chart2 = ScatterChart()
chart2.title = "Clipped Axes"
chart2.x_axis.title = 'x'
chart2.y_axis.title = '1/x'
chart2.legend = None
chart2.x_axis.scaling.min = 0
chart2.y_axis.scaling.min = 0
chart2.x_axis.scaling.max = 11
chart2.y_axis.scaling.max = 1.5
x = Reference(ws_zxzxdz, min_col=1, min_row=2, max_row=22)
y = Reference(ws_zxzxdz, min_col=2, min_row=2, max_row=22)
s = Series(y, xvalues=x)
chart1.series.append(s)
chart2.series.append(s)
ws_zxzxdz.add_chart(chart1, "C1")
ws_zxzxdz.add_chart(chart2, "C20")
import math
ws_zxbsxs = wb.create_sheet('轴线倍数显示', index=0)
ws_zxbsxs.append(['X', 'Gaussian'])
for i, x in enumerate(range(-10, 11)):
ws_zxbsxs.append([x, '=EXP(-(($A${row}/6)^2))'.format(row= i+2)])
chart1 = ScatterChart()
chart1.title = 'No Scaling'
chart1.x_axis.title = 'X'
chart1.y_axis.title = 'Y'
chart1.legend = None
chart2 = ScatterChart()
chart2.title = 'X Log Scale'
chart2.x_axis.title = 'X (log10)'
chart2.y_axis.title = 'Y'
chart2.legend = None
chart2.x_axis.scaling.logBase = 2
chart3 = ScatterChart()
chart3.title = 'Y Log Scale'
chart3.x_axis.title = 'X'
chart3.y_axis.title = 'Y (log10)'
chart3.legend = None
chart3.y_axis.scaling.logBase = 10
chart4 = ScatterChart()
chart4.title = 'Both Log Scale'
chart4.x_axis.title = 'X (log10)'
chart4.y_axis.title = 'Y (log10)'
chart4.legend = None
chart4.x_axis.scaling.logBase = 10
chart4.y_axis.scaling.logBase = 10
chart5 = ScatterChart()
chart5.title = 'Log Scale Base e'
chart5.x_axis.title = 'X (ln)'
chart5.y_axis.title = 'Y (ln)'
chart5.legend = None
chart5.x_axis.scaling.logBase = math.e
chart5.y_axis.scaling.logBase = math.e
x = Reference(ws_zxbsxs, min_col=1, min_row=2, max_row=22)
y = Reference(ws_zxbsxs, min_col=2, min_row=2, max_row=22)
s = Series(y, xvalues=x)
chart1.append(s)
chart2.append(s)
chart3.append(s)
chart4.append(s)
chart5.append(s)
ws_zxbsxs.add_chart(chart1,'C1')
ws_zxbsxs.add_chart(chart2,'K1')
ws_zxbsxs.add_chart(chart3,'C18')
ws_zxbsxs.add_chart(chart4,'K18')
ws_zxbsxs.add_chart(chart5,'F35')
ws_zxfx = wb.create_sheet('轴线方向', 0)
ws_zxfx['A1'] = 'Archimedean Spiral'
ws_zxfx.append(['T', 'X', 'Y'])
for i, t in enumerate(range(100)):
ws_zxfx.append([t/16.0, '=$A${row}*COS($A${row})'.format(row= i+3),
'=$A${row}*SIN($A${row})'.format(row= i+3)])
chart1 = ScatterChart()
chart1.title = 'Default Orientation'
chart1.x_axis.title = 'X'
chart1.y_axis.title = 'Y'
chart1.legend = None
chart2 = ScatterChart()
chart2.title = 'Flip X'
chart2.x_axis.title = 'X'
chart2.y_axis.title = 'Y'
chart2.legend = None
chart2.x_axis.scaling.orientation = 'maxMin'
chart2.y_axis.scaling.orientation = 'minMax'
chart3 = ScatterChart()
chart3.title = 'Flip Y'
chart3.x_axis.title = 'X'
chart3.y_axis.title = 'Y'
chart3.legend = None
chart3.x_axis.scaling.orientation = 'minMax'
chart3.y_axis.scaling.orientation = 'maxMin'
chart4 = ScatterChart()
chart4.title = 'Flip Both'
chart4.x_axis.title = 'X'
chart4.y_axis.title = 'Y'
chart4.legend = None
chart4.x_axis.scaling.orientation = 'maxMin'
chart4.y_axis.scaling.orientation = 'maxMin'
x = Reference(ws_zxfx, min_col=2, min_row=2, max_row=102)
y = Reference(ws_zxfx, min_col=3, min_row=2, max_row=102)
s = Series(y, xvalues=x)
chart1.append(s)
chart2.append(s)
chart3.append(s)
chart4.append(s)
ws_zxfx.add_chart(chart1, 'D1')
ws_zxfx.add_chart(chart2, 'L1')
ws_zxfx.add_chart(chart3, 'D18')
ws_zxfx.add_chart(chart4, 'L18')
ws_tjzx = wb.create_sheet('添加轴线', 0)
rows = [
['Aliens', 2, 3, 4, 5, 6, 7],
['Humans', 10, 40, 50, 20, 10, 50]
]
for row in rows:
ws_tjzx.append(row)
c1 = BarChart()
c1.title = 'Survey results'
c1.x_axis.title = '1 Days'
c1.y_axis.title = '1 Aliens'
c1.y_axis.majorGridlines = None
v1 = Reference(ws_tjzx, min_col=1, max_col=7, min_row=1)
c1.add_data(v1, titles_from_data=True, from_rows=True)
c2 = LineChart()
c2.y_axis.title = '2 Humans'
c2.y_axis.axId = 200
v2 = Reference(ws_tjzx, min_col=1, max_col=7, min_row=2)
c2.add_data(v2, titles_from_data=True, from_rows=True)
c1.y_axis.crosses = 'max'
c1 += c2
ws_tjzx.add_chart(c1, 'D4')
ws_tbbj = wb.create_sheet('图表布局', 0)
rows = [
['Size', 'Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 25],
[6, 25, 35],
[7, 20, 40]
]
for row in rows:
ws_tbbj.append(row)
ch1 = ScatterChart()
xvalues = Reference(ws_tbbj, min_col=1, min_row=2, max_row=7)
for i in range(2,4):
values = Reference(ws_tbbj, min_col=i, min_row=1, max_row=7)
series = Series(values, xvalues, title_from_data=True)
ch1.series.append(series)
ch1.title = 'Default layout'
ch1.style = 13
ch1.x_axis.title = 'Size'
ch1.y_axis.title = 'Percentage'
ch1.legend.position = 'r'
ws_tbbj.add_chart(ch1, 'B10')
ch2 = deepcopy(ch1)
ch2.title = 'Manual chart layout'
ch2.legend.position = 'tr'
ch2.layout = Layout(manualLayout = ManualLayout(x=0.25, y=0.25,
h=0.5, w=0.5
))
ws_tbbj.add_chart(ch2, 'K10')
ch3 = deepcopy(ch1)
ch3.title = 'Manual chart layou, edge mode'
ch3.layout = Layout(ManualLayout(x=0.25, y=0.25,
h=0.5, w=0.5,
xMode='edge',
yMode='edge'
))
ws_tbbj.add_chart(ch3, 'B27')
ch4 = deepcopy(ch1)
ch4.title = 'Manual legend layout'
ch4.legend.layout = Layout(manualLayout = ManualLayout(x=0, y=0.9,
h=0.1, w=0.5,
xMode='edge',
yMode='edge'
))
ws_tbbj.add_chart(ch4, 'K27')
wb.save(wb_filename)