ng2-charts - Angular 4 个工作示例
ng2-charts - Angular 4 working example
我正在尝试将 ng2-charts 与 Angular 4.0 一起使用,我想通过使用 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>
我所做的是
npm install ng2-charts --save
npm install chart.js --save
我已经添加了
<script src="node_modules/chart.js/src/chart.js"></script>
到src/index.html
和
import { ChartsModule } from 'ng2-charts';
imports: [
ChartsModule
]
到src/app/app.module.ts
你能给我一个 src/index.html、src/app/app.component.html 和 app/component.ts 的工作示例吗?因为我在示例中遇到了一些问题
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.
即使我更改 chart.js 位置。
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 rel="icon" 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.component.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]
];
}
改变你的chart.js
到
<script src="node_modules/chart.js/dist/Chart.bundle.min.js"></script>
app模块ts代码
import { ChartsModule } from 'ng2-charts/ng2-charts';
// In your App's module:
imports: [
ChartsModule
]
更新
删除 index.html 中的脚本标签并添加
检查您的 angular-cli.json 和 scripts 数组,然后在其中添加:
"../node_modules/chart.js/dist/Chart.bundle.min.js"
除了将 chart.js
文件添加到 .angular-cli.json
之外,您只缺少作为输入传递给 baseChart
的 chartType
。
<canvas baseChart
[chartType]="'horizontalBar'"
[data]="chartData"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
从 'ng2-charts/ng2-charts' 导入 { ChartsModule};
// 在您应用的模块中:
进口:[
图表模块
]
我正在尝试将 ng2-charts 与 Angular 4.0 一起使用,我想通过使用 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>
我所做的是
npm install ng2-charts --save
npm install chart.js --save
我已经添加了
<script src="node_modules/chart.js/src/chart.js"></script>
到src/index.html
和
import { ChartsModule } from 'ng2-charts';
imports: [
ChartsModule
]
到src/app/app.module.ts
你能给我一个 src/index.html、src/app/app.component.html 和 app/component.ts 的工作示例吗?因为我在示例中遇到了一些问题
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.
即使我更改 chart.js 位置。
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 rel="icon" 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.component.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]
];
}
改变你的chart.js 到
<script src="node_modules/chart.js/dist/Chart.bundle.min.js"></script>
app模块ts代码
import { ChartsModule } from 'ng2-charts/ng2-charts';
// In your App's module:
imports: [
ChartsModule
]
更新 删除 index.html 中的脚本标签并添加
检查您的 angular-cli.json 和 scripts 数组,然后在其中添加:
"../node_modules/chart.js/dist/Chart.bundle.min.js"
除了将 chart.js
文件添加到 .angular-cli.json
之外,您只缺少作为输入传递给 baseChart
的 chartType
。
<canvas baseChart
[chartType]="'horizontalBar'"
[data]="chartData"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
从 'ng2-charts/ng2-charts' 导入 { ChartsModule};
// 在您应用的模块中: 进口:[ 图表模块 ]