summaryrefslogtreecommitdiff
path: root/docs/res/graphplot.js
blob: b06f67f16ab9d46a184f6ba0384c1d40ec95fe6e (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
function GraphPlot(canvas) {
  this.canvas = canvas
  const g = canvas.getContext('2d')
  if (g == null) {
    throw new Error('failed to acquire 2d context')
  }

  this.width    = 0  // dp
  this.height   = 0  // dp
  this.widthPx  = 0  // px
  this.heightPx = 0  // px
  this.pixelRatio = 1
  this.g = g
  this.dataSegments = []
  this.axes = {
    x0: .5,          // % from left to x=0
    y0: .5,          // % from top to y=0
    scalex: 40,      // pixels from x=0 to x=1
    scaley: 40,      // pixels from y=0 to y=1
    negativeX: true,
  }

  if (!this.autosize()) {
    this.setSize(256, 256)
  }
}

GraphPlot.prototype.autosize = function() {
  try {
    this.canvas.width = null
    this.canvas.height = null
    this.canvas.style.width = null
    this.canvas.style.height = null
    var cs = window.getComputedStyle(this.canvas)
    var width = parseFloat(cs.width)
    var height = parseFloat(cs.height)
    this.setSize(width, height)
    return true
  } catch (err) {
    if (typeof console != 'undefined' && console.warn) {
      console.warn('GraphPlot.autosize failed: ' + err)
    }
  }
  return false
}

// setOrigin sets the origin of axis x and y
// The values should be in the range [0-1] and maps to the extremes
// of the canvas.
//
GraphPlot.prototype.setOrigin = function(x, y) {
  var p = this
  p.axes.x0 = x
  p.axes.y0 = y
}

// setScale sets the value scale for x and y axis.
// The values should be provided as display points.
//
GraphPlot.prototype.setScale = function(x, y) {
  var p = this
  if (y === undefined) {
    y = x
  }
  p.axes.scalex = x
  p.axes.scaley = y
}

// setSize sets the size of canvas in display points
//
GraphPlot.prototype.setSize = function(width, height) {
  var p = this
  p.width = width
  p.height = height
  const el = p.canvas, g = p.g
  p.pixelRatio = window.devicePixelRatio || 1
  if (p.pixelRatio != 1) {
    el.width = p.widthPx = width * p.pixelRatio
    el.height = p.heightPx = height * p.pixelRatio
    g.scale(p.pixelRatio, p.pixelRatio)
  } else {
    el.width = p.widthPx = width
    el.height = p.heightPx = height
    g.scale(1, 1)
  }
  el.style.width = `${width}px`
  el.style.height = `${height}px`
}


GraphPlot.prototype.renderAxes = function() {
  var p = this
    , g = p.g
    , x0 = Math.round(p.axes.x0 * p.widthPx) / p.pixelRatio
    , y0 = Math.round(p.axes.y0 * p.heightPx) / p.pixelRatio

  g.beginPath()
  g.strokeStyle = "rgb(0, 0, 0, 0.2)"
  if (y0 > 0 && y0 < p.width) {
    g.moveTo(0, y0); g.lineTo(p.width, y0)  // X axis
  }
  if (x0 > 0 && x0 < p.height) {
    g.moveTo(x0, 0); g.lineTo(x0, p.height)  // Y axis
  }
  g.stroke()
}


// plotf plots an arbitrary function on the graph
//
GraphPlot.prototype.plotf = function(f, color) {
  var p = this
    , g = p.g
    , w = p.width
    , h = p.height
    , x0 = p.axes.x0 * p.width
    , y0 = p.axes.y0 * p.height
    , x = 0
    , y = 0
    , dx = 4 / p.pixelRatio // smaller means finer curves and more CPU
    , scalex = p.axes.scalex * w
    , scaley = p.axes.scaley * h
    , iMax = Math.round((w - x0) / dx)
    , iMin = p.axes.negativeX ? Math.round(-x0 / dx) : 0

  g.beginPath()
  g.lineWidth = 1
  g.strokeStyle = color || "rgb(0, 0, 0, 0.8)"

  for (var i = iMin; i <= iMax; i++) {
    x = dx * i
    y = f(x / scalex) * scaley
    if (i == iMin) {
      g.moveTo(x0 + x, y0 - y)
    } else {
      g.lineTo(x0 + x, y0 - y)
    }
  }
  
  g.stroke()
}


// plotLines draws straight lines between a collection of points
//
GraphPlot.prototype.plotLine = function(points, color) {
  var p = this
    , g = p.g
    , x0 = p.axes.x0 * p.width
    , y0 = p.axes.y0 * p.height
    , x = 0
    , y = 0
    , scalex = p.axes.scalex * p.width
    , scaley = p.axes.scaley * p.height
    , pt

  g.beginPath()
  g.lineWidth = 1
  g.strokeStyle = color || "rgb(0, 0, 0, 0.8)"

  var i = 0
  for (; i < points.length; i++) {
    pt = points[i]
    x = pt[0] * scalex
    y = pt[1] * scaley
    if (i == 0) {
      g.moveTo(x0 + x, y0 - y)
    } else {
      g.lineTo(x0 + x, y0 - y)
    }
  }

  g.stroke()
}


// plotPoints draws points
//
GraphPlot.prototype.plotPoints = function(points, color) {
  var p = this
    , g = p.g
    , x0 = p.axes.x0 * p.width
    , y0 = p.axes.y0 * p.height
    , x = 0
    , y = 0
    , scalex = p.axes.scalex * p.width
    , scaley = p.axes.scaley * p.height
    , pt
    , i = 0

  g.fillStyle = color || "rgb(0, 0, 0, 0.8)"

  for (; i < points.length; i++) {
    pt = points[i]
    x = x0 + pt[0] * scalex
    y = y0 - pt[1] * scaley
    g.beginPath()
    g.arc(x, y, 3, 0, Math.PI + (Math.PI * 2) / 2, false)
    g.fill()
  }
}


GraphPlot.prototype.clear = function() {
  var p = this
  p.g.clearRect(0, 0, p.width, p.height)
  p.renderAxes()
}


GraphPlot.prototype.renderDemo = function() {
  var p = this
    , g = p.g
    , dpscale = p.pixelRatio
    , w = p.widthPx
    , h = p.heightPx

  p.clear()

  p.plotf(
    function(x) { return Math.sin(x) },
    'blue'
  )

  p.plotf(
    function(x) { return Math.cos(3*x) },
    'hotpink'
  )

  // var scale = p.height / 4
  // g.moveTo(0, scale)
  // var i, sine, lines = 200, frag = p.width / lines
  // for (i = 0; i < lines; i++) {
  //   sine = Math.sin(i / scale * 2) * scale
  //   g.lineTo(i * frag, -sine + scale)
  // }
  // g.stroke()
}