Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
83 строки
3.0 KiB
C++
83 строки
3.0 KiB
C++
#include "svg.h"
|
|
#include <algorithm>
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
namespace svg {
|
|
void begin(ostream& out, double width, double height) {
|
|
out << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
|
out << "<svg width='" << width << "' height='" << height << "' "
|
|
<< "viewBox='0 0 " << width << " " << height << "' "
|
|
<< "xmlns='http://www.w3.org/2000/svg'>\n";
|
|
}
|
|
|
|
void end(ostream& out) {
|
|
out << "</svg>\n";
|
|
}
|
|
|
|
void text(ostream& out, double left, double baseline, const string& text) {
|
|
out << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
|
|
}
|
|
|
|
void rect(ostream& out, double x, double y, double width, double height,
|
|
const string& stroke, const string& fill) {
|
|
out << "<rect x='" << x << "' y='" << y << "' width='" << width
|
|
<< "' height='" << height << "' stroke='" << stroke
|
|
<< "' fill='" << fill << "' />\n";
|
|
}
|
|
|
|
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";
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
|
|
double top = 0;
|
|
for (size_t bin : bins) {
|
|
double width = bin * scale;
|
|
|
|
// Ïîäïèñü çíà÷åíèÿ (êðóïíåå è ÷åò÷å)
|
|
out << "<text x='" << TEXT_LEFT << "' y='" << top + TEXT_BASELINE + 15
|
|
<< "' font-size='14' fill='black'>" << bin << "</text>\n";
|
|
|
|
// Îñíîâíîé ñòîëáåö
|
|
rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "#333333", "#4CAF50");
|
|
|
|
// Ïóíêòèðíàÿ ëèíèÿ (òîëùå è êîíòðàñòíåå)
|
|
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 + 10; // Äîáàâëÿåì îòñòóï ìåæäó ñòîëáöàìè
|
|
}
|
|
|
|
// Äîáàâëÿåì ðàìêó âîêðóã âñåé ãèñòîãðàììû
|
|
out << "<rect x='" << TEXT_WIDTH-5 << "' y='0' width='" << max_width+10
|
|
<< "' height='" << top << "' fill='none' stroke='black' stroke-width='2'/>\n";
|
|
|
|
end(out);
|
|
}
|
|
}
|