如何使用 angular 7 缩放 chart.js 中的图表
How to zoom charts in chart.js using angular 7
我有一个 line-chart
,我想缩放它但无法缩放。这是我的代码
let ctx = document.getElementById('myChart')
let chart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [7, 10, 3, 5, 2, 3],
fill: true,
backgroundColor: 'orange',
borderColor: 'green',
pointBorderColor: 'red',
pointBackgroundColor: 'red'
}]
},
options: {
plugins: {
pan: {
enabled: true,
mode: 'x',
onPan: function () { console.log('I was panned!!!'); }
},
zoom: {
enabled: true,
drag: true,
mode: 'x',
onZoom: function () { console.log('I was zoomed!!!'); }
},
},
responsive: true,
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<div class="row">
<div class="col d-flex justify-content-center">
<canvas id="myChart" style="max-width:95%;height:300px !important;"></canvas>
</div>
</div>
我已将以下 plugin
用于 zooming
https://github.com/chartjs/chartjs-plugin-zoom#readme
我还在我的项目中包含了 hammerjs
和 angular/animations
。
更新
为什么 typescript 不知道 pan
和 zoom
?
// package.json
"chart.js": "^2.7.3",
"chartjs-plugin-zoom": "^0.6.6",
缩放选项应在 options
而不是 options.plugins
设置下配置。
options: {
pan: {
enabled: true,
mode: 'x',
},
zoom: {
enabled: true,
mode: 'x',
},
responsive: true
}
看到这个 fiddle -> http://jsfiddle.net/3sx8zon2/2/
首先你需要运行npm install chartjs-plugin-zoom
用npm安装
然后在您的 .ts 文件中执行 import chartjs-plugin-zoom
并像这样编写插件:
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'x'
},
zoom: {
enabled: true,
mode: 'x'
}
}
},
安装chart.js和chartjs-plugin-zoom
npm i chart.js -s
npm i chartjs-plugin-zoom -s
在Component.ts文件中导入图表和chartjs-plugin-zoom
import { Chart } from 'chart.js';
import 'chartjs-plugin-zoom';
加载图表的逻辑
let myChart = new Chart('mapId', {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'xy'
},
zoom: {
enabled: true,
mode: 'xy'
}
}
}
}
});
myChart.render();
在Component.html
<canvas id="mapId"></canvas>
我用过并且工作正常(Angular 12 - Chartjs 3.5.0):
1.
npm i chart.js -s
npm i chartjs-plugin-zoom -s
.2
import 'hammerjs';
import zoomPlugin from 'chartjs-plugin-zoom';
Chart.register(zoomPlugin);
render: Chart;
this.render = new Chart(ctx, {type: 'line',
data: {
labels: xxxxxxxx,
datasets: [
......
]
},
options: {
responsive: true,
interaction: {
mode: 'index',
intersect: false,
},
plugins: {
zoom: {
zoom: {
wheel: {
enabled: true,
},
pinch: {
enabled: true
},
mode: 'xy',
},
pan: {
enabled: true,
mode: 'xy',
},
}
}
},
}
我有一个 line-chart
,我想缩放它但无法缩放。这是我的代码
let ctx = document.getElementById('myChart')
let chart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [7, 10, 3, 5, 2, 3],
fill: true,
backgroundColor: 'orange',
borderColor: 'green',
pointBorderColor: 'red',
pointBackgroundColor: 'red'
}]
},
options: {
plugins: {
pan: {
enabled: true,
mode: 'x',
onPan: function () { console.log('I was panned!!!'); }
},
zoom: {
enabled: true,
drag: true,
mode: 'x',
onZoom: function () { console.log('I was zoomed!!!'); }
},
},
responsive: true,
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<div class="row">
<div class="col d-flex justify-content-center">
<canvas id="myChart" style="max-width:95%;height:300px !important;"></canvas>
</div>
</div>
我已将以下 plugin
用于 zooming
https://github.com/chartjs/chartjs-plugin-zoom#readme
我还在我的项目中包含了 hammerjs
和 angular/animations
。
更新
为什么 typescript 不知道 pan
和 zoom
?
// package.json
"chart.js": "^2.7.3",
"chartjs-plugin-zoom": "^0.6.6",
缩放选项应在 options
而不是 options.plugins
设置下配置。
options: {
pan: {
enabled: true,
mode: 'x',
},
zoom: {
enabled: true,
mode: 'x',
},
responsive: true
}
看到这个 fiddle -> http://jsfiddle.net/3sx8zon2/2/
首先你需要运行npm install chartjs-plugin-zoom
用npm安装
然后在您的 .ts 文件中执行 import chartjs-plugin-zoom
并像这样编写插件:
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'x'
},
zoom: {
enabled: true,
mode: 'x'
}
}
},
安装chart.js和chartjs-plugin-zoom
npm i chart.js -s
npm i chartjs-plugin-zoom -s
在Component.ts文件中导入图表和chartjs-plugin-zoom
import { Chart } from 'chart.js';
import 'chartjs-plugin-zoom';
加载图表的逻辑
let myChart = new Chart('mapId', {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'xy'
},
zoom: {
enabled: true,
mode: 'xy'
}
}
}
}
});
myChart.render();
在Component.html
<canvas id="mapId"></canvas>
我用过并且工作正常(Angular 12 - Chartjs 3.5.0):
1.
npm i chart.js -s
npm i chartjs-plugin-zoom -s
.2
import 'hammerjs';
import zoomPlugin from 'chartjs-plugin-zoom';
Chart.register(zoomPlugin);
render: Chart;
this.render = new Chart(ctx, {type: 'line',
data: {
labels: xxxxxxxx,
datasets: [
......
]
},
options: {
responsive: true,
interaction: {
mode: 'index',
intersect: false,
},
plugins: {
zoom: {
zoom: {
wheel: {
enabled: true,
},
pinch: {
enabled: true
},
mode: 'xy',
},
pan: {
enabled: true,
mode: 'xy',
},
}
}
},
}