code: сделан индивидуальный вариант
Этот коммит содержится в:
@@ -1,8 +1,11 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
void
|
||||
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
|
||||
|
||||
8
svg.cpp
8
svg.cpp
@@ -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 << "' />";
|
||||
}
|
||||
|
||||
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
|
||||
show_histogram_svg(const vector<size_t>& bins) {
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
@@ -46,12 +50,12 @@ show_histogram_svg(const vector<size_t>& bins) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
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_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#ffeeee");
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"grey", color);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
|
||||
3
svg.h
3
svg.h
@@ -15,6 +15,9 @@ show_histogram_svg(const std::vector<size_t>& bins);
|
||||
void
|
||||
svg_text(double left, double baseline, std::string text);
|
||||
|
||||
std::string
|
||||
bin_color(size_t bin, size_t max_count);
|
||||
|
||||
void
|
||||
svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black");
|
||||
|
||||
|
||||
12
unittest.cpp
12
unittest.cpp
@@ -3,6 +3,8 @@
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
@@ -10,3 +12,13 @@ TEST_CASE("distinct positive numbers") {
|
||||
CHECK(min == 1);
|
||||
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);
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user