diff --git a/Tests/ArrayCoreTest.cpp b/Tests/ArrayCoreTest.cpp index d6b4566..e17d2dc 100644 --- a/Tests/ArrayCoreTest.cpp +++ b/Tests/ArrayCoreTest.cpp @@ -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); } @@ -78,6 +78,22 @@ TEST(ArrayCore, save_to_disk_container2D) { EXPECT_EQ(result, true); } +TEST(ArrayCore, save_to_disk_XYdata) { + //! 1-dimensional container + vector vecX; + for (size_t i = 0; i < 10; ++i) { + vecX.emplace_back(i * 2); + } + vector 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 vec = {5, 34}; @@ -193,7 +209,7 @@ TEST(ArrayCore, showChart) { EXPECT_EQ(result, true); } -TEST(ArrayCore, showChartXY) { +TEST(ArrayCore, showChartXYfromContainerOfConteiners) { vector> values; vector vecX = {5, 20, 21, 22, 50}; vector vecY = {1, 2, 3, 4, 5}; @@ -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> values; + vector vecX = {5, 20, 21, 22, 50}; + vector vecY = {1, 2, 3, 4, 5}; + bool result = dv::show(vecX, vecY, "showChartXY_2containers"); EXPECT_EQ(result, true); } diff --git a/array_core/array_core.h b/array_core/array_core.h index 7fef4f9..e6165aa 100644 --- a/array_core/array_core.h +++ b/array_core/array_core.h @@ -52,6 +52,19 @@ template> > bool save(C const& container, const string& filename, const configSaveToDisk& configuration = configSaveToDisk()); + +//! Two 1-dimensional container for X-Y plots +template()))>, + typename = std::enable_if_t> > +bool show(C const& containerX, C const& containerY, const string& htmlPageName = dvs::kAppName, const Config& configuration = Config()); + +template()))>, + typename = std::enable_if_t> > +bool save(C const& containerX, C const& containerY, const string& filename, const configSaveToDisk& configuration = configSaveToDisk()); + + //! 2-dimensional container template()))>, @@ -172,6 +185,53 @@ bool save(C const& container, const string& filename, const configSaveToDisk& co return res; } +template +bool show(C const& containerX, C const& containerY, const string& htmlPageName, const Config& configuration) { + if (containerX.size() != containerY.size()) { + return false; + } + vector dblRowX(containerX.size()); + uint64_t i = 0; + for (auto v : containerX) { + dblRowX[i] = v; + ++i; + } + vector dblRowY(containerY.size()); + i = 0; + for (auto v : containerY) { + dblRowY[i] = v; + ++i; + } + bool res = dvs::showLineChartInBrowser(dblRowX, dblRowY, htmlPageName, configuration); + return res; +} + +template +bool save(C const& containerX, C const& containerY, const string& filename, const configSaveToDisk& configuration) { + if (containerX.size() != containerY.size()) { + return false; + } + vector dblRowX(containerX.size()); + uint64_t i = 0; + for (auto v : containerX) { + dblRowX[i] = v; + ++i; + } + vector dblRowY(containerY.size()); + i = 0; + for (auto v : containerY) { + dblRowY[i] = v; + ++i; + } + + vector> vecVec; + vecVec.emplace_back(dblRowX); + vecVec.emplace_back(dblRowY); + bool res = dvs::saveVecVec(vecVec, filename, configuration); +} + + + template bool show(C const& container_of_containers, const string& htmlPageName, const Config& configuration) { vector> vecVecDbl; diff --git a/array_core/configurator.h b/array_core/configurator.h index 0a6fba9..604c624 100644 --- a/array_core/configurator.h +++ b/array_core/configurator.h @@ -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 }; diff --git a/common_utils/common_utils.h b/common_utils/common_utils.h index 63c0892..b90914c 100644 --- a/common_utils/common_utils.h +++ b/common_utils/common_utils.h @@ -84,18 +84,32 @@ bool saveVecVec(const vector>& 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;