CustomPlotHelper.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "CustomPlotHelper.h"
  2. #include "../vpControls/VCustomPlot.h"
  3. lcolor CustomPlotHelper::colors = lcolor();
  4. lcolor CustomPlotHelper::getColorList()
  5. {
  6. //备用颜色集合 可以自行添加
  7. if (colors.count() == 0) {
  8. colors << QColor(0, 176, 180) << QColor(0, 113, 193) << QColor(255, 192, 0);
  9. colors << QColor(72, 103, 149) << QColor(185, 87, 86) << QColor(0, 177, 125);
  10. colors << QColor(214, 77, 84) << QColor(71, 164, 233) << QColor(34, 163, 169);
  11. colors << QColor(59, 123, 156) << QColor(162, 121, 197) << QColor(72, 202, 245);
  12. colors << QColor(0, 150, 121) << QColor(111, 9, 176) << QColor(250, 170, 20);
  13. }
  14. return colors;
  15. }
  16. QStringList CustomPlotHelper::getColorNames()
  17. {
  18. QList<QColor> colors = getColorList();
  19. QStringList colorNames;
  20. foreach (QColor color, colors) {
  21. colorNames << color.name();
  22. }
  23. return colorNames;
  24. }
  25. QColor CustomPlotHelper::getRandColor()
  26. {
  27. QList<QColor> colors = getColorList();
  28. int index = getRandValue(0, colors.count());
  29. return colors.at(index);
  30. }
  31. double CustomPlotHelper::getRandValue(int min, int max)
  32. {
  33. int value;
  34. #if (QT_VERSION <= QT_VERSION_CHECK(5,10,0))
  35. value = (rand() % (max - min + 1)) + min + 1;
  36. #else
  37. value = QRandomGenerator::global()->bounded(min, max);
  38. #endif
  39. return value;
  40. }
  41. qint16 CustomPlotHelper::strHexToShort(const QString &strHex)
  42. {
  43. bool ok;
  44. return strHex.toUShort(&ok, 16);
  45. }
  46. void CustomPlotHelper::getRange(const vpoint &points, double &minX, double &maxX, double &minY, double &maxY, double offset)
  47. {
  48. int count = points.count();
  49. if (count == 0) {
  50. return;
  51. }
  52. minX = maxX = points.at(0).x();
  53. minY = maxY = points.at(0).y();
  54. //开始索引从1开始因为没必要和自己比较
  55. for (int i = 1; i < count; ++i) {
  56. double x = points.at(i).x();
  57. double y = points.at(i).y();
  58. if (x < minX) {
  59. minX = x;
  60. } else if (x > maxX) {
  61. maxX = x;
  62. }
  63. if (y < minY) {
  64. minY = y;
  65. } else if (y > maxY) {
  66. maxY = y;
  67. }
  68. }
  69. //根据偏移值倍数重新调整
  70. if (offset != 0) {
  71. double offsetX = (maxX - minX) * offset;
  72. double offsetY = (maxY - minY) * offset;
  73. minX -= offsetX;
  74. maxX += offsetX;
  75. minY -= offsetY;
  76. maxY += offsetY;
  77. }
  78. //qDebug() << TIMEMS << points << minX << maxX << minY << maxY;
  79. }
  80. double CustomPlotHelper::getMaxValue(const lvdouble &values)
  81. {
  82. int count = values.count();
  83. if (count == 0) {
  84. return 0;
  85. }
  86. //找出最大值
  87. double max = values.at(0).at(0);
  88. for (int i = 0; i < count; i++)
  89. {
  90. vdouble value = values.at(i);
  91. foreach (double temp, value)
  92. {
  93. if (temp > max)
  94. {
  95. max = temp;
  96. }
  97. }
  98. }
  99. return max;
  100. }
  101. double CustomPlotHelper::getMaxValue2(const lvdouble &values)
  102. {
  103. int count = values.count();
  104. if (count == 0)
  105. {
  106. return 0;
  107. }
  108. double max = values.at(0).at(0);
  109. int column = values.at(0).count();
  110. QList<int> maxs;
  111. for (int j = 0; j < column; j++)
  112. {
  113. maxs << 0;
  114. }
  115. //逐个计算每一列相加的值
  116. for (int i = 0; i < count; i++)
  117. {
  118. vdouble value = values.at(i);
  119. for (int j = 0; j < column; j++)
  120. {
  121. maxs[j] += value.at(j);
  122. }
  123. }
  124. //取出最大值
  125. for (int j = 0; j < column; j++)
  126. {
  127. if (maxs.at(j) > max) {
  128. max = maxs.at(j);
  129. }
  130. }
  131. return max;
  132. }
  133. void CustomPlotHelper::setGraphBrush(QCPGraph *graph, const QColor &color, int type)
  134. {
  135. setGraphBrush(graph, 0, color, type);
  136. }
  137. void CustomPlotHelper::setGraphBrush(QPainter *painter, const QColor &color, int type)
  138. {
  139. setGraphBrush(0, painter, color, type);
  140. }
  141. void CustomPlotHelper::setGraphBrush(QCPGraph *graph, QPainter *painter, const QColor &color, int type)
  142. {
  143. //可以选择设置单色,也可以设置渐变颜色
  144. //可以自行增加渐变规则
  145. QColor bgColor = color;
  146. if (type == 1)
  147. {
  148. bgColor.setAlpha(150);
  149. if (graph != 0)
  150. {
  151. graph->setBrush(bgColor);
  152. }
  153. if (painter != 0)
  154. {
  155. painter->setBrush(bgColor);
  156. }
  157. }
  158. else if (type == 2)
  159. {
  160. QLinearGradient linearGradient(0, 0, 0, 150);
  161. bgColor.setAlpha(255);
  162. linearGradient.setColorAt(0, bgColor);
  163. bgColor.setAlpha(15);
  164. linearGradient.setColorAt(1, bgColor);
  165. if (graph != 0)
  166. {
  167. graph->setBrush(linearGradient);
  168. }
  169. if (painter != 0)
  170. {
  171. painter->setBrush(linearGradient);
  172. }
  173. }
  174. }