#include #include #include #include #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, const string& text) { cout << "" << text << "\n"; } void svg_rect(double x, double y, double width, double height, string stroke, string fill) { cout << "\n"; } void show_histogram_svg(const vector& bins) { const auto IMAGE_WIDTH = 400; const auto TEXT_LEFT = 20; const auto TEXT_BASELINE = 15; const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; const auto MARGIN = 10; // Автоматически рассчитываем высоту изображения const auto IMAGE_HEIGHT = bins.size() * BIN_HEIGHT + 2 * MARGIN; svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); // Находим максимальное значение для масштабирования size_t max_count = 0; for (size_t count : bins) { if (count > max_count) { max_count = count; } } // Масштабируем ширину столбцов const double max_width = IMAGE_WIDTH - TEXT_WIDTH - MARGIN; double top = MARGIN; for (size_t i = 0; i < bins.size(); i++) { size_t bin = bins[i]; // Масштабируем ширину столбца double bin_width = 0; if (max_count > 0) { bin_width = (static_cast(bin) / max_count) * max_width; } // Выводим подпись svg_text(TEXT_LEFT, top + BIN_HEIGHT / 2, to_string(bin)); // Выбираем цвет в зависимости от значения string fill_color; if (max_count > 0) { double ratio = static_cast(bin) / max_count; if (ratio > 0.8) { fill_color = "#ff4444"; } else if (ratio > 0.5) { fill_color = "#ffaa44"; } else if (ratio > 0.2) { fill_color = "#44ff44"; } else { fill_color = "#4444ff"; } } else { fill_color = "#888888"; } // Выводим столбец svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#333333", fill_color); top += BIN_HEIGHT; } svg_end(); }