laurapons
laurapons

Reputation: 1103

QML Create tooltips dynamically

So I have a piechart that changes dynamically. I want to show the value of each slice when mouse over the slice, but I am not sure how to create the tooltip when onHovered is triggered. I use

qt 5.9.1 & import QtQuick.Controls 2.2

UPDATE: I have added some code to explain how I create the slices. Here is the code:

function onUpdateValues(values){
     switch(values.type){
           case PIE_CHART: 
                createPieChart(values.data);
                break;
           ...
           default:
                console.debug("CHART TYPE ERROR");
                break;
            }
     }
}

function createPieChart(data){
    pieserieschart.clear();
    for (var prop in data) {
        var new_slice =  pieserieschart.append(prop, data[prop]);
        new_slice.tooltip = prop + ": " + data[prop]
        //I tried using hovered signal (and without), but it's not doing any difference
        new_slice.hovered.connect(function(state) { new_slice.tooltip.visible = state })
        //If I replace the above line by the next one, I can see the console.log info, but the tooltip is not enabled
        new_slice.hovered.connect(function(state) { sliceHovered(new_slice, state) })

    }
}

function sliceHovered(slice, value){
    slice.enabled = true
    console.log("Slice hovered: " + slice.tooltip + " " + value)
}

ChartView { /* Chart */
    id:chartView

    PieSeries {
       id: pieserieschart;
       size: 1;
       holeSize: 0.55;
       onClicked: sliceClicked(slice);
    }
}

I can see the console.log but I am not able to see the tooltip, and the application output doesn't show any error, but tooltip is not triggered

Upvotes: 0

Views: 1759

Answers (2)

Cmorais
Cmorais

Reputation: 51

I know that maybe you don't need this anymore, but as I had the same problem and reached this question, I'll post my solution to the position on the slice.

First, I did not used the chartView.ToolTip, because it didn't allow me to move on the plane... So I make a ToolTip{ } element, with visibility = false.

The high school math now made it worth it. The PieSlice has 2 useful properties: startAngle (on the circle, where the pie starts) and angleSpan (angle used by the pie). As I have the angle, I can use some math magics to find the X and Y.

Tricky parts: the angle is clockwise, and the geometry stuff is measure counter-clockwise. So I have to convert the angle from clockwise to counter first... And the start point in geometry is different from Qt Charts, I have to compensate by reducing -90 from the origin...

As i want the hint be presented in the middle of the slice, I sum half of the angleSpan to the startAngle.

And after suffer a lot, I realize that the Math.cos and Math.sin uses the input in RADIANS, not in degree. I converted from angle to rad and... that's it. I hope this can help someone else lost like me =) .

      ToolTip{
        id:sliceToolTip
        visible: false;


    }
         onHovered: {
            if (state){


             // 360 + angle clockwise to convert to counter-clw.
             //-90 to compensate the QML charts start point 
                var angle =(360+ (-1*(slice.startAngle-90+(slice.angleSpan/2))));
                if(angle > 360){ //Reduce to one spin, preventing high angles
                  angle -=360;
                }
                angle = (angle*Math.PI)/180; //convert to radians


                var offsetX = Math.cos(angle);
                var offsetY = Math.sin(angle);
                 sliceToolTip.x =  (chartView.width/2 - (sliceToolTip.width/2)) +  ((chartView.width/16)* offsetX*3);
                 sliceToolTip.y = (chartView.height/2 - (sliceToolTip.height/2)) - ((chartView.height/9)*offsetY*3); // My screen is 16:9, so I made a proportion to match the circle, as my charts goes with anchors.centerIn: parent
            }
                     else{
                        sliceToolTip.hide()
            }
        }

Upvotes: 1

jpnurmi
jpnurmi

Reputation: 5836

Relevant docs:

Example:

ChartView {
    id: chartView

    PieSeries {
        onHovered: {
            if (state)
                chartView.ToolTip.show(slice.label + ":" + slice.value)
            else
                chartView.ToolTip.hide()
        }
    }
}

Upvotes: 5

Related Questions