ng2-charts: Chart not showing using Angular2 RC1 in CLI project

I’m having trouble integrating ng2-charts into a CLI project. After spending hours trying to use charts in a CLI project, I finally got it integrated and it builds/runs without errors, but the graph doesn’t show up. I can see that the directive is getting rendered (there’s a <canvas> element within the base-chart element), but it’s all empty.

I took the following steps:

  • Created a new project (named dependencies) using ng new dependencies

  • Followed the installation instructions for ng2-charts, i.e., executed npm install ng2-charts --save npm install chart.js --save and added a script reference to the index.html (<script src="vendor/chart.js/dist/Chart.min.js"></script>)

  • Adjusted the angular-cli-build.js to copy the npm files into the dist directory:

    vendorNpmFiles: [ … ‘chart.js/dist/Chart.min.js’, ‘ng2-charts/*/.js’ ]

  • Adjusted the system.config.js in order to map the name ‘ng2-charts’ to the respective vendor directory and the package name ng2-charts to it’s main file.

    const map: any = { ‘ng2-charts’: ‘vendor/ng2-charts’ }; /** User packages configuration. */ const packages: any = { ‘ng2-charts’: { main: ‘bundles/ng2-charts.min.js’
    }
    };

  • Copied the ts code/html from the line chart example (http://valor-software.com/ng2-charts/)

Anyone knows the issue or has a project using Angular2 CLI with ng2-charts integrated? I uploaded the code to https://github.com/thorstenschaefer/dependencies. All changes from the newly created CLI project can be seen here: https://github.com/thorstenschaefer/dependencies/commit/8169b429d37b72bb9e8718b76d27c258602d1c07

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (4 by maintainers)

Most upvoted comments

Hello thorstenschaefer, I’m facing the exact same issue and I can’t figure out why. 😦

  • Edit * Just tested this in my CSS: .chart {display: block; width: 100%;}

It works… strangely.

I am using the charts in an Ionic 2 project (based on Angular 2). To make them work I had to insert the following in app.ts:

import ‘…/node_modules/chart.js/dist/Chart.bundle.min.js’;

It’s supposedly not recommended referencing files this way, but it’s what made it work in the end (after many tries).

@Warkdev Thanks a lot man. Setting the CSS worked for me too. Just the style attribute { display:block } is enough in my case.

After searching a bit more, it looks to me that this is similar to https://github.com/valor-software/ng2-charts/issues/53 which was marked as fixed. Shouldn’t the base-chart component have the display style set to blocked by default?

I use an Ionic 2.

I have tried:

  1. .chart {display: block; width: 100%;}
  2. .chart {display: block;}
  3. import ‘…/node_modules/chart.js/dist/Chart.bundle.min.js’;

No success…

image

.chart {display: block; width: 100%;} It works for me…I spent 5 hrs to resolve this…thanks…

Agreed, I will do