From 758f18cbb0c34547e78fe355324f7158b4324813 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 11 Oct 2016 22:37:27 +0200 Subject: [PATCH] plots: remove margin and add `origin` option --- .../user-guide/widgets-specifics/plots.md | 13 +++- .../js/app/widgets/plots/_plots_base.js | 66 +++++++++++++------ src/browser/js/app/widgets/plots/eq.js | 1 + src/browser/js/app/widgets/plots/plot.js | 1 + .../js/app/widgets/plots/visualizer.js | 10 +-- 5 files changed, 65 insertions(+), 26 deletions(-) diff --git a/resources/docs/user-guide/widgets-specifics/plots.md b/resources/docs/user-guide/widgets-specifics/plots.md index 4e99e0d9..8bf1f568 100644 --- a/resources/docs/user-guide/widgets-specifics/plots.md +++ b/resources/docs/user-guide/widgets-specifics/plots.md @@ -61,6 +61,10 @@ The plot receives an array of [x,y] coordinates and draws them on a chart with - default: `false` - usage: set to `true` to use logarithmic scale for the `y` axis (log10) +#### `origin` +- type: `number` +- default: `auto` +- usage: defines the y-axis origin. Set to `false` to disable it. ## Eq @@ -106,7 +110,10 @@ The Eq receives an array of filter objects and draws their frequency response be - default: `false` - usage: set to `true` to use logarithmic scale for the `x` axis (log10) - +#### `origin` +- type: `number` +- default: `auto` +- usage: defines the y-axis origin. Set to `false` to disable it. ## Visualizer @@ -139,6 +146,10 @@ The visualizer displays the evolution in time of a received value (through osc) - default: `false` - usage: set to `true` use logarithmic scaling (log10) +#### `origin` +- type: `number` +- default: `auto` +- usage: defines the y-axis origin. Set to `false` to disable it. ## Text diff --git a/src/browser/js/app/widgets/plots/_plots_base.js b/src/browser/js/app/widgets/plots/_plots_base.js index 1b92f7fa..861a3ffb 100644 --- a/src/browser/js/app/widgets/plots/_plots_base.js +++ b/src/browser/js/app/widgets/plots/_plots_base.js @@ -61,27 +61,52 @@ _plots_base.prototype.draw = function() { this.ctx.strokeStyle = this.colors.custom this.ctx.stroke() + + + if (this.widgetData.origin !== false) { + + var origin = mapToScale(this.widgetData.origin||this.rangeY.min,[this.rangeY.min,this.rangeY.max],[this.height,0],0,this.widgetData.logScaleY,true) + + this.ctx.save() + this.ctx.globalAlpha = 0.1 + this.ctx.fillStyle = this.colors.custom + this.ctx.lineTo(this.width,origin) + this.ctx.lineTo(0,origin) + this.ctx.closePath() + this.ctx.fill() + this.ctx.restore() + + } + + this.ctx.font = PXSCALE * 10 + 'px sans-serif' 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,12*PXSCALE,this.height) - this.ctx.textAlign = 'right' - this.ctx.fillText(this.pips.x.max,this.width-10,this.height) + 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) - 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,12*PXSCALE,2*PXSCALE) - this.ctx.textAlign = 'right' - this.ctx.fillText(this.pips.y.max,this.height-10*PXSCALE,2*PXSCALE) - this.ctx.rotate(Math.PI/2) - this.ctx.restore() + 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() + + } } @@ -95,21 +120,22 @@ _plots_base.prototype.draw_data = function() { var newpoint = this.data[i].length? [ - mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[15*PXSCALE,this.width-15*PXSCALE],0,this.widgetData.logScaleX,true), - mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,this.widgetData.logScaleY,true), + mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[0,this.width],0,this.widgetData.logScaleX,true), + mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-PXSCALE,PXSCALE],0,this.widgetData.logScaleY,true), ] : [ - mapToScale(i,[0,this.data.length-1],[15*PXSCALE,this.width-15*PXSCALE],0,this.widgetData.logScaleX,true), - mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,this.widgetData.logScaleY,true), + mapToScale(i,[0,this.data.length-1],[0,this.width],0,this.widgetData.logScaleX,true), + mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-PXSCALE,PXSCALE],0,this.widgetData.logScaleY,true), ] if (first) { this.ctx.moveTo(newpoint[0],newpoint[1]) + this.ctx.lineTo(newpoint[0],newpoint[1]) first = false } else { - this.ctx.moveTo(point[0],point[1]) + // this.ctx.moveTo(point[0],point[1]) this.ctx.lineTo(newpoint[0],newpoint[1]) } point = newpoint diff --git a/src/browser/js/app/widgets/plots/eq.js b/src/browser/js/app/widgets/plots/eq.js index 0365c038..df99d600 100644 --- a/src/browser/js/app/widgets/plots/eq.js +++ b/src/browser/js/app/widgets/plots/eq.js @@ -21,6 +21,7 @@ module.exports.options = { filters:[], resolution:128, rangeY: {min:-20,max:20}, + origin: 'auto', logScaleX: false, separator3:'osc', diff --git a/src/browser/js/app/widgets/plots/plot.js b/src/browser/js/app/widgets/plots/plot.js index 45aa9af1..9c26e4ec 100644 --- a/src/browser/js/app/widgets/plots/plot.js +++ b/src/browser/js/app/widgets/plots/plot.js @@ -20,6 +20,7 @@ module.exports.options = { points:[], rangeX: {min:0,max:1}, rangeY: {min:0,max:1}, + origin: 'auto', logScaleX: false, logScaleY: false, diff --git a/src/browser/js/app/widgets/plots/visualizer.js b/src/browser/js/app/widgets/plots/visualizer.js index cd5f3bd8..eeff6e0d 100644 --- a/src/browser/js/app/widgets/plots/visualizer.js +++ b/src/browser/js/app/widgets/plots/visualizer.js @@ -20,6 +20,7 @@ module.exports.options = { widgetId:'', duration:1, range: {min:0,max:1}, + origin: 'auto', logScale: false, separator3:'osc', @@ -34,8 +35,7 @@ var Visualizer = function(widgetData) { this.pips.y.min = Math.abs(widgetData.range.min)>=1000?widgetData.range.min/1000+'k':widgetData.range.min this.pips.y.max = Math.abs(widgetData.range.max)>=1000?widgetData.range.max/1000+'k':widgetData.range.max - this.pips.x.min = '' - this.pips.x.max = '' + this.pips.x = false this.length = Math.round(clip(60*widgetData.duration,[8,4096])) this.data = new Array(this.length) this.value = widgetData.range.min @@ -77,10 +77,10 @@ Visualizer.prototype.draw_data = function(){ var first = true var point = [] - for (var i=this.length-1;i>=0;i=i-1) { + for (var i=0;i