如何根据angularjs中的值更改标记符号

时间:2017-10-25 14:53:05

标签: javascript angularjs highcharts

我使用angularjs绘制高清数据。我想根据具体价值自定义标记符号 这是代码

/ Directive Highchart  
        App.directive('hcChart', function ($parse) {  
    'use strict';  
    return {  
        restrict: 'E',  
        replace: true,  
        template: '<div><div class="chart"></div></div></div>',  
        link: function (scope, element, attrs) {  
            attrs.chart = new Highcharts.chart(element[0], {  

                xAxis: {
                    title: {
                        text: "Date"
                    }
                },
                yAxis: {
                    title: {
                        text: "Value"
                    }
                },
                dataLabels: {
                    enabled: true
                }
                ,
                title: {
                    text: ""
                },
                chart: {
                    opacity: 0.51,
                    backgroundColor: "#FFFFFF",
                    plotBackgroundColor: 'rgba(0, 0, 0, 0.7)',
                     width: 1600,
                    height: 800,
                    zoomType: 'xy',
                    resetZoomButton: {
                        position: {
                            x: -140,
                            y: 0
                        }
                    }
                },
                tooltip: {
                    crosshairs: {
                        width: 2,
                        color: 'black',
                        dashStyle: 'shortdot'
                    }
                }
                ,
                exporting: {
                    buttons: {
                        contextButton: {
                            enabled: false
                        },
                        exportButton: {
                            text: 'Download',
                            tooltip: 'Download',
                            menuItems: Highcharts.getOptions().exporting.buttons.contextButton.menuItems.splice(2)
                        },
                        printButton: {
                            text: 'Print',
                            onclick: function () {
                                this.print();
                            }
                        }
                    }
                }
            });
        Scope to watch categories
            scope.$watch(function () {
                return attrs.categories;
            }, function () {
                if (attrs.chart.xAxis.length === 0) {
                    attrs.chart.addAxis($parse(attrs.categories)(scope));
                } else {
                    attrs.chart.xAxis[0].setCategories($parse(attrs.categories)(scope));
                }
            });
  scope to watch series
            scope.$watch(function () {
                return attrs.series;
            }, function () {
                var i;
                for (i = 0; i < $parse(attrs.series)(scope).length; i++) {

                    if (attrs.chart.series[i]) {
                        attrs.chart.series[i].setData($parse(attrs.series)(scope)[i].data);
                    } else {
                        attrs.chart.addSeries($parse(attrs.series)(scope)[i]);
                    }
                }
                // remove series if new array passed is smaller
                if (i < attrs.chart.series.length - 1) {
                    var seriesLength = attrs.chart.series.length - 1;
                    for (j = seriesLength; j > i; j--) {
                        attrs.chart.series[j].remove();
                    }
                }
            });
        }
    };
}).controller('IndicatorAnalyticsDashboardController', function ($scope, $filter, $http, $timeout) {

    $scope.dateBegin = moment().subtract(17, "days");
    $scope.dateEnd = moment();
    $scope.aggregationSelected = "hourly";
    $http({

    })
            .then(function (listIndicatorsResult) {
                $scope.indicators = listIndicatorsResult.data;
                $scope.idIndicatorSelected = listIndicatorsResult.data[3];
                $scope.idIndicatorChanged();
            });
// Chart series default view-------------------------------------------

    $scope.myCategories = [];
    var opacity = 1;
    $scope.mySeries = [

        {
            type: "area",
            name: 'red',
            lineWidth: 0,
            fillOpacity: 0.65,
            color: '#FF0000',
            data: [],
            marker: {
                enabled: false
            }
        },
        {
            type: "area",
            name: 'amber',
            fillOpacity: opacity,
            lineWidth: 0,
            color: '#e7cd2c',
            data: [],
            marker: {
                enabled: false
            }
        },
        {
            type: "area",
            name: 'green',
            fillOpacity: 1,
            lineWidth: 0,
            color: '#00ad02',
            data: [],
            marker: {
                enabled: false
            }
        },
        {
            type: "area",
            name: 'light green',
            fillOpacity: opacity,
            lineWidth: 0,
            color: '#66cd67',
            data: [],
            marker: {enabled: false}
        },
        {
            type: "spline",
            name: 'Actual',
            color: "black",
            lineWidth: 3.2,
            data: [],

我试过这个来更新标记

     marker: {
                    enabled: true,
                    symbol: this.y > 40 ? "square" : "triangle"
                }
            },
            {
                type: "spline",
                name: 'Moving Median',
                data: [],
                color: "#00f6ff",
                visible: false,

                marker: {
                    symbol: 'triangle',
                    width: 16,
                    height: 16
                }
            }
        ];
//and inside function series has been update using:


    //                    // Assign values
                        $scope.myCategories = $scope.dateData;
                        $scope.mySeries[4].data = $scope.actual;
    //                    $scope.medianValue = $scope.median.map(Number);
                        $scope.mySeries[5].data = $scope.median;
                        $scope.mySeries[0].data = $scope.red;
                        $scope.mySeries[1].data = $scope.amber;
                        $scope.mySeries[2].data = $scope.green;
                        $scope.mySeries[3].data = $scope.lightGreen;
    //                    $scope.mySeries[4].marker = {
    //                        enabled: true,
    //                        symbol: this.x > 4 ? 'circle' : 'square'
    //                    };
                        console.log($scope.mySeries[4].marker.symbol);
                        $scope.mySeries[3].marker.enabled = "true";

有人能建议如何根据angularjs中的某些条件更新特定点的标记符号吗?我已经尝试在angularjs控制器中添加内部highchart指令和更新功能。

2 个答案:

答案 0 :(得分:1)

您可以使用render事件更新标记:

var redrawEnabled = true;

(...)

  chart: {
    events: {
      render: function() {

        if (redrawEnabled) {
          redrawEnabled = false;
          this.series[0].points.forEach(function(p) {
            p.update({
              marker: {
                symbol: p.y > 100 ? "square" : "triangle"
              }
            }, false);
          });

          this.redraw();

          redrawEnabled = true;
        }
      }
    }
  }

我迭代所有点并根据点的值设置符号。 redrawEnabled标志用于防止无限递归循环 - redraw函数调用render事件。

实时工作示例: https://jsfiddle.net/kkulig/fw442v4b/

API参考:

答案 1 :(得分:0)

我们可以使用Angular foreach函数根据控制器内的某些条件更改点的标记符号...

 angular.forEach($scope.array, function (value, i) {

                    $scope.actual[i] = {
                        y: value.VariableName,
                        marker: someCondition ? {
                            symbol: 'url',
                            width: 23,
                            height: 23}})