From 3db8c1a32d28838370385d7c99c3104e4ad996d7 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Mon, 6 Apr 2015 14:15:34 -0700 Subject: [PATCH] [Plot] Try multiple chart options Choose among multiple chart options, WTD-1070. --- platform/features/plot/src/Canvas2DChart.js | 2 +- platform/features/plot/src/MCTChart.js | 49 ++++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/platform/features/plot/src/Canvas2DChart.js b/platform/features/plot/src/Canvas2DChart.js index 251b6c49d9..244fe75ec3 100644 --- a/platform/features/plot/src/Canvas2DChart.js +++ b/platform/features/plot/src/Canvas2DChart.js @@ -87,7 +87,7 @@ define( } // ...and add points to it... - for (i = 2; i < buf.length - 1; i = i + 2) { + for (i = 2; i < points * 2; i = i + 2) { c2d.lineTo(x(buf[i]), y(buf[i + 1])); } diff --git a/platform/features/plot/src/MCTChart.js b/platform/features/plot/src/MCTChart.js index 43ebaa891f..da044fe39f 100644 --- a/platform/features/plot/src/MCTChart.js +++ b/platform/features/plot/src/MCTChart.js @@ -4,8 +4,8 @@ * Module defining MCTChart. Created by vwoeltje on 11/12/14. */ define( - ["./GLChart"], - function (GLChart) { + ["./GLChart", "./Canvas2DChart"], + function (GLChart, Canvas2DChart) { "use strict"; var TEMPLATE = ""; @@ -43,22 +43,38 @@ define( * @constructor */ function MCTChart($interval, $log) { + // Get an underlying chart implementation + function getChart(Charts, canvas) { + // Try the first available option... + var Chart = Charts[0]; + + // This function recursively try-catches all options; + // if these all fail, issue a warning. + if (!Chart) { + $log.warn("Cannot initialize mct-chart."); + return undefined; + } + + // Try first option; if it fails, try remaining options + try { + return new Chart(canvas); + } catch (e) { + $log.warn([ + "Could not instantiate chart", + Chart.name, + ";", + e.message + ].join(" ")); + + return getChart(Charts.slice(1), canvas); + } + } function linkChart(scope, element) { var canvas = element.find("canvas")[0], activeInterval, chart; - // Try to initialize GLChart, which allows drawing using WebGL. - // This may fail, particularly where browsers do not support - // WebGL, so catch that here. - try { - chart = new GLChart(canvas); - } catch (e) { - $log.warn("Cannot initialize mct-chart; " + e.message); - return; - } - // Handle drawing, based on contents of the "draw" object // in scope function doDraw(draw) { @@ -118,6 +134,15 @@ define( } } + // Try to initialize a chart. + chart = getChart([GLChart, Canvas2DChart], canvas); + + // If that failed, there's nothing more we can do here. + // (A warning will already have been issued) + if (!chart) { + return; + } + // Check for resize, on a timer activeInterval = $interval(drawIfResized, 1000);