javascript - 使用函数删除c3中的图形
问题描述
在以下函数中创建图表时,如何处理图表#.destroy():
function create(i){
chart = 'chart' + i
chart = c3.generate(...)
}
create(1)
create(2)
create(3)
以便它创建chart1、chart2、chart3。
我试图让一个按钮调用一个函数:clear_chart:
function clear_chart(){
for(i=1; i<4; i++){
chart = 'chart' +i
chart.destroy()
}
}
我收到一个错误:未捕获的类型错误:无法读取未定义的属性“销毁”
解决方案
This happens because you're not selecting a chart doing this:
chart = 'chart' +i
You can also remove the same line from the create method as is not doing anything on your code. You're assigning a value to the chart var and then reassigning it when generating the chart the name of the var is still "chart".
The easiest way of working with chart objects is storing them in an array and using them afterwards:
var charts = [];
function create(){
var chart = c3.generate(...);
charts.push(chart);
}
//call create function...
function clear_chart(){
for(i = 1; i < 4; i++){
var chart = charts[i - 1];
chart.destroy();
}
}
If you want to select just a specific one through their name, I suggest taking a look at jQuery selectors (if possible to add jQuery to your project) or d3 select functions.
推荐阅读
- python - 如何使 tkinter 标签(在列表框中选择)动态?
- webpack - 我应该如何找出哪些模块在我的 webpack 中创建了许多包?
- mysql - 使用 MySQL 空间索引,查询错误时出错:3037 Invalid GIS data provided to function st_intersects
- javascript - 如何在后端和前端具有不同子文件夹的代码沙箱中运行应用程序
- r - 取消嵌套数据框并使用 NA 填充新行
- android - 日期转换 24 小时和 12 小时 - Kotlin
- authentication - 在 Windows 终端中存储 SSH/telnet 连接的登录详细信息
- c# - 我们可以用 C# 和 C++ 等语言将数组的内容复制到另一个数组吗
- c# - 从 Xamarin.Forms.GoogleMaps 启动地图时,为什么我的应用程序会关闭?
- r - 安装 ggplot2 会导致错误“正在加载命名空间 'vctrs' 0.3.6,但需要 >= 0.3.8”)