VCustomPlot.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. #include "VCustomPlot.h"
  2. //#include <Qtimer>
  3. #include "GvlManager.h"
  4. // 默认的分块数
  5. #define DEFAULT_CUSTOMPLOT_COUNT 3
  6. VCustomPlot::VCustomPlot(
  7. QWidget* parent,
  8. const QPoint& pos,
  9. const QSize& size,
  10. CONTROL_PROPERTY* pProperty,
  11. CONTROL_PROPERTY_EX* pPropertyEx
  12. )
  13. : QWidget(parent)
  14. , VControlObject(pProperty)
  15. {
  16. // 设置控件指针
  17. m_pWidget = this;
  18. // 设置控件类型
  19. m_Type = VALUE_TYPE::Control_CustomPlot;
  20. //实例化曲线控件
  21. customPlot = new QCustomPlot;
  22. //绑定鼠标移动显示数据
  23. connect(customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
  24. layerable = 0;
  25. tracer = 0;
  26. textTip = 0;
  27. //设置布局加入曲线控件
  28. QHBoxLayout* layout = new QHBoxLayout;
  29. layout->setSpacing(0);
  30. layout->setContentsMargins(0, 0, 0, 0);
  31. layout->addWidget(customPlot);
  32. this->setLayout(layout);
  33. //备用颜色集合
  34. setColorNames(CustomPlotHelper::getColorNames().join("|"));
  35. setBgColor(QColor(38, 41, 74));
  36. setBgColor(QColor(61, 61, 61));
  37. setTextColor(QColor(250, 250, 250));
  38. gridWidth = 1;
  39. setGridColor(QColor(230, 230, 230));
  40. lineWidth = 3;
  41. lineColor = QColor(255, 0, 0);
  42. showLineh = false;
  43. showLinev = false;
  44. fontSize = 12;
  45. pointSize = 8;
  46. offsetX = 1;
  47. offsetY = 20;
  48. toolTipPosition = 0;
  49. interactions = 0;
  50. padding = 5;
  51. axis2Visible = false;
  52. percentY = false;
  53. isLoad = false;
  54. this->init();
  55. //if (parent == nullptr)
  56. //{
  57. // return;
  58. //}
  59. // 作为控件时候的专用初始化代码。
  60. //////////////////////////////////////////////////////////////////////////
  61. // 设置尺寸
  62. if (size == DEFAULT_CONTROL_SIZE)
  63. {
  64. this->resize(DEFAULT_IMAGE_SIZE);
  65. }
  66. else
  67. {
  68. this->resize(size);
  69. }
  70. // 设置中心点坐标
  71. QPoint tempPos;
  72. tempPos.setX(pos.x() - width() / 2);
  73. tempPos.setY(pos.y() - height() / 2);
  74. // 设置位置
  75. this->move(tempPos);
  76. initPropertyEx(pPropertyEx);
  77. //customPlot->axisRect()->setupFullAxesBox();//四边安装轴并显示
  78. //自定义属性在此从属性表中设置到控件
  79. {
  80. setInteractions(m_Property.m_nInteractions);
  81. setDrawingMode(m_Property.m_nDrawingMode);
  82. setText(m_Property.m_strText);
  83. }
  84. }
  85. void VCustomPlot::showEvent(QShowEvent*)
  86. {
  87. if (!isLoad) {
  88. isLoad = true;
  89. if (this->showLineh || this->showLinev) {
  90. this->drawFlowLine();
  91. }
  92. }
  93. }
  94. void VCustomPlot::resizeEvent(QResizeEvent*)
  95. {
  96. if (layerable != 0) {
  97. layerable->drawLine();
  98. }
  99. }
  100. void VCustomPlot::mouseMove(QMouseEvent* event)
  101. {
  102. if (tracer == 0 || textTip == 0) {
  103. return;
  104. }
  105. //跟踪鼠标点击事件点击位置
  106. QCPGraph* graph = (QCPGraph*)customPlot->plottableAt(event->pos(), true);
  107. QRect rect(0, 0, 1, 1);
  108. QString labx, laby;
  109. int offset = 10;
  110. bool toolTip = false;
  111. if (graph != 0) {
  112. double posKey;
  113. double key = 0, value = 0;
  114. QPoint p;
  115. p.setX(event->pos().x());
  116. p.setY(event->pos().y());
  117. #ifdef qcustomplot_v1
  118. foreach(QCPData data, graph->data()->values()) {
  119. key = data.key;
  120. value = data.value;
  121. #else
  122. for (int i = 0; i < graph->dataCount(); i++)
  123. {
  124. key = graph->dataMainKey(i);
  125. value = graph->dataMainValue(i);
  126. #endif
  127. //取出对应key处的label标签,如果标签为空则取key的字符串
  128. //需要根据tickVector来找到对应的tickVectorLabels,有时候拖动表到不可见的部位tickVectorLabels不是全部的tickVectorLabels
  129. vdouble values = customPlot->xAxis->tickVector();
  130. vstring labs = customPlot->xAxis->tickVectorLabels();
  131. int index = values.indexOf(key);
  132. if (index >= 0 && labs.size() > 0)
  133. {
  134. labx = labs.at(index);
  135. }
  136. else {
  137. labx = QString::number(key);
  138. }
  139. //如果启用了百分比则需要后面显示百分比
  140. if (this->getPercentY()) {
  141. laby = QString("%1%").arg(value);
  142. }
  143. else {
  144. laby = QString("%1").arg(value);
  145. }
  146. //计算当前是否在区域内,在的话就显示
  147. posKey = customPlot->xAxis->coordToPixel(key);
  148. if (qAbs(posKey - event->pos().x()) <= offset) {
  149. double posx = graph->keyAxis()->coordToPixel(key);
  150. double posy = graph->valueAxis()->coordToPixel(value);
  151. rect.setRect(posx - offset, posy - offset, offset * 2, offset * 2);
  152. if (!rect.contains(event->pos())) {
  153. continue;
  154. }
  155. else {
  156. break;
  157. }
  158. }
  159. if (posKey - event->pos().x() > offset) {
  160. break;
  161. }
  162. }
  163. if (!graph->realVisibility()) {
  164. toolTip = false;
  165. tracer->setVisible(false);
  166. customPlot->replot();
  167. }
  168. else if (rect.contains(event->pos())) {
  169. //先设置大小和颜色
  170. tracer->setSize(10);
  171. tracer->setPen(QPen(this->m_Property.m_clrText, 3));
  172. tracer->setBrush(QBrush(lineColor, Qt::SolidPattern));
  173. toolTip = true;
  174. tracer->setVisible(true);
  175. tracer->setGraph(graph);
  176. tracer->setGraphKey(key);
  177. customPlot->replot();
  178. }
  179. else if (tracer->visible()) {
  180. toolTip = false;
  181. tracer->setVisible(false);
  182. customPlot->replot();
  183. }
  184. }
  185. else if (tracer->visible()) {
  186. toolTip = false;
  187. tracer->setVisible(false);
  188. customPlot->replot();
  189. }
  190. //处理提示信息
  191. if (toolTip) {
  192. //根据设定的提示信息位置调整
  193. double x = event->pos().x();
  194. double y = event->pos().y();
  195. checkToolTip(x, y);
  196. //先设置颜色
  197. textTip->setColor(this->m_Property.m_clrText);
  198. textTip->setBrush(this->m_Property.m_clrBg);
  199. QString text = "横坐标: " + labx + "\n当前值: " + laby;
  200. textTip->position->setCoords(x, y);
  201. textTip->setText(text);
  202. textTip->setVisible(true);
  203. customPlot->replot();
  204. }
  205. else {
  206. if (textTip->visible()) {
  207. textTip->setVisible(false);
  208. customPlot->replot();
  209. }
  210. }
  211. }
  212. void VCustomPlot::checkToolTip(double& x, double& y)
  213. {
  214. //下面的计算位置偏移值未必精准可以自行调整
  215. //后期可能要根据提示信息面板的宽高进行精准计算
  216. if (toolTipPosition == 0) {
  217. //判断曲线控件的位置已经当前鼠标位置做出调整
  218. bool right = (customPlot->width() - x < 90);
  219. bool bottom = (customPlot->height() - y < 50);
  220. if (right) {
  221. if (bottom) {
  222. x = x - 100;
  223. y = y - 50;
  224. }
  225. else {
  226. x = x - 100;
  227. y = y + 10;
  228. }
  229. }
  230. else {
  231. if (bottom) {
  232. x = x + 10;
  233. y = y - 50;
  234. }
  235. else {
  236. x = x + 10;
  237. y = y + 10;
  238. }
  239. }
  240. }
  241. else if (toolTipPosition == 1) {
  242. x = x - 30;
  243. y = y - 50;
  244. }
  245. else if (toolTipPosition == 2) {
  246. x = x + 10;
  247. y = y - 50;
  248. }
  249. else if (toolTipPosition == 3) {
  250. x = x + 10;
  251. y = y - 20;
  252. }
  253. else if (toolTipPosition == 4) {
  254. x = x + 10;
  255. y = y + 10;
  256. }
  257. else if (toolTipPosition == 5) {
  258. x = x - 30;
  259. y = y + 10;
  260. }
  261. else if (toolTipPosition == 6) {
  262. x = x - 75;
  263. y = y + 10;
  264. }
  265. else if (toolTipPosition == 7) {
  266. x = x - 80;
  267. y = y - 20;
  268. }
  269. else if (toolTipPosition == 8) {
  270. x = x - 80;
  271. y = y - 50;
  272. }
  273. }
  274. lcolor VCustomPlot::getColors() const
  275. {
  276. return this->colors;
  277. }
  278. QString VCustomPlot::getColorNames() const
  279. {
  280. return this->colorNames;
  281. }
  282. QColor VCustomPlot::getBgColor() const
  283. {
  284. return this->m_Property.m_clrBg;
  285. }
  286. QColor VCustomPlot::getTextColor() const
  287. {
  288. return this->m_Property.m_clrText;
  289. }
  290. int VCustomPlot::getGridWidth() const
  291. {
  292. return this->gridWidth;
  293. }
  294. QColor VCustomPlot::getGridColor() const
  295. {
  296. return this->gridColor;
  297. }
  298. int VCustomPlot::getLineWidth() const
  299. {
  300. return this->lineWidth;
  301. }
  302. QColor VCustomPlot::getLineColor() const
  303. {
  304. return this->lineColor;
  305. }
  306. bool VCustomPlot::getShowLineh() const
  307. {
  308. return this->showLineh;
  309. }
  310. bool VCustomPlot::getShowLinev() const
  311. {
  312. return this->showLinev;
  313. }
  314. int VCustomPlot::getFontSize() const
  315. {
  316. return this->fontSize;
  317. }
  318. int VCustomPlot::getPointSize() const
  319. {
  320. return this->pointSize;
  321. }
  322. double VCustomPlot::getOffsetX() const
  323. {
  324. return this->offsetX;
  325. }
  326. double VCustomPlot::getOffsetY() const
  327. {
  328. return this->offsetY;
  329. }
  330. int VCustomPlot::getToolTipPosition() const
  331. {
  332. return this->toolTipPosition;
  333. }
  334. int VCustomPlot::getInteractions() const
  335. {
  336. return this->interactions;
  337. }
  338. int VCustomPlot::getDrawingMode() const
  339. {
  340. return this->m_Property.m_nDrawingMode;
  341. }
  342. int VCustomPlot::getTickCountX() const
  343. {
  344. return this->m_Property.m_nTickCountX;
  345. }
  346. int VCustomPlot::getTickCountY() const
  347. {
  348. return this->m_Property.m_nTickCountY;
  349. }
  350. QSize VCustomPlot::getRangeX()const
  351. {
  352. return this->m_Property.m_RangeX;
  353. }
  354. QSize VCustomPlot::getRangeY()const
  355. {
  356. return this->m_Property.m_RangeY;
  357. }
  358. int VCustomPlot::getPadding() const
  359. {
  360. return this->padding;
  361. }
  362. bool VCustomPlot::getAxis2Visible() const
  363. {
  364. return this->axis2Visible;
  365. }
  366. bool VCustomPlot::getPercentY() const
  367. {
  368. return this->percentY;
  369. }
  370. QSize VCustomPlot::sizeHint() const
  371. {
  372. return QSize(500, 350);
  373. }
  374. QSize VCustomPlot::minimumSizeHint() const
  375. {
  376. return QSize(50, 35);
  377. }
  378. QCustomPlot* VCustomPlot::getPlot() const
  379. {
  380. return this->customPlot;
  381. }
  382. void VCustomPlot::setText(const QString & title)
  383. {
  384. this->m_Property.m_strText = title;
  385. customPlot->yAxis->setLabel(title);
  386. //重新刷新显示
  387. this->replot();
  388. }
  389. void VCustomPlot::setColorNames(const QString & colorNames)
  390. {
  391. //数量小于原来的则依次替换
  392. QStringList names = colorNames.split("|");
  393. QStringList names2 = this->colorNames.split("|");
  394. int count = names.count();
  395. if (count < colors.count()) {
  396. for (int i = 0; i < count; ++i) {
  397. names2[i] = names.at(i);
  398. }
  399. this->colorNames = names2.join("|");
  400. }
  401. else {
  402. this->colorNames = colorNames;
  403. }
  404. //重新替换颜色集合
  405. colors.clear();
  406. names = this->colorNames.split("|");
  407. foreach(QString name, names) {
  408. colors << name;
  409. }
  410. }
  411. void VCustomPlot::setBgColor(const QColor & bgColor)
  412. {
  413. if (this->m_Property.m_clrBg != bgColor)
  414. {
  415. this->m_Property.m_clrBg = bgColor;
  416. customPlot->setBackground(bgColor);
  417. customPlot->axisRect()->setBackground(bgColor);
  418. customPlot->replot();
  419. // 刷新
  420. this->replot();
  421. }
  422. }
  423. void VCustomPlot::setTextColor(const QColor & textColor)
  424. {
  425. if (this->m_Property.m_clrText != textColor)
  426. {
  427. this->m_Property.m_clrText = textColor;
  428. scatterStyle.setSize(pointSize);
  429. scatterStyle.setPen(QPen(textColor, 2));
  430. scatterStyle.setShape(QCPScatterStyle::ssCircle);
  431. customPlot->legend->setTextColor(textColor);
  432. customPlot->replot();
  433. }
  434. }
  435. void VCustomPlot::setGridWidth(int gridWidth)
  436. {
  437. if (this->gridWidth != gridWidth) {
  438. this->gridWidth = gridWidth;
  439. }
  440. }
  441. void VCustomPlot::setGridColor(const QColor & gridColor)
  442. {
  443. if (this->gridColor != gridColor) {
  444. this->gridColor = gridColor;
  445. QPen pen(gridColor, gridWidth);
  446. //第一坐标轴
  447. customPlot->xAxis->setLabelColor(gridColor);
  448. customPlot->yAxis->setLabelColor(gridColor);
  449. customPlot->xAxis->setTickLabelColor(gridColor);
  450. customPlot->yAxis->setTickLabelColor(gridColor);
  451. customPlot->xAxis->setBasePen(pen);
  452. customPlot->yAxis->setBasePen(pen);
  453. customPlot->xAxis->setTickPen(pen);
  454. customPlot->yAxis->setTickPen(pen);
  455. customPlot->xAxis->setSubTickPen(pen);
  456. customPlot->yAxis->setSubTickPen(pen);
  457. customPlot->xAxis->grid()->setPen(pen);
  458. customPlot->yAxis->grid()->setPen(pen);
  459. customPlot->xAxis->grid()->setZeroLinePen(pen);
  460. customPlot->yAxis->grid()->setZeroLinePen(pen);
  461. //第二坐标轴
  462. customPlot->xAxis2->setLabelColor(gridColor);
  463. customPlot->yAxis2->setLabelColor(gridColor);
  464. customPlot->xAxis2->setTickLabelColor(gridColor);
  465. customPlot->yAxis2->setTickLabelColor(gridColor);
  466. customPlot->xAxis2->setBasePen(pen);
  467. customPlot->yAxis2->setBasePen(pen);
  468. customPlot->xAxis2->setTickPen(pen);
  469. customPlot->yAxis2->setTickPen(pen);
  470. customPlot->xAxis2->setSubTickPen(pen);
  471. customPlot->yAxis2->setSubTickPen(pen);
  472. customPlot->xAxis2->grid()->setPen(pen);
  473. customPlot->yAxis2->grid()->setPen(pen);
  474. customPlot->xAxis2->grid()->setZeroLinePen(pen);
  475. customPlot->yAxis2->grid()->setZeroLinePen(pen);
  476. customPlot->replot();
  477. }
  478. }
  479. void VCustomPlot::setLineWidth(int lineWidth)
  480. {
  481. if (this->lineWidth != lineWidth && layerable != 0) {
  482. this->lineWidth = lineWidth;
  483. layerable->setLineWidth(lineWidth);
  484. }
  485. }
  486. void VCustomPlot::setLineColor(const QColor & lineColor)
  487. {
  488. if (this->lineColor != lineColor && layerable != 0) {
  489. this->lineColor = lineColor;
  490. layerable->setLineColor(lineColor);
  491. }
  492. }
  493. void VCustomPlot::setShowLineh(bool showLineh)
  494. {
  495. if (this->showLineh != showLineh && layerable != 0) {
  496. this->showLineh = showLineh;
  497. layerable->setShowLineh(showLineh);
  498. }
  499. }
  500. void VCustomPlot::setShowLinev(bool showLinev)
  501. {
  502. if (this->showLinev != showLinev && layerable != 0) {
  503. this->showLinev = showLinev;
  504. layerable->setShowLinev(showLinev);
  505. }
  506. }
  507. void VCustomPlot::setFontSize(int fontSize)
  508. {
  509. if (this->fontSize != fontSize) {
  510. this->fontSize = fontSize;
  511. QFont font;
  512. font.setPixelSize(fontSize);
  513. customPlot->legend->setFont(font);
  514. customPlot->xAxis->setLabelFont(font);
  515. customPlot->yAxis->setLabelFont(font);
  516. customPlot->xAxis->setTickLabelFont(font);
  517. customPlot->yAxis->setTickLabelFont(font);
  518. customPlot->xAxis2->setLabelFont(font);
  519. customPlot->yAxis2->setLabelFont(font);
  520. customPlot->xAxis2->setTickLabelFont(font);
  521. customPlot->yAxis2->setTickLabelFont(font);
  522. customPlot->replot();
  523. }
  524. }
  525. void VCustomPlot::setPointSize(int pointSize)
  526. {
  527. if (this->pointSize != pointSize)
  528. {
  529. this->pointSize = pointSize;
  530. customPlot->replot();
  531. }
  532. }
  533. void VCustomPlot::setOffsetX(double offsetX)
  534. {
  535. if (this->offsetX != offsetX)
  536. {
  537. this->offsetX = offsetX;
  538. }
  539. }
  540. void VCustomPlot::setOffsetY(double offsetY)
  541. {
  542. if (this->offsetY != offsetY)
  543. {
  544. this->offsetY = offsetY;
  545. }
  546. }
  547. void VCustomPlot::setToolTipPosition(int toolTipPosition)
  548. {
  549. if (this->toolTipPosition != toolTipPosition)
  550. {
  551. this->toolTipPosition = toolTipPosition;
  552. customPlot->replot();
  553. }
  554. }
  555. void VCustomPlot::setInteractions(int interactions)
  556. {
  557. if (this->interactions != interactions)
  558. {
  559. this->interactions = interactions;
  560. if (interactions == 0)
  561. {// 所有对象都可选
  562. customPlot->setInteractions(QCP::iSelectOther);
  563. }
  564. else if (interactions == 1)
  565. {
  566. customPlot->setInteractions(QCP::iRangeDrag);
  567. }
  568. else if (interactions == 2)
  569. {
  570. customPlot->setInteractions(QCP::iRangeZoom);
  571. }
  572. else if (interactions == 3)
  573. {
  574. customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
  575. }
  576. else
  577. {
  578. customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iMultiSelect | QCP::iSelectPlottables | QCP::iSelectAxes);
  579. }
  580. }
  581. m_Property.m_nInteractions = interactions;
  582. // qDebug() << interactions;
  583. }
  584. void VCustomPlot::setDrawingMode(int nMode)
  585. {
  586. switch (nMode)
  587. {
  588. case 0:
  589. {
  590. for (int i = 0; i < customPlot->graphCount(); i++)
  591. {
  592. customPlot->graph(i)->setLineStyle(QCPGraph::LineStyle::lsLine);
  593. }
  594. }
  595. break;
  596. case 1:
  597. {
  598. for (int i = 0; i < customPlot->graphCount(); i++)
  599. {
  600. customPlot->graph(i)->setLineStyle(QCPGraph::LineStyle::lsStepLeft);
  601. }
  602. }
  603. break;
  604. case 2:
  605. {
  606. for (int i = 0; i < customPlot->graphCount(); i++)
  607. {
  608. customPlot->graph(i)->setLineStyle(QCPGraph::LineStyle::lsStepRight);
  609. }
  610. }
  611. break;
  612. case 3:
  613. {
  614. for (int i = 0; i < customPlot->graphCount(); i++)
  615. {
  616. customPlot->graph(i)->setLineStyle(QCPGraph::LineStyle::lsImpulse);
  617. }
  618. }
  619. break;
  620. case 4:
  621. {
  622. for (int i = 0; i < customPlot->graphCount(); i++)
  623. {
  624. customPlot->graph(i)->setLineStyle(QCPGraph::LineStyle::lsStepCenter);
  625. }
  626. }
  627. break;
  628. default:
  629. {
  630. for (int i = 0; i < customPlot->graphCount(); i++)
  631. {
  632. customPlot->graph(i)->setLineStyle(QCPGraph::LineStyle::lsLine);
  633. }
  634. }
  635. break;
  636. }
  637. m_Property.m_nDrawingMode = nMode;
  638. // // 刷新
  639. // this->replot();
  640. }
  641. void VCustomPlot::setTickCountX(int nTickCount)
  642. {
  643. m_Property.m_nTickCountX = nTickCount;
  644. customPlot->xAxis->ticker()->setTickCount(nTickCount);
  645. // 刷新
  646. this->replot();
  647. }
  648. void VCustomPlot::setTickCountY(int nTickCount)
  649. {
  650. m_Property.m_nTickCountY = nTickCount;
  651. customPlot->yAxis->ticker()->setTickCount(nTickCount);
  652. // 刷新
  653. this->replot();
  654. }
  655. void VCustomPlot::setRangeX(QSize RangeX)
  656. {
  657. m_Property.m_RangeX = RangeX;
  658. customPlot->xAxis->setRange(RangeX.width(), RangeX.height());
  659. // 刷新
  660. this->replot();
  661. }
  662. void VCustomPlot::setRangeY(QSize RangeY)
  663. {
  664. m_Property.m_RangeY = RangeY;
  665. customPlot->yAxis->setRange(RangeY.width(), RangeY.height());
  666. // 刷新
  667. this->replot();
  668. }
  669. void VCustomPlot::setPadding(int padding)
  670. {
  671. if (this->padding != padding) {
  672. this->padding = padding;
  673. customPlot->xAxis->setPadding(padding);
  674. customPlot->yAxis->setPadding(padding);
  675. customPlot->xAxis2->setPadding(padding);
  676. customPlot->yAxis2->setPadding(padding);
  677. customPlot->xAxis2->setOffset(25);
  678. }
  679. }
  680. void VCustomPlot::setAxis2Visible(bool axis2Visible)
  681. {
  682. if (this->axis2Visible != axis2Visible) {
  683. this->axis2Visible = axis2Visible;
  684. //设置第二坐标轴是否显示,可以形成闭合的盒子
  685. customPlot->xAxis2->setVisible(axis2Visible);
  686. customPlot->yAxis2->setVisible(axis2Visible);
  687. customPlot->xAxis2->setTicks(false);
  688. customPlot->yAxis2->setTicks(false);
  689. customPlot->xAxis2->setTickLabels(false);
  690. customPlot->yAxis2->setTickLabels(false);
  691. }
  692. }
  693. void VCustomPlot::setPercentY(bool percentY)
  694. {
  695. if (this->percentY != percentY) {
  696. this->percentY = percentY;
  697. }
  698. }
  699. void VCustomPlot::setLegend(bool visible, int position, int column)
  700. {
  701. customPlot->legend->setVisible(visible);
  702. #ifndef qcustomplot_v1
  703. customPlot->legend->setWrap(column);
  704. //设置摆放位置顺序列优先,这样的话就是一行,默认是一列垂直分布
  705. customPlot->legend->setFillOrder(QCPLayoutGrid::FillOrder::foColumnsFirst);
  706. #endif
  707. QCPLayoutInset* layout = customPlot->axisRect()->insetLayout();
  708. if (position == 0) {
  709. layout->setInsetAlignment(0, Qt::AlignTop | Qt::AlignHCenter);
  710. }
  711. else if (position == 1) {
  712. layout->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
  713. }
  714. else if (position == 2) {
  715. layout->setInsetAlignment(0, Qt::AlignVCenter | Qt::AlignRight);
  716. }
  717. else if (position == 3) {
  718. layout->setInsetAlignment(0, Qt::AlignBottom | Qt::AlignRight);
  719. }
  720. else if (position == 4) {
  721. layout->setInsetAlignment(0, Qt::AlignBottom | Qt::AlignHCenter);
  722. }
  723. else if (position == 5) {
  724. layout->setInsetAlignment(0, Qt::AlignBottom | Qt::AlignLeft);
  725. }
  726. else if (position == 6) {
  727. layout->setInsetAlignment(0, Qt::AlignVCenter | Qt::AlignLeft);
  728. }
  729. else if (position == 7) {
  730. layout->setInsetAlignment(0, Qt::AlignTop | Qt::AlignLeft);
  731. }
  732. }
  733. void VCustomPlot::setVisible(int index, bool visible)
  734. {
  735. //画布的数量一定小于等于画布索引
  736. if (customPlot->graphCount() <= index) {
  737. return;
  738. }
  739. customPlot->graph(index)->setVisible(visible);
  740. customPlot->legend->elementAt(index)->setVisible(visible);
  741. }
  742. void VCustomPlot::init()
  743. {
  744. #ifdef qcustomplot_v1
  745. #if 0
  746. customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
  747. customPlot->xAxis->setDateTimeFormat("mm:ss");
  748. customPlot->xAxis->setAutoTicks(false);
  749. customPlot->xAxis->setAutoTickLabels(false);
  750. customPlot->xAxis->setAutoTickStep(false);
  751. customPlot->xAxis->setTickStep(1);
  752. customPlot->yAxis->setAutoTicks(false);
  753. customPlot->yAxis->setAutoTickLabels(false);
  754. customPlot->yAxis->setAutoTickStep(false);
  755. customPlot->yAxis->setTickStep(1);
  756. #endif
  757. #endif
  758. //设置图例
  759. customPlot->legend->setVisible(false);
  760. customPlot->legend->setBrush(QColor(255, 255, 255, 10));
  761. customPlot->legend->setBorderPen(Qt::NoPen);
  762. //设置网格线可见
  763. customPlot->xAxis->grid()->setVisible(true);
  764. customPlot->yAxis->grid()->setVisible(true);
  765. //设置子网格线可见
  766. //customPlot->xAxis->grid()->setSubGridVisible(true);
  767. //customPlot->yAxis->grid()->setSubGridVisible(true);
  768. //设置网格线伸出部分
  769. customPlot->xAxis->setTickLength(0, 0);
  770. //customPlot->yAxis->setTickLength(0, 0);
  771. //设置子网格伸出部分
  772. customPlot->xAxis->setSubTickLength(0, 0);
  773. //customPlot->yAxis->setSubTickLength(0, 0);
  774. customPlot->xAxis->setVisible(true);
  775. customPlot->setInteractions(QCP::iSelectOther);
  776. customPlot->replot();
  777. }
  778. void VCustomPlot::initTracer()
  779. {
  780. if (layerable != 0) {
  781. return;
  782. }
  783. //增加一个层用于绘制游标
  784. layerable = new CustomPlotTracer(customPlot);
  785. layerable->setVisible(true);
  786. }
  787. void VCustomPlot::initItem()
  788. {
  789. if (tracer != 0 || textTip != 0) {
  790. return;
  791. }
  792. //增加数据点指示器高亮显示数据点
  793. tracer = new QCPItemTracer(customPlot);
  794. tracer->setParent(customPlot);
  795. tracer->setStyle(QCPItemTracer::tsCircle);
  796. tracer->setVisible(false);
  797. //增加数据点提示信息
  798. textTip = new QCPItemText(customPlot);
  799. textTip->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  800. textTip->setPositionAlignment(Qt::AlignLeft);
  801. textTip->position->setType(QCPItemPosition::PositionType::ptAbsolute);
  802. textTip->setPadding(QMargins(6, 6, 6, 6));
  803. textTip->setVisible(false);
  804. }
  805. void VCustomPlot::clearData(int index)
  806. {
  807. //画布的数量一定小于等于画布索引
  808. if (customPlot->graphCount() <= index)
  809. {
  810. return;
  811. }
  812. #ifdef qcustomplot_v1
  813. customPlot->graph(index)->clearData();
  814. #else
  815. customPlot->graph(index)->data().clear();
  816. #endif
  817. }
  818. void VCustomPlot::clear(bool clearGraphs, bool clearItems, bool clearPlottables)
  819. {
  820. if (clearGraphs) {
  821. customPlot->clearGraphs();
  822. }
  823. if (clearItems) {
  824. customPlot->clearItems();
  825. tracer = 0;
  826. textTip = 0;
  827. }
  828. if (clearPlottables) {
  829. customPlot->clearPlottables();
  830. }
  831. }
  832. void VCustomPlot::replot(QCustomPlot::RefreshPriority refreshPriority)
  833. {
  834. customPlot->replot(refreshPriority);
  835. }
  836. void VCustomPlot::drawFlowLine()
  837. {
  838. if (layerable != 0) {
  839. layerable->drawLine();
  840. customPlot->replot();
  841. }
  842. }
  843. void VCustomPlot::drawStaticLinev(double x, double y, const QColor & color, int width)
  844. {
  845. if (staticBarv == 0)
  846. {
  847. staticBarv = new QCPBars(customPlot->xAxis, customPlot->yAxis);
  848. }
  849. //用柱状图形式绘制线条
  850. staticBarv->setWidth(width);
  851. staticBarv->setWidthType(QCPBars::wtAbsolute);
  852. staticBarv->setPen(color);
  853. staticBarv->setBrush(color);
  854. staticBarv->setData(vdouble() << x, vdouble() << y);
  855. }
  856. void VCustomPlot::drawStaticLineh(double x, double y, const QColor & color, int width)
  857. {
  858. if (staticBarv == 0)
  859. {
  860. staticBarv = new QCPBars(customPlot->xAxis, customPlot->yAxis);
  861. }
  862. //用柱状图形式绘制线条
  863. staticBarv->setWidth(width);
  864. staticBarv->setWidthType(QCPBars::wtAbsolute);
  865. staticBarv->setPen(color);
  866. staticBarv->setBrush(color);
  867. staticBarv->setData(vdouble() << x, vdouble() << y);
  868. }
  869. QColor VCustomPlot::getRandColor()
  870. {
  871. return CustomPlotHelper::getRandColor();
  872. }
  873. void VCustomPlot::addGraph(int count)
  874. {
  875. for (int i = 0; i < count; i++)
  876. {
  877. customPlot->addGraph();
  878. }
  879. }
  880. void VCustomPlot::initUI(QString strXName, QString strYName)
  881. {
  882. //设置显示横线+竖线
  883. this->setShowLineh(true);
  884. this->setShowLinev(true);
  885. this->setLineWidth(1);
  886. this->setLineColor(qRgb(50, 50, 50));
  887. //设置边距
  888. this->setPadding(5);
  889. //设置数据点的大小
  890. this->setPointSize(0);
  891. //设置文本颜色
  892. this->setTextColor(Qt::white);
  893. //设置XY轴范围值
  894. this->setRangeX(m_Property.m_RangeX.width(), m_Property.m_RangeX.height(), m_Property.m_nTickCountX);
  895. this->setRangeY(m_Property.m_RangeY.width(), m_Property.m_RangeY.height(), m_Property.m_nTickCountY);
  896. customPlot->xAxis->setLabel(strXName);
  897. customPlot->yAxis->setLabel(strYName);
  898. customPlot->legend->setVisible(true);
  899. customPlot->axisRect()->setBackground(QBrush(m_Property.m_clrBg));//背景黑色
  900. customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 1, Qt::PenStyle::DashLine));//网格白色虚线
  901. customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 1, Qt::PenStyle::DashLine));//网格白色虚线
  902. customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(90, 90, 90), 1, Qt::DotLine));//网格浅色点线
  903. customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(90, 90, 90), 1, Qt::DotLine));//网格浅色点线
  904. customPlot->xAxis->grid()->setSubGridVisible(true);//显示x轴子网格线
  905. customPlot->yAxis->grid()->setSubGridVisible(true);//显示要轴子网格线
  906. customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::white));//x轴0线颜色白色
  907. customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::white));//y轴0线颜色白色
  908. customPlot->xAxis->setTicks(false);//x轴不显示文本
  909. //重新刷新显示
  910. this->replot();
  911. }
  912. void VCustomPlot::addGraph(QString strName, QColor color, int nLinsWidth, bool bFull)
  913. {
  914. QCPGraph* graph = customPlot->addGraph();
  915. graph->setName(strName);
  916. if (bFull)
  917. {
  918. CustomPlotHelper::setGraphBrush(graph, color, 2);
  919. }
  920. QPen pen;
  921. pen.setColor(color);
  922. pen.setWidth(nLinsWidth);
  923. graph->setPen(pen);
  924. customPlot->replot();
  925. }
  926. void VCustomPlot::delGraph(QString strName)
  927. {
  928. int nPlot = -1;
  929. for (int i = 0; i < customPlot->graphCount(); i++)
  930. {
  931. if (customPlot->graph(i)->name() == strName)
  932. {
  933. nPlot = i;
  934. }
  935. }
  936. if (customPlot->graphCount() <= nPlot)
  937. {
  938. return;
  939. }
  940. // 删除曲线
  941. customPlot->removeGraph(nPlot);
  942. }
  943. void VCustomPlot::addValve(QString strName, double fKey, double fValue)
  944. {
  945. int nPlot = -1;
  946. for (int i = 0; i < customPlot->graphCount(); i++)
  947. {
  948. if (customPlot->graph(i)->name() == strName)
  949. {
  950. nPlot = i;
  951. }
  952. }
  953. if ((nPlot == -1) || (customPlot->graphCount() <= nPlot))
  954. {
  955. return;
  956. }
  957. customPlot->graph(nPlot)->addData(fKey, fValue);
  958. int size = customPlot->xAxis->ticker()->tickCount();
  959. customPlot->xAxis->setRange(fKey, size, Qt::AlignRight);
  960. }
  961. void VCustomPlot::addValve(int nPlot, double fValue)
  962. {
  963. if ((customPlot->graphCount() <= nPlot))
  964. {
  965. return;
  966. }
  967. int nKey = customPlot->graph(nPlot)->dataCount();
  968. double fKey = (double)nKey;
  969. customPlot->graph(nPlot)->addData(fKey, fValue);
  970. int size = customPlot->yAxis->ticker()->tickCount();
  971. customPlot->xAxis->setRange(fKey, size, Qt::AlignRight);
  972. //customPlot->graph(nPlot)->rescaleValueAxis();
  973. }
  974. void VCustomPlot::setRangeX(double min, double max, int tickCount)
  975. {
  976. customPlot->xAxis->setRange(min, max);
  977. customPlot->xAxis->ticker()->setTickCount(tickCount);
  978. }
  979. void VCustomPlot::setRangeY(double min, double max, int tickCount, bool percentY)
  980. {
  981. customPlot->yAxis->setRange(min, max);
  982. customPlot->yAxis->ticker()->setTickCount(tickCount);
  983. //如果开启了百分比格式,则先自动计算总共显示多少个数字
  984. this->percentY = percentY;
  985. if (percentY) {
  986. vstring lab;
  987. vdouble key;
  988. int step = (max - min) / tickCount;
  989. for (int i = min; i <= max; i = i + step) {
  990. key << i;
  991. lab << QString("%1%").arg(i);
  992. }
  993. setLabY(key, lab);
  994. }
  995. }
  996. void VCustomPlot::setLabX(const vdouble & key, const vstring & lab)
  997. {
  998. //将对应的key用文本替代表示
  999. #ifdef qcustomplot_v1
  1000. customPlot->xAxis->setAutoTicks(false);
  1001. customPlot->xAxis->setAutoTickLabels(false);
  1002. customPlot->xAxis->setTickVector(key);
  1003. customPlot->xAxis->setTickVectorLabels(lab);
  1004. #else
  1005. //设置标尺为文本标尺
  1006. QSharedPointer<QCPAxisTickerText> ticker(new QCPAxisTickerText);
  1007. ticker->addTicks(key, lab);
  1008. customPlot->xAxis->setTicker(ticker);
  1009. #endif
  1010. }
  1011. void VCustomPlot::setLabY(const vdouble & key, const vstring & lab)
  1012. {
  1013. //将对应的key用文本替代表示
  1014. #ifdef qcustomplot_v1
  1015. customPlot->yAxis->setAutoTicks(false);
  1016. customPlot->yAxis->setAutoTickLabels(false);
  1017. customPlot->yAxis->setTickVector(key);
  1018. customPlot->yAxis->setTickVectorLabels(lab);
  1019. #else
  1020. //设置标尺为文本标尺
  1021. QSharedPointer<QCPAxisTickerText> ticker(new QCPAxisTickerText);
  1022. ticker->addTicks(key, lab);
  1023. customPlot->yAxis->setTicker(ticker);
  1024. #endif
  1025. }
  1026. void VCustomPlot::addDataLine(const LineData & data)
  1027. {
  1028. QCPGraph* graph = customPlot->addGraph();
  1029. initGraph(graph, data);
  1030. }
  1031. void VCustomPlot::setDataLine(const LineData & data)
  1032. {
  1033. int index = data.index;
  1034. if (customPlot->graphCount() > index) {
  1035. QCPGraph* graph = customPlot->graph(index);
  1036. initGraph(graph, data);
  1037. //是否自适应坐标轴
  1038. if (data.autoScale) {
  1039. graph->rescaleAxes();
  1040. }
  1041. else {
  1042. customPlot->xAxis->setRange(-offsetX, data.key.count() + offsetX, Qt::AlignLeft);
  1043. }
  1044. }
  1045. }
  1046. void VCustomPlot::initGraph(QCPGraph * graph, const LineData & data)
  1047. {
  1048. //设置画布名称
  1049. graph->setName(data.name);
  1050. //设置画布数据
  1051. graph->setData(data.key, data.value);
  1052. //设置线条画笔
  1053. graph->setPen(QPen(data.lineColor, data.lineWidth));
  1054. //设置连接风格
  1055. graph->setLineStyle((QCPGraph::LineStyle)data.lineStyle);
  1056. //设置数据风格
  1057. QCPScatterStyle scatterStyle;
  1058. scatterStyle.setPen(QPen(data.dataColor, data.dataWidth));
  1059. scatterStyle.setBrush(data.lineColor);
  1060. scatterStyle.setSize(data.lineWidth);
  1061. scatterStyle.setShape((QCPScatterStyle::ScatterShape)data.scatterShape);
  1062. graph->setScatterStyle(scatterStyle);
  1063. //设置填充画布
  1064. CustomPlotHelper::setGraphBrush(graph, data.lineColor, data.fillColor);
  1065. }
  1066. void VCustomPlot::initPropertyEx(CONTROL_PROPERTY_EX * pPropertyEx)
  1067. {
  1068. this->setBgColor(m_Property.m_clrBg);
  1069. //隐藏表格线
  1070. this->getPlot()->xAxis->grid()->setVisible(false);
  1071. //this->getPlot()->yAxis->grid()->setVisible(false);
  1072. //初始化跟随鼠标游标
  1073. this->initTracer();
  1074. //初始化指示器数据高亮及悬停提示等
  1075. this->initItem();
  1076. QString strYName = m_Property.m_strText;
  1077. if (m_Property.m_strText.isEmpty())
  1078. {
  1079. strYName = "曲线图表";
  1080. }
  1081. this->initUI("", strYName);
  1082. // 备用颜色集合(按分块顺序赋值)
  1083. m_defaultColors << QColor(0, 176, 180)
  1084. << QColor(255, 192, 0)
  1085. << QColor(0, 113, 193)
  1086. << QColor(72, 103, 149)
  1087. << QColor(185, 87, 86)
  1088. << QColor(0, 177, 125)
  1089. << QColor(214, 77, 84)
  1090. << QColor(71, 164, 233)
  1091. << QColor(34, 163, 169)
  1092. << QColor(40, 45, 48)
  1093. << QColor(162, 121, 197)
  1094. << QColor(72, 202, 245)
  1095. << QColor(0, 150, 121)
  1096. << QColor(111, 9, 176)
  1097. << QColor(250, 170, 20);
  1098. // 初始化扩展属性
  1099. if (pPropertyEx == nullptr)
  1100. {
  1101. // 初始化曲线的条目数
  1102. this->m_Property.m_nSliceCount = DEFAULT_CUSTOMPLOT_COUNT;
  1103. // 初始化扩展属性
  1104. this->initPropertyEx();
  1105. }
  1106. else
  1107. {
  1108. this->m_PropertyEx = *pPropertyEx;
  1109. // 根据分块数量先设置一个默认显示的Value
  1110. for (int i = 0; i < m_Property.m_nSliceCount; i++)
  1111. {
  1112. QString strValue = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[i].strValue;
  1113. QColor color = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[i].color;
  1114. int nLineWidth = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[i].nLineWidth;
  1115. addGraph(strValue, color, nLineWidth);
  1116. }
  1117. }
  1118. //// 2022-9-11 为刷新链接绑定默认值(系统的执行次数)
  1119. //this->bindDefaultRefreshDataLink();
  1120. }
  1121. /// <summary>
  1122. /// 初始化扩展属性
  1123. /// </summary>
  1124. void VCustomPlot::initPropertyEx()
  1125. {
  1126. // 扩展属性的总名称
  1127. m_PropertyEx.m_strTitle = CUSTOM_PLOT_PROPERTY_EX_NAME;
  1128. // 用于触发刷新的数据链接
  1129. m_PropertyEx.m_refreshLink.title = CUSTOM_PLOT_PROPERTY_EX_REFRESHLINK_NAME;
  1130. // 强制设置
  1131. m_PropertyEx.m_refreshLink.bForce = true;
  1132. // 初始化每一个扩展属性组(Wave控件只有一个属性组)
  1133. PROPERTY_EX_GROUP group;
  1134. // 列信息属性
  1135. group.strTitle = CUSTOM_PLOT_PROPERTY_EX_GROUP_NAME;
  1136. group.strRelationKey = CUSTOM_PLOT_COUNT_NAME;
  1137. //初始化列信息每一个子分组
  1138. for (int i = 0; i < m_Property.m_nSliceCount; i++)
  1139. {
  1140. PROPERTY_EX_SUBGROUP subGroup;
  1141. this->initSubGroup(subGroup, i, GROUP_INDEX_CUSTOM_PLOT);
  1142. // 保存子分组
  1143. group.subGroups.push_back(subGroup);
  1144. // 创建曲线
  1145. addGraph(subGroup.strValue, m_defaultColors[i]);
  1146. }
  1147. // 保存本子分组定义信息
  1148. m_PropertyEx.m_groups.push_back(group);
  1149. }
  1150. /// <summary>
  1151. /// 初始化一个扩展属性组
  1152. /// </summary>
  1153. /// <param name="subItem"></param>
  1154. void VCustomPlot::initSubGroup(PROPERTY_EX_SUBGROUP & subGroup, int nIndex, int nSubGroupID)
  1155. {
  1156. if (nSubGroupID == GROUP_INDEX_CUSTOM_PLOT)
  1157. {
  1158. QString idx = QString::number(nIndex + 1);
  1159. // 子分组名称
  1160. subGroup.strTitle = CUSTOM_PLOT_PROPERTY_EX_SUBGROUP_NAME + idx;
  1161. // 列名称 + 数值
  1162. subGroup.strValueName = CUSTOM_PLOT_PROPERTY_EX_VALUE_NAME + idx;
  1163. subGroup.strValue = CUSTOM_PLOT_PROPERTY_EX_VALUE_NAME + idx;
  1164. subGroup.strLineWidthName = CUSTOM_PLOT_PROPERTY_EX_LINEWIDTH_NAME + idx;
  1165. subGroup.nLineWidth = CUSTOM_PLOT_DEFAULT_LINEWIDTH;
  1166. // 分块颜色名称 + 数值
  1167. subGroup.strColorName = CUSTOM_PLOT_PROPERTY_EX_COLOR_NAME + idx;
  1168. // 按顺序给一个默认值(可能会越界)
  1169. if (nIndex < m_defaultColors.size())
  1170. {
  1171. subGroup.color = m_defaultColors.at(nIndex);
  1172. }
  1173. else
  1174. {
  1175. subGroup.color = m_defaultColors.at(nIndex - m_defaultColors.size());
  1176. }
  1177. // 数据链接名称
  1178. DataLink dataLink;
  1179. dataLink.title = CUSTOM_PLOT_PROPERTY_EX_LINK_NAME + idx;
  1180. dataLink.defaultValue = g_pGvlManager->getDefaultValueByName(DEFAULT_VALUE_NULLSTRING);
  1181. subGroup.dataLinks.push_back(dataLink);
  1182. }
  1183. }
  1184. /// <summary>
  1185. /// 设置块数
  1186. /// </summary>
  1187. /// <param name="count"></param>
  1188. void VCustomPlot::setCustomPlotCount(const int& count)
  1189. {
  1190. if (this->m_Property.m_nSliceCount != count)
  1191. {
  1192. this->m_Property.m_nSliceCount = count;
  1193. this->update();
  1194. }
  1195. }
  1196. /// <summary>
  1197. /// 获取当前块数
  1198. /// </summary>
  1199. int VCustomPlot::getCustomPlotCount()
  1200. {
  1201. return this->m_Property.m_nSliceCount;
  1202. }
  1203. /// <summary>
  1204. /// 设置分块文字
  1205. /// </summary>
  1206. /// <param name="newValue"></param>
  1207. /// <param name="nIndex"></param>
  1208. void VCustomPlot::updateCustomPlotTitle(const QString & newValue, const int nIndex)
  1209. {
  1210. // 更新分块文字
  1211. QString& strValue = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].strValue;
  1212. if (strValue != newValue)
  1213. {
  1214. strValue = newValue;
  1215. if (customPlot->graphCount() >= nIndex)
  1216. {
  1217. customPlot->graph(nIndex)->setName(strValue);
  1218. this->replot();
  1219. }
  1220. }
  1221. qDebug() << "VCustomPlot::updateColTitle - update ["
  1222. << m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].strValueName << "] to [" << newValue << "]";
  1223. }
  1224. /// <summary>
  1225. /// 设置分块颜色
  1226. /// </summary>
  1227. /// <param name="newValue"></param>
  1228. /// <param name="nIndex"></param>
  1229. void VCustomPlot::updateCustomPlotColor(QColor newValue, const int nIndex)
  1230. {
  1231. QColor& color = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].color;
  1232. int& nLinsWidth = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].nLineWidth;
  1233. if (color != newValue)
  1234. {
  1235. color = newValue;
  1236. if (customPlot->graphCount() >= nIndex)
  1237. {
  1238. QPen pen;
  1239. pen.setColor(color);
  1240. pen.setWidth(nLinsWidth);
  1241. customPlot->graph(nIndex)->setPen(pen);
  1242. this->replot();
  1243. }
  1244. }
  1245. qDebug() << "VCustomPlot::updateCustomPlotColor - update ["
  1246. << m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].strColorName
  1247. << "] to [" << newValue << "]";
  1248. }
  1249. /// <summary>
  1250. /// 更新图层线宽
  1251. /// </summary>
  1252. /// <param name="lineWidth"></param>
  1253. /// <param name="index"></param>
  1254. void VCustomPlot::updateLineWidth(const int lineWidth, const int nIndex)
  1255. {
  1256. QColor& color = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].color;
  1257. int& nLinsWidth = m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].nLineWidth;
  1258. if (nLinsWidth != lineWidth)
  1259. {
  1260. nLinsWidth = lineWidth;
  1261. // TODO:刷新预览
  1262. if (customPlot->graphCount() >= nIndex)
  1263. {
  1264. QPen pen;
  1265. pen.setColor(color);
  1266. pen.setWidth(nLinsWidth);
  1267. customPlot->graph(nIndex)->setPen(pen);
  1268. this->replot();
  1269. }
  1270. }
  1271. qDebug() << "VCustomPlot::updateLineWidth - update ["
  1272. << m_PropertyEx.m_groups[GROUP_INDEX_CUSTOM_PLOT].subGroups[nIndex].strLineWidthName
  1273. << "] to [" << lineWidth << "]";
  1274. }
  1275. /// <summary>
  1276. /// 设置分块数值
  1277. /// </summary>
  1278. /// <param name="newValue"></param>
  1279. /// <param name="nIndex"></param>
  1280. void VCustomPlot::updateCustomPlotValue(const int& newValue, const int nIndex)
  1281. {
  1282. addValve(nIndex, newValue);
  1283. qDebug() << "VCustomPlot::updateCustomPlotValue - update m_Values["
  1284. << nIndex << "] to [" << newValue << "]";
  1285. if (!this->isHidden())
  1286. {
  1287. this->replot(QCustomPlot::rpQueuedReplot);
  1288. }
  1289. }
  1290. /// <summary>
  1291. /// 调整块属性数量(虚函数)
  1292. /// </summary>
  1293. /// <param name="fixCount"></param>
  1294. void VCustomPlot::updateExPropertyCount(const int fixCount, const QString & strPropName)
  1295. {
  1296. // 获取属性的当前数量
  1297. int curCount = this->getExPropertyCountByName(strPropName);
  1298. // 子分组编号
  1299. int curGroup = this->getGroupIDByName(strPropName);
  1300. // 检查有效性
  1301. if (curGroup < 0)
  1302. {
  1303. qDebug() << "[Error] VCustomPlot::updateExPropertyCount - nGroupID in invalid: " << curGroup;
  1304. return;
  1305. }
  1306. // 如果需要增加
  1307. if (fixCount > 0)
  1308. {
  1309. int nEnd = curCount + fixCount;
  1310. for (int i = curCount; i < nEnd; i++)
  1311. {
  1312. PROPERTY_EX_SUBGROUP subItem;
  1313. this->initSubGroup(subItem, i, curGroup);
  1314. m_PropertyEx.m_groups[curGroup].subGroups.push_back(subItem);
  1315. addGraph(subItem.strValue, m_defaultColors[i]);
  1316. }
  1317. }
  1318. // 如果需要减少
  1319. else if (fixCount < 0)
  1320. {
  1321. for (int i = 0; i < qAbs(fixCount); i++)
  1322. {
  1323. // 去掉对应的数据结构信息
  1324. m_PropertyEx.m_groups[curGroup].subGroups.pop_back();
  1325. // 同时去掉预览数值
  1326. customPlot->removeGraph(customPlot->graphCount() - 1);
  1327. // 刷新
  1328. this->replot();
  1329. }
  1330. }
  1331. }
  1332. /// <summary>
  1333. /// 修改扩展属性(虚函数)
  1334. /// </summary>
  1335. /// <param name="strValueTile"></param>
  1336. /// <param name="newValue"></param>
  1337. void VCustomPlot::changeExProperties(QString strValueTitle, const QVariant & newValue)
  1338. {
  1339. // 如果改变了分块标题
  1340. if (strValueTitle.contains(CUSTOM_PLOT_PROPERTY_EX_VALUE_NAME))
  1341. {
  1342. // 获取修改属性的索引
  1343. int nIndex = strValueTitle.remove(CUSTOM_PLOT_PROPERTY_EX_VALUE_NAME).toInt() - 1;
  1344. // 如果索引值有效,则对应更新
  1345. if (nIndex >= 0)
  1346. {
  1347. // 修改对应Pie的数据结构内容
  1348. this->updateCustomPlotTitle(newValue.toString(), nIndex);
  1349. }
  1350. }
  1351. // 如果改变了分块颜色
  1352. else if (strValueTitle.contains(CUSTOM_PLOT_PROPERTY_EX_COLOR_NAME))
  1353. {
  1354. // 获取修改属性的索引
  1355. int nIndex = strValueTitle.remove(CUSTOM_PLOT_PROPERTY_EX_COLOR_NAME).toInt() - 1;
  1356. // 如果索引值有效,则对应更新
  1357. if (nIndex >= 0)
  1358. {
  1359. // 修改对应Pie的数据结构内容
  1360. this->updateCustomPlotColor(newValue.value<QColor>(), nIndex);
  1361. }
  1362. }
  1363. // 如果改变了线宽
  1364. else if (strValueTitle.contains(CUSTOM_PLOT_PROPERTY_EX_LINEWIDTH_NAME))
  1365. {
  1366. // 获取修改属性的索引
  1367. int nIndex = strValueTitle.remove(CUSTOM_PLOT_PROPERTY_EX_LINEWIDTH_NAME).toInt() - 1;
  1368. // 如果索引值有效,则对应更新
  1369. if (nIndex >= 0)
  1370. {
  1371. // 刷新线宽
  1372. this->updateLineWidth(newValue.toInt(), nIndex);
  1373. }
  1374. }
  1375. // 如果改变了数据链接(此处应该不需要手工更新)
  1376. else if (strValueTitle.contains(CUSTOM_PLOT_PROPERTY_EX_REFRESHLINK_NAME)
  1377. || strValueTitle.contains(CUSTOM_PLOT_PROPERTY_EX_LINK_NAME)
  1378. )
  1379. {
  1380. }
  1381. // Error:不应该执行到这里
  1382. else
  1383. {
  1384. qWarning() << "[Error]: VCustomPlot::changeExProperties - invalid strValueTitle: " << strValueTitle;
  1385. }
  1386. }