VWaveChart.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include "VWavechart.h"
  2. QPainterPath SmoothCurveCreator::createSmoothCurve(const QVector<QPointF>& points)
  3. {
  4. QPainterPath path;
  5. int len = points.size();
  6. if (len < 2) {
  7. return path;
  8. }
  9. QVector<QPointF> firstControlPoints;
  10. QVector<QPointF> secondControlPoints;
  11. calculateControlPoints(points, &firstControlPoints, &secondControlPoints);
  12. path.moveTo(points[0].x(), points[0].y());
  13. for (int i = 0; i < len - 1; ++i) {
  14. path.cubicTo(firstControlPoints[i], secondControlPoints[i], points[i + 1]);
  15. }
  16. return path;
  17. }
  18. void SmoothCurveCreator::calculateFirstControlPoints(double*& result, const double* rhs, int n)
  19. {
  20. result = new double[n];
  21. double* tmp = new double[n];
  22. double b = 2.0;
  23. result[0] = rhs[0] / b;
  24. for (int i = 1; i < n; i++) {
  25. tmp[i] = 1 / b;
  26. b = (i < n - 1 ? 4.0 : 3.5) - tmp[i];
  27. result[i] = (rhs[i] - result[i - 1]) / b;
  28. }
  29. for (int i = 1; i < n; i++) {
  30. result[n - i - 1] -= tmp[n - i] * result[n - i];
  31. }
  32. delete tmp;
  33. }
  34. void SmoothCurveCreator::calculateControlPoints(const QVector<QPointF>& knots, QVector<QPointF>* firstControlPoints, QVector<QPointF>* secondControlPoints)
  35. {
  36. int n = knots.size() - 1;
  37. for (int i = 0; i < n; ++i) {
  38. firstControlPoints->append(QPointF());
  39. secondControlPoints->append(QPointF());
  40. }
  41. if (n == 1) {
  42. (*firstControlPoints)[0].rx() = (2 * knots[0].x() + knots[1].x()) / 3;
  43. (*firstControlPoints)[0].ry() = (2 * knots[0].y() + knots[1].y()) / 3;
  44. (*secondControlPoints)[0].rx() = 2 * (*firstControlPoints)[0].x() - knots[0].x();
  45. (*secondControlPoints)[0].ry() = 2 * (*firstControlPoints)[0].y() - knots[0].y();
  46. return;
  47. }
  48. double* xs = 0;
  49. double* ys = 0;
  50. double* rhsx = new double[n];
  51. double* rhsy = new double[n];
  52. for (int i = 1; i < n - 1; ++i) {
  53. rhsx[i] = 4 * knots[i].x() + 2 * knots[i + 1].x();
  54. rhsy[i] = 4 * knots[i].y() + 2 * knots[i + 1].y();
  55. }
  56. rhsx[0] = knots[0].x() + 2 * knots[1].x();
  57. rhsx[n - 1] = (8 * knots[n - 1].x() + knots[n].x()) / 2.0;
  58. rhsy[0] = knots[0].y() + 2 * knots[1].y();
  59. rhsy[n - 1] = (8 * knots[n - 1].y() + knots[n].y()) / 2.0;
  60. calculateFirstControlPoints(xs, rhsx, n);
  61. calculateFirstControlPoints(ys, rhsy, n);
  62. for (int i = 0; i < n; ++i) {
  63. (*firstControlPoints)[i].rx() = xs[i];
  64. (*firstControlPoints)[i].ry() = ys[i];
  65. if (i < n - 1) {
  66. (*secondControlPoints)[i].rx() = 2 * knots[i + 1].x() - xs[i + 1];
  67. (*secondControlPoints)[i].ry() = 2 * knots[i + 1].y() - ys[i + 1];
  68. }
  69. else {
  70. (*secondControlPoints)[i].rx() = (knots[n].x() + xs[n - 1]) / 2;
  71. (*secondControlPoints)[i].ry() = (knots[n].y() + ys[n - 1]) / 2;
  72. }
  73. }
  74. delete xs;
  75. delete ys;
  76. delete rhsx;
  77. delete rhsy;
  78. }
  79. VWaveChart::VWaveChart(
  80. QWidget* parent,
  81. const QPoint& pos,
  82. const QSize& size,
  83. CONTROL_PROPERTY* pProperty
  84. )
  85. : QWidget(parent)
  86. , VControlObject(pProperty)
  87. {
  88. minValue = 0;
  89. maxValue = 100;
  90. xStep = 10;
  91. yStep = 10;
  92. space = 40;
  93. title = "曲线图";
  94. smooth = false;
  95. showHLine = true;
  96. showPoint = true;
  97. showPointBg = true;
  98. bgColorStart = QColor(79, 79, 79);
  99. bgColorEnd = QColor(51, 51, 51);
  100. textColor = QColor(250, 250, 250);
  101. pointColor = QColor(38, 114, 179);
  102. // 设置尺寸
  103. if (size == DEFAULT_CONTROL_SIZE)
  104. {
  105. this->resize(DEFAULT_WAVE_SIZE);
  106. }
  107. else
  108. {
  109. this->resize(size);
  110. }
  111. // 设置位置
  112. this->move(pos);
  113. this->m_pWidget = this;
  114. m_Type = VALUE_TYPE::Control_Wavechart;
  115. }
  116. void VWaveChart::paintEvent(QPaintEvent *)
  117. {
  118. //绘制准备工作,启用反锯齿
  119. QPainter painter(this);
  120. painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  121. //绘制背景
  122. drawBg(&painter);
  123. //绘制盒子
  124. drawBox(&painter);
  125. //绘制文字
  126. drawText(&painter);
  127. //绘制标题
  128. drawTitle(&painter);
  129. //绘制数据点
  130. drawPoint(&painter);
  131. }
  132. void VWaveChart::drawBg(QPainter *painter)
  133. {
  134. painter->save();
  135. painter->setPen(Qt::NoPen);
  136. QLinearGradient topGradient(rect().topLeft(), rect().bottomLeft());
  137. topGradient.setColorAt(0.0, bgColorStart);
  138. topGradient.setColorAt(1.0, bgColorEnd);
  139. painter->setBrush(topGradient);
  140. painter->drawRect(rect());
  141. painter->restore();
  142. }
  143. void VWaveChart::drawBox(QPainter *painter)
  144. {
  145. painter->save();
  146. QPointF topLeftPot(space, space);
  147. QPointF bottomRightPot(width() - space / 2, height() - space / 2);
  148. painter->setPen(textColor);
  149. painter->setBrush(Qt::NoBrush);
  150. pointRect = QRectF(topLeftPot, bottomRightPot);
  151. painter->drawRect(pointRect);
  152. //绘制横线
  153. if (showHLine) {
  154. QPen pen(textColor, 1, Qt::DotLine);
  155. painter->setPen(pen);
  156. int step = (maxValue - minValue) / xStep;
  157. double increment = (double)pointRect.height() / step;
  158. double startY = pointRect.topLeft().y();
  159. for (int i = 0; i < step - 1; i++) {
  160. startY += increment;
  161. QPointF leftPot(pointRect.topLeft().x(), startY);
  162. QPointF rightPot(pointRect.topRight().x(), startY);
  163. painter->drawLine(leftPot, rightPot);
  164. }
  165. }
  166. painter->restore();
  167. }
  168. void VWaveChart::drawText(QPainter *painter)
  169. {
  170. painter->save();
  171. painter->setPen(textColor);
  172. painter->setBrush(Qt::NoBrush);
  173. int step = (maxValue - minValue) / xStep;
  174. int value = maxValue;
  175. double increment = (double)pointRect.height() / step;
  176. double startY = pointRect.topLeft().y();
  177. QString strValue;
  178. for (int i = 0; i <= step; i++) {
  179. strValue = QString("%1").arg(value);
  180. double textWidth = fontMetrics().width(strValue);
  181. double textHeight = fontMetrics().height();
  182. QPointF textPot(pointRect.topLeft().x() - 5 - textWidth, startY + textHeight / 2);
  183. painter->drawText(textPot, strValue);
  184. value -= xStep;
  185. startY += increment;
  186. }
  187. painter->restore();
  188. }
  189. void VWaveChart::drawTitle(QPainter *painter)
  190. {
  191. painter->save();
  192. painter->setPen(textColor);
  193. painter->setBrush(Qt::NoBrush);
  194. double titleX = (double)width() / 2;
  195. double titleY = space;
  196. double textWidth = fontMetrics().width(title);
  197. double textHeight = fontMetrics().height();
  198. //标题加粗显示
  199. QFont titleFont;
  200. titleFont.setBold(true);
  201. titleFont.setPixelSize(13);
  202. painter->setFont(titleFont);
  203. QPointF textPot(titleX - textWidth / 2, titleY / 2 + textHeight / 2);
  204. painter->drawText(textPot, title);
  205. painter->restore();
  206. }
  207. void VWaveChart::drawPoint(QPainter *painter)
  208. {
  209. painter->save();
  210. double startX = pointRect.topRight().x();
  211. QVector<QPointF> points;
  212. if (showPointBg) {
  213. points.push_back(QPointF(startX, pointRect.bottomRight().y()));
  214. }
  215. for (int i = 0; i < listData.count(); i++) {
  216. QPointF dataPot(startX, pointRect.bottomRight().y() - (double)listData.at(i) * (pointRect.height() / (maxValue - minValue)));
  217. points.push_back(dataPot);
  218. startX -= yStep;
  219. }
  220. if (showPointBg) {
  221. points.push_back(QPointF(startX, pointRect.bottomRight().y()));
  222. }
  223. //如果只有两个数据点不需要处理
  224. if (showPointBg && points.count() <= 2) {
  225. painter->restore();
  226. return;
  227. }
  228. painter->setPen(pointColor);
  229. if (showPointBg) {
  230. painter->setBrush(QColor(pointColor.red(), pointColor.green(), pointColor.blue(), 150));
  231. if (!smooth) {
  232. painter->drawPolygon(QPolygonF(points));
  233. }
  234. } else {
  235. painter->setBrush(Qt::NoBrush);
  236. if (!smooth) {
  237. painter->drawPolyline(QPolygonF(points));
  238. }
  239. }
  240. //绘制平滑曲线
  241. if (smooth) {
  242. QPainterPath path = SmoothCurveCreator::createSmoothCurve(points);
  243. painter->drawPath(path);
  244. }
  245. //绘制坐标点
  246. if (showPoint) {
  247. for (int i = 0; i < points.count(); i++) {
  248. QPointF dataPot = points.at(i);
  249. painter->setBrush(pointColor);
  250. painter->drawEllipse(dataPot, 3, 3);
  251. }
  252. }
  253. painter->restore();
  254. }
  255. void VWaveChart::updateData()
  256. {
  257. int count = pointRect.width() / yStep;
  258. int i = listData.count() - count;
  259. if (i > 0) {
  260. listData.remove(count, i);
  261. }
  262. update();
  263. }
  264. double VWaveChart::getMinValue() const
  265. {
  266. return this->minValue;
  267. }
  268. double VWaveChart::getMaxValue() const
  269. {
  270. return this->maxValue;
  271. }
  272. double VWaveChart::getXStep() const
  273. {
  274. return this->xStep;
  275. }
  276. double VWaveChart::getYStep() const
  277. {
  278. return this->yStep;
  279. }
  280. double VWaveChart::getSpace() const
  281. {
  282. return this->space;
  283. }
  284. QString VWaveChart::getTitle() const
  285. {
  286. return this->title;
  287. }
  288. bool VWaveChart::getSmooth() const
  289. {
  290. return this->smooth;
  291. }
  292. bool VWaveChart::getShowHLine() const
  293. {
  294. return this->showHLine;
  295. }
  296. bool VWaveChart::getShowPoint() const
  297. {
  298. return this->showPoint;
  299. }
  300. bool VWaveChart::getShowPointBg() const
  301. {
  302. return this->showPointBg;
  303. }
  304. QColor VWaveChart::getBgColorStart() const
  305. {
  306. return this->bgColorStart;
  307. }
  308. QColor VWaveChart::getBgColorEnd() const
  309. {
  310. return this->bgColorEnd;
  311. }
  312. QColor VWaveChart::getTextColor() const
  313. {
  314. return this->textColor;
  315. }
  316. QColor VWaveChart::getPointColor() const
  317. {
  318. return this->pointColor;
  319. }
  320. QSize VWaveChart::sizeHint() const
  321. {
  322. return QSize(500, 300);
  323. }
  324. QSize VWaveChart::minimumSizeHint() const
  325. {
  326. return QSize(200, 70);
  327. }
  328. void VWaveChart::addData(double data)
  329. {
  330. listData.push_front(data);
  331. updateData();
  332. }
  333. void VWaveChart::setData(QVector<double> data)
  334. {
  335. listData = data;
  336. updateData();
  337. }
  338. void VWaveChart::clearData()
  339. {
  340. listData.clear();
  341. update();
  342. }
  343. void VWaveChart::setMinValue(double minValue)
  344. {
  345. if (this->minValue != minValue) {
  346. this->minValue = minValue;
  347. update();
  348. }
  349. }
  350. void VWaveChart::setMaxValue(double maxValue)
  351. {
  352. if (this->maxValue != maxValue) {
  353. this->maxValue = maxValue;
  354. update();
  355. }
  356. }
  357. void VWaveChart::setXStep(double xStep)
  358. {
  359. if (this->xStep != xStep) {
  360. this->xStep = xStep;
  361. update();
  362. }
  363. }
  364. void VWaveChart::setYStep(double yStep)
  365. {
  366. if (this->yStep != yStep) {
  367. this->yStep = yStep;
  368. update();
  369. }
  370. }
  371. void VWaveChart::setSpace(double space)
  372. {
  373. if (this->space != space) {
  374. this->space = space;
  375. update();
  376. }
  377. }
  378. void VWaveChart::setTitle(const QString &title)
  379. {
  380. if (this->title != title) {
  381. this->title = title;
  382. update();
  383. }
  384. }
  385. void VWaveChart::setSmooth(bool smooth)
  386. {
  387. if (this->smooth != smooth) {
  388. this->smooth = smooth;
  389. update();
  390. }
  391. }
  392. void VWaveChart::setShowHLine(bool showHLine)
  393. {
  394. if (this->showHLine != showHLine) {
  395. this->showHLine = showHLine;
  396. update();
  397. }
  398. }
  399. void VWaveChart::setShowPoint(bool showPoint)
  400. {
  401. if (this->showPoint != showPoint) {
  402. this->showPoint = showPoint;
  403. update();
  404. }
  405. }
  406. void VWaveChart::setShowPointBg(bool showPointBg)
  407. {
  408. if (this->showPointBg != showPointBg) {
  409. this->showPointBg = showPointBg;
  410. update();
  411. }
  412. }
  413. void VWaveChart::setBgColorStart(const QColor &bgColorStart)
  414. {
  415. if (this->bgColorStart != bgColorStart) {
  416. this->bgColorStart = bgColorStart;
  417. update();
  418. }
  419. }
  420. void VWaveChart::setBgColorEnd(const QColor &bgColorEnd)
  421. {
  422. if (this->bgColorEnd != bgColorEnd) {
  423. this->bgColorEnd = bgColorEnd;
  424. update();
  425. }
  426. }
  427. void VWaveChart::setTextColor(const QColor &textColor)
  428. {
  429. if (this->textColor != textColor) {
  430. this->textColor = textColor;
  431. update();
  432. }
  433. }
  434. void VWaveChart::setPointColor(const QColor &pointColor)
  435. {
  436. if (this->pointColor != pointColor) {
  437. this->pointColor = pointColor;
  438. update();
  439. }
  440. }