diff --git a/visualizer/plotting.py b/visualizer/plotting.py index c86a44ce24e160be219b39a7f7ce8fdf91339958..c88d8f99e44af9bf1fbfa67e061d47a026ab5481 100644 --- a/visualizer/plotting.py +++ b/visualizer/plotting.py @@ -14,7 +14,7 @@ class Selector: """Information about a `<select>`.""" def __init__(self, **kwargs): - if kwargs['type'] not in ('station', 'year', 'measurement'): + if kwargs['type'] not in ('measurement', 'station', 'year'): raise ValueError('unknown type') self.type = kwargs['type'] self.name = kwargs['name'] @@ -23,10 +23,10 @@ class Selector: self.onchange = kwargs.get('onchange') def get_param(self): - if self.type == 'year': - to = year_type - elif self.type == 'measurement': + if self.type == 'measurement': to = meas_type + elif self.type == 'year': + to = year_type else: to = str return get_param(self.name, to=to, call_abort=False) @@ -43,18 +43,18 @@ class Plotter: class TimeSeries(Plotter): - """Plot one station/year/measurement.""" + """Plot one measurement/station/year.""" name = 'time-series' nav_title = 'Time Series' page_title = 'Plot Data' onsubmit_fn = 'timeSeriesVisualize' selectors = ( + Selector(type='measurement', name='measurement', id='measurement', + label='Measurement'), Selector(type='station', name='station', id='station', label='Station', onchange='getYears()'), Selector(type='year', name='year', id='year', label='Year'), - Selector(type='measurement', name='measurement', id='measurement', - label='Measurement'), ) source_param_maps = ( {'station': 'station', 'year': 'year'}, @@ -70,9 +70,9 @@ class TimeSeries(Plotter): plt.rcParams['axes.xmargin'] = 0 matplotlib.use('Agg') + meas = get_param('measurement', to=meas_type) station_id = get_param('station') year = get_param('year', to=year_type) - meas = get_param('measurement', to=meas_type) station = get_station(station_id) data = read_data(station, year) fig, axes = plt.subplots() @@ -96,26 +96,26 @@ class TimeSeries(Plotter): plt.suptitle(f'{meas.title.capitalize()} measurements, ' f'{name} Station, {data[0, 0].year}') return savefig_response(fig, filename=( - f'{cls.name}.{station_id}.{year}.{meas.slug}.png' + f'{cls.name}.{meas.slug}.{station_id}.{year}.png' )) class Overlay(Plotter): - """Plot two station/years @ one measurement.""" + """Plot one measurement @ two station/years.""" name = 'overlay' nav_title = 'Overlay' page_title = 'Overlay Stations' onsubmit_fn = 'overlayVisualize' selectors = ( + Selector(type='measurement', name='measurement', id='measurement', + label='Measurement'), Selector(type='station', name='station1', id='station-1', label='Station #1', onchange='getYears(1)'), Selector(type='year', name='year1', id='year-1', label='Year #1'), Selector(type='station', name='station2', id='station-2', label='Station #2', onchange='getYears(2)'), Selector(type='year', name='year2', id='year-2', label='Year #2'), - Selector(type='measurement', name='measurement', id='measurement', - label='Measurement'), ) source_param_maps = ( {'station': 'station1', 'year': 'year1'}, @@ -132,6 +132,7 @@ class Overlay(Plotter): plt.rcParams['axes.xmargin'] = 0 matplotlib.use('Agg') + meas = get_param('measurement', to=meas_type) num_datasets = 2 datasets = tuple(SimpleNamespace() for _ in range(num_datasets)) for n, dset in enumerate(datasets, start=1): @@ -139,7 +140,6 @@ class Overlay(Plotter): dset.year = get_param(f'year{n}', to=year_type) dset.station = get_station(dset.station_id) dset.name = dset.station['name'] - meas = get_param('measurement', to=meas_type) def ignore_feb_29(rows): return [row for row in rows @@ -189,24 +189,24 @@ class Overlay(Plotter): filename_dsets = '.'.join(f'{dset.station_id}.{dset.year}' for dset in datasets) return savefig_response(fig, filename=( - f'{cls.name}.{filename_dsets}.{meas.slug}.png' + f'{cls.name}.{meas.slug}.{filename_dsets}.png' )) class Boxplot(Plotter): - """Boxplot one station/measurement @ multiple years.""" + """Boxplot one measurement/station @ multiple years.""" name = 'boxplot' nav_title = 'Boxplot' page_title = 'Boxplot Years' onsubmit_fn = 'boxplotVisualize' selectors = ( + Selector(type='measurement', name='measurement', id='measurement', + label='Measurement'), Selector(type='station', name='station', id='station', label='Station', onchange='boxplotGetYears()'), Selector(type='year', name='year1', id='year-1', label='Year #1'), Selector(type='year', name='year2', id='year-2', label='Year #2'), - Selector(type='measurement', name='measurement', id='measurement', - label='Measurement'), ) source_param_maps = ( {'station': 'station', 'year': 'year1'}, @@ -221,10 +221,10 @@ class Boxplot(Plotter): plt.rcParams['axes.xmargin'] = 0 matplotlib.use('Agg') + meas = get_param('measurement', to=meas_type) station_id = get_param('station') year1 = get_param('year1', to=year_type) year2 = get_param('year2', to=year_type) - meas = get_param('measurement', to=meas_type) station = get_station(station_id) plot_data = [] @@ -267,7 +267,7 @@ class Boxplot(Plotter): plt.suptitle(f'{meas.title.capitalize()} measurements, ' f'{name} Station, {start_year} - {end_year}.') return savefig_response(fig, filename=( - f'{cls.name}.{station_id}.{year1}.{year2}.{meas.slug}.png' + f'{cls.name}.{meas.slug}.{station_id}.{year1}.{year2}.png' )) diff --git a/visualizer/templates/macros.html b/visualizer/templates/macros.html index 9b929ddf88d4567f8dd781d01220a12a591af75d..196e70612c9944194d5702ab4d85c1da0481b929 100644 --- a/visualizer/templates/macros.html +++ b/visualizer/templates/macros.html @@ -3,6 +3,15 @@ {%- endmacro %} +{% macro measurement_options(selected=none) %} + {% for meas in g.measurements.values() %} + <option value="{{ meas.slug }}" + {{- _selected(meas == selected) }}> + {{- meas.title | title -}} + </option> + {% endfor %} +{% endmacro %} + {% macro station_options(selected=none) %} {% for station in g.stations %} <option value="{{ station['id'] }}" @@ -20,19 +29,10 @@ {% endfor %} {% endmacro %} -{% macro measurement_options(selected=none) %} - {% for meas in g.measurements.values() %} - <option value="{{ meas.slug }}" - {{- _selected(meas == selected) }}> - {{- meas.title | title -}} - </option> - {% endfor %} -{% endmacro %} - {% set options = { + 'measurement': measurement_options, 'station': station_options, 'year': year_options, - 'measurement': measurement_options, } %}