Search code examples
javascriptangularjsdrawangular-chartangular-chartist.js

Draw constant line in angular-chart.js


I need to draw a constant line in my chart. I'm using "angular-chart.js".

This is my HTML section

                <canvas 
                        id="line" 
                        class="chart chart-line"
                        chart-data="dataCPC"
                        chart-labels="labelsCPC" 
                        chart-series="seriesCPC"
                        chart-options="optionsCPC"
                        chart-dataset-override="datasetOverride"
                        >
                </canvas>

This is my angular code

  angular.module("app", ["chart.js"]).controller("LineCtrl", function 
   ($scope, $http) {
     var request = {
      method: 'get',
    url: 'asp/data.asp',
    dataType: 'json',
    contentType: "application/json"
   };
  $scope.arrLabelsACI = new Array;
  $scope.arrDataACI = new Array;
  $scope.dataACI = new Array;
  $scope.labelsACI = new Array;
   $http(request)
        .success(function (jsonData) {
            // LOOP THROUGH DATA IN THE JSON FILE.
            angular.forEach(jsonData, function (item) {
                //$scope.arrSeriesACI.push(item.TX_Total);
                $scope.arrDataACI.push(item.AVG);
                $scope.arrLabelsACI.push(item.HOUR);
            });
            // UPDATE SCOPE PROPERTIES FOR DATA.
            $scope.dataACI.push($scope.arrDataACI.slice(0));
            for (var i = 0; i < $scope.arrLabelsACI.length; i++) {
                $scope.labelsACI.push($scope.arrLabelsACI[i]);
            }
        })
        .error(function () {
        });
  $scope.datasetOverride = [{yAxisID: 'y-axis-1'}, {yAxisID: 'y-axis-2'}];
  $scope.optionsACI = {
    scales: {
        yAxes: [
            {
                id: 'y-axis-1',
                type: 'linear',
                display: true,
                position: 'left',
                suggestedMin: 0,
                ticks: {
                    min: 0
                }
            }
        ]
    }
 };

});

This is how it works right now

https://i.screenshot.net/2e9pyf0

This is what I need.

https://i.screenshot.net/30lg5sr

Thanks a lot


Solution

  • finally I found a solution

    add chartjs-plugin-annotation.js

    <script src="js/chartjs-plugin-annotation.js"></script>
    

    then this part and that's all.

    annotation: {
                   annotations: [{
                           id: 'a-line-1',
                           type: 'line',
                           mode: 'horizontal',
                           scaleID: 'y-axis-1',
                           value: '5',
                           borderColor: 'red',
                           borderWidth: 1
                       }
    

    This is the option code

     $scope.optionsACI = {
        scales: {
            yAxes: [
                {
                    id: 'y-axis-1',
                    type: 'linear',
                    display: true,
                    position: 'left',
                    suggestedMin: 0,
                    ticks: {
                        min: 0
                    }
                }
            ]
        },
        annotation: {
                annotations: [{
                        id: 'a-line-1',
                        type: 'line',
                        mode: 'horizontal',
                        scaleID: 'y-axis-1',
                        value: '5',
                        borderColor: 'red',
                        borderWidth: 1
                    }
    
        ]
                }
    
    
            };
    

    I hope this will help someone one day.