Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

78 строки
1.9 KiB
C++

#include "svg.h"
using namespace std;
void
svg_begin(double width, double height)
{
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
cout << "<svg ";
cout << "width='" << width << "' ";
cout << "height='" << height << "' ";
cout << "viewBox='0 0 " << width << " " << height << "' ";
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void
svg_end()
{
cout << "</svg>\n";
}
void
svg_text(double left, double baseline, string text)
{
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
}
void
svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black")
{
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
}
void show_histogram_svg(const vector<size_t>& 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<double>(v) > mx) {
mx = static_cast<double>(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<double>(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();
}