19static std::string charset[] =
20#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
22 {
" ",
" ",
"█",
" ",
"█",
"█",
"█",
"█",
"█"};
24 {
" ",
"▗",
"▐",
"▖",
"▄",
"▟",
"▌",
"▙",
"█"};
27class Graph :
public Node {
30 : graph_function_(std::move(graph_function)) {}
32 void ComputeRequirement()
override {
33 requirement_.flex_grow_x = 1;
34 requirement_.flex_grow_y = 1;
35 requirement_.flex_shrink_x = 1;
36 requirement_.flex_shrink_y = 1;
37 requirement_.min_x = 3;
38 requirement_.min_y = 3;
41 void Render(Screen& screen)
override {
42 const int width = (box_.x_max - box_.x_min + 1) * 2;
43 const int height = (box_.y_max - box_.y_min + 1) * 2;
44 if (width <= 0 || height <= 0) {
47 auto data = graph_function_(width, height);
49 for (
int x = box_.x_min; x <= box_.x_max; ++x) {
50 const int height_1 = 2 * box_.y_max - data[i++];
51 const int height_2 = 2 * box_.y_max - data[i++];
52 for (
int y = box_.y_min; y <= box_.y_max; ++y) {
54 int i_1 = yy < height_1 ? 0 : yy == height_1 ? 3 : 6;
55 int i_2 = yy < height_2 ? 0 : yy == height_2 ? 1 : 2;
56 screen.at(x, y) = charset[i_1 + i_2];
70 return std::make_shared<Graph>(std::move(graph_function));
void Render(Screen &screen, const Element &element)
在 ftxui::Screen 上显示元素。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< Node > Element
std::function< std::vector< int >(int, int)> GraphFunction
Element graph(GraphFunction)
使用 GraphFunction 绘制图表。