Browse Source

update: enhance SvgLine module

legacy
Dnomd343 2 years ago
parent
commit
1c326055b2
  1. 11
      src/graph/graph.cc
  2. 2
      src/graph/svg/CMakeLists.txt
  3. 55
      src/graph/svg/svg.cc
  4. 13
      src/graph/svg/svg.h

11
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];

2
src/graph/svg/CMakeLists.txt

@ -1,3 +1 @@
cmake_minimum_required(VERSION 3.0)
add_library(svg svg.cc)

55
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"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1")";
xml += " width=\"" + std::to_string(width) + "\"";
xml += " height=\"" + std::to_string(height) + "\">\n";
for (const auto *object : objects) {
xml += " " + object->dump() + "\n";
}
return xml + "</svg>\n";
}
/// SvgLine render as XML content
std::string SvgLine::dump() const {
/// basic attributes of svg-line
std::string xml = "<line ";
xml += "x1=\"" + std::to_string(start.x) + "\" ";
xml += "y1=\"" + std::to_string(start.y) + "\" ";
xml += "x2=\"" + std::to_string(end.x) + "\" ";
xml += "y2=\"" + std::to_string(end.y) + "\" ";
if (!dasharray.empty()) {
xml += "stroke-dasharray=\"" + dasharray + "\" ";
}
std::string style = "stroke:#636379;stroke-width:1;";
// <line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"/>
// 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 = "<rect ";
@ -42,7 +61,7 @@ std::string SvgRect::dump() const {
xml += "rx=\"" + std::to_string(radius) + "\" ";
}
/// style attribute of svg-rect
std::string style = "stroke-width:" + std::to_string(stroke).substr(0, 3) + ";";
std::string style = "stroke-width:" + float_to_string(stroke) + ";";
if (!color.empty()) {
style += "fill:" + color + ";";
}
@ -50,20 +69,10 @@ std::string SvgRect::dump() const {
style += "stroke:" + line_color + ";";
}
if (opacity != 0) {
style += "fill-opacity:" + std::to_string(opacity).substr(0, 4) + ";";
style += "fill-opacity:" + float_to_string(opacity) + ";";
}
if (line_opacity != 0) {
style += "stroke-opacity:" + std::to_string(line_opacity).substr(0, 4) + ";";
style += "stroke-opacity:" + float_to_string(line_opacity) + ";";
}
return xml + "style=\"" + style + "\" />";
}
std::string SvgGraph::dump() const {
std::string xml = R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1")";
xml += " width=\"" + std::to_string(width) + "\"";
xml += " height=\"" + std::to_string(height) + "\">\n";
for (const auto *object : objects) {
xml += " " + object->dump() + "\n";
}
return xml + "</svg>\n";
}

13
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;

Loading…
Cancel
Save