Açıklama Yok

census.cpp 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "census.h"
  2. #include <QFile>
  3. #include <QJsonDocument>
  4. #include <QJsonArray>
  5. #include <QJsonObject>
  6. #include <qDebug>
  7. ///
  8. /// \brief
  9. /// Census::readDataFromFile - Populates the Census object with data from a file.
  10. /// at the end we should have Map with (key,value) = (city name, some metric).
  11. /// \param fileName: name of the JSON file
  12. /// \return true if the file was openned and read
  13. ///
  14. bool Census::readDataFromFile(QString fileName) {
  15. QFile loadFile(fileName);
  16. if (!loadFile.open(QIODevice::ReadOnly)) {
  17. qWarning("Couldn't open save file.");
  18. return false;
  19. }
  20. QByteArray saveData = loadFile.readAll();
  21. QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
  22. QJsonArray topLevelArray = loadDoc.array();
  23. int ctr = 1;
  24. minValue = maxValue = topLevelArray[0].toObject()["percent_change"].toDouble();
  25. foreach(QJsonValue obj, topLevelArray) {
  26. double value = obj.toObject()["percent_change"].toDouble();
  27. (*this)[obj.toObject()["city"].toString()] = value;
  28. if (value > maxValue) maxValue = value;
  29. if (value < minValue) minValue = value;
  30. ctr++;
  31. }
  32. Census::iterator it = this->begin();
  33. for (; it != this->end(); ++it)
  34. qDebug() << it.key() << it.value();
  35. factor = 128.0 / (maxValue - minValue) ;
  36. }