diff --git a/svg.cpp b/svg.cpp index aff8945..75ae8ad 100644 --- a/svg.cpp +++ b/svg.cpp @@ -18,7 +18,7 @@ svg_begin(double width, double height) { cout << "\n"; } @@ -40,13 +40,30 @@ svg_rect(double x, double y, double width, double height) { void show_histogram_svg(const vector& bins) { - double top = 0; + double top = 0.0; + double move = 0.0; svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + double k = 1.0; + size_t max_bin = bins[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 (bin > max_bin) { + max_bin = bin; + } + } + if (max_bin * 10 > IMAGE_HEIGHT) { + k = double (IMAGE_HEIGHT - 10) / (max_bin * 10); + } + for (size_t bin : bins) { + svg_text(TEXT_LEFT + move, TEXT_BASELINE, to_string(bin)); + move += BIN_HEIGHT; + } + top += 3*BLOCK_WIDTH; + move = 13.0; + for (size_t bin : bins) { + + const double bin_width = BLOCK_WIDTH * bin * k; + svg_rect(move, top, BIN_HEIGHT, bin_width); + move += BIN_HEIGHT; } svg_end(); } \ No newline at end of file