javascript - 如何销毁由同一原始函数构建的多个 C3 图

标签 javascript d3.js c3.js

我正在使用函数生成多个 C3 图。当我调用 Chart.destroy() 时,只有最近创建的图表被销毁。

另一种表达方式可能是:如何销毁所有图表并重置我的页面?

我正在使用通用函数来调用:

chart = c3.generate(...)

如果我调用函数 3 次并创建了 3 个图表,chart.destroy() 只会删除第 3 个图表,但会留下另外 2 个图表。

最佳答案

一种解决方案是使用唯一的变量名称生成图表,例如

var chart1 = c3.generate({...});
var chart2 = c3.generate({...});

然后您可以使用以下方法销毁图表:

chart = chart1.destroy();
chart = chart2.destroy();

我为您创建了一个示例 fiddle :http://jsfiddle.net/901jm783/ ,使用如下超时函数进行演示:

setTimeout(function () {
   chart = chart1.destroy();
   chart = chart2.destroy();
}, 5000);

编辑:下面是 fiddle 的完整代码,您可以将其作为可执行代码片段运行。请注意,我将每个图表绑定(bind)到不同的 div id。

var chart1 = c3.generate({
     bindto: '#c3_chart_1',
     padding: {
       top: 10,
       right: 70,
       bottom: 50,
       left: 75,
     },
     data: {
       columns: [
         ['data1', 100, 200, 150, 300, 200],
         ['data2', 200, 150, 25, 250, 100],
       ],
     },
});

var chart2 = c3.generate({
     bindto: '#c3_chart_2',
     padding: {
       top: 10,
       right: 70,
       bottom: 50,
       left: 75,
     },
     data: {
       columns: [
         ['data1', 100, 200, 150, 300, 200],
         ['data2', 200, 150, 25, 250, 100],
       ],
     },
});
  
// the following function will destroy both chart1 and chart2
setTimeout(function () {
  chart = chart1.destroy();
  chart = chart2.destroy();
}, 5000);
  
.c3 svg {
  font: 10px sans-serif
}

.c3 line,
.c3 path {
  fill: none;
  stroke: #000;
}

.c3 text {
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none
}

.c3-bars path,
.c3-event-rect,
.c3-legend-item-tile,
.c3-xgrid-focus,
.c3-ygrid {
  shape-rendering: crispEdges
}

.c3-chart-arc path {
  stroke: #fff
}

.c3-chart-arc text {
  fill: #fff;
  font-size: 13px
}

.c3-grid line {
  stroke: #aaa
}

.c3-grid text {
  fill: #aaa
}

.c3-xgrid,
.c3-ygrid {
  stroke-dasharray: 3 3
}

.c3-text.c3-empty {
  fill: gray;
  font-size: 2em
}

.c3-line {
  stroke-width: 1px
}

.c3-circle._expanded_ {
  stroke-width: 1px;
  stroke: #fff
}

.c3-selected-circle {
  fill: #fff;
  stroke-width: 2px
}

.c3-bar {
  stroke-width: 0
}

.c3-bar._expanded_ {
  fill-opacity: .75
}

.c3-target.c3-focused {
  opacity: 1
}

.c3-target.c3-focused path.c3-line,
.c3-target.c3-focused path.c3-step {
  stroke-width: 2px
}

.c3-target.c3-defocused {
  opacity: .3!important
}

.c3-region {
  fill: #4682b4;
  fill-opacity: .1
}

.c3-brush .extent {
  fill-opacity: .1
}

.c3-legend-item {
  font-size: 12px
}

.c3-legend-item-hidden {
  opacity: .15
}

.c3-legend-background {
  opacity: .75;
  fill: #fff;
  stroke: #d3d3d3;
  stroke-width: 1
}

.c3-tooltip-container {
  z-index: 10
}

.c3-tooltip {
  border-collapse: collapse;
  border-spacing: 0;
  background-color: #fff;
  empty-cells: show;
  -webkit-box-shadow: 7px 7px 12px -9px #777;
  -moz-box-shadow: 7px 7px 12px -9px #777;
  box-shadow: 7px 7px 12px -9px #777;
  opacity: .9
}

.c3-tooltip tr {
  border: 1px solid #CCC
}

.c3-tooltip th {
  background-color: #aaa;
  font-size: 14px;
  padding: 2px 5px;
  text-align: left;
  color: #FFF
}

.c3-tooltip td {
  font-size: 13px;
  padding: 3px 6px;
  background-color: #fff;
  border-left: 1px dotted #999
}

.c3-tooltip td>span {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px
}

.c3-tooltip td.value {
  text-align: right
}

.c3-area {
  stroke-width: 0;
  opacity: .2
}

.c3-chart-arcs-title {
  dominant-baseline: middle;
  font-size: 1.3em
}

.c3-chart-arcs .c3-chart-arcs-background {
  fill: #e0e0e0;
  stroke: none
}

.c3-chart-arcs .c3-chart-arcs-gauge-unit {
  fill: #000;
  font-size: 16px
}

.c3-chart-arcs .c3-chart-arcs-gauge-max,
.c3-chart-arcs .c3-chart-arcs-gauge-min {
  fill: #777
}

.c3-chart-arc .c3-gauge-value {
  fill: #000
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js">
</script>

<body>
<br><br>

<div id="c3_chart_1" style="width: 95%; height: 200px"></div>
<div id="c3_chart_2" style="width: 95%; height: 200px"></div>

关于javascript - 如何销毁由同一原始函数构建的多个 C3 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57009842/

相关文章:

javascript - jquery 中的多个 Ajax 请求卡住浏览器

javascript - d3 图表 + jQuery 数据表 : trouble reading nested array

javascript - 需要在子图选择上显示 X 轴标签

javascript - 使用主干调用保存时如何刷新模型的属性

javascript - React 列表的无限滚动

javascript - 递归 setTimeout 导致行为不一致

javascript - 使用 d3 和 React 的散点图

javascript - 从键盘写入 SVG 文本

javascript - 如何使 C3.js 样式更改仅应用于一个图表

javascript - 如何删除 c3.js 中的填充?