Home Ask Login Register

Developers Planet

Your answer is one click away!

ssm February 2016

associating plotly graphs and angular JS data

I am trying to create a plotly.js plot with angularJS.

This is the app.js ...

var app = angular.module('graphPlotterDemoApp', []);

app.controller('PlotCtrl1', function ($scope) {
    $scope.data = [{
        x: [1, 2, 3, 4],
        y: [10, 15, 12, 17]}];

app.directive('linePlot', function () {

    // Create a link function
    function linkFunc(scope, element, attrs) {
        scope.$watch('data', function (plots) {
            var layout = {
                'width': attrs.width,
                'height': attrs.height,
                'margin': { 't': 0, 'b':20, 'l':40, 'r':0 },

            Plotly.newPlot(element[0], plots, layout);

    // Return this function for linking ...
    return {
        link: linkFunc

And I have this html

<!DOCTYPE html>
<head lang="en">
    <title>Plotly Graph Plotter Directive for AngularJS - Demo</title>
    <script src="jquery.min.js"></script>
    <script src="angular.min.js"></script>
    <script src="plotly.min.js"></script>
    <script src="app.js"></script>
<body ng-app="graphPlotterDemoApp">

before div = PlotCtrl1
<div  ng-controller="PlotCtrl1">
    <tbody ng-repeat='x1 in data[0].x'>
            <td><input type='number' ng-model='data[0].x[$index]'></td>
            <td><input type='number' ng-model='data[0].y[$index]'></td>

    <line-plot width=300, height=200>


The plot appears, but it turns out that when I am changing the value within the input boxes, although the


Simon McClive February 2016

Try using $watchCollection or $watch with the objectEquality parameter set to true for complex objects. e.g $watch('data',function(){},true);

Post Status

Asked in February 2016
Viewed 2,343 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life