izzi
SVG SUBSET C++ API
Loading...
Searching...
No Matches
a60-svg-graphs-bar.h
Go to the documentation of this file.
1// alpha60 bar graphs/charts -*- mode: C++ -*-
2
3// alpha60
4// bittorrent x scrape x data + analytics
5
6// Copyright (c) 2020-2021, 2025 Benjamin De Kosnik <b.dekosnik@gmail.com>
7
8// This file is part of the alpha60 library. This library is free
9// software; you can redistribute it and/or modify it under the terms
10// of the GNU General Public License as published by the Free Software
11// Foundation; either version 3, or (at your option) any later
12// version.
13
14// This library is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// General Public License for more details.
18
19#ifndef a60_SVG_BAR_GRAPHS_H
20#define a60_SVG_BAR_GRAPHS_H 1
21
22
23namespace svg {
24
25//// Distance between center and top/bottom block
26const double ctopspace(60);
27
28/// Distance between label and top/bottom of line or text
29const double ltopspace(10);
30
31// Type sizes.
32const auto lsz = 16; // large bold
33const auto asz = 12; // sub headings
34const auto ssz = 10; // sub sub headings
35
36/// Compute xoffset.
37uint
38get_xoffset(const string& id, const uint xstart, const double hspace)
39{ return xstart + (k::age_value_to_multiple(id, true) * hspace); };
40
41using id_size = ulong;
42using umids = std::unordered_map<string, id_size>;
43using vumids = std::vector<umids>;
44
46make_bar_graph(umids agesvo, const area<> a = svg::k::v1080p_h,
47 const double yscale = 0.7, const uint rwidth = 10)
48{
49 // Vector of total age_value counts per decile for birth ages.
50 vul vsum(k::age_values.size(), 0);
51 for (const auto& val : agesvo)
52 {
53 auto [ id, count ] = val;
54 uint xoff = get_xoffset(id, xstart, hspace);
55
56 // rect
57 double rheight = scale_value_on_range(count, 1, maxn, 10, ydelta);
58 double rystart = voffstart - (rheight / 2);
59 point_to_rect_centered(obj, std::make_tuple(xoff, rystart),
60 get_metadata_style(), rwidth, rheight);
61
62 // text
63 sized_text(obj, typov, asz, to_string(count), xoff,
64 voffstart - rheight - ltopspace);
65}
66
67
68} // namepace a60::metadata
69
70#endif
constexpr area v1080p_h
const auto asz
double scale_value_on_range(const ssize_type value, const ssize_type min, const ssize_type max, const ssize_type nfloor, const ssize_type nceil)
Scale value from min to max on range (nfloor, nceil).
Definition a60-svg.h:67
const auto lsz
const double ltopspace(10)
Distance between label and top/bottom of line or text.
const string to_string(const unit e)
unsigned long ulong
Definition a60-svg.h:58
const double ctopspace(60)
void point_to_rect_centered(element_base &obj, const point_2t origin, style s, double width=4, double height=4, const string filterstr="")
Center a rectangle at this point.
void sized_text(element_base &obj, svg::typography typo, const int sz, const string text, const int tx, const int ty)
Text at size.
uint get_xoffset(const string &id, const uint xstart, const double hspace)
Compute xoffset.
unsigned int uint
Definition a60-svg.h:57
const auto ssz