使用 XlsxWriter 的仪表图?
A gauge chart using XlsxWriter?
我试图找到基于 XlsxWriter 的仪表图,但我在网上找不到任何东西。我不想重新发明轮子。有人已经构建了这个图表或知道我在哪里可以找到 python 脚本吗?
下面是一个示例,说明如何基于此 tutorial 通过组合圆环图和饼图来创建仪表图。
注意,这需要 XlsxWriter >= 1.0.8:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_gauge.xlsx')
worksheet = workbook.add_worksheet()
chart_doughnut = workbook.add_chart({'type': 'doughnut'})
chart_pie = workbook.add_chart({'type': 'pie'})
# Add some data for the Doughnut and Pie charts. This is set up so the
# gauge goes from 0-100. It is initially set at 75%.
worksheet.write_column('H2', ['Donut', 25, 50, 25, 100])
worksheet.write_column('I2', ['Pie', 75, 1, '=200-I4-I3'])
# Configure the doughnut chart as the background for the gauge.
chart_doughnut.add_series({
'name': '=Sheet1!$H',
'values': '=Sheet1!$H:$H',
'points': [
{'fill': {'color': 'green'}},
{'fill': {'color': 'yellow'}},
{'fill': {'color': 'red'}},
{'fill': {'none': True}}],
})
# Rotate chart so the gauge parts are above the horizontal.
chart_doughnut.set_rotation(270)
# Turn off the chart legend.
chart_doughnut.set_legend({'none': True})
# Turn off the chart fill and border.
chart_doughnut.set_chartarea({
'border': {'none': True},
'fill': {'none': True},
})
# Configure the pie chart as the needle for the gauge.
chart_pie.add_series({
'name': '=Sheet1!$I',
'values': '=Sheet1!$I:$I',
'points': [
{'fill': {'none': True}},
{'fill': {'color': 'black'}},
{'fill': {'none': True}}],
})
# Rotate the pie chart/needle to align with the doughnut/gauge.
chart_pie.set_rotation(270)
# Combine the pie and doughnut charts.
chart_doughnut.combine(chart_pie)
# Insert the chart into the worksheet.
worksheet.insert_chart('A1', chart_doughnut)
workbook.close()
输出:
我试图找到基于 XlsxWriter 的仪表图,但我在网上找不到任何东西。我不想重新发明轮子。有人已经构建了这个图表或知道我在哪里可以找到 python 脚本吗?
下面是一个示例,说明如何基于此 tutorial 通过组合圆环图和饼图来创建仪表图。
注意,这需要 XlsxWriter >= 1.0.8:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_gauge.xlsx')
worksheet = workbook.add_worksheet()
chart_doughnut = workbook.add_chart({'type': 'doughnut'})
chart_pie = workbook.add_chart({'type': 'pie'})
# Add some data for the Doughnut and Pie charts. This is set up so the
# gauge goes from 0-100. It is initially set at 75%.
worksheet.write_column('H2', ['Donut', 25, 50, 25, 100])
worksheet.write_column('I2', ['Pie', 75, 1, '=200-I4-I3'])
# Configure the doughnut chart as the background for the gauge.
chart_doughnut.add_series({
'name': '=Sheet1!$H',
'values': '=Sheet1!$H:$H',
'points': [
{'fill': {'color': 'green'}},
{'fill': {'color': 'yellow'}},
{'fill': {'color': 'red'}},
{'fill': {'none': True}}],
})
# Rotate chart so the gauge parts are above the horizontal.
chart_doughnut.set_rotation(270)
# Turn off the chart legend.
chart_doughnut.set_legend({'none': True})
# Turn off the chart fill and border.
chart_doughnut.set_chartarea({
'border': {'none': True},
'fill': {'none': True},
})
# Configure the pie chart as the needle for the gauge.
chart_pie.add_series({
'name': '=Sheet1!$I',
'values': '=Sheet1!$I:$I',
'points': [
{'fill': {'none': True}},
{'fill': {'color': 'black'}},
{'fill': {'none': True}}],
})
# Rotate the pie chart/needle to align with the doughnut/gauge.
chart_pie.set_rotation(270)
# Combine the pie and doughnut charts.
chart_doughnut.combine(chart_pie)
# Insert the chart into the worksheet.
worksheet.insert_chart('A1', chart_doughnut)
workbook.close()
输出: