|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <vector> |
| 4 | +#include <regex> |
| 5 | +#include <utility> |
| 6 | +#include <algorithm> |
| 7 | +#include <unordered_map> |
| 8 | +#include <numeric> |
| 9 | +#include "graph_manager.h" |
| 10 | +#include "file_reader.h" |
| 11 | +#include "data_utilities.h" |
| 12 | + |
| 13 | +const std::regex graphAxisRePattern{"^(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)", std::regex_constants::icase}; |
| 14 | + |
| 15 | +GraphPlotInfo getGraphInfoFromFile(std::string filename); |
| 16 | +std::vector<std::string> getDotGraphLineStrings(int width, int height, GraphPlotInfo); |
| 17 | +std::string getDotGraphXAxisTicks(int width, std::vector<double> xValues); |
| 18 | + |
| 19 | +DotGraph::DotGraph(std::string filename, int width, int height) |
| 20 | +{ |
| 21 | + if (width < 20) width = 20; |
| 22 | + if (height < 10) height = 10; |
| 23 | + GraphPlotInfo info = getGraphInfoFromFile(filename); |
| 24 | + this->xLabel = info.xLabel; |
| 25 | + this->yLabel = info.yLabel; |
| 26 | + for (auto i : info.values) |
| 27 | + this->values.push_back(i); |
| 28 | + this->width = width; |
| 29 | + this->height = height; |
| 30 | +} |
| 31 | + |
| 32 | +std::string DotGraph::drawGraph() |
| 33 | +{ |
| 34 | + std::string graph = ""; |
| 35 | + graph = yLabel + "\n"; |
| 36 | + GraphPlotInfo plotInfo{ |
| 37 | + .xLabel = xLabel, .yLabel = yLabel, .values = values |
| 38 | + }; |
| 39 | + std::vector<std::string> graphLines = getDotGraphLineStrings( |
| 40 | + width, height, plotInfo |
| 41 | + ); |
| 42 | + |
| 43 | + for (auto i : graphLines){ |
| 44 | + graph += i + "\n"; |
| 45 | + } |
| 46 | + int xLabelPaddingCount = width / 2 - xLabel.length() / 2; |
| 47 | + graph += "\n" + std::string(xLabelPaddingCount, ' ') + xLabel; |
| 48 | + return graph; |
| 49 | +} |
| 50 | + |
| 51 | +GraphPlotInfo getGraphInfoFromFile(std::string filename) |
| 52 | +{ |
| 53 | + GraphPlotInfo graphInfo{}; |
| 54 | + std::vector<std::string> fileLines = getFileLines(filename); |
| 55 | + std::smatch reMatch; |
| 56 | + for (auto line : fileLines) |
| 57 | + { |
| 58 | + if (std::regex_search(line, reMatch, graphAxisRePattern)){ |
| 59 | + double xValue = std::stod(reMatch[1].str()); |
| 60 | + double yValue = std::stod(reMatch[2].str()); |
| 61 | + std::pair<double, double> valuePair = std::make_pair(xValue, yValue); |
| 62 | + graphInfo.values.push_back(valuePair); |
| 63 | + } |
| 64 | + else if (std::regex_search(line, reMatch, csvDelimiterPattern)){ |
| 65 | + bool isXLabel = strToLower(reMatch[1].str()) == "x"; |
| 66 | + bool isYLabel = strToLower(reMatch[1].str()) == "y"; |
| 67 | + if (isXLabel) |
| 68 | + graphInfo.xLabel = reMatch[2].str(); |
| 69 | + else if (isYLabel) |
| 70 | + graphInfo.yLabel = reMatch[2].str(); |
| 71 | + } |
| 72 | + } |
| 73 | + return graphInfo; |
| 74 | +} |
| 75 | + |
| 76 | +std::vector<std::string> getDotGraphLineStrings( |
| 77 | + int width, int height, GraphPlotInfo graphInfo) |
| 78 | +{ |
| 79 | + std::vector<std::string> dotGraphLines{}; |
| 80 | + auto xyValues = extractVectorsFromPair(graphInfo.values); |
| 81 | + |
| 82 | + double lowestXValue = findMinMaxDoubleInVector(xyValues.first).first; |
| 83 | + double greatestXValue = findMinMaxDoubleInVector(xyValues.first).second; |
| 84 | + double lowestYValue = findMinMaxDoubleInVector(xyValues.second).first; |
| 85 | + double greatestYValue = findMinMaxDoubleInVector(xyValues.second).second; |
| 86 | + // Length of greatest `y` axis value when converted to a string. |
| 87 | + int maxYValueStrLength = std::to_string((int)greatestYValue).size(); |
| 88 | + |
| 89 | + // Computes the (x, y) coordinates from a given value. |
| 90 | + auto getValueCoordinates = [=](std::pair<double, double> value){ |
| 91 | + int xCoordinate = value.first / greatestXValue * width; |
| 92 | + int yCoordinate = value.second / greatestYValue * height; |
| 93 | + return std::make_pair(xCoordinate, yCoordinate); |
| 94 | + }; |
| 95 | + |
| 96 | + // Determining where each data point will appear on our graph. |
| 97 | + std::vector<std::pair<int, int>> plotAppearances{}; |
| 98 | + for (auto i : graphInfo.values){ |
| 99 | + auto valuePair = std::make_pair(i.first, i.second); |
| 100 | + auto coordinates = getValueCoordinates(valuePair); |
| 101 | + plotAppearances.push_back(coordinates); |
| 102 | + } |
| 103 | + |
| 104 | + // Create an unordered map to store line numbers as keys and their tick labels as values. |
| 105 | + std::unordered_map<int, int> yAxisTicks{}; |
| 106 | + for (int lineNumber = 0; lineNumber <= height; ++lineNumber){ |
| 107 | + int valueAtLine = (greatestYValue - lowestYValue) / height * lineNumber; |
| 108 | + bool tickExists = false; |
| 109 | + for (auto it : yAxisTicks) |
| 110 | + if (it.second == valueAtLine){ |
| 111 | + tickExists = 1; |
| 112 | + break; |
| 113 | + } |
| 114 | + if (!tickExists) |
| 115 | + yAxisTicks.insert({lineNumber, valueAtLine}); |
| 116 | + } |
| 117 | + |
| 118 | + // Initialize the beginning of a string to the dot graph. |
| 119 | + auto initLineString = [&](int line){ |
| 120 | + std::string result = ""; |
| 121 | + if (!yAxisTicks.count(line)) |
| 122 | + return result + std::string(maxYValueStrLength, ' ') + " |"; |
| 123 | + int value = yAxisTicks.at(line); |
| 124 | + int valueStrLength = std::to_string(value).size(); |
| 125 | + std::string padding(maxYValueStrLength - valueStrLength, ' '); |
| 126 | + result += std::to_string(value) + padding + " |"; |
| 127 | + return result; |
| 128 | + }; |
| 129 | + |
| 130 | + for (int lineNumber = height; lineNumber > 0; --lineNumber) |
| 131 | + { |
| 132 | + std::string currentLineString = initLineString(lineNumber); |
| 133 | + for (int columnNumber = 0; columnNumber <= width; ++columnNumber) |
| 134 | + { |
| 135 | + auto coordinates = std::make_pair(columnNumber, lineNumber); |
| 136 | + if (std::find(plotAppearances.begin(), plotAppearances.end(), coordinates) != plotAppearances.end()) |
| 137 | + currentLineString += "*"; |
| 138 | + else |
| 139 | + currentLineString += " "; |
| 140 | + } |
| 141 | + dotGraphLines.push_back(currentLineString); |
| 142 | + } |
| 143 | + dotGraphLines.push_back(std::string(maxYValueStrLength + 2, ' ') + std::string(width, '-')); |
| 144 | + std::string xTicksString(maxYValueStrLength + 2, ' '); |
| 145 | + xTicksString += getDotGraphXAxisTicks(width, xyValues.first); |
| 146 | + dotGraphLines.push_back(xTicksString); |
| 147 | + return dotGraphLines; |
| 148 | +} |
| 149 | + |
| 150 | +std::string getDotGraphXAxisTicks(int width, std::vector<double> xValues) |
| 151 | +{ |
| 152 | + std::string result = ""; |
| 153 | + std::sort(xValues.begin(), xValues.end()); |
| 154 | + std::pair<double, double> minMaxValue = findMinMaxDoubleInVector(xValues); |
| 155 | + int minSpacesPerTick = width / xValues.size() + static_cast<int>(width / xValues.size() == 0); |
| 156 | + |
| 157 | + auto getColumnValue = [minMaxValue, width](int column){ |
| 158 | + double range = minMaxValue.second - minMaxValue.first; |
| 159 | + int value = range / width * column; |
| 160 | + return value; |
| 161 | + }; |
| 162 | + |
| 163 | + std::vector<int> usedTickValues{}; |
| 164 | + for (int columnIndex = 0; columnIndex < width; ++columnIndex) |
| 165 | + { |
| 166 | + int tickValue = getColumnValue(columnIndex); |
| 167 | + |
| 168 | + // Check if there is enough padding to display `x` axis tick labels. |
| 169 | + bool isPadded = false; |
| 170 | + if (result.find_last_not_of(' ') != result.size() - 1 || columnIndex == 0) |
| 171 | + isPadded = result.find_last_of(' ') - result.find_last_not_of(' ') >= minSpacesPerTick || columnIndex == 0; |
| 172 | + |
| 173 | + // Add tick label if unused and padding has enough spaces. |
| 174 | + if (!vectorContains(usedTickValues, tickValue) && isPadded){ |
| 175 | + usedTickValues.push_back(tickValue); |
| 176 | + result += std::to_string(tickValue); |
| 177 | + } |
| 178 | + else { |
| 179 | + result += " "; |
| 180 | + } |
| 181 | + if (result.size() >= width) |
| 182 | + break; |
| 183 | + } |
| 184 | + return result; |
| 185 | +} |
0 commit comments