甘特图画图指南(Python)

举报
Python爱好者 发表于 2021/04/26 14:39:08 2021/04/26
【摘要】 Python画甘特图
对于刚画甘特图的时候,大部分人第一印象就是联想到matplotlylib,但是说实话,matplotly画图真的是一言难尽,反正不是特别好看,今天就推荐一个特别适合画甘特图的库ChartDirector,ChartDirector几乎对所有语言都有支持的库,关于python的下载版本我已经贴出来了,需要手动安装,不能直接使用pip一步到位,这也算是他的一些弊端吧.

ChartDirector 的安装文件为 zip 压缩格式,解压后,我们可以看到 ChartDirector 文件夹下有这样几个文件夹:

将 lib 文件夹下所有文件拷贝到 python 安装目录的 site-packages 文件夹下面,下面的文件是拷贝的文件:








from pychartdir import *

# the names of the tasks
labels = ["Market Research",
          "Define Specifications",
          "Overall Archiecture",
          "Project Planning",
          "Detail Design",
          "Software Development",
          "Test Plan",
          "Testing and QA",
          "User Documentation"]


# the planned start dates and end dates for the tasks
startDate = [chartTime(2020, 8, 16),
             chartTime(2020, 8, 30),
             chartTime(2020, 9, 13),
             chartTime(2020, 9, 20),
             chartTime(2020, 9, 27),
             chartTime(2020, 10, 4),
             chartTime(2020, 10, 25),
             chartTime(2020, 11, 1),
             chartTime(2020, 11, 8)]


endDate = [chartTime(2020, 8, 30),
           chartTime(2020, 9, 13),
           chartTime(2020, 9, 27),
           chartTime(2020, 10, 4),
           chartTime(2020, 10, 11),
           chartTime(2020, 11, 8),
           chartTime(2020, 11, 8),
           chartTime(2020, 11, 22),
           chartTime(2020, 11, 22)]

# the actual start dates and end dates for the tasks up to now
actualStartDate = [chartTime(2020, 8, 16),
                   chartTime(2020, 8, 27),
                   chartTime(2020, 9, 9),
                   chartTime(2020, 9, 18),
                   chartTime(2020, 9, 22)]

actualEndDate = [chartTime(2020, 8, 27),
                 chartTime(2020, 9, 9),
                 chartTime(2020, 9, 27),
                 chartTime(2020, 10, 2),
                 chartTime(2020, 10, 8)]

# Create a XYChart object of size 620 x 280 pixels. Set background color to light
# green (ccffcc) with 1 pixel 3D border effect.
c = XYChart(620, 280, 0xccffcc, 0x000000, 1)

# Add a title to the chart using 15 points Times Bold Itatic font, with white
# (ffffff) text on a dark green (0x6000) background
c.addTitle("Multi-Layer Gantt Chart Demo", "timesbi.ttf", 15, 0xffffff
           ).setBackground(0x006000)

# Set the plotarea at (140, 55) and of size 460 x 200 pixels. Use alternative
# white/grey background. Enable both horizontal and vertical grids by setting their
# colors to grey (c0c0c0). Set vertical major grid (represents month boundaries) 2
# pixels in width
c.setPlotArea(140, 55, 460, 200, 0xffffff, 0xeeeeee, LineColor, 0xc0c0c0, 0xc0c0c0
              ).setGridWidth(2, 1, 1, 1)

# swap the x and y axes to create a horziontal box-whisker chart
c.swapXY()

# Set the y-axis scale to be date scale from Aug 16, 2020 to Nov 22, 2020, with ticks
# every 7 days (1 week)
c.yAxis().setDateScale(chartTime(2020, 8, 16), chartTime(2020, 11, 22), 86400 * 7)

# Add a red (ff0000) dash line to represent the current day
c.yAxis().addMark(chartTime(2020, 10, 8), c.dashLineColor(0xff0000, DashLine))

# Set multi-style axis label formatting. Month labels are in Arial Bold font in "mmm
# d" format. Weekly labels just show the day of month and use minor tick (by using
# '-' as first character of format string).
c.yAxis().setMultiFormat(StartOfMonthFilter(), "<*font=arialbd.ttf*>{value|mmm d}",
                         StartOfDayFilter(), "-{value|d}")

# Set the y-axis to shown on the top (right + swapXY = top)
c.setYAxisOnRight()

# Set the labels on the x axis
c.xAxis().setLabels(labels)

# Reverse the x-axis scale so that it points downwards.
c.xAxis().setReverse()

# Set the horizontal ticks and grid lines to be between the bars
c.xAxis().setTickOffset(0.5)

# Use blue (0000aa) as the color for the planned schedule
plannedColor = 0x0000aa

# Use a red hash pattern as the color for the actual dates. The pattern is created as
# a 4 x 4 bitmap defined in memory as an array of colors.
actualColor = c.patternColor([0xffffff, 0xffffff, 0xffffff, 0xff0000, 0xffffff,
                              0xffffff, 0xff0000, 0xffffff, 0xffffff, 0xff0000,
                              0xffffff, 0xffffff, 0xff0000, 0xffffff, 0xffffff,
                              0xffffff], 4)

# Add a box whisker layer to represent the actual dates. We add the actual dates
# layer first, so it will be the top layer.
actualLayer = c.addBoxLayer(
    actualStartDate, actualEndDate, actualColor, "Actual")

# Set the bar height to 8 pixels so they will not block the bottom bar
actualLayer.setDataWidth(8)

# Add a box-whisker layer to represent the planned schedule date
c.addBoxLayer(startDate, endDate, plannedColor, "Planned").setBorderColor(
    SameAsMainColor)

# Add a legend box on the top right corner (595, 60) of the plot area with 8 pt Arial
# Bold font. Use a semi-transparent grey (80808080) background.
b = c.addLegend(595, 60, 0, "arialbd.ttf", 8)
b.setAlignment(TopRight)
b.setBackground(0x80808080, -1, 2)

# Output the chart
c.makeChart("layergantt.png")

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。