0

I'm having a strange error when creating charts with the code below.

The problem is that the series are not rendered until I resize the browser window.

Before resize: enter image description here

After resize: enter image description here

Code in html-template:

<article class="charts-wrapper">
  <single-series-chart data="vm.data"></single-series-chart>
</article>

Code in directive:

angular.module('app.cdn')
  .directive('singleSeriesChart', singleSeriesChart);

singleSeriesChart.$inject = ['_', 'Highcharts', 'ChartHelper'];

function singleSeriesChart(_, Highcharts, ChartHelper) {
  return {
    restrict: 'AE',
    scope: {
      title: '@',
      data: '=',
      metrics: '=',
      charttype: '=',
      showLoading: '='
    },
    template: '<section class="stackedCharts"><article class="stackedChart" id="volumeChart"></article><article class="stackedChart" id="viewsChart"></article><article class="stackedChart" id="hitsChart"></article></section>',
    link: function(scope, element, attrs) {
      var charts;

      function createChart() {
        Highcharts.setOptions({
          chart: {
            type: 'area'
          }
        });

        volumeChart = new Highcharts.Chart({
          chart: {
            renderTo: 'volumeChart'
          },
          title: {
            text: 'Distributed Volume'
          }

        });
        viewsChart = new Highcharts.Chart({
          chart: {
            renderTo: 'viewsChart',
          },
          title: {
            text: 'Asset Views'

          }

        });
        hitsChart = new Highcharts.Chart({
          chart: {
            renderTo: 'hitsChart'
          },
          title: {
            text: 'Number of Hits'
          }

        });
        charts = [volumeChart, viewsChart, hitsChart];
      }

      scope.$watch('data', function(newValue) {
        if (Object.keys(newValue).length === 3) {
          createChart();
          redrawSeries(newValue, charts);
        }

      }, true);

      function redrawSeries(newValue, charts) {
        for (var i = 0; i < charts.length; i++) {
          if (charts[i].series.length > 0) {
            charts[i].series[0].remove(false);
          }
          charts[i].addSeries({
            name: charts[i].title.textStr,
            color: ChartHelper.getColorByMetric(charts[i].title.textStr)
          }, true);

          _.each(newValue, function(data, index) {
            for (var j = 0; j < data.dataSeries.length; j++) {
              charts[i].series[0].addPoint([data.dataSeries[j].tValue, data.dataSeries[j].value], false, false);
            }
          });
        }

      }
    }
  };

}
Joel
  • 380
  • 3
  • 16
  • Could you create a plunker? – Vadim Gremyachev Oct 31 '16 at 10:29
  • maybe check [this SO post](http://stackoverflow.com/questions/16216722/highcharts-hidden-charts-dont-get-re-size-properly). I rather have experienced similar issues with google maps API with AngularJS. I suggest you try the solution window resize to rule out that this is not related to AngularJS. – Mahesh Oct 31 '16 at 14:15

0 Answers0