[Interest] Plotting 2d line with color depending on value (like colormap)

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Thu Jun 21 12:41:15 CEST 2018


On 21/06/18 11:51, Massimiliano Maini wrote:
> I'd need to do something like this: https://imgur.com/MLAEuad
> 
> I have a set of (x,y,z) data points and want to plot them on a usual 2d 
> plot ( x and y values), but with the line color depending on the z 
> values (colormap).
> 
> How can this be achieved in Qt ?

I'm not sure if I understood correctly; the graph in the picture isn't 
using a third independent variable for the color of each point, rather 
it's just using the y (in other words: the color of each point in the 
line is just a function of its y coordinate.)

Anyhow, short of using a complete graphing solution, you can easily 
generate a vertical gradient using QLinearGradient (with the number of 
stops you need), and the curve as a QPainterPath. Then you have a couple 
of easy options:

1) set the curve as a clip path on a QPainter, and fill a rectangle with 
the gradient (=> only the path will be filled), or

2) create a transparent QImage of suitable size, draw the path in some 
solid color, and draw a rectangle filled with the gradient over the path 
using some composition mode, e.g. SourceIn. Then draw the image on your 
chart.

HTH,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4007 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180621/2d8d9720/attachment.bin>


More information about the Interest mailing list