|
| 1 | +import csv |
| 2 | +import pygal |
| 3 | +from pygal.style import Style |
| 4 | + |
| 5 | +def read_csv_as_nested_dict(filename, keyfield, separator, quote): |
| 6 | + """ |
| 7 | + Inputs: |
| 8 | + filename - Name of CSV file |
| 9 | + keyfield - Field to use as key for rows |
| 10 | + separator - Character that separates fields |
| 11 | + quote - Character used to optionally quote fields |
| 12 | + Output: |
| 13 | + Returns a dictionary of dictionaries where the outer dictionary |
| 14 | + maps the value in the key_field to the corresponding row in the |
| 15 | + CSV file. The inner dictionaries map the field names to the |
| 16 | + field values for that row. |
| 17 | + """ |
| 18 | + info = [] |
| 19 | + sample = 0 |
| 20 | + with open(filename, newline='') as csvfile: |
| 21 | + |
| 22 | + reader = csv.DictReader(csvfile, delimiter=separator, quotechar=quote) |
| 23 | + |
| 24 | + for row in reader: |
| 25 | + info.append((sample, float(row[keyfield]))) |
| 26 | + sample = sample + 1 |
| 27 | + |
| 28 | + return info |
| 29 | + |
| 30 | +# +-------------------------------------------------------------------------+ |
| 31 | +# | | |
| 32 | +# | MAIN CODE | |
| 33 | +# | | |
| 34 | +# +-------------------------------------------------------------------------+ |
| 35 | + |
| 36 | +# Custom style for pygal charts |
| 37 | +custom_style = Style( |
| 38 | + background='#fff', |
| 39 | + plot_background='#fff', |
| 40 | + foreground='#212529', |
| 41 | + foreground_strong='#212529', |
| 42 | + foreground_subtle='#630C0D', |
| 43 | + opacity='.6', |
| 44 | + opacity_hover='.9', |
| 45 | + transition='400ms ease-in', |
| 46 | + colors=('#F55', '#212529') |
| 47 | +) |
| 48 | + |
| 49 | +# +-------------------------------------------------------------------------+ |
| 50 | +# | | |
| 51 | +# | DISCHARGE CHART | |
| 52 | +# | | |
| 53 | +# +-------------------------------------------------------------------------+ |
| 54 | +voltage = read_csv_as_nested_dict("1_Discharge.csv", 'Bateria[mV]', ",", '"') |
| 55 | + |
| 56 | +discharge_chart = pygal.XY(fill=True, style=custom_style, |
| 57 | + title=u'Battery discharge / Descarga de la batería', |
| 58 | + x_title='Samples / Muestras', |
| 59 | + y_title='Voltage / Voltaje [v]', |
| 60 | + show_legend=False, dots_size=2) |
| 61 | + |
| 62 | +discharge_chart.add('Voltage [V]', voltage) |
| 63 | + |
| 64 | +discharge_chart.render_to_file("1_Discharge.svg") |
| 65 | + |
| 66 | +# +-------------------------------------------------------------------------+ |
| 67 | +# | | |
| 68 | +# | CHARGE CHART | |
| 69 | +# | | |
| 70 | +# +-------------------------------------------------------------------------+ |
| 71 | +voltage = read_csv_as_nested_dict("2_Charge.csv", 'Bateria[mV]', ",", '"') |
| 72 | +current = read_csv_as_nested_dict("2_Charge.csv", 'Current[A]', ",", '"') |
| 73 | + |
| 74 | +charge_chart = pygal.XY(fill=True, style=custom_style, |
| 75 | + title=u'Battery charge / Carga de la batería', |
| 76 | + x_title='Samples / Muestras', |
| 77 | + legend_at_bottom=True, dots_size=1) |
| 78 | + |
| 79 | +charge_chart.add('Voltage [V]', voltage) |
| 80 | +charge_chart.add('Current [A]', current, secondary=True) |
| 81 | + |
| 82 | +charge_chart.render_to_file("2_Charge.svg") |
0 commit comments