spectrumplot.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "spectrumplot.h"
  2. SpectrumPlot::SpectrumPlot()
  3. {
  4. QCustomPlot* pCustomPlot = this->customPlot();
  5. // 创建QCPColorMap
  6. m_pColorMap = new QCPColorMap(pCustomPlot->xAxis, pCustomPlot->yAxis);
  7. // pCustomPlot->xAxis->setVisible(false);
  8. // pCustomPlot->yAxis->setVisible(false);
  9. // pCustomPlot->xAxis2->setVisible(false);
  10. // pCustomPlot->yAxis2->setVisible(false);
  11. pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
  12. int nx = 1232;
  13. int ny = 128;
  14. m_pColorMap->data()->setSize(nx, ny); // we want the color map to have nx * ny data points
  15. m_pColorMap->data()->setRange(QCPRange(0, 10), QCPRange(0, 8000)); // and span the coordinate range -4..4 in both key (x) and value
  16. QFile file(":/other/spectrumdata.txt");
  17. file.open(QIODevice::ReadOnly);
  18. quint64 num = 0;
  19. for (int xIndex=0; xIndex<nx; ++xIndex)
  20. {
  21. for (int yIndex=0; yIndex<ny; ++yIndex)
  22. {
  23. if(file.atEnd())
  24. goto end;
  25. QByteArray byteArray = file.readLine();
  26. QString str = QString(byteArray);
  27. str.remove("\n");
  28. QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8());
  29. QJsonObject obj = doc.object();
  30. m_pColorMap->data()->setCell(xIndex, yIndex, obj["s"].toDouble());
  31. ++num;
  32. }
  33. }
  34. end:
  35. qDebug() << "num" << num;
  36. file.close();
  37. this->setGradient();
  38. m_pColorMap->rescaleDataRange();
  39. pCustomPlot->replot();
  40. pCustomPlot->rescaleAxes();
  41. }
  42. SpectrumPlot::~SpectrumPlot()
  43. {
  44. customPlot()->removePlottable(m_pColorMap);
  45. }
  46. void SpectrumPlot::setGradient()
  47. {
  48. QFile file(":/conf/gradient.json");
  49. if(file.open(QIODevice::ReadOnly))
  50. {
  51. QByteArray byteArray = file.readAll();
  52. file.close();
  53. QJsonParseError jsonError;
  54. QJsonDocument doc = QJsonDocument::fromJson(byteArray, &jsonError);
  55. if(jsonError.error == QJsonParseError::NoError)
  56. {
  57. QJsonObject obj = doc.object();
  58. QJsonArray array = obj["gradient"].toArray();
  59. QJsonObject temp;
  60. QCPColorGradient customGradient;
  61. customGradient.clearColorStops();
  62. customGradient.setColorInterpolation(QCPColorGradient::ciRGB);
  63. customGradient.setLevelCount(256); // 设置颜色级别的数量
  64. for(int i=0;i<array.size();++i)
  65. {
  66. temp = array.at(i).toObject();
  67. customGradient.setColorStopAt(temp["position"].toDouble(), temp["color"].toString());
  68. }
  69. m_pColorMap->setGradient(customGradient);
  70. }
  71. else
  72. qDebug() << "load gradient conf error";
  73. }
  74. else
  75. qDebug() << file.errorString();
  76. }