Родитель
554cdd5d11
Сommit
b7fd7da9cb
После Ширина: | Высота: | Размер: 933 B |
@ -0,0 +1,6 @@
|
||||
142
|
||||
1 1 1 1 1 1 1 1 1
|
||||
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
|
||||
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
|
||||
3
|
||||
6
|
@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
#include "svg.h"
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
static void
|
||||
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
|
||||
max = numbers[0];
|
||||
for (double element : numbers) {
|
||||
if (element > max) {
|
||||
max = element;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
svg_begin (double width, double height) {
|
||||
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
std::cout << "<svg ";
|
||||
std::cout << "width='" << width << "' ";
|
||||
std::cout << "height='" << height << "' ";
|
||||
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
|
||||
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
}
|
||||
|
||||
void
|
||||
svg_end() {
|
||||
std::cout << "</svg>\n";
|
||||
}
|
||||
|
||||
void
|
||||
svg_text(double left, double baseline, std::string text) {
|
||||
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
||||
}
|
||||
|
||||
void
|
||||
svg_rect (double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black") {
|
||||
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
show_histogram_svg (const std::vector<size_t>& bins, std::size_t& interval_task) {
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
//const auto IMAGE_HEIGHT = 300;
|
||||
const auto TEXT_LEFT = 20;
|
||||
const auto TEXT_BASELINE = 20;
|
||||
const auto TEXT_WIDTH = 50;
|
||||
const auto BIN_HEIGHT = 30;
|
||||
//const auto BLOCK_WIDTH = 50;
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
//const auto SPACE_BETWEEN_INTERVALS = 10;
|
||||
const auto INTERVAL_SCALED = interval_task * 10;
|
||||
const auto SCALE_WIDTH = 8;
|
||||
//const auto HALF_SPACE_BETWEEN_INTERVALS = static_cast<double>(SPACE_BETWEEN_INTERVALS) / (2);
|
||||
const auto HALF_DEFAULT_FONT = 4;
|
||||
const auto MINIMAL_FONT_SPACE = 4 * 4;
|
||||
|
||||
std::size_t max_bin;
|
||||
find_max(bins, max_bin);
|
||||
double modifier;
|
||||
|
||||
modifier = static_cast<double>(MAX_WIDTH) / (max_bin);
|
||||
|
||||
size_t times;
|
||||
|
||||
for (times = 0; (times * INTERVAL_SCALED) < (MAX_WIDTH - MINIMAL_FONT_SPACE); ++times);
|
||||
|
||||
double interval_space = static_cast<double>(MAX_WIDTH - INTERVAL_SCALED * (times - 1)) / (times - 2);
|
||||
double half_interval_space = interval_space / 2;
|
||||
|
||||
svg_begin(410, 300);
|
||||
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = modifier * bin;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#483D8B", "#9370DB");
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
top += 10;
|
||||
|
||||
times -= 1;
|
||||
for (size_t i = 0; i < times; ++i) {
|
||||
svg_rect(TEXT_WIDTH + i * (INTERVAL_SCALED + interval_space), top, INTERVAL_SCALED, SCALE_WIDTH, "chocolate", "tan");
|
||||
}
|
||||
|
||||
top += 10;
|
||||
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(0));
|
||||
svg_text(TEXT_WIDTH + INTERVAL_SCALED + half_interval_space - HALF_DEFAULT_FONT, top + TEXT_BASELINE, std::to_string(interval_task));
|
||||
svg_text(TEXT_WIDTH + (INTERVAL_SCALED * times) + interval_space * (times - 1) - (HALF_DEFAULT_FONT * 2), top + TEXT_BASELINE, std::to_string(interval_task * times));
|
||||
|
||||
svg_end();
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<std::size_t>& bins, std::size_t& interval_task);
|
Загрузка…
Ссылка в новой задаче