FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
graph.cpp
Go to the documentation of this file.
1#include <functional> // for function
2#include <memory> // for allocator, make_shared
3#include <string> // for string
4#include <utility> // for move
5#include <vector> // for vector
6
7#include "ftxui/dom/elements.hpp" // for GraphFunction, Element, graph
8#include "ftxui/dom/node.hpp" // for Node
9#include "ftxui/dom/requirement.hpp" // for Requirement
10#include "ftxui/screen/box.hpp" // for Box
11#include "ftxui/screen/screen.hpp" // for Screen
12
13namespace ftxui {
14
15// NOLINTNEXTLINE
16static std::string charset[] =
17#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
18 // Microsoft's terminals often use fonts not handling the 8 unicode
19 // characters for representing the whole graph. Fallback with less.
20 {" ", " ", "█", " ", "█", "█", "█", "█", "█"};
21#else
22 {" ", "▗", "▐", "▖", "▄", "▟", "▌", "▙", "█"};
23#endif
24
25class Graph : public Node {
26 public:
27 explicit Graph(GraphFunction graph_function)
28 : graph_function_(std::move(graph_function)) {}
29
30 void ComputeRequirement() override {
37 }
38
39 void Render(Screen& screen) override {
40 int width = (box_.x_max - box_.x_min + 1) * 2;
41 int height = (box_.y_max - box_.y_min + 1) * 2;
42 auto data = graph_function_(width, height);
43 int i = 0;
44 for (int x = box_.x_min; x <= box_.x_max; ++x) {
45 int height_1 = 2 * box_.y_max - data[i++];
46 int height_2 = 2 * box_.y_max - data[i++];
47 for (int y = box_.y_min; y <= box_.y_max; ++y) {
48 int yy = 2 * y;
49 int i_1 = yy < height_1 ? 0 : yy == height_1 ? 3 : 6; // NOLINT
50 int i_2 = yy < height_2 ? 0 : yy == height_2 ? 1 : 2; // NOLINT
51 screen.at(x, y) = charset[i_1 + i_2]; // NOLINT
52 }
53 }
54 }
55
56 private:
57 GraphFunction graph_function_;
58};
59
60/// @brief Draw a graph using a GraphFunction.
61/// @param graph_function the function to be called to get the data.
62Element graph(GraphFunction graph_function) {
63 return std::make_shared<Graph>(std::move(graph_function));
64}
65
66} // namespace ftxui
67
68// Copyright 2020 Arthur Sonzogni. All rights reserved.
69// Use of this source code is governed by the MIT license that can be found in
70// the LICENSE file.
Requirement requirement_
Definition node.hpp:54
Box box_
Definition node.hpp:55
std::shared_ptr< Node > Element
Definition elements.hpp:18
std::function< std::vector< int >(int, int)> GraphFunction
Definition elements.hpp:21
Element graph(GraphFunction)
Draw a graph using a GraphFunction.
Definition graph.cpp:62
int x_max
Definition box.hpp:8
int y_min
Definition box.hpp:9
int y_max
Definition box.hpp:10
int x_min
Definition box.hpp:7