"""
openpyxl_chart_type_demos.py
(openpyxl四:图表类型)
使用:
图表类型:
#AreaChart面积图表、BarChartm条形图/柱形图/成交量图、BubbleChart气泡图、
#LineChart折线图/LineChart3D折线图、
#ScatterChart散点图、PieChart饼图/PieChart3D饼图/ProjectedPieChart投影饼图、
#StockChart股票图;
"""
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_type_demos_case1.xlsx'
ws_AreaChart2D=wb.create_sheet('AreaChart面积图表2D')
rows=[
['Number','Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
[6, 25, 5],
[7, 50, 10]]
for row in rows:
ws_AreaChart2D.append(row)
chart=AreaChart()
chart.title='Area Chart'
chart.style=30
chart.x_axis.title='Test'
chart.y_axis.title='Percentage'
labels=Reference(ws_AreaChart2D, min_col=1, min_row=1, max_row=2)
data=Reference(ws_AreaChart2D, min_col=2, min_row=1, max_col=3,max_row=7)
chart.set_categories(labels)
chart.add_data(data, titles_from_data=True)
ws_AreaChart2D.add_chart(chart,'A10')
ws_AreaChart3D=wb.create_sheet('AreaChart面积图表3D')
for row in rows:
ws_AreaChart3D.append(row)
chart=AreaChart3D()
chart.title='Area Chart'
chart.style=13
chart.x_axis.title='Test'
chart.y_axis.title='Percentage'
chart.legend=None
labels=Reference(ws_AreaChart3D, min_col=1, min_row=1, max_row=7)
data=Reference(ws_AreaChart3D, min_col=2, min_row=1, max_col=3, max_row=7)
chart.set_categories(labels)
chart.add_data(data, titles_from_data=True)
ws_AreaChart3D.add_chart(chart,'A10')
ws_BarChart=wb.create_sheet('BarChart条形图和柱形图',index=0)
rows=[
('Number', 'Batch1', 'Batch2'),
(2, 10, 30),
(3, 40, 60),
(4, 50, 70),
(5, 20, 10),
(6, 10, 40),
(7, 50, 30)]
for row in rows:
ws_BarChart.append(row)
chart1=BarChart()
chart1.title='Bar Chart'
chart1.style=10
chart1.type='col'
chart1.x_axis.title='Test Number'
chart1.y_axis.title='Sample length(mm)'
labels=Reference(ws_BarChart, min_col=1, min_row=2, max_row=7)
data=Reference(ws_BarChart, min_col=2, min_row=1, max_row=7, max_col=3)
chart1.set_categories(labels)
chart1.add_data(data,titles_from_data=True)
chart1.shape=4
ws_BarChart.add_chart(chart1, 'A10')
chart2=deepcopy(chart1)
chart2.title='Horizontal Bar Chart'
chart2.style=11
chart2.type='bar'
ws_BarChart.add_chart(chart2, 'I10')
chart3=deepcopy(chart1)
chart3.title='Stacked Chart'
chart3.style=12
chart3.type='col'
chart3.grouping='stacked'
chart3.overlap=100
ws_BarChart.add_chart(chart3, 'A27')
chart4=deepcopy(chart1)
chart4.title='Percent Stacked Chart'
chart4.style=13
chart4.type='bar'
chart4.grouping='percentStacked'
chart4.overlap=100
ws_BarChart.add_chart(chart4, 'I27')
ws_BubbleChart=wb.create_sheet('BubbleChart气泡图')
rows=[
('Number of Products','Sales in USD', 'Market share'),
(14, 12200, 15),
(20, 60000, 33),
(18, 24400, 10),
(22, 32000, 42),
(),
(12, 8200, 18),
(15, 50000, 30),
(19, 22400, 15),
(25, 25000, 50)]
for row in rows:
ws_BubbleChart.append(row)
chart=BubbleChart()
chart.style=18
xvalues=Reference(ws_BubbleChart, min_col=1, min_row=2, max_row=5)
yvalues=Reference(ws_BubbleChart, min_col=2, min_row=2, max_row=5)
size=Reference(ws_BubbleChart, min_col=3, min_row=2, max_row=5)
series=Series(values=yvalues, xvalues=xvalues, zvalues=size, title='2013')
chart.series.append(series)
xvalues=Reference(ws_BubbleChart, min_col=1, min_row=7, max_row=10)
yvalues=Reference(ws_BubbleChart, min_col=2, min_row=7, max_row=10)
size=Reference(ws_BubbleChart, min_col=3, min_row=7, max_row=10)
series=Series(values=yvalues, xvalues=xvalues, zvalues=size, title='2014')
chart.series.append(series)
ws_BubbleChart.add_chart(chart, 'E1')
from datetime import date
ws_LineChart=wb.create_sheet('LineChart折线图')
rows=[
['Data', 'Batch1', 'Batch2', 'Batch3'],
[date(2015,9,1), 40, 30, 25],
[date(2015,9,2), 40, 25, 30],
[date(2015,9,3), 50, 30, 45],
[date(2015,9,4), 30, 25, 40],
[date(2015,9,5), 25, 35, 30],
[date(2015,9,6), 20, 40, 35]]
for row in rows:
ws_LineChart.append(row)
c1=LineChart()
c1.title='Line Chart'
c1.style=13
c1.x_axis.title='Test Number'
c1.y_axis.title='Size'
data=Reference(ws_LineChart, min_col=2, max_col=4, min_row=1, max_row=7)
c1.add_data(data, titles_from_data=True)
s1=c1.series[0]
s1.marker.symbol='triangle'
s1.marker.graphicalProperties.solidFill='FF0000'
s1.marker.graphicalProperties.line.solidFill='FF0000'
s1.graphicalProperties.line.noFill=True
s2=c1.series[1]
s2.graphicalProperties.line.solifFill='00AAAA'
s2.graphicalProperties.line.dashStyle='sysDot'
s2.graphicalProperties.line.width=100050
s3=c1.series[2]
s3.smooth=True
ws_LineChart.add_chart(c1, 'A10')
stacked=deepcopy(c1)
stacked.grouping='stacked'
stacked.title='Stacked Line Chart'
ws_LineChart.add_chart(stacked, 'A27')
percent_stacked=deepcopy(c1)
percent_stacked.grouping='percentStacked'
percent_stacked.title='Percent Stacked Line Chart'
ws_LineChart.add_chart(percent_stacked, 'A44')
c4=LineChart()
c4.title='Date Axis'
c4.style=12
c4.y_axis.title='Size'
c4.y_axis.crossAx=500
c4.x_axis.title='Date'
c4.x_axis=DateAxis(crossAx=100)
c4.x_axis.number_format='d-mmm'
c4.x_axis.majorTimeUnit='days'
c4.add_data(data, titles_from_data=True)
dates=Reference(ws_LineChart, min_col=1, min_row=2, max_row=7)
c4.set_categories(dates)
ws_LineChart.add_chart(c4, 'A61')
ws_LineChart3D=wb.create_sheet('LineChart3D')
for row in rows:
ws_LineChart3D.append(row)
c1=LineChart3D()
c1.title='3D Line Chart'
c1.legend=None
c1.style=15
c1.y_axis.title='Size'
c1.x_axis.title='Test Number'
data=Reference(ws_LineChart3D, min_col=2, max_col=4, min_row=1, max_row=7)
c1.add_data(data, titles_from_data=True)
ws_LineChart3D.add_chart(c1, 'A10')
ws_ScatterChart=wb.create_sheet('ScatterChart散点图')
rows=[
['Size', 'Batch 1', 'Batch 3'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 25],
[6, 25, 35],
[7, 20, 40]]
for row in rows:
ws_ScatterChart.append(row)
chart=ScatterChart()
chart.title='Scatter Chart'
chart.style=13
chart.x_axis.title='Size'
chart.y_axis.title='Percentage'
xvalues=Reference(ws_ScatterChart, min_col=1, min_row=2, max_row=7)
for i in range(2,4):
values=Reference(ws_ScatterChart, min_col=i, min_row=1, max_row=7)
series=Series(values, xvalues, title_from_data=True)
chart.series.append(series)
ws_ScatterChart.add_chart(chart, 'A10')
ws_PieChart=wb.create_sheet('PieChart饼图')
data=[
['Pie', 'Sold'],
['Apple', 50],
['Cherry', 30],
['Pumpkin', 10],
['Chocolate', 40]]
for row in data:
ws_PieChart.append(row)
pie=PieChart()
labels=Reference(ws_PieChart, min_col=1, min_row=2, max_row=5)
data=Reference(ws_PieChart, min_col=2, min_row=1, max_row=5)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title='Pies sold by category'
slice=DataPoint(idx=0, explosion=20)
pie.series[0].data_points=[slice]
ws_PieChart.add_chart(pie, 'D1')
pie = PieChart3D()
labels = Reference(ws_PieChart, min_col=1, min_row=2, max_row=5)
data = Reference(ws_PieChart, min_col=2, min_row=1, max_row=5)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws_PieChart.add_chart(pie, "D20")
ws_ProjectedPieChart=wb.create_sheet('ProjectedPieChart投影饼图')
data=[
['Page', 'Views'],
['Search', 95],
['Products', 4],
['Offers', 0.5],
['Sales', 0.5]]
for row in data:
ws_ProjectedPieChart.append(row)
projected_pie=ProjectedPieChart()
projected_pie.type='pie'
projected_pie.splitType='val'
labels=Reference(ws_ProjectedPieChart, min_col=1, min_row=2, max_row=5)
data=Reference(ws_ProjectedPieChart, min_col=2, min_row=1, max_row=5)
projected_pie.add_data(data, titles_from_data=True)
projected_pie.set_categories(labels)
ws_ProjectedPieChart.add_chart(projected_pie, 'A10')
projected_bar=deepcopy(projected_pie)
projected_bar.type='bar'
projected_bar.splitType='pos'
ws_ProjectedPieChart.add_chart(projected_bar, 'A27')
from openpyxl import Workbook
from openpyxl.chart import (StockChart,
BarChart,
Reference,
Series
)
from openpyxl.chart.axis import (DateAxis,
ChartLines
)
from openpyxl.chart.updown_bars import UpDownBars
ws_StockChart=wb.create_sheet('StcokChart股票图',index=0)
rows=[
['Date', 'Valume', 'Open', 'High', 'Low', 'Close'],
['2015-01-01', 20000, 26.2, 27.2, 23.49, 25.45],
['2015-01-02', 10000, 25.45, 25.03, 19.55, 23.05],
['2015-01-03', 15000, 25.45, 25.03, 19.55, 23.05],
['2015-01-04', 2000, 22.42, 23.97, 20.07, 21.90],
['2015-01-05', 12000, 21.9, 23.65, 19.50, 21.51]]
for row in rows:
ws_StockChart.append(row)
c1=StockChart()
c1.title='High-low-close'
c1.hiLowLines=ChartLines()
labels=Reference(ws_StockChart, min_col=1, min_row=2, max_row=6)
data=Reference(ws_StockChart, min_col=4, max_col=6, min_row=1, max_row=6)
c1.add_data(data, titles_from_data= True)
c1.set_categories(labels)
for s in c1.series:
s.graphicalProperties.line.noFill= True
s.marker.symbol='dot'
s.marker.size=5
from openpyxl.chart.data_source import (NumData,
NumVal)
pts=[NumVal(idx=i) for i in range(len(data) -1)]
cache=NumData(pt=pts)
c1.series[-1].val.numRef.numCache = cache
ws_StockChart.add_chart(c1, 'A10')
c2=StockChart()
c2.title='Open-hight-low-close'
c2.hiLowLines=ChartLines()
c2.upDownBars=UpDownBars()
labels=Reference(ws_StockChart, min_col=1, min_row=2, max_row=6)
data=Reference(ws_StockChart, min_col=3, max_col=6, min_row=1, max_row=6)
c2.add_data(data, titles_from_data=True)
c2.set_categories(labels)
for s in c2.series:
s.graphicalProperties.line.noFill = True
from openpyxl.chart.data_source import (NumData,
NumVal)
pts=[NumVal(idx=i) for i in range(len(data) -1)]
cache=NumData(pt=pts)
c2.series[-1].val.numRef.numCache = cache
ws_StockChart.add_chart(c2, 'I10')
bar=BarChart()
data = Reference(ws_StockChart, min_col=2, min_row=1, max_row=6)
bar.add_data(data, titles_from_data=True)
bar.set_categories(labels)
b1=deepcopy(bar)
c3=deepcopy(c1)
c3.y_axis.majorGridlines = None
c3.y_axis.title='Price'
b1.y_axis.axId=20
b1.z_axis = c3.y_axis
b1.y_axis.crosses = 'max'
b1 += c3
c3.title='High low close volume'
ws_StockChart.add_chart(b1, 'A27')
b2 = deepcopy(bar)
c4 = deepcopy(c2)
c4.y_axis.majorGridlines = None
c4.y_axis.title = 'Price'
b2.y_axis.axId = 20
b2.z_axis = c4.y_axis
b2.y_axis.crosses = 'max'
b2 += c4
ws_StockChart.add_chart(b2, 'I27')
wb.save(wb_filename)