CStatisticScreen.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * CStatisticScreen.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CStatisticScreen.h"
  12. #include "../CGameInfo.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/WindowHandler.h"
  15. #include "../eventsSDL/InputHandler.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../render/Graphics.h"
  18. #include "../render/IImage.h"
  19. #include "../widgets/ComboBox.h"
  20. #include "../widgets/Images.h"
  21. #include "../widgets/GraphicalPrimitiveCanvas.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../widgets/Buttons.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../widgets/Slider.h"
  26. #include "../../lib/gameState/GameStatistics.h"
  27. #include "../../lib/gameState/CGameState.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h"
  29. #include "../../lib/texts/TextOperations.h"
  30. #include <vstd/DateUtils.h>
  31. std::string CStatisticScreen::getDay(int d)
  32. {
  33. return std::to_string(CGameState::getDate(d, Date::MONTH)) + "/" + std::to_string(CGameState::getDate(d, Date::WEEK)) + "/" + std::to_string(CGameState::getDate(d, Date::DAY_OF_WEEK));
  34. }
  35. CStatisticScreen::CStatisticScreen(StatisticDataSet stat)
  36. : CWindowObject(BORDERED), statistic(stat)
  37. {
  38. OBJECT_CONSTRUCTION;
  39. pos = center(Rect(0, 0, 800, 600));
  40. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  41. filledBackground->setPlayerColor(PlayerColor(1));
  42. contentArea = Rect(10, 40, 780, 510);
  43. layout.push_back(std::make_shared<CLabel>(400, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.statisticWindow.statistics")));
  44. layout.push_back(std::make_shared<TransparentFilledRectangle>(contentArea, ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 80, 128, 255), 1));
  45. layout.push_back(std::make_shared<CButton>(Point(725, 558), AnimationPath::builtin("MUBCHCK"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT));
  46. buttonSelect = std::make_shared<CToggleButton>(Point(10, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){ onSelectButton(); });
  47. buttonSelect->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.selectView"), EFonts::FONT_SMALL, Colors::YELLOW);
  48. buttonCsvSave = std::make_shared<CToggleButton>(Point(150, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){ GH.input().copyToClipBoard(statistic.toCsv("\t")); });
  49. buttonCsvSave->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.tsvCopy"), EFonts::FONT_SMALL, Colors::YELLOW);
  50. mainContent = getContent(OVERVIEW, EGameResID::NONE);
  51. }
  52. void CStatisticScreen::onSelectButton()
  53. {
  54. std::vector<std::string> texts;
  55. for(auto & val : contentInfo)
  56. texts.push_back(CGI->generaltexth->translate(std::get<0>(val.second)));
  57. GH.windows().createAndPushWindow<StatisticSelector>(texts, [this](int selectedIndex)
  58. {
  59. OBJECT_CONSTRUCTION;
  60. if(!std::get<1>(contentInfo[(Content)selectedIndex]))
  61. mainContent = getContent((Content)selectedIndex, EGameResID::NONE);
  62. else
  63. {
  64. auto content = (Content)selectedIndex;
  65. auto possibleRes = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};
  66. std::vector<std::string> resourceText;
  67. for(auto & res : possibleRes)
  68. resourceText.push_back(CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()));
  69. GH.windows().createAndPushWindow<StatisticSelector>(resourceText, [this, content, possibleRes](int selectedIndex)
  70. {
  71. OBJECT_CONSTRUCTION;
  72. mainContent = getContent(content, possibleRes[selectedIndex]);
  73. });
  74. }
  75. });
  76. }
  77. TData CStatisticScreen::extractData(StatisticDataSet stat, std::function<float(StatisticDataSetEntry val)> selector)
  78. {
  79. auto tmpData = stat.data;
  80. std::sort(tmpData.begin(), tmpData.end(), [](StatisticDataSetEntry v1, StatisticDataSetEntry v2){ return v1.player == v2.player ? v1.day < v2.day : v1.player < v2.player; });
  81. PlayerColor tmpColor = PlayerColor::NEUTRAL;
  82. std::vector<float> tmpColorSet;
  83. TData plotData;
  84. for(auto & val : tmpData)
  85. {
  86. if(tmpColor != val.player)
  87. {
  88. if(tmpColorSet.size())
  89. {
  90. plotData.push_back({graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet)});
  91. tmpColorSet.clear();
  92. }
  93. tmpColor = val.player;
  94. }
  95. if(val.status == EPlayerStatus::INGAME)
  96. tmpColorSet.push_back(selector(val));
  97. }
  98. if(tmpColorSet.size())
  99. plotData.push_back({graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet)});
  100. return plotData;
  101. }
  102. std::shared_ptr<CIntObject> CStatisticScreen::getContent(Content c, EGameResID res)
  103. {
  104. TData plotData;
  105. TIcons icons;
  106. switch (c)
  107. {
  108. case OVERVIEW:
  109. return std::make_shared<OverviewPanel>(contentArea.resize(-15), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), statistic);
  110. case CHART_RESOURCES:
  111. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.resources[res]; });
  112. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  113. case CHART_INCOME:
  114. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.income; });
  115. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  116. case CHART_NUMBER_OF_HEROES:
  117. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberHeroes; });
  118. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  119. case CHART_NUMBER_OF_TOWNS:
  120. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberTowns; });
  121. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  122. case CHART_NUMBER_OF_ARTIFACTS:
  123. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberArtifacts; });
  124. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  125. case CHART_NUMBER_OF_DWELLINGS:
  126. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberDwellings; });
  127. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  128. case CHART_NUMBER_OF_MINES:
  129. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.numMines[res]; });
  130. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  131. case CHART_ARMY_STRENGTH:
  132. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.armyStrength; });
  133. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  134. case CHART_EXPERIENCE:
  135. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.totalExperience; });
  136. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  137. case CHART_RESOURCES_SPENT_ARMY:
  138. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.spentResourcesForArmy[res]; });
  139. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  140. case CHART_RESOURCES_SPENT_BUILDINGS:
  141. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.spentResourcesForBuildings[res]; });
  142. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  143. case CHART_MAP_EXPLORED:
  144. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.mapExploredRatio; });
  145. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 1);
  146. }
  147. return nullptr;
  148. }
  149. StatisticSelector::StatisticSelector(std::vector<std::string> texts, std::function<void(int selectedIndex)> cb)
  150. : CWindowObject(BORDERED | NEEDS_ANIMATED_BACKGROUND), texts(texts), cb(cb)
  151. {
  152. OBJECT_CONSTRUCTION;
  153. pos = center(Rect(0, 0, 128 + 16, std::min((int)texts.size(), LINES) * 40));
  154. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  155. filledBackground->setPlayerColor(PlayerColor(1));
  156. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, [this](int to){ update(to); redraw(); }, LINES, texts.size(), 0, Orientation::VERTICAL, CSlider::BLUE);
  157. slider->setPanningStep(40);
  158. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, pos.h));
  159. update(0);
  160. }
  161. void StatisticSelector::update(int to)
  162. {
  163. OBJECT_CONSTRUCTION;
  164. buttons.clear();
  165. for(int i = to; i < LINES + to; i++)
  166. {
  167. if(i>=texts.size())
  168. continue;
  169. auto button = std::make_shared<CToggleButton>(Point(0, 10 + (i - to) * 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this, i](bool on){ close(); cb(i); });
  170. button->setTextOverlay(texts[i], EFonts::FONT_SMALL, Colors::WHITE);
  171. buttons.push_back(button);
  172. }
  173. }
  174. OverviewPanel::OverviewPanel(Rect position, std::string title, StatisticDataSet stat)
  175. : CIntObject(), data(stat)
  176. {
  177. OBJECT_CONSTRUCTION;
  178. pos = position + pos.topLeft();
  179. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  180. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, Y_OFFS, pos.w - 16, pos.h - Y_OFFS));
  181. dataExtract = {
  182. {
  183. CGI->generaltexth->translate("vcmi.statisticWindow.param.playerName"), [this](PlayerColor color){
  184. return playerDataFilter(color).front().playerName;
  185. }
  186. },
  187. {
  188. CGI->generaltexth->translate("vcmi.statisticWindow.param.daysSurvived"), [this](PlayerColor color){
  189. return CStatisticScreen::getDay(playerDataFilter(color).size());
  190. }
  191. },
  192. {
  193. CGI->generaltexth->translate("vcmi.statisticWindow.param.maxHeroLevel"), [this](PlayerColor color){
  194. int maxLevel = 0;
  195. for(auto val : playerDataFilter(color))
  196. if(maxLevel < val.maxHeroLevel)
  197. maxLevel = val.maxHeroLevel;
  198. return std::to_string(maxLevel);
  199. }
  200. },
  201. {
  202. CGI->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioHero"), [this](PlayerColor color){
  203. auto val = playerDataFilter(color).back();
  204. if(!val.numBattlesPlayer)
  205. return std::string("");
  206. float tmp = ((float)val.numWinBattlesPlayer / (float)val.numBattlesPlayer) * 100;
  207. return std::to_string((int)tmp) + " %";
  208. }
  209. },
  210. {
  211. CGI->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioNeutral"), [this](PlayerColor color){
  212. auto val = playerDataFilter(color).back();
  213. if(!val.numWinBattlesNeutral)
  214. return std::string("");
  215. float tmp = ((float)val.numWinBattlesNeutral / (float)val.numBattlesNeutral) * 100;
  216. return std::to_string((int)tmp) + " %";
  217. }
  218. },
  219. {
  220. CGI->generaltexth->translate("vcmi.statisticWindow.param.battlesHero"), [this](PlayerColor color){
  221. auto val = playerDataFilter(color).back();
  222. return std::to_string(val.numBattlesPlayer);
  223. }
  224. },
  225. {
  226. CGI->generaltexth->translate("vcmi.statisticWindow.param.battlesNeutral"), [this](PlayerColor color){
  227. auto val = playerDataFilter(color).back();
  228. return std::to_string(val.numBattlesNeutral);
  229. }
  230. },
  231. {
  232. CGI->generaltexth->translate("vcmi.statisticWindow.param.obeliskVisited"), [this](PlayerColor color){
  233. auto val = playerDataFilter(color).back();
  234. return std::to_string((int)(val.obeliskVisitedRatio * 100)) + " %";
  235. }
  236. },
  237. {
  238. CGI->generaltexth->translate("vcmi.statisticWindow.param.maxArmyStrength"), [this](PlayerColor color){
  239. int maxArmyStrength = 0;
  240. for(auto val : playerDataFilter(color))
  241. if(maxArmyStrength < val.armyStrength)
  242. maxArmyStrength = val.armyStrength;
  243. return TextOperations::formatMetric(maxArmyStrength, 6);
  244. }
  245. },
  246. {
  247. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GOLD).get()), [this](PlayerColor color){
  248. auto val = playerDataFilter(color).back();
  249. return std::to_string(val.tradeVolume[EGameResID::GOLD]);
  250. }
  251. },
  252. {
  253. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::WOOD).get()), [this](PlayerColor color){
  254. auto val = playerDataFilter(color).back();
  255. return std::to_string(val.tradeVolume[EGameResID::WOOD]);
  256. }
  257. },
  258. {
  259. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::MERCURY).get()), [this](PlayerColor color){
  260. auto val = playerDataFilter(color).back();
  261. return std::to_string(val.tradeVolume[EGameResID::MERCURY]);
  262. }
  263. },
  264. {
  265. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::ORE).get()), [this](PlayerColor color){
  266. auto val = playerDataFilter(color).back();
  267. return std::to_string(val.tradeVolume[EGameResID::ORE]);
  268. }
  269. },
  270. {
  271. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::SULFUR).get()), [this](PlayerColor color){
  272. auto val = playerDataFilter(color).back();
  273. return std::to_string(val.tradeVolume[EGameResID::SULFUR]);
  274. }
  275. },
  276. {
  277. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::CRYSTAL).get()), [this](PlayerColor color){
  278. auto val = playerDataFilter(color).back();
  279. return std::to_string(val.tradeVolume[EGameResID::CRYSTAL]);
  280. }
  281. },
  282. {
  283. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GEMS).get()), [this](PlayerColor color){
  284. auto val = playerDataFilter(color).back();
  285. return std::to_string(val.tradeVolume[EGameResID::GEMS]);
  286. }
  287. },
  288. };
  289. int usedLines = dataExtract.size();
  290. slider = std::make_shared<CSlider>(Point(pos.w - 16, Y_OFFS), pos.h - Y_OFFS, [this](int to){ update(to); setRedrawParent(true); redraw(); }, LINES - 1, usedLines, 0, Orientation::VERTICAL, CSlider::BLUE);
  291. slider->setPanningStep(canvas->pos.h / LINES);
  292. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, canvas->pos.h));
  293. fieldSize = Point(canvas->pos.w / (graphics->playerColors.size() + 2), canvas->pos.h / LINES);
  294. for(int x = 0; x < graphics->playerColors.size() + 1; x++)
  295. for(int y = 0; y < LINES; y++)
  296. {
  297. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x;
  298. int yStart = y * fieldSize.y;
  299. if(x == 0 || y == 0)
  300. canvas->addBox(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(0, 0, 0, 100));
  301. canvas->addRectangle(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(127, 127, 127, 255));
  302. }
  303. update(0);
  304. }
  305. std::vector<StatisticDataSetEntry> OverviewPanel::playerDataFilter(PlayerColor color)
  306. {
  307. std::vector<StatisticDataSetEntry> tmpData;
  308. std::copy_if(data.data.begin(), data.data.end(), std::back_inserter(tmpData), [color](StatisticDataSetEntry e){ return e.player == color; });
  309. return tmpData;
  310. }
  311. void OverviewPanel::update(int to)
  312. {
  313. OBJECT_CONSTRUCTION;
  314. content.clear();
  315. for(int y = to; y < LINES - 1 + to; y++)
  316. {
  317. if(y >= dataExtract.size())
  318. continue;
  319. for(int x = 0; x < PlayerColor::PLAYER_LIMIT_I + 1; x++)
  320. {
  321. if(y == to && x < PlayerColor::PLAYER_LIMIT_I)
  322. content.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), x, 0, 180 + x * fieldSize.x, 35));
  323. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x + (x == 0 ? fieldSize.x : (fieldSize.x / 2));
  324. int yStart = Y_OFFS + (y + 1 - to) * fieldSize.y + (fieldSize.y / 2);
  325. PlayerColor tmpColor(x - 1);
  326. if(playerDataFilter(tmpColor).size() || x == 0)
  327. content.push_back(std::make_shared<CLabel>(xStart, yStart, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, (x == 0 ? dataExtract[y].first : dataExtract[y].second(tmpColor)), x == 0 ? (fieldSize.x * 2) : fieldSize.x));
  328. }
  329. }
  330. }
  331. LineChart::LineChart(Rect position, std::string title, TData data, TIcons icons, float maxY)
  332. : CIntObject(), maxVal(0), maxDay(0)
  333. {
  334. OBJECT_CONSTRUCTION;
  335. addUsedEvents(LCLICK | MOVE);
  336. pos = position + pos.topLeft();
  337. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  338. chartArea = pos.resize(-50);
  339. chartArea.moveTo(Point(50, 50));
  340. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
  341. statusBar = CGStatusBar::create(0, 0, ImagePath::builtin("radialMenu/statusBar"));
  342. ((std::shared_ptr<CIntObject>)statusBar)->setEnabled(false);
  343. // additional calculations
  344. bool skipMaxValCalc = maxY > 0;
  345. maxVal = maxY;
  346. for(auto & line : data)
  347. {
  348. for(auto & val : line.second)
  349. if(maxVal < val && !skipMaxValCalc)
  350. maxVal = val;
  351. if(maxDay < line.second.size())
  352. maxDay = line.second.size();
  353. }
  354. // draw
  355. for(auto & line : data)
  356. {
  357. Point lastPoint = Point(-1, -1);
  358. for(int i = 0; i < line.second.size(); i++)
  359. {
  360. float x = ((float)chartArea.w / (float)(maxDay-1)) * (float)i;
  361. float y = (float)chartArea.h - ((float)chartArea.h / maxVal) * line.second[i];
  362. Point p = Point(x, y) + chartArea.topLeft();
  363. if(lastPoint.x != -1)
  364. canvas->addLine(lastPoint, p, line.first);
  365. lastPoint = p;
  366. }
  367. }
  368. // Axis
  369. canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
  370. canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
  371. Point p = chartArea.topLeft() + Point(-5, chartArea.h + 10);
  372. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "0"));
  373. p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
  374. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CStatisticScreen::getDay(maxDay)));
  375. p = chartArea.topLeft() + Point(-5, -10);
  376. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, std::to_string((int)maxVal)));
  377. p = chartArea.bottomLeft() + Point(chartArea.w / 2, + 20);
  378. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.64")));
  379. }
  380. void LineChart::updateStatusBar(const Point & cursorPosition)
  381. {
  382. statusBar->moveTo(cursorPosition + Point(-statusBar->pos.w / 2, 20));
  383. Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
  384. statusBar->setEnabled(r.isInside(cursorPosition));
  385. if(r.isInside(cursorPosition))
  386. {
  387. float x = ((float)maxDay / (float)chartArea.w) * ((float)cursorPosition.x - (float)r.x) + 1.0f;
  388. float y = maxVal - ((float)maxVal / (float)chartArea.h) * ((float)cursorPosition.y - (float)r.y);
  389. statusBar->write(CGI->generaltexth->translate("core.genrltxt.64") + ": " + CStatisticScreen::getDay(x) + " " + CGI->generaltexth->translate("vcmi.statisticWindow.value") + ": " + ((int)y > 0 ? std::to_string((int)y) : std::to_string(y)));
  390. }
  391. GH.windows().totalRedraw();
  392. }
  393. void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  394. {
  395. updateStatusBar(cursorPosition);
  396. }
  397. void LineChart::clickPressed(const Point & cursorPosition)
  398. {
  399. updateStatusBar(cursorPosition);
  400. }