【问题标题】:How to destroy multiple C3 graphs that were built from the same original function如何销毁从相同原始函数构建的多个 C3 图
【发布时间】:2019-11-22 09:23:22
【问题描述】:

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

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

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

chart = c3.generate(...)

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

【问题讨论】:

    标签: javascript d3.js c3.js


    【解决方案1】:

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

    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);
    

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

    【讨论】:

      猜你喜欢
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多