Advertisement
tasuku

Untitled

Jan 10th, 2013
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. import QtQuick 2.0
  2.  
  3. Rectangle {
  4. id: root
  5. width: 360
  6. height: 360
  7. state: "month"
  8.  
  9. ListModel {
  10. id: dayData
  11. Component.onCompleted: {
  12. var data_points = [50,0,7,2,6,2,7,9]
  13. for (var i = 0; i < data_points.length; i++)
  14. append({'value': data_points[i]})
  15. }
  16. }
  17.  
  18. ListModel {
  19. id: weekData
  20. Component.onCompleted: {
  21. var data_points = [50,9,14,10,12,11,13,14]
  22. for (var i = 0; i < data_points.length; i++)
  23. append({'value': data_points[i]})
  24. }
  25. }
  26.  
  27. ListModel {
  28. id: monthData
  29. Component.onCompleted: {
  30. var data_points = [50,15,14,18,19,30,40,35]
  31. for (var i = 0; i < data_points.length; i++)
  32. append({'value': data_points[i]})
  33. }
  34. }
  35.  
  36. //モードの切替
  37. Row {
  38. id: tabs
  39. Repeater {
  40. model: ['day', 'week', 'month']
  41. delegate: MouseArea {
  42. width: 100
  43. height: 20
  44. Text {
  45. anchors.centerIn: parent
  46. text: '1 %1'.arg(model.modelData)
  47. }
  48. onClicked: root.state = model.modelData
  49. }
  50. }
  51. }
  52.  
  53. //絵を描く
  54. Canvas {
  55. id:canvas
  56. anchors.top: tabs.bottom
  57. anchors.left: parent.left
  58. anchors.right: parent.right
  59. anchors.bottom: parent.bottom
  60. anchors.margins: 10
  61. property string strokeStyle:"#dc9ee7"
  62. property string fillStyle:"red"
  63. property int lineWidth: 1
  64. property bool fill:false
  65. property bool stroke:true
  66. property real alpha:0.8
  67. property real scaleX : 1
  68. property real scaleY : 1
  69. property real centerX: 0//width / 2
  70. property real centerY: height
  71. property var model: monthData
  72.  
  73. antialiasing: true
  74. renderTarget: Canvas.Image
  75. renderStrategy: Canvas.Immediate
  76.  
  77. //各種プロパティの変更イベントで再描画させる
  78. onLineWidthChanged:requestPaint();
  79. onFillChanged:requestPaint();
  80. onStrokeChanged:requestPaint();
  81. onAlphaChanged:requestPaint();
  82. onScaleXChanged:requestPaint();
  83. onScaleYChanged:requestPaint();
  84. onModelChanged: requestPaint()
  85.  
  86. onPaint: {
  87. if (typeof model === 'undefined') return
  88. var pich = canvas.width / (model.count - 1);
  89. var ctx = canvas.getContext('2d');
  90. //状態保存
  91. ctx.save();
  92. //描画エリアのクリア
  93. ctx.clearRect(0, 0, canvas.width, canvas.height);
  94. //各種設定
  95. ctx.fillStyle = canvas.fillStyle;
  96. //中心をずらす
  97. ctx.translate(centerX, centerY);
  98. //拡縮する
  99. ctx.scale(canvas.scaleX, canvas.scaleY);
  100. //回転する
  101. // ctx.rotate(canvas.rotate);
  102. //パス描画の開始
  103. ctx.beginPath();
  104. //グラフ
  105. ctx.moveTo(0, -model.get(0).value*2);
  106. for(var i=1; i<model.count; i++){
  107. ctx.lineTo(i*pich, -model.get(i).value*2);
  108. }
  109. //塗りを適用する
  110. if (canvas.fill) ctx.fill();
  111. //ストロークを適用させる
  112. if (canvas.stroke) ctx.stroke();
  113. //状態復帰
  114. ctx.restore();
  115. }
  116. }
  117.  
  118. //状態管理
  119. states: [
  120. State{
  121. name: "month"
  122. PropertyChanges {
  123. target: canvas
  124. state: 'month'
  125. model: monthData
  126. }
  127. },
  128. State{
  129. name: "week"
  130. PropertyChanges {
  131. target: canvas
  132. state: 'week'
  133. model: weekData
  134. }
  135. },
  136. State{
  137. name: "day"
  138. PropertyChanges {
  139. target: canvas
  140. state: 'day'
  141. model: dayData
  142. }
  143. }
  144. ]
  145.  
  146. transitions: [
  147. Transition {
  148. // skip the first animation
  149. from: ""
  150. to: "*"
  151. },
  152. Transition {
  153. SequentialAnimation {
  154. NumberAnimation { target: canvas; property: "scaleX"; to: root.scaleFactor(canvas.state, root.state); duration: 500; easing.type: Easing.InOutQuad }
  155. PropertyAction { target: canvas; property: "scaleX"; value: 1 }
  156. }
  157. }
  158. ]
  159.  
  160. //うにょーんてするスケールサイズを求める
  161. property var scaleFactors: {'month': 1, 'week': 4, 'day': 4*7}
  162. function scaleFactor(from, to) {
  163. switch(from) {
  164. case "month": return scaleFactors[to]
  165. case "week": return scaleFactors[to] / scaleFactors[from]
  166. case "day": return 1 / scaleFactors[from]
  167. }
  168. return 1
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement