5 (6): create svg colored histogram

main
DobrovolskaY 12 месяцев назад
Родитель 3b5ba758d3
Сommit 5c687ccecd

@ -14,6 +14,11 @@
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
</Compiler> </Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
</Linker>
</Target> </Target>
<Target title="Release"> <Target title="Release">
<Option output="bin/Release/lab1" prefix_auto="1" extension_auto="1" /> <Option output="bin/Release/lab1" prefix_auto="1" extension_auto="1" />
@ -32,7 +37,20 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="text.cpp" />
<Unit filename="text.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -33,11 +33,24 @@ input_data() {
} }
int main() { //int main (){
auto in = input_data(); // auto in = input_data();
//auto bins = make_histogram(in.numbers, in.bin_count);
// svg_rect(50, 0, bins[0] * 10, 30);
// show_histogram_svg(bins);
// return 0;
//}
int main(){
bool res = false;
Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
svg_rect(50, 0, bins[0] * 10, 30);
show_histogram_svg(bins); show_histogram_svg(bins);
return 0;
} }

@ -2,29 +2,33 @@
#include <vector> #include <vector>
#include "svg.h" #include "svg.h"
void svg_begin(double width, double height) { void
svg_begin(double width, double height) {
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n"; std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
std::cout << "<svg "; std::cout << "<svg ";
std::cout << "width='" << width << "' "; std::cout << "width='" << width << "' ";
std::cout << "height='" << height << "' "; std::cout << "height='" << height << "' ";
std::cout << "viewBox='0 0 " << width << " " << height << "' "; std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n"; std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
} }
void svg_end() { void
svg_end() {
std::cout << "</svg>\n"; std::cout << "</svg>\n";
} }
void svg_text(double left, double baseline, std::string text) { void
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n"; svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='"<< baseline << "' > " << text << " </text>";
} }
void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill) { void svg_rect(double x, double y, double width, double height){
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />\n"; std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='#561A75' fill='#EDB1DB' ></rect>";
} }
void show_histogram_svg(const std::vector<size_t>& bins) { void
show_histogram_svg(const std::vector<std::size_t>& bins) {
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;
@ -32,20 +36,34 @@ void show_histogram_svg(const std::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 std::size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0; double top = 0;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin; std::size_t maxbin=bins[0];
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); for (std::size_t i=0; i < bins.size(); i++){
svg_rect(TEXT_WIDTH, top, bin_width); if (maxbin < bins[i]){
top += BIN_HEIGHT; maxbin=bins[i];
}
} }
svg_end(); if (maxbin<=76){
for (std::size_t i=0; i < bins.size(); i++) {
double bin_width = BLOCK_WIDTH * bins[i];
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
} else {
for (std::size_t i=0; i < bins.size(); i++) {
double bin_width= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
} }

16
svg.h

@ -1,15 +1,11 @@
#ifndef SVG_H #ifndef SVG_H_INCLUDED
#define SVG_H #define SVG_H_INCLUDED
#include <iostream>
#include <string>
#include <vector> #include <vector>
//void svg_begin(double width, double height); void
//void svg_end(); show_histogram_svg(const std::vector<std::size_t>& bins);
//void svg_text(double left, double baseline, std::string text);
void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black"); #endif // SVG_H_INCLUDED
void show_histogram_svg(const std::vector<size_t>& bins);
#endif // SVG_H

@ -31,6 +31,10 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
</Compiler> </Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cbp" />
<Unit filename="unittest.cpp" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

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