izzi
SVG SUBSET C++ API
Loading...
Searching...
No Matches
a60-svg-radial-arc.h
Go to the documentation of this file.
1// svg radial, sunburst / RAIL forms -*- mode: C++ -*-
2
3// alpha60
4// bittorrent x scrape x data + analytics
5
6// Copyright (c) 2018-2021, 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 MiL_SVG_RENDER_RADIAL_ARC_H
20#define MiL_SVG_RENDER_RADIAL_ARC_H 1
21
22
23namespace svg {
24
25/// Hash map of unique id to (not necessarily) unique value.
26/// Use this for sorting by id.
27using value_type = long long;
28using value_set = std::set<value_type>;
29
30
31/// Hash multimap of unique value to (perhaps multiple) unique ids.
32/// Use this form for sorting by value.
33using id_value_umap = std::unordered_map<string, value_type>;
34using value_id_ummap = std::unordered_multimap<value_type, string>;
35
36
37/// Remove all from map that match the input (matches) strings.
38/// Return found match entries.
41{
42 id_value_umap foundmap;
43 for (const auto& key: matches)
44 {
45 id_value_umap::iterator iter = ivm.find(key);
46 if (iter != ivm.end())
47 {
48 // Insert found element into return map....
49 foundmap.insert(*iter);
50
51 // Remove found elment from originating map (ivm)
52 ivm.erase(iter);
53 }
54 }
55 return foundmap;
56}
57
58
59/// Convert id_value_umap to value_id_mmap + set of unique values.
62{
63 value_id_ummap vimm;
64 for (auto& e: ivm)
65 {
66 string s = e.first;
67 ssize_type i = e.second;
68 vimm.insert(make_pair(i, s));
69 uniquev.insert(i);
70 }
71 return vimm;
72}
73
74
75/**
76 Draw text on the circumference of a circle of radius r centered at (cx, cy)
77 corresponding to the angle above.
78*/
79void
81 const typography& typo, string pname,
82 ssize_type pvalue, ssize_type pmax, double r,
83 bool rotatep)
84{
85 const double angled = get_angle(pvalue, pmax);
86 string label = make_label_for_value(pname, pvalue, get_label_spaces());
87 if (rotatep)
88 radial_text_r(obj, label, typo, r, origin, angled);
89 else
90 radial_text_r(obj, label, typo, r, origin, 0);
91}
92
93
94/**
95 Create radial viz of names from input file arranged clockwise around
96 the edge of a circle circumference. The text of the names can be
97 rotated, or not.
98
99 Arguments are:
100
101 rdenom == scaling factor for radius of circle used for display, where
102 larger values (used as a denominator) make smaller (tighter) circles.
103
104 rotatep == rotate name text to be on an arc from the origin of the
105 circle.
106*/
107svg_element
109 const typography& typo,
110 const id_value_umap& ivm,
111 const ssize_type value_max,
112 const int radius, bool rotatep)
113{
114 // Probe/Marker display.
115 // Loop through map key/values and put on canvas.
116 for (const auto& v : ivm)
117 {
118 string pname(v.first);
119 ssize_type pvalue(v.second);
120 if (pvalue)
121 radiate_id_at_value(obj, origin, typo, pname, pvalue, value_max,
122 radius, rotatep);
123 }
124
125 return obj;
126}
127
128
129/// Map ids with one value to a point cluster radiating out from a center.
130void
132 const typography& typo, const strings& ids,
133 ssize_type pvalue, ssize_type pmax, double r,
134 double rspace)
135{
136 // Find point on the circumference of the circle closest to value
137 // (pvalue).
138 const double angled = get_angle(pvalue, pmax);
139
140 // Consolidate label text to be "VALUE -> " with labelspaces spaces.
141 string label = make_label_for_value("", pvalue, get_label_spaces());
142 radial_text_r(obj, label, typo, r, origin, angled);
143
144 // Next, print out the various id's on an arc with a bigger radius.
145 //splay_ids_around(obj, typo, ids, angled, origin, r + rspace, rspace);
146 //splay_ids_after(obj, typo, ids, angled, origin, r + rspace, rspace);
147 //splay_ids_stagger(obj, typo, ids, angled, origin, r + rspace, rspace);
148 //stack_ids_at(obj, typo, ids, angled, origin, r + 65, 10);
149
150 append_ids_at(obj, typo, ids, angled, origin, r + rspace);
151}
152
153
154/**
155 Radiate as above, but group similar values such that they are
156 splayed, and not written on top of each other on the same
157 arc/angle.
158*/
159svg_element
161 const typography& typo, const id_value_umap& ivm,
162 const ssize_type value_max,
163 const int radius, const int rspace)
164{
165 // Make circle perimeter with an arrow to orientate display of data.
166 svg::style styl(typo._M_style);
168 styl._M_fill_opacity = 0;
169 styl._M_stroke_opacity = 1;
170 styl._M_stroke_size = 3;
171 direction_arc_at(obj, origin, radius, styl);
172
173 // Convert from string id-keys to int value-keys, plus an ordered
174 // set of all the unique values.
175 value_set uvalues;
176 value_id_ummap uvaluemm = to_value_id_mmap(ivm, uvalues);
177 for (const auto& v : uvalues)
178 {
179 // Extract all the ids for a given value.
180 auto irange = uvaluemm.equal_range(v);
181 auto ibegin = irange.first;
182 auto iend = irange.second;
183 strings ids;
184 for (auto i = ibegin; i != iend; ++i)
185 ids.push_back(i->second);
187
188 if (v)
189 radiate_ids_at_uvalue(obj, origin, typo, ids, v, value_max,
190 radius, rspace);
191 }
192 return obj;
193}
194
195} // namespace svg
196
197#endif
void append_ids_at(svg_element &obj, const typography &typo, const strings &ids, const double angled, const point_2t origin, double r)
Concatenate ids onto one line.
std::unordered_multimap< value_type, string > value_id_ummap
void radial_text_r(svg_element &obj, string text, const typography &typo, const int r, const point_2t origin, const double deg, const bool roriginp=false)
std::unordered_map< string, value_type > id_value_umap
Hash multimap of unique value to (perhaps multiple) unique ids. Use this form for sorting by value.
ssize_type & get_label_spaces()
Get the label space. Value -> Name, as a string where value has labelspaces of fill NB: Should be the...
double get_angle(ssize_type pvalue, ssize_type pmax)
Transform a value on a range to an angle on the radial range.
void radiate_id_at_value(svg_element &obj, const point_2t origin, const typography &typo, string pname, ssize_type pvalue, ssize_type pmax, double r, bool rotatep)
int ssize_type
Definition a60-svg.h:59
id_value_umap remove_matches_id_value_map(id_value_umap &ivm, const strings &matches)
Remove all from map that match the input (matches) strings. Return found match entries.
string make_label_for_value(string pname, ssize_type pvalue, const uint valuewidth=9)
Make radial labels.
std::set< value_type > value_set
long long value_type
Hash map of unique id to (not necessarily) unique value. Use this for sorting by id.
void direction_arc_at(svg_element &obj, const point_2t origin, const double rr, svg::style s, const double spacer=10)
Arc + arrow glyph that traces path of start to finish trajectory.
svg_element radiate_ids_per_value_on_arc(svg_element &obj, const point_2t origin, const typography &typo, const id_value_umap &ivm, const ssize_type value_max, const int radius, bool rotatep)
svg_element radiate_ids_per_uvalue_on_arc(svg_element &obj, const point_2t origin, const typography &typo, const id_value_umap &ivm, const ssize_type value_max, const int radius, const int rspace)
std::tuple< space_type, space_type > point_2t
Point (x,y) in 2D space.
Definition a60-svg.h:65
value_id_ummap to_value_id_mmap(const id_value_umap &ivm, value_set &uniquev)
Convert id_value_umap to value_id_mmap + set of unique values.
std::vector< string > strings
Definition a60-svg.h:37
void radiate_ids_at_uvalue(svg_element &obj, const point_2t origin, const typography &typo, const strings &ids, ssize_type pvalue, ssize_type pmax, double r, double rspace)
Map ids with one value to a point cluster radiating out from a center.
void sort_strings_by_size(strings &ids)
Sort vectors of strings to largest length string first. (Or use set<>).
Datum consolidating style preferences.
color_qi _M_fill_color