Beispiel #1
0
class PieChart:
    def __init__(self):
        self.Backend = Backend()
        self.database = Database()
        self.sorted_data = {}

    def send_data(self):
        self.database.createTable()
        for key, value in self.Backend.parse().items():
            self.database.insert(key, value)

    def get_data(self):
        database = Database()
        data = database.get_all()
        for key, value in data:
            self.sorted_data[key] = value
        return self.sorted_data

    def pie_chart(self, data):
        count = 0
        percentage = []
        country = []
        explode = (0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0)

        for cntry, pnt in data.items():
            if count < 10 and cntry != "European Union":
                percentage.append(pnt)
                country.append(cntry)
                count += 1

        plt.pie(
            percentage, labels=country,
            wedgeprops={'edgecolor': 'white'},
            # autopct=lambda percentage: f'{percentage:.2f}%',
            autopct='%1.1f%%',
            shadow=True,
            explode=explode
        )
        plt.title("Top 10 Country by C02 Emission In 2017")
        plt.axis('equal')
        plt.show()
        with open(filename, "r") as fp:
            lines = fp.readlines()[5:]
            for line in lines: 
                #Regular expression to read HTML table data.            
                data = re.findall(r'(?i)<td.*?>([^<]+)</td.*?>', line)
                clist.append(data)
        return clist



if __name__ == "__main__":

    at = AnnualTemperature()
    data = at.openfile('lab1\Temperature.html')
    dt = Database()
    dt.createTable()
    for items in data:
        if len(items) > 0 and int(items[0]) > 1959 and int(items[0]) < 2019):
            print(items)
        # dt.create(items[0],items[2])
   
    graph = Graph()
    data = dt.get_all()
    x = []
    y = []

    for row in data:
        x.append(row[0])
        y.append(row[1])

    graph.thread_read(x, y)