Chart.coffee 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. class Chart extends Class
  2. constructor: () ->
  3. @query = ""
  4. @title = ""
  5. @value = ""
  6. @line_data = []
  7. @details = []
  8. @colorize = "cc00ff0a"
  9. @chart_ctx = null
  10. @chart_type_name = null
  11. @need_update = false
  12. initChart: (node) =>
  13. @chart_canvas = node
  14. @chart_ctx = node.getContext("2d")
  15. getTitle: =>
  16. @title
  17. update: =>
  18. Page.cmd "chartDbQuery", @getChartQuery(), (res) =>
  19. @line_data = []
  20. for row in res
  21. @line_data.push(row.value)
  22. @line_data.reverse()
  23. @updateChart()
  24. query_type_data = """
  25. SELECT * FROM data
  26. WHERE
  27. type_id IN :type_ids AND
  28. date_added = (SELECT date_added FROM data ORDER BY data_id DESC LIMIT 1)
  29. """
  30. Page.cmd "chartDbQuery", [query_type_data, {type_ids: Page.page_stats.type_id_db[type_name] for type_name in @type_names}], (res) =>
  31. type_data = {}
  32. for row in res
  33. type_data[Page.page_stats.type_name_db[row.type_id]] = row.value
  34. @details = @formatDetails?(type_data)
  35. @value = @formatValue?(type_data)
  36. Page.projector.scheduleRender()
  37. updateChart: =>
  38. @chart_ctx.clearRect(0, 0, @chart_canvas.width, @chart_canvas.height)
  39. stroke = @chart_ctx.createLinearGradient(0, 0, 900, 0)
  40. stroke.addColorStop(0, @chart_stroke[0])
  41. stroke.addColorStop(1, @chart_stroke[1])
  42. #@chart_ctx.shadowColor = 'rgba(0, 0, 0, 0.7)'
  43. #@chart_ctx.shadowBlur = 3
  44. #@chart_ctx.shadowOffsetY = 0
  45. @chart_ctx.lineWidth = 4
  46. @chart_ctx.strokeStyle = stroke
  47. @chart_ctx.fillStyle = '#66666611'
  48. gradient = @chart_ctx.createLinearGradient(0, 200, 0, 400)
  49. gradient.addColorStop(0, "#42324599")
  50. gradient.addColorStop(1, "#2C2E3700")
  51. @chart_ctx.fillStyle = gradient
  52. @chart_ctx.beginPath()
  53. @chart_ctx.moveTo(-10,0)
  54. step = 900 / (@line_data.length - 2)
  55. data_max = Math.max.apply(null, @line_data)
  56. data_min = Math.min.apply(null, @line_data)
  57. for data, i in @line_data
  58. line_y = 250 - ((data - data_min) / (data_max - data_min)) * 120
  59. @chart_ctx.lineTo((i - 1) * step, line_y)
  60. @chart_ctx.lineTo((i + 1) * step, line_y)
  61. @chart_ctx.lineTo(i * step, 450)
  62. @chart_ctx.lineTo(0, 450)
  63. @chart_ctx.fill()
  64. @chart_ctx.stroke()
  65. @chart_ctx.shadowBlur = 0
  66. render: =>
  67. if @need_update
  68. @update()
  69. @need_update = false
  70. h("div.Chart", {style: "background-image: radial-gradient(at 29% top, #eaaeda05, #{@colorize})"}, [
  71. h("div.titles", [
  72. h("div.title", @getTitle())
  73. h("div.value", @value)
  74. h("div.details", @details.map (detail) =>
  75. [detail, h("br", key: detail)]
  76. )
  77. ]),
  78. h("canvas.canvas", {afterCreate: @initChart, width: 900, height: 400})
  79. ])
  80. window.Chart = Chart