From 7fc474ccfdf58cfef53263b051608b1ee7e51b96 Mon Sep 17 00:00:00 2001 From: ShchipkovMY Date: Sun, 10 Sep 2023 20:03:08 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=207=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- svg.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/svg.cpp b/svg.cpp index c98b906..24dd384 100644 --- a/svg.cpp +++ b/svg.cpp @@ -5,8 +5,24 @@ #include #include "svg.h" + using namespace std; +double sr(const vector& bins) { + if (bins.empty()) { + return 0; + } + size_t totalHeight = 0; + for (size_t bin : bins) { + totalHeight += bin; + } + + return static_cast(totalHeight) / bins.size(); +} + + + + void svg_begin(double width, double height) { @@ -50,8 +66,10 @@ show_histogram_svg(const vector& bins) const auto BLOCK_WIDTH = 10; const auto BLACK = "black"; const auto RED = "red"; + const auto GREEN = "green"; const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; + size_t x = sr(bins); svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); @@ -69,7 +87,13 @@ show_histogram_svg(const vector& bins) { double bin_width = (MAX_WIDTH)*(bin/max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + if (bin_width<=MAX_WIDTH/max_count*x) + { + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, GREEN); + } + else{ svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED); + } top += BIN_HEIGHT; }