diff --git a/svg.cpp b/svg.cpp index 35737c7..c5e9af9 100644 --- a/svg.cpp +++ b/svg.cpp @@ -30,7 +30,7 @@ void svg_rect(double x, double y, double width, double height, string stroke = " void -show_histogram_svg(const vector& bins) { +show_histogram_svg(const vector& bins, size_t user_block_width) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; @@ -38,7 +38,7 @@ show_histogram_svg(const vector& bins) { const auto TEXT_BASELINE = 20; const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; - const auto BLOCK_WIDTH = 10; + const auto BLOCK_WIDTH = user_block_width; const size_t MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH - 1; @@ -49,27 +49,24 @@ show_histogram_svg(const vector& bins) { } } - if (maxbin > MAX_WIDTH) { - - svg_begin(400, 300); + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); double top = 0; - for (size_t bin : bins) { - const double bin_width = MAX_WIDTH * (bin / maxbin); - svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); - top += BIN_HEIGHT; - } - svg_end(); - } - - svg_begin(400, 300); - double top = 0; - for (size_t bin : bins) { - const double bin_width = BLOCK_WIDTH * bin; - svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); - top += BIN_HEIGHT; + if (maxbin<=76){ + for (size_t bin : bins) { + double bin_width = BLOCK_WIDTH * bin; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + top += BIN_HEIGHT; + } + svg_end(); + } else { + for (size_t bin : bins) { + double bin_width= MAX_WIDTH * (static_cast(bin) / maxbin); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + top += BIN_HEIGHT; + } + svg_end(); } - svg_end(); } diff --git a/svg.h b/svg.h index adca9a4..04f0f6c 100644 --- a/svg.h +++ b/svg.h @@ -1,6 +1,6 @@ #ifndef SVG_H_INCLUDED #define SVG_H_INCLUDED void -show_histogram_svg(const std::vector& bins); +show_histogram_svg(const std::vector& bins, size_t user_block_width); #endif // SVG_H_INCLUDED