From ab7728268d4053a27744cecf810945c8a75e9aa4 Mon Sep 17 00:00:00 2001 From: EfremovSI Date: Sat, 20 Apr 2024 20:26:27 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=92=D0=90=D0=A04,=20=D1=86=D0=B2?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=B3=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 2 +- svg.cpp | 30 ++++++++++++++++++++---------- svg.h | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index b71ca3a..4c7ecad 100644 --- a/main.cpp +++ b/main.cpp @@ -43,7 +43,7 @@ int main() auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); auto procent = make_histogram_proc(in.numbers, in.bin_count, bins); - show_histogram_svg(bins); + show_histogram_svg(bins, procent); return 0; } diff --git a/svg.cpp b/svg.cpp index 11e48d9..960fe4a 100644 --- a/svg.cpp +++ b/svg.cpp @@ -22,7 +22,7 @@ svg_end() { void svg_text(double left, double baseline, string text) { - cout << " "<< text <<" "; + cout << " "<< text <<" "; } void @@ -31,7 +31,12 @@ svg_rect(double x, double y, double width, double height){ } void -show_histogram_svg(const vector& bins) { +svg_procent(double left_procent, double baseline, string text) { + cout << " "<< text <<"% "; +} + +void +show_histogram_svg(const vector& bins, const vector& procent) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; @@ -39,8 +44,9 @@ show_histogram_svg(const vector& bins) { const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; + const double SCALE = IMAGE_WIDTH - TEXT_WIDTH; - svg_begin(400, 300); + svg_begin(800, 300); double max_count = 0; @@ -49,22 +55,27 @@ show_histogram_svg(const vector& bins) { max_count = x; } } + double top = 0; if ((max_count*BLOCK_WIDTH)>SCALE){ - for (size_t bin : bins) { - const double bin_width = SCALE * ( bin / max_count); - svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + for (size_t i = 0; i < bins.size(); i++) { + const double bin_width = SCALE * ( bins[i] / max_count); + const auto TEXT_LEFT_PROCENT = SCALE+TEXT_WIDTH+20; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i])); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + svg_procent(TEXT_LEFT_PROCENT, top + TEXT_BASELINE, to_string(procent[i])); top += BIN_HEIGHT; cout << endl; cout << bin_width; } } else{ - for (size_t bin : bins) { - const double bin_width = BLOCK_WIDTH * bin; - svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + for (size_t i = 0; i < bins.size(); i++) { + const double bin_width = BLOCK_WIDTH * bins[i]; + const auto TEXT_LEFT_PROCENT = (max_count * BLOCK_WIDTH)+TEXT_WIDTH+20; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i])); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + svg_procent(TEXT_LEFT_PROCENT, top + TEXT_BASELINE, to_string(procent[i])); top += BIN_HEIGHT; cout << endl; cout << bin_width; @@ -75,4 +86,3 @@ show_histogram_svg(const vector& bins) { } - diff --git a/svg.h b/svg.h index ac36df3..120612c 100644 --- a/svg.h +++ b/svg.h @@ -2,6 +2,6 @@ #define SVG_H_INCLUDED void -show_histogram_svg(const std::vector& bins); +show_histogram_svg(const std::vector& bins, const std::vector& procent); #endif // SVG_H_INCLUDED