iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 16
0
Modern Web

用 Python 玩 PDF,結合Django 變成一個網頁系統系列 第 16

[Day 16] PDF 圖表應用(2)

  • 分享至 

  • xImage
  •  

昨天將所有欄位都加到表格上,今天來將這個表格資料圖形化,今天畫兩個圖表,畫在表格下面。

以各區分類,第一個是折線圖,欄位有溫度、體感溫度,X軸為時間00, 03 ...,Y軸為溫度度C,分別用藍色及綠色顯示

第二個為長條圖,用降雨機率,這邊要注意的是降雨機率是6小時顯示,為了要好看一點可以比對,把他切成3小時,就會看起來整齊了,X軸為時間00, 03 ...,Y軸為降雨機率百分比。

程式碼,程式就把一個圖表當一個func來寫
溫度折線圖

def getPDFChartDistrict(dataDict, showDateText):
    categoryNames = ["00:00", "03:00", "06:00", "09:00",
                     "12:00", "15:00", "18:00", "21:00"]
    colorNamePairs = [
        (colors.green, "溫度"),
        (colors.blue, "體感溫度"),
    ]
    colorList = [colors.green, colors.blue, ]
    dataList = []

    # 跑溫度的迴圈
    temperatureList = []
    for temperatureDict in dataDict["temperatureDictList"]:
        dataTime = temperatureDict["dataTime"]
        if showDateText in dataTime:
            value = int(temperatureDict["value"])
            temperatureList.append(value)
    dataList.append(temperatureList)
    aTemperatureList = []
    for aTemperatureDict in dataDict["aTemperatureDictList"]:
        dataTime = aTemperatureDict["dataTime"]
        if showDateText in dataTime:
            value = int(aTemperatureDict["value"])
            aTemperatureList.append(value)
    dataList.append(aTemperatureList)

    drawing = Drawing(400, 180)
    horizontalLineChart = HorizontalLineChart()
    horizontalLineChart.x = 25
    horizontalLineChart.y = 30
    horizontalLineChart.height = 130
    horizontalLineChart.width = 400
    horizontalLineChart.data = dataList
    horizontalLineChart.categoryAxis.categoryNames = categoryNames
    horizontalLineChart.valueAxis.valueMin = 18
    horizontalLineChart.valueAxis.valueStep = 1
    horizontalLineChart.valueAxis.valueMax = 30
    for index, co in enumerate(colorList):
        horizontalLineChart.lines[index].strokeColor = co
        horizontalLineChart.lines[index].strokeWidth = 2

    legend = LineLegend()
    legend.fontName = 'kaiu'
    legend.alignment = 'right'
    legend.x = 150
    legend.y = 0
    legend.columnMaximum = 1
    legend.colorNamePairs = colorNamePairs
    drawing.add(legend)
    horizontalLineChart.valueAxis.visibleGrid = True
    drawing.add(horizontalLineChart)
    return drawing

第二個降雨機率長條圖

def getPDFChartDistrict2(dataDict, showDateText):
    categoryNames = ["00:00", "03:00", "06:00", "09:00",
                     "12:00", "15:00", "18:00", "21:00"]
    colorNamePairs = [
        (colors.lightblue, "降雨機率"),
    ]
    colorList = [colors.lightblue, ]
    dataList = []

    # 跑溫度的迴圈
    pop6HList = []
    for pop6HDict in dataDict["pop6HDictList"]:
        dataTime = pop6HDict["startTime"]
        if showDateText in dataTime:
            value = int(pop6HDict["value"])
            # 這裡直接append兩次
            pop6HList.append(value)
            pop6HList.append(value)
    dataList.append(pop6HList)

    drawing = Drawing(400, 180)
    horizontalLineChart = VerticalBarChart()
    horizontalLineChart.x = 25
    horizontalLineChart.y = 30
    horizontalLineChart.height = 130
    horizontalLineChart.width = 400
    horizontalLineChart.data = dataList
    horizontalLineChart.categoryAxis.categoryNames = categoryNames
    horizontalLineChart.valueAxis.valueMin = 0
    horizontalLineChart.valueAxis.valueStep = 10
    horizontalLineChart.valueAxis.valueMax = 100
    for index, co in enumerate(colorList):
        horizontalLineChart.bars[index].fillColor = co

    legend = Legend()
    legend.fontName = 'kaiu'
    legend.alignment = 'right'
    legend.x = 200
    legend.y = 0
    legend.columnMaximum = 1
    legend.colorNamePairs = colorNamePairs
    drawing.add(legend)
    horizontalLineChart.valueAxis.visibleGrid = True
    drawing.add(horizontalLineChart)
    return drawing

pdf 處理再補上圖表的部份,也來弄個小標題

...
for dataDict in dataDictList:
    # ...table
    story.append(Spacer(1, 0.15 * inch))
    story.append(Paragraph("未來鄉鎮溫度折線圖", style=styleNormalCustomHeader2))
    chart = getPDFChartDistrict(dataDict, tomorrowText)
    story.append(chart)
    story.append(Spacer(1, 0.2 * inch))
    story.append(Paragraph("未來鄉鎮降雨機率折線圖", style=styleNormalCustomHeader2))
    chart = getPDFChartDistrict2(dataDict, tomorrowText)
    story.append(chart)
    story.append(PageBreak())
...

結果 可以看到隔天天氣變好,只有凌晨會下一點雨,上午時間降雨機率30%左右
降雨機率逐漸下降,看來要不會下雨了


參考資料:

如果有任何寫得不好的地方,請跟我說,謝謝。


上一篇
[Day 15] PDF 表格應用(2)
下一篇
[Day 17] Django
系列文
用 Python 玩 PDF,結合Django 變成一個網頁系統30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言