From e8a5c83b00fb9ae4d7de32d00b0a9c31c00a9ffe Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 29 Jan 2018 11:21:12 +0100 Subject: [PATCH] plots: add `bars` options (issue #130) --- CHANGELOG.md | 2 +- resources/docs/widgets/plots.md | 5 + .../js/app/widgets/plots/_plots_base.js | 124 +++++++++++------- src/browser/js/app/widgets/plots/plot.js | 1 + 4 files changed, 82 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 473d4493..2ad972d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/resources/docs/widgets/plots.md b/resources/docs/widgets/plots.md index eb672c9b..e35f55a4 100644 --- a/resources/docs/widgets/plots.md +++ b/resources/docs/widgets/plots.md @@ -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` diff --git a/src/browser/js/app/widgets/plots/_plots_base.js b/src/browser/js/app/widgets/plots/_plots_base.js index 7e07fe8c..1b6eec32 100644 --- a/src/browser/js/app/widgets/plots/_plots_base.js +++ b/src/browser/js/app/widgets/plots/_plots_base.js @@ -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() } diff --git a/src/browser/js/app/widgets/plots/plot.js b/src/browser/js/app/widgets/plots/plot.js index 6fc17b46..7a277f19 100644 --- a/src/browser/js/app/widgets/plots/plot.js +++ b/src/browser/js/app/widgets/plots/plot.js @@ -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,