|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cmath> |
| 4 | +#include <util/string/printf.h> |
| 5 | +#include <util/stream/str.h> |
| 6 | +#include <util/generic/set.h> |
| 7 | +#include <util/generic/map.h> |
| 8 | +#include "data.h" |
| 9 | +#include "util.h" |
| 10 | + |
| 11 | +namespace NAnalytics { |
| 12 | + |
| 13 | +struct TAxis { |
| 14 | + TString Name; |
| 15 | + double From; |
| 16 | + double To; |
| 17 | + char Symbol; |
| 18 | + bool Ticks; |
| 19 | + |
| 20 | + TAxis(const TString& name, double from, double to, char symbol = '+', bool ticks = true) |
| 21 | + : Name(name) |
| 22 | + , From(from) |
| 23 | + , To(to) |
| 24 | + , Symbol(symbol) |
| 25 | + , Ticks(ticks) |
| 26 | + {} |
| 27 | + |
| 28 | + double Place(double value) const |
| 29 | + { |
| 30 | + return (value - From) / (To - From); |
| 31 | + } |
| 32 | + |
| 33 | + bool Get(const TRow& row, double& value) const |
| 34 | + { |
| 35 | + return row.Get(Name, value); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +struct TChart { |
| 40 | + int Width; |
| 41 | + int Height; |
| 42 | + bool Frame; |
| 43 | + |
| 44 | + TChart(int width, int height, bool frame = true) |
| 45 | + : Width(width) |
| 46 | + , Height(height) |
| 47 | + , Frame(frame) |
| 48 | + { |
| 49 | + if (Width < 2) |
| 50 | + Width = 2; |
| 51 | + if (Height < 2) |
| 52 | + Height = 2; |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +struct TPoint { |
| 57 | + int X; |
| 58 | + int Y; |
| 59 | + char* Pixel; |
| 60 | +}; |
| 61 | + |
| 62 | +class TScreen { |
| 63 | +public: |
| 64 | + TChart Chart; |
| 65 | + TAxis Ox; |
| 66 | + TAxis Oy; |
| 67 | + TVector<char> Screen; |
| 68 | +public: |
| 69 | + TScreen(const TChart& chart, const TAxis& ox, const TAxis& oy) |
| 70 | + : Chart(chart) |
| 71 | + , Ox(ox) |
| 72 | + , Oy(oy) |
| 73 | + , Screen(Chart.Width * Chart.Height, ' ') |
| 74 | + {} |
| 75 | + |
| 76 | + int X(double x) const |
| 77 | + { |
| 78 | + return llrint(Ox.Place(x) * (Chart.Width - 1)); |
| 79 | + } |
| 80 | + |
| 81 | + int Y(double y) const |
| 82 | + { |
| 83 | + return Chart.Height - 1 - llrint(Oy.Place(y) * (Chart.Height - 1)); |
| 84 | + } |
| 85 | + |
| 86 | + TPoint At(double x, double y) |
| 87 | + { |
| 88 | + TPoint pt{X(x), Y(y), nullptr}; |
| 89 | + if (Fits(pt)) { |
| 90 | + pt.Pixel = &Screen[pt.Y * Chart.Width + pt.X]; |
| 91 | + } |
| 92 | + return pt; |
| 93 | + } |
| 94 | + |
| 95 | + bool Fits(TPoint pt) const |
| 96 | + { |
| 97 | + return pt.X >= 0 && pt.X < Chart.Width |
| 98 | + && pt.Y >= 0 && pt.Y < Chart.Height; |
| 99 | + } |
| 100 | + |
| 101 | + TString Str() const |
| 102 | + { |
| 103 | + TStringStream ss; |
| 104 | + size_t i = 0; |
| 105 | + TString lmargin; |
| 106 | + TString x1label; |
| 107 | + TString x2label; |
| 108 | + TString xtick = "-"; |
| 109 | + TString y1label; |
| 110 | + TString y2label; |
| 111 | + TString ytick = "|"; |
| 112 | + if (Ox.Ticks) { |
| 113 | + x1label = Sprintf("%-7.2le", Ox.From); |
| 114 | + x2label = Sprintf("%-7.2le", Ox.To); |
| 115 | + xtick = "+"; |
| 116 | + } |
| 117 | + if (Oy.Ticks) { |
| 118 | + y1label = Sprintf("%-7.2le ", Oy.From); |
| 119 | + y2label = Sprintf("%-7.2le ", Oy.To); |
| 120 | + int sz = Max(y1label.size(), y2label.size()); |
| 121 | + y1label = TString(sz - y1label.size(), ' ') + y1label; |
| 122 | + y2label = TString(sz - y2label.size(), ' ') + y2label; |
| 123 | + lmargin = TString(sz, ' '); |
| 124 | + ytick = "+"; |
| 125 | + } |
| 126 | + if (Chart.Frame) { |
| 127 | + ss << lmargin << "." << TString(Chart.Width, '-') << ".\n"; |
| 128 | + } |
| 129 | + for (int iy = 0; iy < Chart.Height; iy++) { |
| 130 | + if (iy == 0) { |
| 131 | + ss << y2label; |
| 132 | + if (Chart.Frame) |
| 133 | + ss << ytick; |
| 134 | + } else if (iy == Chart.Height - 1) { |
| 135 | + ss << y1label; |
| 136 | + if (Chart.Frame) |
| 137 | + ss << ytick; |
| 138 | + } else { |
| 139 | + ss << lmargin; |
| 140 | + if (Chart.Frame) |
| 141 | + ss << "|"; |
| 142 | + } |
| 143 | + for (int ix = 0; ix < Chart.Width; ix++) |
| 144 | + ss << Screen[i++]; |
| 145 | + if (Chart.Frame) |
| 146 | + ss << "|"; |
| 147 | + ss << "\n"; |
| 148 | + } |
| 149 | + if (Chart.Frame) { |
| 150 | + ss << lmargin << "'" << xtick |
| 151 | + << TString(Chart.Width - 2, '-') |
| 152 | + << xtick << "'\n"; |
| 153 | + } |
| 154 | + if (Ox.Ticks) { |
| 155 | + ss << lmargin << " " << x1label |
| 156 | + << TString(Max(Chart.Width - int(x1label.size()) - int(x2label.size()), 1), ' ') |
| 157 | + << x2label << "\n"; |
| 158 | + } |
| 159 | + return ss.Str(); |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +class TLegend { |
| 164 | +public: |
| 165 | + TMap<TString, char> Symbols; |
| 166 | + char DefSymbol = '+'; |
| 167 | +public: |
| 168 | + void Register(const TString& name) |
| 169 | + { |
| 170 | + if (name) |
| 171 | + Symbols[name] = DefSymbol; |
| 172 | + } |
| 173 | + |
| 174 | + void Build() |
| 175 | + { |
| 176 | + char c = 'A'; |
| 177 | + for (auto& kv : Symbols) { |
| 178 | + if (!kv.first) |
| 179 | + continue; |
| 180 | + kv.second = c; |
| 181 | + if (c == '9') |
| 182 | + c = 'a'; |
| 183 | + else if (c == 'z') |
| 184 | + c = 'A'; |
| 185 | + else if (c == 'Z') |
| 186 | + c = '1'; |
| 187 | + else |
| 188 | + c++; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + char Get(const TString& name) const |
| 193 | + { |
| 194 | + auto iter = Symbols.find(name); |
| 195 | + return iter != Symbols.end()? iter->second: DefSymbol; |
| 196 | + } |
| 197 | + |
| 198 | + TString Str(size_t columns) const |
| 199 | + { |
| 200 | + if (columns == 0) |
| 201 | + columns = 1; |
| 202 | + size_t height = (Symbols.size() + columns - 1) / columns; |
| 203 | + TVector<TString> all; |
| 204 | + TVector<size_t> widths; |
| 205 | + size_t width = 0; |
| 206 | + size_t count = 0; |
| 207 | + for (auto kv : Symbols) { |
| 208 | + TString s = Sprintf("(%ld) %c = %s", count + 1, kv.second, kv.first.data()); |
| 209 | + width = Max(width, s.size()); |
| 210 | + all.push_back(s); |
| 211 | + if (++count % height == 0) { |
| 212 | + widths.push_back(width); |
| 213 | + width = 0; |
| 214 | + } |
| 215 | + } |
| 216 | + widths.push_back(width); |
| 217 | + |
| 218 | + TStringStream ss; |
| 219 | + for (size_t row = 0; row < height; ++row) { |
| 220 | + bool first = true; |
| 221 | + for (size_t col = 0; col < widths.size(); col++) { |
| 222 | + size_t idx = col * height + row; |
| 223 | + if (idx < all.size()) { |
| 224 | + ss << (first? "": " ") << Sprintf("%-*s", (int)widths[col], all[idx].data()); |
| 225 | + first = false; |
| 226 | + } |
| 227 | + } |
| 228 | + ss << Endl; |
| 229 | + } |
| 230 | + return ss.Str(); |
| 231 | + } |
| 232 | +}; |
| 233 | + |
| 234 | +inline TString PlotAsAsciiArt(const TTable& in, const TChart& chart, const TAxis& ox, const TAxis& oy, bool showLegend = true, size_t columns = 4) |
| 235 | +{ |
| 236 | + TLegend legend; |
| 237 | + legend.DefSymbol = oy.Symbol; |
| 238 | + for (const TRow& row : in) { |
| 239 | + legend.Register(row.Name); |
| 240 | + } |
| 241 | + legend.Build(); |
| 242 | + |
| 243 | + TScreen screen(chart, ox, oy); |
| 244 | + for (const TRow& row : in) { |
| 245 | + double x, y; |
| 246 | + if (ox.Get(row, x) && oy.Get(row, y)) { |
| 247 | + TPoint pt = screen.At(Finitize(x), Finitize(y)); |
| 248 | + if (pt.Pixel) { |
| 249 | + *pt.Pixel = legend.Get(row.Name); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + return screen.Str() + (showLegend? TString("\n") + legend.Str(columns): TString()); |
| 254 | +} |
| 255 | + |
| 256 | +} |
0 commit comments