From 1c326055b2b8b4b83e00a811eb60b8a3b9dfca41 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 22 Jan 2023 02:27:54 +0800 Subject: [PATCH] update: enhance SvgLine module --- src/graph/graph.cc | 11 ++++++++ src/graph/svg/CMakeLists.txt | 2 -- src/graph/svg/svg.cc | 55 +++++++++++++++++++++--------------- src/graph/svg/svg.h | 13 +++++---- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/graph/graph.cc b/src/graph/graph.cc index 3259c44..64b09a3 100644 --- a/src/graph/graph.cc +++ b/src/graph/graph.cc @@ -8,6 +8,17 @@ std::string Graph::svg_demo(Analyse::track_data_t track_data) { + auto s = SvgGraph(1000, 1200); + + auto *sl = new SvgLine({100, 200}, {500, 800}); + sl->color = std::string("blue"); + sl->dasharray = "10,5"; + sl->opacity = 0.5; + s.insert(sl); + + std::cout << s.dump() << std::endl; + return ""; + // for (uint32_t i = 0; i < track_data.size(); ++i) { // // const auto &ly = track_data[i]; diff --git a/src/graph/svg/CMakeLists.txt b/src/graph/svg/CMakeLists.txt index 1c8e024..6ca873c 100644 --- a/src/graph/svg/CMakeLists.txt +++ b/src/graph/svg/CMakeLists.txt @@ -1,3 +1 @@ -cmake_minimum_required(VERSION 3.0) - add_library(svg svg.cc) diff --git a/src/graph/svg/svg.cc b/src/graph/svg/svg.cc index 22b11e5..db05a42 100644 --- a/src/graph/svg/svg.cc +++ b/src/graph/svg/svg.cc @@ -1,5 +1,11 @@ #include "svg.h" +inline std::string float_to_string(float data) { + auto str = std::to_string(data); + return str.substr(0, str.find('.') + 3); // format -> xxx.__ +} + +/// SvgGraph interface void SvgGraph::insert(SvgObject *obj) { objects.emplace_back(obj); } @@ -10,27 +16,40 @@ SvgGraph::~SvgGraph() { } } -std::string SvgLine::dump() const { +std::string SvgGraph::dump() const { + std::string xml = R"(\n"; + for (const auto *object : objects) { + xml += " " + object->dump() + "\n"; + } + return xml + "\n"; +} +/// SvgLine render as XML content +std::string SvgLine::dump() const { /// basic attributes of svg-line - std::string xml = " - - - // TODO: svg-line css style - + /// style attribute of svg-line + std::string style = "stroke-width:" + float_to_string(width) + ";"; + if (!color.empty()) { + style += "stroke:" + color + ";"; + } + if (opacity != 0) { + style += "stroke-opacity:" + float_to_string(opacity) + ";"; + } return xml + "style=\"" + style + "\" />"; - } +/// SvgRect render as XML content std::string SvgRect::dump() const { /// basic attributes of svg-rect std::string xml = ""; } - -std::string SvgGraph::dump() const { - std::string xml = R"(\n"; - for (const auto *object : objects) { - xml += " " + object->dump() + "\n"; - } - return xml + "\n"; -} diff --git a/src/graph/svg/svg.h b/src/graph/svg/svg.h index 09ab76c..1540fe5 100644 --- a/src/graph/svg/svg.h +++ b/src/graph/svg/svg.h @@ -11,20 +11,23 @@ public: uint64_t y; }; -/// basic class of SVG element +/// SVG basic element class SvgObject { public: virtual ~SvgObject() = default; virtual std::string dump() const = 0; }; +// TODO: SVG text element + /// SVG line element class SvgLine : public SvgObject { public: - Point start; - Point end; - - // TODO: more options for svg-line + Point start, end; + std::string color; + float width = 1.0; + float opacity = 0; + std::string dasharray; ~SvgLine() override = default; std::string dump() const override;