forked from toby20130333/qchart.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QChart.qml
176 lines (152 loc) · 4.6 KB
/
QChart.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* QChart.qml ---
*
* Author: Julien Wintz
* Created: Thu Feb 13 20:59:40 2014 (+0100)
* Version:
* Last-Updated: jeu. mars 6 12:55:14 2014 (+0100)
* By: Julien Wintz
* Update #: 69
*/
/* Change Log:
*
*/
import QtQuick 2.0
import "QChart.js" as Charts
Canvas {
id: canvas;
// ///////////////////////////////////////////////////////////////
property var chart;
property var chartData;
property int chartType: 0;
property bool chartAnimated: true;
property alias chartAnimationEasing: chartAnimator.easing.type;
property alias chartAnimationDuration: chartAnimator.duration;
property int chartAnimationProgress: 0;
property var chartOptions: ({});
property var events: ({}); /* internal use */
// /////////////////////////////////////////////////////////////////
// Callbacks
// /////////////////////////////////////////////////////////////////
onPaint: {
if(!chart) {
switch(chartType) {
case Charts.ChartType.BAR:
chart = new Charts.Chart(canvas.getContext("2d")).Bar(chartData, chartOptions);
break;
case Charts.ChartType.DOUGHNUT:
chart = new Charts.Chart(canvas.getContext("2d")).Doughnut(chartData, chartOptions);
break;
case Charts.ChartType.LINE:
console.log("here")
chart = new Charts.Chart(canvas.getContext("2d")).Line(chartData, chartOptions);
break;
case Charts.ChartType.PIE:
chart = new Charts.Chart(canvas.getContext("2d")).Pie(chartData, chartOptions);
break;
case Charts.ChartType.POLAR:
chart = new Charts.Chart(canvas.getContext("2d")).PolarArea(chartData, chartOptions);
break;
case Charts.ChartType.RADAR:
chart = new Charts.Chart(canvas.getContext("2d")).Radar(chartData, chartOptions);
break;
default:
console.log('Chart type should be specified.');
}
if (chartAnimated)
chartAnimator.start();
else
chartAnimationProgress = 100;
}
if (!chartAnimator.running && events.mouseout) {
var cb = eventCallback("mouseout")
if (cb)
cb(events.mouseout);
events.mouseout = null;
events.mousemove = null;
}
else if (!chartAnimator.running && events.mousemove) {
var cb = eventCallback("mousemove")
if (cb)
cb(events.mousemove);
events.mousemove = null;
chartAnimationProgress = 100;
}
else {
// On some charts 0 draws the full picture, negative values is always buggy
chart.draw((chartAnimationProgress > 0) ?
chartAnimationProgress/100 : 0.0001);
}
}
onHeightChanged: {
requestPaint();
}
onWidthChanged: {
requestPaint();
}
onChartAnimationProgressChanged: {
requestPaint();
}
onChartDataChanged: {
requestPaint();
}
// /////////////////////////////////////////////////////////////////
// Functions
// /////////////////////////////////////////////////////////////////
function repaint() {
chartAnimationProgress = 0;
chartAnimator.start();
}
function addEventListener(eventType, method) {
/* empty function that must exist on the canvas (called by Chart.js) */
}
function getBoundingClientRect() {
return {
left: 0,
top: 0,
right: canvasWindow.width,
bottom: canvasWindow.height
}
}
function eventCallback(name) {
if (chart && chart.events) {
return chart.events[name];
}
return null;
}
// /////////////////////////////////////////////////////////////////
// Internals
// /////////////////////////////////////////////////////////////////
MouseArea {
id: mouseArea;
anchors.fill: parent;
hoverEnabled: true;
onPositionChanged: {
if (parent.eventCallback("mousemove")) {
parent.events.mousemove = {
type: 'mousemove',
clientX: mouse.x,
clientY: mouse.y,
srcElement: parent
};
parent.requestPaint();
}
}
onExited: {
if (parent.eventCallback("mouseout")) {
parent.events.mousemove = {
type: 'mouseout',
srcElement: parent
};
parent.requestPaint();
}
}
}
PropertyAnimation {
id: chartAnimator;
target: canvas;
property: "chartAnimationProgress";
to: 100;
duration: 500;
easing.type: Easing.InOutElastic;
}
}