From c3f361f11063d8c1d50d817549d302534c21bb17 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 28 Jan 2018 14:00:51 +0100 Subject: [PATCH] plot: receive stringified arrays (issue #130) --- CHANGELOG.md | 1 + resources/docs/widgets/plots.md | 18 ++++++++++++------ .../js/app/widgets/plots/_plots_base.js | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8475eb1d..473d4493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +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 - 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 9de40718..eb672c9b 100644 --- a/resources/docs/widgets/plots.md +++ b/resources/docs/widgets/plots.md @@ -38,7 +38,12 @@ This one is like the `led` except it takes either three arguments (red, green an ## Plot -The plot receives an array of [x,y] coordinates and draws them on a chart with linear or logarithmic interpolation. +The plot receives an array of coordinates and draws them on a chart. Coordinates can be : +- `y` values evenly spaced on the `x` axis +- `[x, y]` coordinates arrays + +!!! info + Coordinates also can be sent as a stringified array ```js { @@ -51,10 +56,11 @@ The plot receives an array of [x,y] coordinates and draws them on a chart with - type: `array|string` - default: `[]` - usage: - - as a `string`: a widget's `id` whose multiple values will be displayed on the `y` axis, evenly spaced on the `x` axis (only *[matrices](matrices.md)* and *[pads](pads.md)* will work) - - as an `array`: each element must be an `array` of `[x,y]` coordinates, where `x` and `y` can either be a: - - `number`: constant values - - `string`: a widget's `id` whose value will be used + - as a `string`: a widget's `id` whose values will be used + - as an `array`: where items can be + - `number` + - `string` (widget's `id`) + - `[x, y]`: array of `number` or `string` ### `range` - type: `object` @@ -78,7 +84,7 @@ The plot receives an array of [x,y] coordinates and draws them on a chart with ### `smooth` - type: `boolean` - default: `false` -- usage: set to true to make the line smooth. Float values are also acceptable (works fine between 0 and 0.5) +- usage: set to `true` to make the line smooth. Float values are also acceptable (works fine between 0 and 0.5) ### `origin` - type: `number` diff --git a/src/browser/js/app/widgets/plots/_plots_base.js b/src/browser/js/app/widgets/plots/_plots_base.js index 4a9b0437..7e07fe8c 100644 --- a/src/browser/js/app/widgets/plots/_plots_base.js +++ b/src/browser/js/app/widgets/plots/_plots_base.js @@ -145,10 +145,19 @@ module.exports = class _plots_base extends _canvas_base { setValue(v, options={}) { - this.data = v - this.batchDraw() + if (typeof v == 'string' && v.indexOf('[') == 0) { + v = JSON.parseFlex(v) + } + + if (Array.isArray(v)) { + + this.data = v + this.batchDraw() + + if (options.sync) this.changed(options) + + } - if (options.sync) this.changed(options) }