code: ВАР-4, цветная гистограмма
Этот коммит содержится в:
2
main.cpp
2
main.cpp
@@ -43,7 +43,7 @@ int main()
|
|||||||
auto in = input_data();
|
auto in = input_data();
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
auto procent = make_histogram_proc(in.numbers, in.bin_count, bins);
|
auto procent = make_histogram_proc(in.numbers, in.bin_count, bins);
|
||||||
show_histogram_svg(bins);
|
show_histogram_svg(bins, procent);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
32
svg.cpp
32
svg.cpp
@@ -31,7 +31,12 @@ svg_rect(double x, double y, double width, double height){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
show_histogram_svg(const vector<size_t>& bins) {
|
svg_procent(double left_procent, double baseline, string text) {
|
||||||
|
cout << "<text x='"<< left_procent << "' y='"<< baseline << "'> "<< text <<"% </text>";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const vector<size_t>& bins, const vector<size_t>& procent) {
|
||||||
const auto IMAGE_WIDTH = 400;
|
const auto IMAGE_WIDTH = 400;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const auto IMAGE_HEIGHT = 300;
|
||||||
const auto TEXT_LEFT = 20;
|
const auto TEXT_LEFT = 20;
|
||||||
@@ -39,8 +44,9 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
const auto TEXT_WIDTH = 50;
|
const auto TEXT_WIDTH = 50;
|
||||||
const auto BIN_HEIGHT = 30;
|
const auto BIN_HEIGHT = 30;
|
||||||
const auto BLOCK_WIDTH = 10;
|
const auto BLOCK_WIDTH = 10;
|
||||||
const double SCALE = IMAGE_WIDTH - TEXT_WIDTH;
|
|
||||||
svg_begin(400, 300);
|
const double SCALE_JOPA = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
svg_begin(800, 300);
|
||||||
|
|
||||||
|
|
||||||
double max_count = 0;
|
double max_count = 0;
|
||||||
@@ -49,22 +55,27 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
max_count = x;
|
max_count = x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double top = 0;
|
double top = 0;
|
||||||
if ((max_count*BLOCK_WIDTH)>SCALE){
|
if ((max_count*BLOCK_WIDTH)>SCALE_JOPA){
|
||||||
for (size_t bin : bins) {
|
for (size_t i = 0; i < bins.size(); i++) {
|
||||||
const double bin_width = SCALE * ( bin / max_count);
|
const double bin_width = SCALE_JOPA * ( bins[i] / max_count);
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
const auto TEXT_LEFT_PROCENT = SCALE_JOPA+TEXT_WIDTH+20;
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]));
|
||||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
||||||
|
svg_procent(TEXT_LEFT_PROCENT, top + TEXT_BASELINE, to_string(procent[i]));
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
cout << bin_width;
|
cout << bin_width;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
for (size_t bin : bins) {
|
for (size_t i = 0; i < bins.size(); i++) {
|
||||||
const double bin_width = BLOCK_WIDTH * bin;
|
const double bin_width = BLOCK_WIDTH * bins[i];
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
const auto TEXT_LEFT_PROCENT = (max_count * BLOCK_WIDTH)+TEXT_WIDTH+20;
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]));
|
||||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
||||||
|
svg_procent(TEXT_LEFT_PROCENT, top + TEXT_BASELINE, to_string(procent[i]));
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
cout << bin_width;
|
cout << bin_width;
|
||||||
@@ -76,3 +87,4 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
2
svg.h
2
svg.h
@@ -2,6 +2,6 @@
|
|||||||
#define SVG_H_INCLUDED
|
#define SVG_H_INCLUDED
|
||||||
|
|
||||||
void
|
void
|
||||||
show_histogram_svg(const std::vector<size_t>& bins);
|
show_histogram_svg(const std::vector<size_t>& bins, const std::vector<size_t>& procent);
|
||||||
|
|
||||||
#endif // SVG_H_INCLUDED
|
#endif // SVG_H_INCLUDED
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user