plots: add bars options (issue #130)
This commit is contained in:
parent
275bdc10a6
commit
1fd874d144
4 changed files with 82 additions and 50 deletions
|
|
@ -16,7 +16,7 @@
|
|||
- switch: added `showValues` to display both values and labels in the buttons; array values can now be selected via osc
|
||||
- rgb: **removed** `touchAddress` option
|
||||
- labels/texts: multiple white spaces are no longer merged
|
||||
- plot: can receive stringified arrays of coordinates
|
||||
- plot: can receive stringified arrays of coordinates; added `bars` option for drawing barcharts
|
||||
- widgets' `precision` option allows setting a specific data type for numbers (sliders can send doubles !)
|
||||
- misc
|
||||
- event system rewritten
|
||||
|
|
|
|||
|
|
@ -71,6 +71,11 @@ The plot receives an array of coordinates and draws them on a chart. Coordinates
|
|||
}`
|
||||
- usage: defines the `min` and `max` values for the `x` and `y` axis
|
||||
|
||||
### `bars`
|
||||
- type: `boolean`
|
||||
- default: `false`
|
||||
- usage: set to `true` to use draw bars instead (disables `logScaleX` and forces `x-axis` even spacing)
|
||||
|
||||
### `logScaleX`
|
||||
- type: `boolean`
|
||||
- default: `false`
|
||||
|
|
|
|||
|
|
@ -63,11 +63,69 @@ module.exports = class _plots_base extends _canvas_base {
|
|||
draw() {
|
||||
|
||||
this.ctx.clearRect(0,0,this.width,this.height)
|
||||
|
||||
if (this.getProp('bars')) {
|
||||
this.draw_bars()
|
||||
} else {
|
||||
this.draw_line()
|
||||
}
|
||||
|
||||
if (this.getProp('pips')) this.draw_pips()
|
||||
|
||||
}
|
||||
|
||||
draw_pips() {
|
||||
|
||||
this.ctx.globalAlpha = 1
|
||||
this.ctx.fillStyle = this.colors.text
|
||||
|
||||
if (this.pips.x) {
|
||||
|
||||
this.ctx.textBaseline = "bottom"
|
||||
this.ctx.textAlign = 'left'
|
||||
this.ctx.fillText(this.pips.x.min,(this.pips.y?12:2)*PXSCALE,this.height)
|
||||
this.ctx.textAlign = 'right'
|
||||
this.ctx.fillText(this.pips.x.max,this.width-2*PXSCALE,this.height)
|
||||
|
||||
}
|
||||
|
||||
if (this.pips.y) {
|
||||
|
||||
this.ctx.save()
|
||||
this.ctx.translate(0, this.height)
|
||||
this.ctx.rotate(-Math.PI/2)
|
||||
|
||||
this.ctx.textBaseline = "top"
|
||||
this.ctx.textAlign = 'left'
|
||||
this.ctx.fillText(this.pips.y.min,(this.pips.x?12:2)*PXSCALE,2*PXSCALE)
|
||||
this.ctx.textAlign = 'right'
|
||||
this.ctx.fillText(this.pips.y.max,this.height-2*PXSCALE,2*PXSCALE)
|
||||
this.ctx.rotate(Math.PI/2)
|
||||
this.ctx.restore()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
draw_line() {
|
||||
|
||||
var points = []
|
||||
|
||||
for (let i in this.data) {
|
||||
|
||||
if (this.data[i].length) {
|
||||
points.push(mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[0,this.width],0,this.logScaleX,true))
|
||||
points.push(mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true))
|
||||
} else {
|
||||
points.push(mapToScale(i,[0,this.data.length-1],[0,this.width],0,this.logScaleX,true))
|
||||
points.push(mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.ctx.beginPath()
|
||||
|
||||
if (!this.width) return
|
||||
|
||||
this.draw_data()
|
||||
this.ctx.curve(points, this.smooth, Math.round(this.width/(points.length/2 - 1)))
|
||||
|
||||
this.ctx.globalAlpha = 0.7
|
||||
this.ctx.lineWidth = 2 * PXSCALE
|
||||
|
|
@ -87,59 +145,27 @@ module.exports = class _plots_base extends _canvas_base {
|
|||
|
||||
}
|
||||
|
||||
|
||||
this.ctx.globalAlpha = 1
|
||||
this.ctx.font = PXSCALE * 10 + 'px sans-serif'
|
||||
this.ctx.fillStyle = this.colors.text
|
||||
|
||||
if (this.getProp('pips')) {
|
||||
|
||||
if (this.pips.x) {
|
||||
|
||||
this.ctx.textBaseline = "bottom"
|
||||
this.ctx.textAlign = 'left'
|
||||
this.ctx.fillText(this.pips.x.min,(this.pips.y?12:2)*PXSCALE,this.height)
|
||||
this.ctx.textAlign = 'right'
|
||||
this.ctx.fillText(this.pips.x.max,this.width-2*PXSCALE,this.height)
|
||||
|
||||
}
|
||||
|
||||
if (this.pips.y) {
|
||||
|
||||
this.ctx.save()
|
||||
this.ctx.translate(0, this.height)
|
||||
this.ctx.rotate(-Math.PI/2)
|
||||
|
||||
this.ctx.textBaseline = "top"
|
||||
this.ctx.textAlign = 'left'
|
||||
this.ctx.fillText(this.pips.y.min,(this.pips.x?12:2)*PXSCALE,2*PXSCALE)
|
||||
this.ctx.textAlign = 'right'
|
||||
this.ctx.fillText(this.pips.y.max,this.height-2*PXSCALE,2*PXSCALE)
|
||||
this.ctx.rotate(Math.PI/2)
|
||||
this.ctx.restore()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
draw_data() {
|
||||
draw_bars() {
|
||||
|
||||
var points = [],
|
||||
barWidth = Math.round(this.width / this.data.length),
|
||||
offset = Math.round((this.width - barWidth * this.data.length) / 2)
|
||||
|
||||
var origin = mapToScale(this.getProp('origin') || this.rangeY.min,[this.rangeY.min,this.rangeY.max],[this.height,0],0,this.getProp('logScaleY'),true)
|
||||
|
||||
this.ctx.beginPath()
|
||||
|
||||
var points = []
|
||||
for (let i in this.data) {
|
||||
|
||||
if (this.data[i].length) {
|
||||
points.push(mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[0,this.width],0,this.logScaleX,true))
|
||||
points.push(mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true))
|
||||
} else {
|
||||
points.push(mapToScale(i,[0,this.data.length-1],[0,this.width],0,this.logScaleX,true))
|
||||
points.push(mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true))
|
||||
}
|
||||
var y = mapToScale(this.data[i].length ? this.data[i][1] : this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true)
|
||||
this.ctx.rect(offset + i * barWidth, Math.min(y, origin), barWidth - PXSCALE, Math.abs(Math.min(y - origin)))
|
||||
|
||||
}
|
||||
|
||||
this.ctx.curve(points, this.smooth, Math.round(this.width/(points.length/2 - 1)))
|
||||
this.ctx.globalAlpha = 0.4
|
||||
this.ctx.fillStyle = this.colors.custom
|
||||
this.ctx.fill()
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ module.exports = class Plot extends _plots_base {
|
|||
rangeX: {min:0,max:1},
|
||||
rangeY: {min:0,max:1},
|
||||
origin: 'auto',
|
||||
bars: false,
|
||||
logScaleX: false,
|
||||
logScaleY: false,
|
||||
smooth: false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue