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; }