2

I am pretty new to angularjs and flot bar chart

I am trying to display filtered data in a list on click based on filters.

I have created a custom directive to plot the chart and a custom filter which I use with data gotten on the click method but the list is not displayed.

Please help take a look

http://jsfiddle.net/6h1gL2sh/22/

App.directive('chart', function () {
    return {
        restrict: 'EA',
        link: function (scope, elem, attrs) {
            var chart = null,
                options = {
                    series: {
                        stack: true,
                        bars: {
                            show: true,
                            clickable: true,
                            barWidth: 0.1,
                            align: "center"
                        }
                    },
                    axisLabels: {
                        show: true
                    },

                    xaxis: {
                        axisLabel: 'Test Dates',
                        axisLabelUseCanvas: true,
                        axisLabelFontSizePixels: 12,
                        axisLabelPadding: 5,
                        mode: "categories",
                        tickLength: 0
                    },
                    yaxis: {
                        axisLabel: 'Pass/Fail Count',
                        axisLabelUseCanvas: true,
                        axisLabelFontSizePixels: 12,
                        axisLabelPadding: 5

                    },
                    grid: {
                        hoverable: true,
                        clickable: true
                    }

                }
            var data = scope[attrs.ngModel];

            var getKeyByValue = function (obj, value) {
                for (var prop in obj) {
                    if (obj.hasOwnProperty(prop)) {
                        if (obj[prop] === value) return prop;
                    }
                }
            }


            scope.$watch(attrs.ngModel, function (v) {
                if (!chart) {
                    chart = $.plot(elem, v, options);
                    elem.show();
                } else {
                    chart.setData(v);
                    chart.setupGrid();
                    chart.draw();
                }
            });
            elem.bind("plotclick", function (event, pos, item) {
                if (item) {
                    scope.show = false
                    scope.plotdate = getKeyByValue(item.series.xaxis.categories, item.dataIndex);
                    switch (item.series.label) {
                        case 'Passed':
                            scope.details = scope.arr.passdetails;
                            break;
                        case 'Failed':
                            scope.details = scope.arr.faildetails;
                            break;
                            scope.$apply();
                    }
                }
                console.log('details= ', scope.details)
                console.log('Plotdate= ', scope.plotdate)
                console.log('Show= ', scope.show)


            });


        }
    }

});
App.filter('getdata', function () {

    return function (items, date) {

        var arrayToReturn = [];
        for (var i in items) {
            if (items[i].start_date == date) {
                arrayToReturn.push(items[i]);
            }
        }

        return arrayToReturn;
    };
});
6
  • stackoverflow.com/help/mcve (look for the m) Commented Apr 13, 2015 at 17:37
  • @Raidri I do not understand your comment Commented Apr 13, 2015 at 19:35
  • You should build a minimal code example. Your fiddle has over 1000 lines of JS and a lot of CSS, I have not enough time to read all of that. Commented Apr 13, 2015 at 19:52
  • I have minimized the code. Commented Apr 13, 2015 at 20:07
  • @Raidri, I have minimized the code Commented Apr 14, 2015 at 7:20

1 Answer 1

1

I finally fixed it. I was applying scope.$apply() in my case statement block.

I was also calling my expressions in a div outside the div that specifies the controller

See js Fiddle http://jsfiddle.net/6h1gL2sh/24/

 elem.bind("plotclick", function (event, pos, item) {
            if (item) {
                scope.show=false
                scope.plotdate= getKeyByValue(item.series.xaxis.categories, item.dataIndex);
                scope.stats=item.series.label
                switch (scope.stats){
                case 'Passed':
                    scope.details = scope.arr.passdetails;
                    break;
                case 'Failed':
                    scope.details = scope.arr.faildetails;
                    break;

                }
            }
            scope.$apply();
            console.log('details= ', scope.details)
             console.log('Plotdate= ', scope.plotdate)
              console.log('Show= ',scope.show)


        });


    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good, the first error I had also found, but not the second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.