spectrum.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "spectrum.h"
  2. #include <QFile>
  3. #include <QJsonObject>
  4. #include <QJsonDocument>
  5. Spectrum::Spectrum(QQuickItem* parent)
  6. : QQuickPaintedItem{parent}
  7. {
  8. // 创建QCustomPlot对象
  9. m_pCustomPlot = new QCustomPlot();
  10. // configure axis rect:
  11. m_pCustomPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom); // this will also allow rescaling the color scale by dragging/zooming
  12. m_pCustomPlot->axisRect()->setupFullAxesBox(true);
  13. m_pCustomPlot->setOpenGl(true);
  14. m_pCustomPlot->xAxis->setLabel("时间(T/s)");
  15. m_pCustomPlot->yAxis->setLabel("频率(F/Hz)");
  16. m_pCustomPlot->xAxis->setVisible(false);
  17. m_pCustomPlot->yAxis->setVisible(false);
  18. m_pCustomPlot->xAxis2->setVisible(false);
  19. m_pCustomPlot->yAxis2->setVisible(false);
  20. // 创建QCPColorMap
  21. m_pColorMap = new QCPColorMap(m_pCustomPlot->xAxis, m_pCustomPlot->yAxis);
  22. int nx = 1232;
  23. int ny = 128;
  24. m_pColorMap->data()->setSize(nx, ny); // we want the color map to have nx * ny data points
  25. m_pColorMap->data()->setRange(QCPRange(0, 10), QCPRange(0, 8000)); // and span the coordinate range -4..4 in both key (x) and value (y) dimensions
  26. // now we assign some data, by accessing the QCPColorMapData instance of the color map:
  27. QFile file(":/other/spectrumdata.txt");
  28. file.open(QIODevice::ReadOnly);
  29. quint64 num = 0;
  30. for (int xIndex=0; xIndex<nx; ++xIndex)
  31. {
  32. for (int yIndex=0; yIndex<ny; ++yIndex)
  33. {
  34. if(file.atEnd())
  35. goto end;
  36. QByteArray byteArray = file.readLine();
  37. QString str = QString(byteArray);
  38. str.remove("\n");
  39. QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8());
  40. QJsonObject obj = doc.object();
  41. m_pColorMap->data()->setCell(xIndex, yIndex, obj["s"].toDouble());
  42. ++num;
  43. }
  44. }
  45. end:
  46. qDebug() << "num" << num;
  47. // while(!file.atEnd())
  48. // {
  49. // QByteArray byteArray = file.readLine();
  50. // QString str = QString(byteArray);
  51. // str.remove("\n");
  52. // QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8());
  53. // QJsonObject obj = doc.object();
  54. // m_pColorMap->data()->setCell(obj["t"].toDouble(), obj["f"].toDouble(), obj["s"].toDouble());
  55. // }
  56. file.close();
  57. // double x, y, z;
  58. // for (int xIndex=0; xIndex<nx; ++xIndex)
  59. // {
  60. // for (int yIndex=0; yIndex<ny; ++yIndex)
  61. // {
  62. // m_pColorMap->data()->cellToCoord(xIndex, yIndex, &x, &y);
  63. // double r = 3*qSqrt(x*x+y*y)+1e-2;
  64. // z = 2*x*(qCos(r+2)/r-qSin(r+2)/r); // the B field strength of dipole radiation (modulo physical constants)
  65. // m_pColorMap->data()->setCell(xIndex, yIndex, z);
  66. // }
  67. // }
  68. // add a color scale:
  69. // QCPColorScale *colorScale = new QCPColorScale(m_pCustomPlot);
  70. // m_pCustomPlot->plotLayout()->addElement(0, 1, colorScale); // add it to the right of the main axis rect
  71. // colorScale->setType(QCPAxis::atRight); // scale shall be vertical bar with tick/axis labels right (actually atRight is already the default)
  72. // m_pColorMap->setColorScale(colorScale); // associate the color map with the color scale
  73. // colorScale->axis()->setLabel("Magnetic Field Strength");
  74. // set the color gradient of the color map to one of the presets:
  75. this->setGradient();
  76. // we could have also created a QCPColorGradient instance and added own colors to
  77. // the gradient, see the documentation of QCPColorGradient for what's possible.
  78. // rescale the data dimension (color) such that all data points lie in the span visualized by the color gradient:
  79. m_pColorMap->rescaleDataRange();
  80. // make sure the axis rect and color scale synchronize their bottom and top margins (so they line up):
  81. QCPMarginGroup *marginGroup = new QCPMarginGroup(m_pCustomPlot);
  82. m_pCustomPlot->axisRect()->setMarginGroup(QCP::msBottom|QCP::msTop, marginGroup);
  83. m_pCustomPlot->axisRect()->setAutoMargins(QCP::msNone);
  84. m_pCustomPlot->axisRect()->setMargins(QMargins(0, 0, 0, 0));
  85. m_pCustomPlot->plotLayout()->setMargins(QMargins(0, 0, 0, 0));
  86. m_pCustomPlot->replot();
  87. // colorScale->setMarginGroup(QCP::msBottom|QCP::msTop, marginGroup);
  88. // rescale the key (x) and value (y) axes so the whole color map is visible:
  89. m_pCustomPlot->rescaleAxes();
  90. }
  91. Spectrum::~Spectrum()
  92. {
  93. m_pCustomPlot->removePlottable(m_pColorMap);
  94. delete m_pCustomPlot;
  95. }
  96. void Spectrum::paint(QPainter *painter)
  97. {
  98. m_pCustomPlot->setGeometry(0,0,this->width(),this->height());
  99. painter->drawPixmap(0,0,this->width(),this->height(), m_pCustomPlot->toPixmap());
  100. }
  101. void Spectrum::setGradient()
  102. {
  103. QFile file(":/conf/gradient.json");
  104. if(file.open(QIODevice::ReadOnly))
  105. {
  106. QByteArray byteArray = file.readAll();
  107. file.close();
  108. QJsonParseError jsonError;
  109. QJsonDocument doc = QJsonDocument::fromJson(byteArray, &jsonError);
  110. if(jsonError.error == QJsonParseError::NoError)
  111. {
  112. QJsonObject obj = doc.object();
  113. QJsonArray array = obj["gradient"].toArray();
  114. QJsonObject temp;
  115. QCPColorGradient customGradient;
  116. customGradient.clearColorStops();
  117. customGradient.setColorInterpolation(QCPColorGradient::ciRGB);
  118. customGradient.setLevelCount(256); // 设置颜色级别的数量
  119. for(int i=0;i<array.size();++i)
  120. {
  121. temp = array.at(i).toObject();
  122. customGradient.setColorStopAt(temp["position"].toDouble(), temp["color"].toString());
  123. }
  124. m_pColorMap->setGradient(customGradient);
  125. }
  126. else
  127. qDebug() << "load gradient conf error";
  128. }
  129. else
  130. qDebug() << file.errorString();
  131. }