ng2-graphiques - Angulaire 4 exemple de travail

Je suis en train d'utiliser ng2-graphiques Angulaire 4.0 et je voudrais avoir la même chose, comme ici, en utilisant chart.js:

    ...
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script>
    ...

    <canvas id="myChart" width="400" height="400"></canvas>
    <script>
        var ctx = document.getElementById("myChart").getContext('2d');
        var chart = new Chart(ctx, {
            //The type of chart we want to create
            type: 'horizontalBar',

            //The data for our dataset
            data: {
                labels: ["example1","example2","example3","example4","example5","example6","example7"],
                datasets: [{
                    label: "My First dataset",
                    backgroundColor: 'rgb(255, 99, 132)',
                    borderColor: 'rgb(255, 99, 132)',
                    data: [0, 10, 5, 2, 20, 30, 45]
                },{
                    label: "My second dataset",
                    backgroundColor: 'rgb(2, 99, 132)',
                    borderColor: 'rgb(255, 99, 132)',
                    data: [3, 10, 5, 2, 20, 30, 45]
                }]
            },

            //Configuration options go here
            options: {
                scales: {
                    xAxes: [{
                        stacked: true
                    }],
                    yAxes: [{
                        stacked: true
                    }]
                }
            }
        });
    </script>

Ce que j'ai fait est

npm install ng2-charts --save
npm install chart.js --save

et j'ai ajouté

<script src="node_modules/chart.js/src/chart.js"></script>

à src/index.html

et

import { ChartsModule } from 'ng2-charts';

imports: [
   ChartsModule
]

à la src/app/app.le module.ts

Pouvez-vous me montrer un exemple de travail de src/index.html, src/app/app.component.html et app/composant.ts pour elle? Parce que j'ai quelques problèmes avec elle, comme dans l'exemple

GET 
http://localhost:4200/node_modules/chart.js/src/chart.js [HTTP/1.1 404 Not Found 8 ms]
ERROR Error: "undefined" is not a chart type.

même si je change l'chart.js emplacement.

src/index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Chart</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  type="image/x-icon" href="favicon.ico">

  <script src="../node_modules/chart.js/src/chart.js"></script>

</head>

<body>
  <app-root>Loading...</app-root>
</body>
</html>

src/app/app.component.html

<h1>
  {{title}}
</h1>

    <div style="display: block">
      <canvas baseChart
              [data]="chartData"
              (chartHover)="chartHovered($event)"
              (chartClick)="chartClicked($event)"></canvas>
    </div>
    <button (click)="randomize()">Update</button>

src/app/app.composante.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';

  public chartData: Array<any> = [
    [65, 59, 80, 81, 56, 55, 40],
    [28, 48, 40, 19, 86, 27, 90]
  ];

}
  • Essayez d'ajouter chartType="horizontalBar" à <canvas baseChart
  • Je peux voir quelque chose maintenant, au lieu de la page blanche, merci! Mais j'ai encore un problème avec GET localhost:4200/node_modules/chart.js/dist/Chart.bundle.min.js [HTTP/1.1 404 not Found 1 ms]
  • Si vous avez chart.js installé comme une dépendance, vous pouvez obtenir le bundle à partir de l'adresse @EA_ vous a donné. Sinon, à partir de node_modules/ng2-chart/node_modules/chart.js/dist/Chart.bundle.min.js
InformationsquelleAutor EA_ | 2017-06-14