QCustomPlot Discussion and Comments

draw rectangle area on x-axisReturn to overview

Is there a way to draw a background at a range on the x-axis? I quess I could fake it with an QCPItemRect drawn at the right position and having basically endless dimension on the y-axis, but is there a way to implement something like setting only the x-range? In the end I want to have a function blockXAxis('2000-01-01 00:00','2000-02-01 00:00') which should then paint a different background in the specified x-range..

you can use QCPItemRect

thank you lancelot..

In my implementation I am using an layout in the QCustomplot with 2 rows and 1 column.. I now add one QCPItemRect in ech subplot, but all added QCPItemRect's are added to the upper grid 0,0.. how can I set the subplot to add the QCPItemRect to?

I use the following code:

		QCPItemRect* rect=new QCPItemRect(this);
		rect->topLeft->setCoords(QPointF(0,100));
		rect->bottomRight->setCoords(QPointF(100,0));
		rect->setBrush(QBrush(QColor(200,100,100,100)));
		// set to correct subplot-grid..
		rect->setClipAxisRect(defaultAxisRect);
		rect->setClipToAxisRect(true);

I tried setting the ClipAxisRect with the setClipAxisRect, but thats not working.. the first QCPItemRect is displayed correct, but if I drag it slower it is displayed in the lower subplot too. The second added QCPItemRect is not displayed at all..

I also looked into registerItem, but it works on the Customplot and not on the subplot, so I guess, it will not solve the problem here..

another Ide wa to use the AxisRect's insetLayout like that:

		defaultAxisRect->insetLayout()->addElement(rect,defaultAxisRect->rect());

but I cant use the QCPItemRect* in the Function, because it need an QCPLayoutElement*

Any idea, what I am doing wrong?

got it working now.. I did not realize, that you have to set the axis on the positions of the QCPItemRect aka topLeft or bottomRight to the QCPAxisRect's axis.. So the following code works:

QCPItemRect* rect=new QCPItemRect(this);
rect->topLeft->setCoords(QPointF(0,100));
rect->bottomRight->setCoords(QPointF(100,0));
rect->setBrush(QBrush(QColor(200,100,100,100)));
// set to correct subplot-grid..
rect->setClipAxisRect(defaultAxisRect);
// rect->setClipToAxisRect(true);
rect->topLeft->setAxes(
	defaultAxisRect->axis(QCPAxis::atBottom),
	defaultAxisRect->axis(QCPAxis::atLeft));
rect->bottomRight->setAxes(
	defaultAxisRect->axis(QCPAxis::atBottom),
	defaultAxisRect->axis(QCPAxis::atLeft));

now the drawn rect is bound to the subplot and correctly zooming with the left-bottom axis..

unfortunately there is no way to set the y-coordinates to unlimited, but I use 1e10 and -1e10 for now, which works good enough. setting the y-coordinate to numeric_limits<float>::min() did not work here..

thanks for the tip with QCPItemRect again lancelot..