Darth Ionic
Darth Ionic

Reputation: 103

Ionic 2 - ng2-chartjs2 working on browser but not in device

I have managed to implement ng2-chartjs2 in my ionic2 application. It is perfectly working when i run it on browser (i.e ionic serve or ionic run android -l -c) but when i tried it on device (i.e ionic run android), it simply shows a blank page.

I have uploaded the working sample project in my repo here

Any help would be appreciated. Thanks.

Still expecting someone to clear this issue.

Upvotes: 1

Views: 329

Answers (1)

dlcardozo
dlcardozo

Reputation: 4013

Well the issue here is that the Chart.bundle.js isn't on the expected path on android build.

Add this:

var availablePlatforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []);

var filestocopy = [
  {"src/assets/libs/Chart.bundle.js": "platforms/android/assets/libs/Chart.bundle.js"}
];

for(var x=0; x<availablePlatforms.length; x++) {

  var currentPlatform = availablePlatforms[x].trim().toLowerCase();

  if (currentPlatform == 'android') {
    filestocopy.forEach(function(obj) {
      Object.keys(obj).forEach(function(key) {
        var val = obj[key];
        var srcfile = path.join(rootdir, key);
        var destfile = path.join(rootdir, val);
        var destdir = path.dirname(destfile);
        if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {
          fs.createReadStream(srcfile).pipe(
              fs.createWriteStream(destfile));
        }
      });
    });

  }
}

At the end of your hooks -> after_prepare -> 010_add_platform_class.js, then change the script reference inside of your index.html to <script src="assets/libs/Chart.bundle.js"></script> (note: changed chart for Chart).

Good luck!

Upvotes: 2

Related Questions