build: Осуществлен вывод гистограммы в формате SVG

main
Igor (ZharkovIG) 6 месяцев назад
Родитель 29c09ca832
Сommit cd149ef988

@ -1,7 +1,7 @@
#include "histogram.h"
void
find_minmax(const std::vector<double>& numbers, double& min, double& max){
find_minmax(std::vector<double>& numbers, double& min, double& max){
min = numbers[0];
max = numbers[0];
@ -17,7 +17,7 @@ find_minmax(const std::vector<double>& numbers, double& min, double& max){
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count){
make_histogram(std::vector<double>& numbers, size_t bin_count){
std::vector<size_t> bins(bin_count);
@ -42,37 +42,3 @@ make_histogram(const std::vector<double>& numbers, size_t bin_count){
}
return bins;
}
/*#include "histogram.h"
using namespace std;
static void
find_minmax(std::vector<double>& numbers, double& min, double& max){
min = numbers[0];
max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
std::vector<size_t>
make_histogram(std::vector<double>& numbers, size_t bin_count) {
double min, max;
find_minmax(numbers, min, max);
std::vector<size_t> bins(bin_count, 0);
double bin_size = (max - min) / bin_count;
for (double number : numbers) {
size_t bin_index = bin_count - 1; // default to last bin
if (number < max) {
bin_index = static_cast<size_t>((number - min) / bin_size);
}
bins[bin_index]++;
}
return bins;
}*/

@ -4,7 +4,7 @@
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);
make_histogram(std::vector<double>& numbers, size_t bin_count);

@ -1,7 +1,7 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
@ -28,48 +28,12 @@ input_data(){
return in;
}
/*
void
show_histogram_text(const vector<size_t>& bins){
size_t i;
size_t height = 0;
size_t max_count = 0;
for (size_t x: bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count > MAX_ASTERISK) {
for(size_t bin:bins){
size_t height = bin;
height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
for(size_t i = 0; i < height; i++){
cout<<"*";
}
cout<<"|";
if(bin<100) cout<<" ";
if(bin<10) cout<<" ";
cout<<bin<<endl;
}
} else {
for(size_t bin:bins){
size_t height = bin;
for(size_t i = 0; i < height; i++){
cout<<"*";
}
cout<<"|";
if(bin<100) cout<<" ";
if(bin<10) cout<<" ";
cout<<bin<<endl;
}
}
}*/
int
main()
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
show_histogram_svg(bins);
return 0;
}

@ -0,0 +1,59 @@
#include <iostream>
#include <string>
#include <vector>
#include "svg.h"
using namespace std;
void
svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
cout << "<svg ";
cout << "width='" << width << "' ";
cout << "height='" << height << "' ";
cout << "viewBox='0 0 " << width << " " << height << "' ";
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void
svg_end() {
cout << "</svg>\n";
}
void
svg_text(double left, double baseline, string text){
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
}
void
svg_rect(double x, double y, double width, double height, string stroke, string fil){
cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fil << "' />";
}
void
show_histogram_svg(const vector<size_t>& bins) {
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
size_t max_count = 0;
for (size_t x: bins) {
if (x > max_count) {
max_count = x;
}
}
double top = 0;
for (size_t bin : bins) {
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#ffeeee");
top += BIN_HEIGHT;
}
svg_end();
}

23
svg.h

@ -0,0 +1,23 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <string>
#include <vector>
void
svg_begin(double width, double height);
void
svg_end();
void
show_histogram_svg(const std::vector<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 fil = "black");
#endif // SVG_H_INCLUDED
Загрузка…
Отмена
Сохранить