简体   繁体   English

使用 openpyxl(透明度)创建复杂的面积图

[英]Creating complex Area Chart using openpyxl (transparency)

I am trying to create an area chart using openpyxl.我正在尝试使用 openpyxl 创建面积图。 I wish for the area chart to look more complex than the basic excel charts.我希望面积图看起来比基本的 excel 图表更复杂。 Here is my data这是我的数据

data数据

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

在此处输入图像描述

Desired期望的

在此处输入图像描述

Doing正在做

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")

I have found this in the documentation, but unsure of how to implement:我在文档中找到了这个,但不确定如何实现:

     openpyxl.drawing.effect.AlphaBiLevelEffect

I'd like to add transparency to the area chart and have it reflect my desired output.我想为面积图增加透明度,让它反映我想要的 output。 Any suggestion is appreciated任何建议表示赞赏

Transparency working, with SO link of source in code:透明度工作,代码中的源代码链接:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM