code: сделан индивидуальный вариант

master
Artem 1 год назад
Родитель 5deb516796
Сommit 7bb15b9746

@ -1,8 +1,11 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED #ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector> #include <vector>
#include <string>
void void
find_minmax(const std::vector<double>& numbers, double& min, double& max); find_minmax(const std::vector<double>& numbers, double& min, double& max);
std::string
bin_color(size_t bin, size_t max_count);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED #endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -28,6 +28,10 @@ svg_rect(double x, double y, double width, double height, string stroke, string
cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fil << "' />"; cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fil << "' />";
} }
string bin_color(size_t bin, size_t max_count){
return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count));
}
void void
show_histogram_svg(const vector<size_t>& bins) { show_histogram_svg(const vector<size_t>& bins) {
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
@ -46,12 +50,12 @@ show_histogram_svg(const vector<size_t>& bins) {
} }
} }
double top = 0; double top = 0;
for (size_t bin : bins) { for (size_t bin : bins) {
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count; const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count;
const auto color = bin_color(bin,max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#ffeeee"); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"grey", color);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();

@ -15,6 +15,9 @@ show_histogram_svg(const std::vector<size_t>& bins);
void void
svg_text(double left, double baseline, std::string text); svg_text(double left, double baseline, std::string text);
std::string
bin_color(size_t bin, size_t max_count);
void void
svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black"); svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black");

@ -3,6 +3,8 @@
#include "doctest.h" #include "doctest.h"
#include "histogram_internal.h" #include "histogram_internal.h"
using namespace std;
TEST_CASE("distinct positive numbers") { TEST_CASE("distinct positive numbers") {
double min = 0; double min = 0;
double max = 0; double max = 0;
@ -10,3 +12,13 @@ TEST_CASE("distinct positive numbers") {
CHECK(min == 1); CHECK(min == 1);
CHECK(max == 2); CHECK(max == 2);
} }
TEST_CASE("bin color") {
size_t max_bin = 10;
size_t min_bin = 3;
string rat = "#" + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin);
auto test1 = bin_color(max_bin,max_bin);
auto test2 = bin_color(min_bin,max_bin);
CHECK(test1 == "#111");
CHECK(test2 == rat);
}

Загрузка…
Отмена
Сохранить