【问题标题】:Creating complex Area Chart using openpyxl (transparency)使用 openpyxl(透明度)创建复杂的面积图
【发布时间】:2021-03-24 23:34:09
【问题描述】:

我正在尝试使用 openpyxl 创建面积图。我希望面积图看起来比基本的 Excel 图表更复杂。这是我的数据

数据

month   group1  group2
jan     15      13
feb     19      15
mar     20      17
apr     25      24
may     29      30
jun     24      30
jul     16      40
aug     20      24
sep     22      20
oct     27      17
nov     17      26
dec     21      29

想要的

在做

from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference, Series, ScatterChart, LineChart, StockChart, AreaChart, AreaChart3D



path = "C:/Users/thud/wb1.xlsx"
wb_obj = load_workbook(path)
sheet_obj = wb_obj.active



c1 = AreaChart()
c1.title = "Area Chart"
c1.y_axis.title = "Cost"
c1.x_axis.title = "Date"
c1.x_axis.number_format = 'mm/dd/yy'
c1.x_axis.majorTimeUnit = "days"
c1 = deepcopy(c1)
c1.style = 39


data = Reference(sheet_obj, min_col=2, min_row=2, max_col=3, max_row=13)
c1.add_data(data, titles_from_data=True)

dates = Reference(sheet_obj, min_col=1, min_row=2, max_row=13)
c1.set_categories(dates)


sheet_obj.add_chart(c1, "I2")
wb_obj.save("sample.xlsx")

我在文档中找到了这个,但不确定如何实现:

     openpyxl.drawing.effect.AlphaBiLevelEffect

我想为面积图增加透明度并让它反映我想要的输出。 任何建议表示赞赏

【问题讨论】:

  • 我通过以下操作使透明度工作:stackoverflow.com/a/51006976/15161640,但我无法让 gradientFillProperties 工作。
  • @MarkH 您能否发布您的答案/回复。透明度是目标,不一定是梯度。

标签: python openpyxl


【解决方案1】:

透明度工作,代码中的源代码链接:

from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference, Series, ScatterChart, LineChart, StockChart, AreaChart, AreaChart3D
from copy import deepcopy

path = "wb1.xlsx"
wb_obj = load_workbook(path)
sheet_obj = wb_obj.active

c1 = AreaChart()
c1.title = "Area Chart"
c1.y_axis.title = "Cost"
c1.x_axis.title = "Date"
c1.x_axis.number_format = 'mm/dd/yy'
c1.x_axis.majorTimeUnit = "days"
c1.style = 39

data = Reference(sheet_obj, min_col=2, min_row=2, max_col=3, max_row=13)
c1.add_data(data, titles_from_data=True)

dates = Reference(sheet_obj, min_col=1, min_row=2, max_row=13)
c1.set_categories(dates)

########## BEGIN INSERT

#### BEGIN CUSTOM CLASS
#### https://stackoverflow.com/a/51006976/15161640
from openpyxl.drawing.colors import ColorChoice
from openpyxl.descriptors import Typed
from openpyxl.descriptors.serialisable import Serialisable
from openpyxl.descriptors.nested import NestedInteger
from openpyxl.xml.constants import DRAWING_NS

class MyRGBColor(Serialisable):
    tagname = 'srgbClr'
    namespace = DRAWING_NS
    val = Typed(expected_type=str)
    alpha = NestedInteger(allow_none=True)
    __elements__ = ('alpha', )

    def __init__(self, val, alpha=None):
        self.val = val
        self.alpha = alpha

class MyColorChoice(ColorChoice):
    srgbClr = Typed(expected_type=MyRGBColor, allow_none=True)
#### END CUSTOM CLASS

# A list of colors - add more to handle more series
colors = ["0000ff","ffa500"]
color_gen = (c for c in colors)

# set a pattern for the whole series
for series in c1.series:
    clr = next(color_gen)
    aclr = MyColorChoice(srgbClr=MyRGBColor(clr, alpha=50000))
    series.graphicalProperties.solidFill = aclr
    series.graphicalProperties.line.solidFill = clr

########## END INSERT

sheet_obj.add_chart(c1, "I2")
wb_obj.save("sample.xlsx")

【讨论】:

  • 让我试试@markH - 你也知道我最新帖子的答案吗?
  • 图表标题和轴标签不适合我,即使是直接来自教程的示例。我正在使用 openpyxl 3.0.7 和 LibreOffice 6.4.6.2。我以后也许可以研究一下。
  • 实际上,它们在 Google Drive 中看起来不错,所以 LibreOffice 一定是问题所在。
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 2013-05-28
相关资源
最近更新 更多