Exemple #1
0
void DataExplorer::on_dispUnit_currentIndexChanged(int)
{
	QVector<double> ticks;
	ui->selectMetric->clearPlottables();
	QBrush boxBrush(QColor(60, 60, 255, 100),Qt::Dense6Pattern);

	QVector<QString> labels;
	int i = -1;
	for (DataSeries* p : exp->data) {
		if (p == nullptr) continue;
		if (p->descriptor->unit() != ui->dispUnit->currentText()) continue;
		++i;
		StatisticalSet v = exp->aggregate(p->descriptor->uid(),exp);
		QCPStatisticalBox* b = new QCPStatisticalBox(ui->selectMetric->xAxis,ui->selectMetric->yAxis);
		b->setBrush(boxBrush);
		b->setProperty("UID",p->descriptor->uid());
		b->setData(i,v.min(),v.quantile(0.25),v.median(),v.quantile(0.75),v.max());
		ui->selectMetric->addPlottable(b);
		ticks.append(i);
		labels.append(p->descriptor->name());
	}
	ui->selectMetric->xAxis->setSubTickCount(0);
	ui->selectMetric->xAxis->setTickLength(0, 4);
	ui->selectMetric->xAxis->setTickLabelRotation(40);
	ui->selectMetric->xAxis->setAutoTicks(false);
	ui->selectMetric->xAxis->setAutoTickLabels(false);
	ui->selectMetric->xAxis->setTickVector(ticks);
	ui->selectMetric->xAxis->setTickVectorLabels(labels);
	ui->selectMetric->setInteractions(QCP::iMultiSelect | QCP::iSelectPlottables);

	ui->selectMetric->rescaleAxes();
	ui->selectMetric->xAxis->scaleRange(1.1, ui->selectMetric->xAxis->range().center());
	connect(ui->selectMetric,SIGNAL(selectionChangedByUser()), this, SLOT(updateEnvironment()));
	if (ui->axisFromZero->isChecked())
		ui->selectMetric->yAxis->setRangeLower(0);
	ui->selectMetric->replot();
}
void CMidSubDrawHelper::drawHistoryBarData(CHistoryDataManager* pHistoryDataManager, QCustomPlot* pCustomPlot, QCPAxisRect* pRect)
{
	QBrush redBrush(QColor(255, 0, 0, 255));//red
	QPen redPen(QColor(255, 0, 0, 255));//red
	QBrush greenBrush(QColor(0, 255, 0, 255));//green
	QPen greenPen(QColor(0, 255, 0, 255));//green

	QBrush* pBoxBrushRef = NULL;
	QPen* pBoxPenRef = NULL;
	QCPStatisticalBox *pStatisticalBoxRef = NULL;

	double fMinimum = 0;
	double fMaximum = 0;
	double fLowerQuartile = 0;
	double fUpperQuartile = 0;

	QMap<unsigned int, Bar>::iterator iterMap;
	int nIndex = 0;
	unsigned int nTimeFrom = 0;
	unsigned int nTimeTo = 0;
	double nLeftAxisRangeMin = 0;
	double nLeftAxisRangeMax = 0;

	int nBarWith = 0;
	nBarWith = pHistoryDataManager->getBarType();//FIVE_MINUTES
	nTimeFrom = pHistoryDataManager->getTimeFrom();
	nTimeTo = pHistoryDataManager->getTimeTo();

	// prepare axes:
	pRect->axis(QCPAxis::atLeft)->setLabel(QObject::tr("Y-value"));
	pRect->axis(QCPAxis::atBottom)->setLabel(QObject::tr("X-time"));
	pRect->axis(QCPAxis::atBottom)->setTickLabelType(QCPAxis::ltDateTime);
	pRect->axis(QCPAxis::atBottom)->setDateTimeFormat(DEF_STRING_FORMAT_TIME.c_str());


	nIndex = 0;
	iterMap = pHistoryDataManager->m_pHistoryACK->m_MapBarData.begin();
	while (iterMap != pHistoryDataManager->m_pHistoryACK->m_MapBarData.end())
	{
		//iterMap->second;
		// create empty statistical box plottables:
		pStatisticalBoxRef = NULL;
		pStatisticalBoxRef = new QCPStatisticalBox(pRect->axis(QCPAxis::atBottom), pRect->axis(QCPAxis::atLeft));
		pCustomPlot->addPlottable(pStatisticalBoxRef);

		fMinimum = iterMap->low;
		fMaximum= iterMap->high;

		if (iterMap->open > iterMap->close)
		{
			//high->low
			fLowerQuartile = iterMap->close;
			fUpperQuartile = iterMap->open;
			pBoxBrushRef = &redBrush;
			pBoxPenRef = &redPen;
		}
		else
		{
			fLowerQuartile = iterMap->open;
			fUpperQuartile = iterMap->close;
			pBoxBrushRef = &greenBrush;
			pBoxPenRef = &greenPen;
		}

		//boxBrush.setStyle(Qt::SolidPattern); // make it look oldschool Qt::SolidPattern  Qt::Dense6Pattern
		pStatisticalBoxRef->setBrush(*pBoxBrushRef);
		pStatisticalBoxRef->setPen(*pBoxPenRef);
		pStatisticalBoxRef->setWhiskerPen(*pBoxPenRef);
		pStatisticalBoxRef->setWhiskerBarPen(*pBoxPenRef);
		pStatisticalBoxRef->setMedianPen(*pBoxPenRef);

		// set data:
		//pStatisticalBoxTmp->setKey(nIndex);
		pStatisticalBoxRef->setBoxType(QCPStatisticalBox::btBar);
		pStatisticalBoxRef->setKey(iterMap->timestamp);
		pStatisticalBoxRef->setMinimum(fMinimum);
		pStatisticalBoxRef->setLowerQuartile(fLowerQuartile);
		pStatisticalBoxRef->setMedian(fLowerQuartile);
		pStatisticalBoxRef->setUpperQuartile(fUpperQuartile);
		pStatisticalBoxRef->setMaximum(fMaximum);

		//pStatisticalBoxTmp->setWidth(1);//矩形宽度
		pStatisticalBoxRef->setWidth(nBarWith);//矩形宽度

		pStatisticalBoxRef->setWhiskerWidth(0);//上顶,下底 直线宽度


		if (fMinimum < nLeftAxisRangeMin)
		{
			nLeftAxisRangeMin = fMinimum;
		}

		if (fMaximum > nLeftAxisRangeMax)
		{
			nLeftAxisRangeMax = fMaximum;
		}

		nIndex++;
		iterMap++;
	}//while

	// make key axis range scroll with the data (at a constant range size of 8):
	pRect->axis(QCPAxis::atBottom)->setRange(nTimeFrom, nTimeTo);
	pRect->axis(QCPAxis::atLeft)->setRange(nLeftAxisRangeMin, nLeftAxisRangeMax);


// 	pCustomPlot->rescaleAxes();
// 	pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

	pBoxBrushRef = NULL;
	pBoxPenRef = NULL;
	pStatisticalBoxRef = NULL;
}