From 3965123393897ff2a9dedd7a64c92ea96c033f1a Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Thu, 17 Mar 2022 15:22:18 +0000 Subject: [PATCH] fixed report bug on concentration graph y range --- cara/apps/calculator/report_generator.py | 7 ++-- cara/apps/calculator/static/js/report.js | 35 +++++++++++++------ .../templates/base/calculator.report.html.j2 | 6 ++-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/cara/apps/calculator/report_generator.py b/cara/apps/calculator/report_generator.py index 6f35c88a..58b6c5e1 100644 --- a/cara/apps/calculator/report_generator.py +++ b/cara/apps/calculator/report_generator.py @@ -110,12 +110,13 @@ def calculate_report_data(model: models.ExposureModel): ] sr_breathing_concentrations = [] - if len(short_range_intervals) != 0: + for time in times: for index, (start, stop) in enumerate(short_range_intervals): # For visualization issues, add short range breathing activity to the initial long range concentrations - if short_range_activities[index] == 'Breathing': - sr_breathing_concentrations.append(np.array(model.concentration(float(stop))).mean()) + if start <= time <= stop and short_range_activities[index] == 'Breathing': + sr_breathing_concentrations.append(np.array(model.concentration(float(time))).mean()) break + sr_breathing_concentrations.append(np.array(model.concentration_model.concentration(float(time))).mean()) highest_const = max(short_range_concentrations) prob = np.array(model.infection_probability()).mean() diff --git a/cara/apps/calculator/static/js/report.js b/cara/apps/calculator/static/js/report.js index adfeff5b..a4a7c32a 100644 --- a/cara/apps/calculator/static/js/report.js +++ b/cara/apps/calculator/static/js/report.js @@ -1,5 +1,5 @@ /* Generate the concentration plot using d3 library. */ -function draw_plot(svg_id, times, concentrations, short_range_concentrations, cumulative_doses) { +function draw_plot(svg_id, times, concentrations, short_range_concentrations, cumulative_doses, exposed_presence_intervals, short_range_intervals) { // Used for controlling the short range interactions let button_full_exposure = document.getElementById("button_full_exposure"); @@ -124,9 +124,16 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu .attr('stroke-linejoin', 'round') .attr('fill', 'none'); + var clip = vis.append("defs").append("svg:clipPath") + .attr("id", "clip") + .append("svg:rect"); + + var draw_area = vis.append('svg:g') + .attr('clip-path', 'url(#clip)'); // Line representing the mean concentration. var lineFunc = d3.line(); - var draw_line = vis.append('svg:path') + draw_area.append('svg:path') + .attr('class', 'line') .attr('stroke', '#1f77b4') .attr('stroke-width', 2) .attr('fill', 'none'); @@ -144,7 +151,7 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu var drawArea = {}; exposed_presence_intervals.forEach((b, index) => { exposedArea[index] = d3.area(); - drawArea[index] = vis.append('svg:path') + drawArea[index] = draw_area.append('svg:path') .attr('fill', '#1f77b4') .attr('fill-opacity', '0.1'); }); @@ -154,7 +161,8 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu var drawShortRangeArea = {}; short_range_intervals.forEach((b, index) => { shortRangeArea[index] = d3.area(); - drawShortRangeArea[index] = vis.append('svg:path') + drawShortRangeArea[index] = draw_area.append('svg:path') + .attr('class', 'draw_short_range_area') .attr('fill', '#1f00b4') .attr('fill-opacity', '0.1'); }); @@ -197,12 +205,12 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu function update_concentration_plot(data) { yRange.domain([0., Math.max(...data)]); yAxisEl.transition().duration(1000).call(yAxis); - + + // Concentration line lineFunc.defined(d => !isNaN(d.concentration)) .x(d => xTimeRange(d.time)) .y(d => yRange(d.concentration)); - draw_line.enter() - .merge(draw_line) + draw_area.select('.line') .transition() .duration(1000) .attr("d", lineFunc(data_for_graphs.concentrations)); @@ -286,7 +294,7 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu var margins = { top: 30, right: 20, bottom: 50, left: 60 }; div_width = 900; graph_width = div_width * (2/3); - const svg_margins = {'margin-left': '0rem', 'margin-top': '0rem'}; + const svg_margins = {'margin-left': '0rem'}; Object.entries(svg_margins).forEach(([prop,val]) => vis.style(prop,val)); } else { @@ -294,7 +302,7 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu div_width = div_width * 1.1 graph_width = div_width * .9; graph_height = div_height * 0.65; // On mobile screen sizes we want the legend to be on the bottom of the graph. - const svg_margins = {'margin-left': '-1rem', 'margin-top': '3rem'}; + const svg_margins = {'margin-left': '-1rem'}; Object.entries(svg_margins).forEach(([prop,val]) => vis.style(prop,val)); }; @@ -304,6 +312,12 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu // SVG components according to the width and height. + // clipPath: everything out of this area won't be drawn. + clip.attr("x", margins.left) + .attr("y", margins.top) + .attr("width", graph_width - margins.right - margins.left) + .attr("height", graph_height - margins.top - margins.bottom); + // Axis ranges. xRange.range([margins.left, graph_width - margins.right]); xTimeRange.range([margins.left, graph_width - margins.right]); @@ -434,7 +448,8 @@ function draw_plot(svg_id, times, concentrations, short_range_concentrations, cu // Redraw based on the new size whenever the browser window is resized. window.addEventListener("resize", e => { redraw(); - update_concentration_plot(short_range_concentrations); + if (button_full_exposure.disabled) update_concentration_plot(short_range_concentrations); + else update_concentration_plot(concentrations) }); diff --git a/cara/apps/templates/base/calculator.report.html.j2 b/cara/apps/templates/base/calculator.report.html.j2 index d7d3e1d1..8350f884 100644 --- a/cara/apps/templates/base/calculator.report.html.j2 +++ b/cara/apps/templates/base/calculator.report.html.j2 @@ -89,19 +89,19 @@ {% endblock report_summary_footnote %}

* The results are based on the parameters and assumptions published in the CERN Open Report CERN-OPEN-2021-004.

- {% if (form.short_range_option == "short_range_yes" and concentrations|length > 0 and concentrations|max|int != short_range_concentrations|max|int ) %} + {% if (form.short_range_option == "short_range_yes" and concentrations|max|int != short_range_concentrations|max|int ) %} {% endif %}