|
|
|
@ -1,34 +1,10 @@
|
|
|
|
|
#include "svg.h"
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
namespace svg {
|
|
|
|
|
size_t BLOCK_WIDTH = 10;
|
|
|
|
|
|
|
|
|
|
size_t input_block_width() {
|
|
|
|
|
size_t width;
|
|
|
|
|
while (true) {
|
|
|
|
|
cout << "Ââåäèòå øèðèíó áëîêà ãèñòîãðàììû (3-30px): ";
|
|
|
|
|
cin >> width;
|
|
|
|
|
|
|
|
|
|
if (width < 3) {
|
|
|
|
|
cout << "Øèðèíà ñëèøêîì ìàëà. Ìèíèìàëüíîå çíà÷åíèå: 3px. Ïîæàëóéñòà, ïîâòîðèòå ââîä.\n";
|
|
|
|
|
} else if (width > 30) {
|
|
|
|
|
cout << "Øèðèíà ñëèøêîì âåëèêà. Ìàêñèìàëüíîå çíà÷åíèå: 30px. Ïîæàëóéñòà, ïîâòîðèòå ââîä.\n";
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_block_width(size_t width) {
|
|
|
|
|
BLOCK_WIDTH = width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void begin(ostream& out, double width, double height) {
|
|
|
|
|
out << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
|
|
|
|
out << "<svg width='" << width << "' height='" << height << "' "
|
|
|
|
@ -51,37 +27,56 @@ namespace svg {
|
|
|
|
|
<< "' fill='" << fill << "' />\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void show_histogram_svg(ofstream& out, const vector<size_t>& bins, size_t block_width) {
|
|
|
|
|
begin(out, IMAGE_WIDTH, IMAGE_HEIGHT);
|
|
|
|
|
|
|
|
|
|
if (bins.empty()) {
|
|
|
|
|
end(out);
|
|
|
|
|
return;
|
|
|
|
|
void line(ostream& out, double x1, double y1, double x2, double y2,
|
|
|
|
|
const string& stroke, int dash_length, int gap_length) {
|
|
|
|
|
out << "<line x1='" << x1 << "' y1='" << y1 << "' "
|
|
|
|
|
<< "x2='" << x2 << "' y2='" << y2 << "' "
|
|
|
|
|
<< "stroke='" << stroke << "' stroke-width='2' "
|
|
|
|
|
<< "stroke-dasharray='" << dash_length << " " << gap_length << "' />\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t max_count = *max_element(bins.begin(), bins.end());
|
|
|
|
|
if (max_count == 0) {
|
|
|
|
|
end(out);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
void show_histogram_svg(ofstream& out, const vector<size_t>& bins, int dash_length, int gap_length) {
|
|
|
|
|
begin(out, IMAGE_WIDTH, IMAGE_HEIGHT);
|
|
|
|
|
|
|
|
|
|
if (bins.empty()) {
|
|
|
|
|
end(out);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const double max_svg_width = IMAGE_WIDTH - TEXT_WIDTH - 40;
|
|
|
|
|
size_t max_count = *max_element(bins.begin(), bins.end());
|
|
|
|
|
if (max_count == 0) {
|
|
|
|
|
end(out);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Óâåëè÷èâàåì ìàêñèìàëüíóþ øèðèíó äëÿ ëó÷øåãî îòîáðàæåíèÿ
|
|
|
|
|
double max_width = IMAGE_WIDTH - TEXT_WIDTH - 100;
|
|
|
|
|
double scale = max_width / max_count;
|
|
|
|
|
|
|
|
|
|
const double scale = max_svg_width / max_count;
|
|
|
|
|
double top = 0;
|
|
|
|
|
for (size_t bin : bins) {
|
|
|
|
|
double width = bin * scale;
|
|
|
|
|
|
|
|
|
|
double top = 0;
|
|
|
|
|
for (size_t bin : bins) {
|
|
|
|
|
// Ïîäïèñü çíà÷åíèÿ (êðóïíåå è ÷åò÷å)
|
|
|
|
|
out << "<text x='" << TEXT_LEFT << "' y='" << top + TEXT_BASELINE + 15
|
|
|
|
|
<< "' font-size='14' fill='black'>" << bin << "</text>\n";
|
|
|
|
|
|
|
|
|
|
double width = bin * scale * (block_width / 10.0);
|
|
|
|
|
// Îñíîâíîé ñòîëáåö
|
|
|
|
|
rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "#333333", "#4CAF50");
|
|
|
|
|
|
|
|
|
|
text(out, TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
|
|
|
|
rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "black", "#aaffaa");
|
|
|
|
|
// Ïóíêòèðíàÿ ëèíèÿ (òîëùå è êîíòðàñòíåå)
|
|
|
|
|
out << "<line x1='" << TEXT_WIDTH << "' y1='" << top + BIN_HEIGHT/2
|
|
|
|
|
<< "' x2='" << TEXT_WIDTH + width << "' y2='" << top + BIN_HEIGHT/2
|
|
|
|
|
<< "' stroke='white' stroke-width='3' "
|
|
|
|
|
<< "stroke-dasharray='" << dash_length << "," << gap_length << "' />\n";
|
|
|
|
|
|
|
|
|
|
top += BIN_HEIGHT;
|
|
|
|
|
}
|
|
|
|
|
top += BIN_HEIGHT + 10; // Äîáàâëÿåì îòñòóï ìåæäó ñòîëáöàìè
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
end(out);
|
|
|
|
|
}
|
|
|
|
|
// Äîáàâëÿåì ðàìêó âîêðóã âñåé ãèñòîãðàììû
|
|
|
|
|
out << "<rect x='" << TEXT_WIDTH-5 << "' y='0' width='" << max_width+10
|
|
|
|
|
<< "' height='" << top << "' fill='none' stroke='black' stroke-width='2'/>\n";
|
|
|
|
|
|
|
|
|
|
end(out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|