1

So I know that JQuery is used often with highcharts, but for my purposes I am not using it. I have implemented highcharts into angular using just javascript and now I am trying to populate the series data for the chart using a .json file. For some reason when I try to load the chart within a http.get call (to read the json file), the chart no longer displays. I can't figure out why. Any insight on why this might happen?

http.get('data.json').success(function(data) {

 $scope.chartOptions =  {
   chart: { type: 'line' },
   title: { text: 'Fruit Consumption' },
   xAxis: { categories: ['Apples', 'Bananas', 'Oranges'] },
   yAxis: { title: { text: 'Fruit eaten' } },
   series: [
      { name: 'Jane', data: [1, 0, 4] }, 
      { name: 'John', data: [5, 7, 3] }
   ]
 };
});

app.directive("highcharts", function() {
  return {
    link: function(scope, element, attrs) {
    var options = scope.$eval(attrs.highcharts);
    options.chart.renderTo = element[0];
    new Highcharts.Chart(options);
  }
};

});

1
  • So the problem seems to be that options is undefined since this is occurring asynchronously and highcharts hasn't been populated by the time it reaches that line. Any idea how to solve this? Commented Jun 26, 2014 at 23:58

2 Answers 2

1

In your chart tag, add an ng-if="chartOptions" so that it wont load before that object exists.

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

Comments

0

Other than of course radically changing your build and adding requireJs I'd set up a loop to poll and check every xxx unit of time until the function is in the dom:

//I would use a rather ugly checker 
var timeFrame = 6000, //some preiod of time
    timer = setInterval(function () { checkForFunction(timeFrame) }, );

function checkForFunction() {
    if(Highcharts){ //if Highcharts is in the dom it has been loaded
       //now do the stuff you want
       clearInterval(timer);
    }
}

Comments

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.