Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read data from file #57

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Tests/res/plotly-2.27.0.min.js

This file was deleted.

22 changes: 22 additions & 0 deletions common_utils/common_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,26 @@ bool deleteFolder(const char* fname) {
}
}

bool getDataFromFile(const string& path, string& result) {

//TODO different scenarious and sanitizing
if (!is_file_exists(path)) {
return false;
}
if (!result.empty()) {
result.clear();
}
std::fstream file;
file.open(path, std::ios::in);
if (file.is_open()) {
string temp;
while (std::getline(file, temp)) {
result.append(temp).append(";");
}
} else {
return false;
}
return true;
}

}; // namespace davis
2 changes: 2 additions & 0 deletions common_utils/common_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ void sleepMs(unsigned long milisec);

void openPlotlyHtml(const string& file_name);

bool getDataFromFile(const string& path, string& result);

// Now it doesn't work.
bool deleteFolder(const char* fname);

Expand Down
26 changes: 25 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
int main(int argc, char* argv[]) {
cxxopts::Options options("davis", "data visualization utility");
options.add_options()
("h,help", "davis commands")
("l,linechart", "linechart values", cxxopts::value<std::string>())
("m,heatmap", "heatmap values", cxxopts::value<std::string>())
("h,help", "davis commands")
("f,datapath", "path to data", cxxopts::value<std::string>())
("t,charttype", "chart type", cxxopts::value<std::string>())
;
auto result = options.parse(argc, argv);
if (result.count("help")) {
Expand All @@ -37,6 +39,28 @@ int main(int argc, char* argv[]) {
davis::showHeatMapInBrowser(data, "comand_line_heatmap", davis::showSettings());
return EXIT_SUCCESS;
}
if (result.count("datapath")) {
auto data_path = result["datapath"].as<std::string>();
std::string str_data;
if (davis::getDataFromFile(data_path, str_data)) {
if (result.count("charttype")) {
auto chart_type = result["charttype"].as<std::string>();
if (chart_type == "l" || chart_type == "linechart") {
davis::showLineChartInBrowser(str_data, "file_data", davis::showSettings());
} else if (chart_type == "s" || chart_type == "surface") {
davis::showSettings settings = davis::showSettings();
settings.visualType = davis::visualizationTypes::SURFACE;
settings.colorscale = davis::colorscales::GLAMOUR;
davis::showSurfaceInBrowser(str_data, "surface", settings);
} else if (chart_type == "m" || chart_type == "heatmap") {

}
} else {
davis::showHeatMapInBrowser(str_data, "file_data", davis::showSettings());
}
}
}

// example how to show values via ArrayCore using PlotlyMaker lib
std::vector<std::vector<int>> values = {{300, 40, 98, 76}, {99, 45, 20, 1}, {5, 56, 93, 25}, {45, 23, 90, 2}};
davis::show(values, "values");
Expand Down
39 changes: 27 additions & 12 deletions plotly_maker/plotly_maker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,7 @@ bool showHeatMapInBrowser(const std::string& values,
const std::string& title,
const showSettings& settings) {
vector<vector<double>>heat_map_values;
istringstream f_lines(values);
string lines;
while (std::getline(f_lines, lines, ';')) {
vector<double>vals;
istringstream f_values(lines);
string str_value;
while (std::getline(f_values, str_value, ',')) {
vals.push_back(std::stod(str_value));
}
heat_map_values.push_back(vals);
}
getMatrixValuesFromString(values, heat_map_values);
showHeatMapInBrowser(heat_map_values, title, settings);
return true;
};
Expand Down Expand Up @@ -198,11 +188,36 @@ bool showLineChartInBrowser(const std::string& values,
return true;
};

bool showSurfaceInBrowser(const vector<vector<double> >& values,
bool showSurfaceInBrowser(const vector<vector<double>>& values,
const std::string& title,
const showSettings& settings) {
return heatmap_and_surface(values, title, settings);
}

bool showSurfaceInBrowser(const std::string& values,
const std::string& title,
const showSettings& settings) {
vector<vector<double>>surface_values;
getMatrixValuesFromString(values, surface_values);
showSurfaceInBrowser(surface_values, title, settings);
return true;
}

bool getMatrixValuesFromString(const std::string& in_values,
vector<vector<double>>& out_values) {
istringstream f_lines(in_values);
string lines;
while (std::getline(f_lines, lines, ';')) {
vector<double>vals;
istringstream f_values(lines);
string str_value;
while (std::getline(f_values, str_value, ',')) {
vals.push_back(std::stod(str_value));
}
out_values.push_back(vals);
}
return true;
}

}; // namespace davis

6 changes: 6 additions & 0 deletions plotly_maker/plotly_maker.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ bool showLineChartInBrowser(const std::string& values, const std::string& title,

bool showSurfaceInBrowser(const vector<vector<double>>& values, const std::string& title,
const showSettings& settings);

bool showSurfaceInBrowser(const std::string& values, const std::string& title,
const showSettings& settings);

bool getMatrixValuesFromString(const std::string& in_values,
vector<vector<double>>& out_values);
}; // namespace davis

#endif // PLOTLY_MAKER_PLOTLY_MAKER_H_
Expand Down