Skip to content

Commit

Permalink
dv::show&save XY 2 containers
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonMrt committed Aug 28, 2024
1 parent b130f93 commit cbe2178
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 15 deletions.
32 changes: 28 additions & 4 deletions Tests/ArrayCoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ TEST(ArrayCore, save_to_disk_pseudo_2d) {
}
}
dv::configSaveToDisk conf;
conf.separatorOfCols = "*";
conf.separatorOfRows = "____";
conf.separatorOfCols = ";";
conf.separatorOfRows = "\n";
bool result = dv::save(vals, rows, cols, "./data/test_saving_save_to_disk_pseudo_2d.csv", conf);
EXPECT_EQ(result, true);
}
Expand Down Expand Up @@ -78,6 +78,22 @@ TEST(ArrayCore, save_to_disk_container2D) {
EXPECT_EQ(result, true);
}

TEST(ArrayCore, save_to_disk_XYdata) {
//! 1-dimensional container
vector<double> vecX;
for (size_t i = 0; i < 10; ++i) {
vecX.emplace_back(i * 2);
}
vector<double> vecY;
for (size_t i = 0; i < 10; ++i) {
vecY.emplace_back(i * 3);
}
dv::configSaveToDisk config;
config.isTranspose = true;
bool result = dv::save(vecX, vecY, "./data/test_saving_save_to_disk_XYdata.csv", config);
EXPECT_EQ(result, true);
}

TEST(ArrayCore, universal_1d_conteiner) {
EXPECT_EQ(dvs::isPlotlyScriptExists(), true);
std::list<double> vec = {5, 34};
Expand Down Expand Up @@ -193,7 +209,7 @@ TEST(ArrayCore, showChart) {
EXPECT_EQ(result, true);
}

TEST(ArrayCore, showChartXY) {
TEST(ArrayCore, showChartXYfromContainerOfConteiners) {
vector<vector<double>> values;
vector<double> vecX = {5, 20, 21, 22, 50};
vector<double> vecY = {1, 2, 3, 4, 5};
Expand All @@ -203,7 +219,15 @@ TEST(ArrayCore, showChartXY) {
config.chart.title = "ChartXY";
config.chart.xLabel = "xLabel";
config.chart.yLabel = "yLabel";
bool result = dv::show(values, "showChartXY", config);
bool result = dv::show(values, "showChartXY_ContainerOfContainers", config);
EXPECT_EQ(result, true);
}

TEST(ArrayCore, showChartXYfrom2Containers) {
vector<vector<double>> values;
vector<double> vecX = {5, 20, 21, 22, 50};
vector<double> vecY = {1, 2, 3, 4, 5};
bool result = dv::show(vecX, vecY, "showChartXY_2containers");
EXPECT_EQ(result, true);
}

Expand Down
60 changes: 60 additions & 0 deletions array_core/array_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ template<typename C,
typename = std::enable_if_t<std::is_convertible_v<T, double>> >
bool save(C const& container, const string& filename, const configSaveToDisk& configuration = configSaveToDisk());


//! Two 1-dimensional container for X-Y plots
template<typename C, //https://devblogs.microsoft.com/oldnewthing/20190619-00/?p=102599
typename T = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename = std::enable_if_t<std::is_convertible_v<T, double>> >
bool show(C const& containerX, C const& containerY, const string& htmlPageName = dvs::kAppName, const Config& configuration = Config());

template<typename C, //https://devblogs.microsoft.com/oldnewthing/20190619-00/?p=102599
typename T = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename = std::enable_if_t<std::is_convertible_v<T, double>> >
bool save(C const& containerX, C const& containerY, const string& filename, const configSaveToDisk& configuration = configSaveToDisk());


//! 2-dimensional container
template<typename C,
typename E = std::decay_t<decltype(*begin(std::declval<C>()))>,
Expand Down Expand Up @@ -172,6 +185,53 @@ bool save(C const& container, const string& filename, const configSaveToDisk& co
return res;
}

template<typename C, typename T, typename>
bool show(C const& containerX, C const& containerY, const string& htmlPageName, const Config& configuration) {
if (containerX.size() != containerY.size()) {
return false;
}
vector<double> dblRowX(containerX.size());
uint64_t i = 0;
for (auto v : containerX) {
dblRowX[i] = v;
++i;
}
vector<double> dblRowY(containerY.size());
i = 0;
for (auto v : containerY) {
dblRowY[i] = v;
++i;
}
bool res = dvs::showLineChartInBrowser(dblRowX, dblRowY, htmlPageName, configuration);
return res;
}

template<typename C, typename T, typename>
bool save(C const& containerX, C const& containerY, const string& filename, const configSaveToDisk& configuration) {
if (containerX.size() != containerY.size()) {
return false;
}
vector<double> dblRowX(containerX.size());
uint64_t i = 0;
for (auto v : containerX) {
dblRowX[i] = v;
++i;
}
vector<double> dblRowY(containerY.size());
i = 0;
for (auto v : containerY) {
dblRowY[i] = v;
++i;
}

vector<vector<double>> vecVec;
vecVec.emplace_back(dblRowX);
vecVec.emplace_back(dblRowY);
bool res = dvs::saveVecVec<T>(vecVec, filename, configuration);
}



template<typename C, typename E, typename T, typename >
bool show(C const& container_of_containers, const string& htmlPageName, const Config& configuration) {
vector<vector<double>> vecVecDbl;
Expand Down
4 changes: 3 additions & 1 deletion array_core/configurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ struct Config {
struct configSaveToDisk {
configSaveToDisk():
separatorOfRows("\n"),
separatorOfCols(";") {}
separatorOfCols(";"),
isTranspose(false) {}
std::string separatorOfRows;
std::string separatorOfCols;
bool isTranspose; //rows-cols or cols-rows
};


Expand Down
34 changes: 24 additions & 10 deletions common_utils/common_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,32 @@ bool saveVecVec(const vector<vector<T>>& vecVec, const string& filename, dv::con
if (!fout.is_open()) {
return false;
}
size_t rows = vecVec.size();
size_t cols = vecVec.at(0).size();

for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
double val = vecVec.at(i).at(j);
fout << val;
if (j < cols - 1) { // we dont need sep al row end
fout << config.separatorOfCols;
if (config.isTranspose) {
size_t rows = vecVec.at(0).size();
size_t cols = vecVec.size();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
double val = vecVec.at(j).at(i);
fout << val;
if (j < cols - 1) { // we dont need sep at row end
fout << config.separatorOfCols;
}
}
fout << config.separatorOfRows;
}
} else {
size_t rows = vecVec.size();
size_t cols = vecVec.at(0).size();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
double val = vecVec.at(i).at(j);
fout << val;
if (j < cols - 1) { // we dont need sep at row end
fout << config.separatorOfCols;
}
}
fout << config.separatorOfRows;
}
fout << config.separatorOfRows;
}
fout.close();
return true;
Expand Down

0 comments on commit cbe2178

Please sign in to comment.