html - 更新数据变化的echart
问题描述
我正在寻找一种解决方案来在新数据进入时更新 echart。目前我有一个图表和一个包含一些数据的下拉列表。当我打开页面时,数据在图表上显示得非常好。但是当我使用下拉菜单并将选项更改为下一个数据时,什么也没有发生。之前的数据还在图表上。任何想法如何在数据更改时更新图表(对象)?
我的代码:
chart1: EChartOption = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
legend: {
data: ['Tests Open','Tests Approved', 'Tests Failed']
},
toolbox: {
show: true,
feature: {
mark: { show: true },
magicType: { title: '1', show: true, type: ['line', 'bar',] },
restore: { title: 'Restore', show: true },
saveAsImage: { title: 'Save Chart',show: true }
}
},
xAxis: [
{
type: 'category',
axisTick: { show: false },
data: []
}
],
yAxis: [
{
type: 'value'
}
],
series: [
{
name: 'Tests Open',
type: 'bar',
data: [],
itemStyle: {
color: '#FDD051'
}
},
{
name: 'Tests Approved',
type: 'bar',
data: [],
itemStyle: {
color: '#2EAD6D'
}
},
{
name: 'Tests Failed',
type: 'bar',
data: [],
itemStyle: {
color:'#F0533F'
}
},
]
};
refreshChart(statistics: TestResultSiteStatistics) : void {
let months = [];
let open = [];
let approved = [];
let failed = [];
for (let month in statistics.monthly){
months.push(month);
approved.push(statistics.monthly[month].approved);
open.push(statistics.monthly[month].open);
failed.push(statistics.monthly[month].failed);
}
this.chart1.xAxis[0].data = months;
this.chart1.series[0].data = open;
this.chart1.series[1].data = failed;
this.chart1.series[2].data = approved;
}
<div #chart style="height:590px; width:1190px;" echarts [options]="chart1" ></div>
解决方案
您不能直接向实例添加数据,因为 Echarts 封装了复杂的逻辑来处理数据。你需要使用方法myChart.setOption({series: [new_data]})
。它在 API 文档中进行了解释:https ://echarts.apache.org/en/api.html#echartsInstance.setOption和https://echarts.apache.org/en/tutorial.html#Loading%20and%20Updating%20of% 20异步%20数据
推荐阅读
- php - 在每次 git 提交时运行 PHP_Codesniffer 和 PHPStan
- python - Spyder没有找到pytrends
- reactjs - Expo,React Native:URL 和刷新问题
- python - 直接在数据库上从 python 执行 SQL Server 存储过程
- javascript - 在javascript中重叠旋转图像
- python - 我对平行四边形图案有问题,它从正到负
- javascript - Mongo CLI 与客户端速度
- jsp - pageContext.request.contextPath 适用于一台服务器,但不适用于 .jsp 文件中的另一台服务器
- sql - 在 PostgreSQL 数组字段上具有“或”功能的无序模式/规则匹配(?)
- java - 如何遍历哈希图中的哈希集?