Browse Source

perf: svg-rect xml dump

master
Dnomd343 2 years ago
parent
commit
386e28d5f8
  1. 6
      src/graph/graph.cc
  2. 66
      src/graph/svg/svg.cc
  3. 6
      src/graph/svg/svg.h

6
src/graph/graph.cc

@ -19,6 +19,12 @@ void Graph::svg_demo(Analyse::track_data_t track_data) {
s.radius = 20;
s.opacity = 0.8;
s.line_opacity = 0.5;
s.color = "blue";
s.line_color = "green";
printf(R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="%d" height="%d">)", 1000, 1000);
printf("\n");

66
src/graph/svg/svg.cc

@ -1,50 +1,28 @@
#include "svg.h"
#include <cstdio>
#include <iostream>
std::string SvgRect::dump() const {
/// <rect x="120" width="100" height="100" rx="15" />
// char *radius_str = nullptr;
// if (radius == 0) {
// asprintf(&radius_str, " ");
// } else {
// asprintf(&radius_str, R"( rx="%lu" )", radius);
// }
std::string style;
// style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9"
char *xml = nullptr;
asprintf(&xml, R"(<rect x="%ld" y="%ld" width="%ld" height="%ld")", left, top, width, height);
std::string ret(xml);
free(xml);
/// basic attribute of svg-rect
std::string xml = "<rect ";
xml += "x=\"" + std::to_string(left) + "\" ";
xml += "y=\"" + std::to_string(top) + "\" ";
xml += "width=\"" + std::to_string(width) + "\" ";
xml += "height=\"" + std::to_string(height) + "\" ";
if (radius != 0) {
char *radius_str = nullptr;
asprintf(&radius_str, R"( rx="%lu")", radius);
ret += radius_str;
free(radius_str);
xml += "rx=\"" + std::to_string(radius) + "\" ";
}
// std::cout << xml << std::endl;
// std::string result = xml;
// free(xml);
// std::string xml("<rect ");
// xml += "x = ";
//
// xml += atoi();
return ret + " />";
/// style attribute of svg-rect
std::string style = "stroke-width:" + std::to_string(stroke) + ";";
if (!color.empty()) {
style += "fill:" + color + ";";
}
if (!line_color.empty()) {
style += "stroke:" + line_color + ";";
}
if (opacity != 0) {
style += "fill-opacity:" + std::to_string(opacity).substr(0, 4) + ";";
}
if (line_opacity != 0) {
style += "stroke-opacity:" + std::to_string(line_opacity).substr(0, 4) + ";";
}
return xml + "style=\"" + style + "\" />";
}

6
src/graph/svg/svg.h

@ -9,13 +9,15 @@ public:
uint64_t left;
uint64_t width;
uint64_t height;
uint64_t radius = 0;
uint32_t stroke = 1;
std::string color;
std::string line_color;
float opacity = 0;
float line_opacity = 0;
std::string color = "pink";
std::string line_color = "blue";
std::string dump() const;
};

Loading…
Cancel
Save