From a38d857465c32035fa00a845c726785757b399a5 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Fri, 27 Jan 2023 05:22:45 -0800 Subject: [PATCH] Allow line_gap_fraction to be used with bar plots. Fixes issue #818. --- bin/weeplot/genplot.py | 10 +++++----- bin/weewx/imagegenerator.py | 23 ++++++++++++----------- docs/changes.txt | 2 ++ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/bin/weeplot/genplot.py b/bin/weeplot/genplot.py index b22cd9e0..83b2d349 100644 --- a/bin/weeplot/genplot.py +++ b/bin/weeplot/genplot.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2009-2021 Tom Keffer +# Copyright (c) 2009-2023 Tom Keffer # # See the file LICENSE.txt for your full rights. # @@ -366,8 +366,8 @@ class GeneralPlot(object): # Calculate the size of a gap in data maxdx = None - if this_line.gap_fraction is not None: - maxdx = this_line.gap_fraction * (self.xscale[1] - self.xscale[0]) + if this_line.line_gap_fraction is not None: + maxdx = this_line.line_gap_fraction * (self.xscale[1] - self.xscale[0]) if this_line.plot_type == 'line': ms = this_line.marker_size @@ -611,7 +611,7 @@ class PlotLine(object): """Represents a single line (or bar) in a plot. """ def __init__(self, x, y, label='', color=None, fill_color=None, width=None, plot_type='line', line_type='solid', marker_type=None, marker_size=10, - bar_width=None, vector_rotate = None, gap_fraction=None): + bar_width=None, vector_rotate = None, line_gap_fraction=None): self.x = x self.y = y self.label = to_text(label) # Make sure the label is in unicode @@ -624,7 +624,7 @@ class PlotLine(object): self.width = width self.bar_width = bar_width self.vector_rotate = vector_rotate - self.gap_fraction = gap_fraction + self.line_gap_fraction = line_gap_fraction class UniDraw(ImageDraw.ImageDraw): diff --git a/bin/weewx/imagegenerator.py b/bin/weewx/imagegenerator.py index 284dbcf6..3689582e 100644 --- a/bin/weewx/imagegenerator.py +++ b/bin/weewx/imagegenerator.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2009-2021 Tom Keffer +# Copyright (c) 2009-2023 Tom Keffer # # See the file LICENSE.txt for your full rights. # @@ -255,6 +255,10 @@ class ImageGenerator(weewx.reportengine.ReportGenerator): # Get the type of plot ('bar', 'line', or 'vector') plot_type = line_options.get('plot_type', 'line').lower() + if plot_type not in {'line', 'bar', 'vector'}: + log.error(f"Unknown plot type {plot_type}. Ignored") + continue + if aggregate_type and plot_type != 'bar': # If aggregating, put the point in the middle of the interval start_vec_t = ValueTuple( @@ -302,7 +306,7 @@ class ImageGenerator(weewx.reportengine.ReportGenerator): width = to_int(line_options.get('width')) interval_vec = None - gap_fraction = None + line_gap_fraction = None vector_rotate = None # Some plot types require special treatments: @@ -313,15 +317,12 @@ class ImageGenerator(weewx.reportengine.ReportGenerator): elif plot_type == 'bar': interval_vec = [x[1] - x[0] for x in zip(start_vec_t.value, stop_vec_t.value)] - elif plot_type == 'line': - gap_fraction = to_float(line_options.get('line_gap_fraction')) - if gap_fraction is not None and not 0 < gap_fraction < 1: + if plot_type in ('line', 'bar'): + line_gap_fraction = to_float(line_options.get('line_gap_fraction')) + if line_gap_fraction and not 0 <= line_gap_fraction <= 1: log.error("Gap fraction %5.3f outside range 0 to 1. Ignored.", - gap_fraction) - gap_fraction = None - else: - log.error("Unknown plot type '%s'. Ignored", plot_type) - continue + line_gap_fraction) + line_gap_fraction = None # Get the type of line (only 'solid' or 'none' for now) line_type = line_options.get('line_type', 'solid') @@ -344,7 +345,7 @@ class ImageGenerator(weewx.reportengine.ReportGenerator): marker_size=marker_size, bar_width=interval_vec, vector_rotate=vector_rotate, - gap_fraction=gap_fraction)) + line_gap_fraction=line_gap_fraction)) # Return the constructed plot if it has any non-null data, otherwise return None return plot if have_data else None diff --git a/docs/changes.txt b/docs/changes.txt index 796bea25..7019666c 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -25,6 +25,8 @@ Added new unit "hertz". PR #812. Again, thanks to user Karen! Calculate `*.wind.maxtime` out of `windGust` like `*.wind.max` Fixes issue #833 +Option line_gap_fraction can now be used with bar plots. Fixes issue #818. + 4.9.1 10/25/2022 Fix problem with `wind` for older versions of sqlite.