highcharts - Angular5 - 具有配置的响应式 Highcharts 不起作用
问题描述
我正在尝试Highcharts
使用Angular5
来自https://www.highcharts.com/docs/chart-concepts/responsive的响应式配置进行响应,如下所示:
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
legend: {
enabled: false
}
}
}]
}
我正在使用angular-highcharts
库来执行此操作typescript
。以下是我的代码,其响应式配置与网站上提到的完全相同Highcharts
:
import {Component, OnInit} from '@angular/core';
import {Chart} from 'angular-highcharts';
import * as Highcharts from 'highcharts';
@Component({
selector: 'historical-health-data',
templateUrl: './historical-health-data.component.html',
styleUrls: ['./historical-health-data.component.less']
})
export class HistoricalHealthDataComponent {
chart: Chart;
ngOnInit() {
this.chart = new Chart({
chart: {
type: 'column',
height: this.height,
style: {fontFamily: 'inherit'}
},
title: {
text: null
},
exporting: {
enabled: false
},
legend: {
enabled: false
},
credits: {
enabled: false
},
lang: {
noData: null
},
plotOptions: {
series: {
animation: true,
connectNulls: true,
marker: {
symbol: 'circle',
lineWidth: 1,
lineColor: '#fff'
}
},
column: {
states: {
hover: {
enabled: false
}
},
pointPadding: 0,
borderWidth: 0.1,
pointWidth: 20,
dataLabels: {
enabled: false
}
}
},
xAxis: {
type: 'datetime',
tickInterval: 24 * 3600 * 1000,
labels: {
rotation: -60
}
},
yAxis: {
min: 0,
title: {
text: null
}
},
series: [{
data: [{
x: Date.UTC(2012, 0, 1),
y: 1
}, {
x: Date.UTC(2012, 0, 8),
y: 3
}, {
x: Date.UTC(2012, 0, 15),
y: 2
}, {
x: Date.UTC(2012, 0, 22),
y: 4
}],
pointRange: 24 * 3600 * 1000
}],
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
legend: {
enabled: false
}
}
}]
}
});
});
}
}
历史健康数据.component.html:
<div [chart]="chart"></div>
通过完全按照Highcharts
文档中所述添加响应式配置:https ://www.highcharts.com/demo/responsive我收到以下错误:
error TS2345: Argument of type '{ chart: { type: string; height: number; style: { fo
ntFamily: string; }; events: { click: () => v...' is not assignable to parameter of type 'Options'.
Types of property 'responsive' are incompatible.
Type '{ rules: { condition: { maxWidth: number; }; chartOptions: { legend: { enabled: boolean; }; }; }[...' is not assignable to type 'ResponsiveOptions[]'.
Object literal may only specify known properties, and 'rules' does not exist in type 'ResponsiveOptions[]'.
我在这里做错了什么?有没有更好的方法来实现响应式图表?
我知道图表必须默认响应,但在我的情况下,这是当浏览器最小化为时 x 轴的行为方式< 700px
:
x 轴正在扩展并位于页面上的下一个面板下方。
这就是它必须是或类似的方式:
解决方案
我遇到了同样的问题,这是我的解决方案,希望对任何人都有帮助。它可能在大型数据集上存在性能问题。
ngOnInit() {
this.innerWidth = window.innerWidth;
this.chartOptions = {
chart: {
type: 'line',
height: 120,
width: this.innerWidth - 50
},.....
};
this.chart = new Chart(this.chartOptions);
}
直接换屏重画。
@HostListener('window:resize', ['$event'])
onResize(event) {
this.innerWidth = window.innerWidth;
this.chartOptions.chart.width = this.innerWidth - 50;
this.chart = new Chart(this.chartOptions);
}
推荐阅读
- firebase - 从多个数据库节点填充 Flutter ListView
- arrays - 正确设置循环/如果语句
- java - 关于程序世界生成的问题 - 我只能生成丘陵地形
- c# - AutoMapper:将嵌套对象映射到平面
- python-3.x - Python - Jira 使用 /rest/api/2 创建具有共享配置的项目(现有项目)
- c++ - 程序中的输入和输出
- c++ - 为什么我可以访问其他班级的私人成员?
- typescript - Vue.js 中奇怪的“无限更新循环”
- types - 不同 ocaml 模块中的相同类型不匹配
- angular - 如何在 Typescript 中的字符串中添加新行 - Angular 8