我正在使用一个函数生成多个C3图。当我调用chart.destroy()时,只会破坏最近创建的图表。

这种说法的另一种方式可能是:如何销毁所有图形并重置页面?

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

chart = c3.generate(...)


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

最佳答案

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

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


然后,您可以使用以下方法破坏图表:

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


我为您创建了一个小提琴示例:http://jsfiddle.net/901jm783/,它使用以下超时功能进行演示:

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


编辑:下面是小提琴的完整代码,您可以将其作为可执行代码段运行。请注意,我将每个图表绑定到不同的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/

10-10 00:51