MichaelVoelkel / ChartJs2QML

QML adaptor for Chart.js 2.x that supports startup animations and tooltips
MIT License
104 stars 20 forks source link

Please provide Qt-conform signals to make more interaction possible #4

Open ArsMasiuk opened 4 years ago

ArsMasiuk commented 4 years ago

Hi! I'm just curious how would it be possible to make Qt-application react on clicks on the chart items?

I.e. when I have a bar chart then I would like to intercept a click on bar's segment in my host Qt application, in order to obtain clicked bar's data (i.e. index and segment where the click has occurred).

There is submitEvent() function in Chart.qml, but how to use it from Qt code? Thanks!

ArsMasiuk commented 4 years ago

Well. Found a solution, maybe not the best one, but it works. In order to fire itemClickedSignal, I've added to Chart.qml:

Canvas {
    id: root

....
// signal is sent when clicked on charts's item (i.e. barchart)
// itemIndex: index of the clicked bar in the dataset
// datasetIndex: index of the clicked bar's segment if any

signal itemClickedSignal(int itemIndex, int datasetIndex)

MouseArea {
...
        onClicked: {
            submitEvent(mouse, "click");

            var element = jsChart.getElementAtEvent(event.mouseEvent)[0];
            root.itemClickedSignal(element._index, element._datasetIndex);
        }
...