5 (6): create svg colored histogram

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

@ -14,6 +14,11 @@
<Compiler>
<Add option="-g" />
</Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin/Release/lab1" prefix_auto="1" extension_auto="1" />
@ -32,7 +37,20 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</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="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>
<lib_finder disable_auto="1" />
</Extensions>

@ -33,11 +33,24 @@ input_data() {
}
//int main (){
// 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(){
auto in = input_data();
bool res = false;
Input 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;
}

@ -2,29 +2,33 @@
#include <vector>
#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 << "<svg ";
std::cout << "width='" << width << "' ";
std::cout << "height='" << height << "' ";
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void svg_end() {
void
svg_end() {
std::cout << "</svg>\n";
}
void svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
void
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) {
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />\n";
void svg_rect(double x, double y, double width, double height){
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_HEIGHT = 300;
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 BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
const std::size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width);
top += BIN_HEIGHT;
std::size_t maxbin=bins[0];
for (std::size_t i=0; i < bins.size(); i++){
if (maxbin < bins[i]){
maxbin=bins[i];
}
}
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
#define SVG_H
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
//void svg_begin(double width, double height);
//void svg_end();
//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");
void show_histogram_svg(const std::vector<size_t>& bins);
void
show_histogram_svg(const std::vector<std::size_t>& bins);
#endif // SVG_H_INCLUDED
#endif // SVG_H

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

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