ScatterChart implementation
Created originally on Bitbucket by hjunes (Heikki Junes)
Attached is an implementation of ScatterChart. The implementation is based on the earlier implementation of LineChart.
Below is an example how the implementation of ScatterChart can be used:
{{{ #!python #!/usr/bin/python
-- coding: iso-8859-1 --
import random
from openpyxl.workbook import Workbook from openpyxl.writer.excel import save_workbook from openpyxl.chart import ScatterChart, Serie, Reference
def test_scatterchart_write(): wb = Workbook() ws = wb.get_active_sheet() ws.title = u'scatterdata'
ws.cell(row=0, column=1).value = 'Scatter 1'
ws.cell(row=0, column=2).value = 'Scatter 2'
for i in range(1,12):
ws.cell(row=i, column=0).value = (i-1) / 10.
ws.cell(row=i, column=1).value = random.random()
ws.cell(row=i, column=2).value = random.random()
xyserie1 = Serie( Reference(ws, (1,1), (11,1)),
xvalues=Reference(ws, (1,0), (11,0)),
legend=Reference(ws, (0,1)))
xyserie2 = Serie( Reference(ws, (2,1), (11,1)),
xvalues=Reference(ws, (1,0), (11,0)),
legend=Reference(ws, (0,2)))
scatterchart = ScatterChart()
scatterchart.add_serie(xyserie1)
scatterchart.add_serie(xyserie2)
scatterchart.x_axis.min = 0
scatterchart.x_axis.max = 1
scatterchart.x_axis.unit = 0.2
scatterchart.y_axis.min = 0
scatterchart.y_axis.max = 1
scatterchart.y_axis.unit = 0.2
# chart container dimensions in pixels
scatterchart.drawing.left = 200
scatterchart.drawing.top = 20
scatterchart.drawing.height = 200
scatterchart.drawing.width = 500
# chart area in percentage of the container
scatterchart.width = .7
scatterchart.height = .7
scatterchart.margin_top = .2
ws.add_chart(scatterchart)
wb.save(r'chart.xlsx')
}}}
Best regards, Heikki
Attachments: openpyxl_rev326_to_tip.hgopenpyxl_rev326_to_tip_B.hg