MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / CreateGnuPlotFile

Function CreateGnuPlotFile

lib/mdflib/mdfviewer/src/mdfdocument.cpp:87–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

85}
86
87std::string CreateGnuPlotFile(const mdf::ChannelObserverList& list, const std::string& csv_file, const std::string& title) {
88 if (list.empty() || csv_file.empty()) {
89 LOG_ERROR() << "No CSV file or observer list is empty.";
90 return "";
91 }
92
93 std::string gp_file;
94 try {
95 std::filesystem::path p(csv_file);
96 p.replace_extension(".gp");
97 gp_file = p.generic_string();
98 } catch(const std::exception&) {
99 LOG_ERROR() << "Failed to create gnuplot file. CSV File: " << csv_file;
100 }
101 const bool master = list.size() > 1 && list[0]->IsMaster();
102 std::FILE* file = fopen(gp_file.c_str(), "wt");
103 std::ostringstream x_label;
104 std::ostringstream y_label;
105 if (!master) {
106 x_label << "Sample";
107 y_label << list[0]->Name();
108 if (!list[0]->Unit().empty()) {
109 y_label << " [" << list[0]->Unit() << "]";
110 }
111 } else {
112 x_label << list[0]->Name();
113 if (!list[0]->Unit().empty()) {
114 x_label << " [" << list[0]->Unit() << "]";
115 }
116
117 y_label << list[1]->Name();
118 if (!list[1]->Unit().empty()) {
119 y_label << " [" << list[1]->Unit() << "]";
120 }
121 }
122
123 if (file != nullptr) {
124 fprintf(file, "set terminal wxt noenhanced title \"%s\" size 1200, 1000\n", title.c_str());
125 fprintf(file, "set datafile separator comma\n");
126 fprintf(file, "set key outside autotitle columnheader\n");
127 fprintf(file, "set xlabel \"%s\"\n", x_label.str().c_str());
128 fprintf(file, "set ylabel \"%s\"\n", y_label.str().c_str());
129 fprintf(file, "set autoscale\n");
130 fprintf(file, "plot \"%s\" using 1:2 with lines\n",csv_file.c_str());
131 fprintf(file, "exit\n");
132 fclose(file);
133 }
134 return gp_file;
135}
136
137} // end namespace
138

Callers 1

OnPlotChannelDataMethod · 0.85

Calls 5

emptyMethod · 0.80
sizeMethod · 0.45
IsMasterMethod · 0.45
NameMethod · 0.45
UnitMethod · 0.45

Tested by

no test coverage detected