iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
0

今天來將昨天的資料轉成折線圖,可以更直觀、直覺看到隔天的溫度曲線圖表。

折線圖呈現,X軸代表時間,所以就是 00:00 ~ 21:00,Y軸代表溫度,一個區一條線,有12區,就有12條線。
這裡有用LineLegend() 圖例,需要要顏色的跟線的配對,我這邊有隨便選的顏色,在用給參數的時候,記得要給每一條線顏色(strokeColor)。

加上 圖表的程式碼

def getPDFChartDay(dataDictList, showDateText):
    categoryNames = ["00:00", "03:00", "06:00", "09:00",
                  "12:00", "15:00", "18:00", "21:00"]
    colorNamePairs = []
    colorList = [
        HexColor("#115b87"), HexColor("#4ba1d5"), HexColor("#b26cb7"),
        HexColor("#90cf42"), HexColor("#b290b2"), HexColor("#2c5459"),
        HexColor("#4e79ed"), HexColor("#d53ab9"), HexColor("#dcb114"),
        HexColor("#5b5b7c"), HexColor("#b3090b"), HexColor("#c2d77d"),
    ]
    dataList = []
    for index, dataDict in enumerate(dataDictList):
        # categoryNames.append(dataDict["locationName"])
        colorNamePairs.append(
            (colorList[index], dataDict["locationName"])
        )
        # 跑溫度的迴圈
        temperatureList = []
        for temperatureDict in dataDict["temperatureDictList"]:
            dataTime = temperatureDict["dataTime"]
            if showDateText in dataTime:
                value = int(temperatureDict["value"])
                temperatureList.append(value)
        dataList.append(temperatureList)

    drawing = Drawing(300, 250)
    horizontalLineChart = HorizontalLineChart()
    horizontalLineChart.x = 0
    horizontalLineChart.y = 100
    horizontalLineChart.height = 130
    horizontalLineChart.width = 300
    horizontalLineChart.data = dataList
    horizontalLineChart.categoryAxis.categoryNames = categoryNames
    horizontalLineChart.categoryAxis.labels.boxAnchor = 'n'
    horizontalLineChart.valueAxis.valueMin = 15
    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 = 0
    legend.y = 70
    legend.colorNamePairs = colorNamePairs
    drawing.add(legend)
    horizontalLineChart.valueAxis.visibleGrid = True
    drawing.add(horizontalLineChart)
    return drawing
    
    
pdfmetrics.registerFont(TTFont('kaiu', "font/kaiu.ttf"))
fileName = "example.pdf"
pdfTemplate = SimpleDocTemplate(fileName)
story = []

today = datetime.datetime.today().date()
showDate = today + datetime.timedelta(days=1)

todayDate = datetime.datetime.today().date()
tomorrow = todayDate + datetime.timedelta(days=1)
dayAfterTomorrow = tomorrow + datetime.timedelta(days=1)
tomorrowText = tomorrow.strftime('%Y-%m-%d')
dayAfterTomorrowText = dayAfterTomorrow.strftime('%Y-%m-%d')

table = getPDFTableDay(dataDictList, tomorrowText)
story.append(table)
# 加在 表格下面
chart = getPDFChartDay(dataDictList, tomorrowText)
story.append(chart)
# 跳到下一頁
story.append(PageBreak())
table = getPDFTableDay(dataDictList, dayAfterTomorrowText)
story.append(table)
# 加在 表格下面
chart = getPDFChartDay(dataDictList, dayAfterTomorrowText)
story.append(chart)
pdfTemplate.build(story)

結果,可以更直觀看到明天28號 中午的時候 溫度比較高,最高到24度,至於各區的曲線,因為類型太像了,顏色相近,還有重疊的部份,就會看不太清楚各區的曲線,可能就要一個區一張圖,這樣就會比較清楚,有時間再做吧

結語
今天就完成台北市各區明後天溫度曲線圖,圖表呈現還可以更好,之後要做排版。


參考資料:

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


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

尚未有邦友留言

立即登入留言