From 867cd52c6aff907e5a8fc639d3327763ba45ab70 Mon Sep 17 00:00:00 2001 From: Brian Cappello Date: Sun, 9 Oct 2016 12:15:44 -0400 Subject: [PATCH] refactor rsi overbought/oversold lines --- src/plot/plot.js | 11 ++++++++--- src/plot/rsi.js | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/plot/plot.js b/src/plot/plot.js index 8c198771..ab263b53 100644 --- a/src/plot/plot.js +++ b/src/plot/plot.js @@ -238,15 +238,20 @@ module.exports = function(d3_svg_line, d3_svg_area, d3_line_interpolate, d3_sele appendPathsUpDownEqual: appendPathsUpDownEqual, - horizontalPathLine: function(accessor_date, x, accessor_value, y) { + horizontalPathLine: function(accessor_date, x, yValue, y) { return function(d) { if(!d.length) return null; + // "accessor_value" compatibility + if(typeof yValue === 'function') { + yValue = y(yValue(firstDatum)); + } + var firstDatum = d[0], lastDatum = d[d.length-1]; - return 'M ' + x(accessor_date(firstDatum)) + ' ' + y(accessor_value(firstDatum)) + - ' L ' + x(accessor_date(lastDatum)) + ' ' + y(accessor_value(lastDatum)); + return 'M ' + x(accessor_date(firstDatum)) + ' ' + yValue + + ' L ' + x(accessor_date(lastDatum)) + ' ' + yValue; }; }, diff --git a/src/plot/rsi.js b/src/plot/rsi.js index 013abeee..b00636ae 100644 --- a/src/plot/rsi.js +++ b/src/plot/rsi.js @@ -1,9 +1,13 @@ 'use strict'; module.exports = function(accessor_rsi, plot, plotMixin) { // Injected dependencies - return function() { // Closure function + return function(options) { // Closure function + if (!options) options = {}; var p = {}, // Container for private, direct access mixed in variables - rsiLine = plot.pathLine(); + rsiLine = plot.pathLine(), + overbought = options.overbought ? options.overbought : 70, + middle = options.middle ? options.middle : 50, + oversold = options.overbought ? options.overbought : 30; function rsi(g) { var group = p.dataSelector(g); @@ -17,7 +21,7 @@ module.exports = function(accessor_rsi, plot, plotMixin) { // Injected dependen } rsi.refresh = function(g) { - refresh(p.dataSelector.select(g), p.accessor, p.xScale, p.yScale, plot, rsiLine); + refresh(p.dataSelector.select(g), p.accessor, p.xScale, p.yScale, plot, rsiLine, overbought, middle, oversold); }; function binder() { @@ -32,9 +36,9 @@ module.exports = function(accessor_rsi, plot, plotMixin) { // Injected dependen }; }; -function refresh(selection, accessor, x, y, plot, rsiLine) { - selection.select('path.overbought').attr('d', plot.horizontalPathLine(accessor.d, x, accessor.ob, y)); - selection.select('path.middle').attr('d', plot.horizontalPathLine(accessor.d, x, accessor.m, y)); - selection.select('path.oversold').attr('d', plot.horizontalPathLine(accessor.d, x, accessor.os, y)); +function refresh(selection, accessor, x, y, plot, rsiLine, overbought, middle, oversold) { + selection.select('path.overbought').attr('d', plot.horizontalPathLine(accessor.d, x, overbought, y)); + selection.select('path.middle').attr('d', plot.horizontalPathLine(accessor.d, x, middle, y)); + selection.select('path.oversold').attr('d', plot.horizontalPathLine(accessor.d, x, oversold, y)); selection.select('path.rsi').attr('d', rsiLine); -} \ No newline at end of file +}