#include "svg.h" using namespace std; void svg_begin(double width, double height) { cout << "\n"; cout << "\n"; } void svg_end() { cout << "\n"; } void svg_text(double left, double baseline, string text) { cout << "" << text << ""; } void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") { cout << ""; } void show_histogram_svg(const vector& bins) { const double IMAGE_WIDTH = 400.0; const double IMAGE_HEIGHT = 300.0; const double LEFT_MARGIN = 50.0; const double LABEL_MARGIN = 30.0; const double BIN_HEIGHT = 30.0; const double LABEL_OFFSET = 5.0; const double axis = IMAGE_WIDTH - LABEL_MARGIN; const double MAX_WIDTH = axis - LEFT_MARGIN; double mx = 0.0; for (size_t v : bins) { if (static_cast(v) > mx) { mx = static_cast(v); } } svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); double top = 0.0; for (size_t v : bins) { double bar_w; if (mx > 0) { bar_w = MAX_WIDTH * static_cast(v) / mx; } else { bar_w = 0.0; } double x0 = axis - bar_w; svg_rect(x0, top, bar_w, BIN_HEIGHT, "green", "yellow"); double label_x = x0 + bar_w + LABEL_OFFSET; double label_y = top + BIN_HEIGHT / 2 + 5; svg_text(label_x, label_y, to_string(v)); top += BIN_HEIGHT; } svg_end(); }