2014-09-19 150 views
1

是否可以按降序排列x軸?自然c3排序x軸順序。我的圖如下所示。如何按降序對x軸排序

jQuery(function($) { 
 
    var chart = c3.generate({ 
 
    bindto: '#chart', 
 
    data: { 
 
     x: 'x', 
 
     columns: [ 
 
     ['x', 20, 19, 18, 17, 16, 15, 14, 13, 12, 11], 
 
     ['data1', 180, 232, 250, 289, 424, 473, 414, 428, 590, 600] 
 
     ], 
 
     axes: { 
 
     data1: 'y2' 
 
     }, 
 
     regions: { 
 
     'data1': [{start: 16, style: 'dashed'}] 
 
     } 
 
    }, 
 
    axis: { 
 
     y: { 
 
     show: false 
 
     }, 
 
     y2: { 
 
     show: true 
 
     } 
 
    } 
 
    }); 
 
});
<link href="http://cdnjs.cloudflare.com/ajax/libs/c3/0.3.0/c3.min.css" rel="stylesheet"/> 
 

 
<div id="chart"></div> 
 

 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<script src="http://cdnjs.cloudflare.com/ajax/libs/c3/0.3.0/c3.min.js"></script>

我希望圖形x軸是完全按照我的X系列一樣的降序排列。任何幫助將不勝感激。

回答

4

是的我想出瞭如何實現這一目標。只需給出負x值,c3將按降序繪製x軸。然後格式化標籤,使其顯示正值。

jQuery(function($) { 
 
    var chart = c3.generate({ 
 
    bindto: '#chart', 
 
    data: { 
 
     x: 'x', 
 
     columns: [ 
 
     ['x', -20, -19, -18, -17, -16, -15, -14, -13, -12, -11], 
 
     ['data1', 180, 232, 250, 289, 424, 473, 414, 428, 590, 600] 
 
     ], 
 
     axes: { 
 
     data1: 'y2' 
 
     }, 
 
     regions: { 
 
     'data1': [{start: -16, style: 'dashed'}] 
 
     } 
 
    }, 
 
    axis: { 
 
     y: { 
 
     show: false 
 
     }, 
 
     y2: { 
 
     show: true 
 
     }, 
 
     x: { 
 
     tick: { 
 
      format: function (x) { 
 
      return -x; 
 
      } 
 
     } 
 
     } 
 
    } 
 
    }); 
 
});
<link href="http://cdnjs.cloudflare.com/ajax/libs/c3/0.3.0/c3.min.css" rel="stylesheet"/> 
 

 
<div id="chart"></div> 
 

 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<script src="http://cdnjs.cloudflare.com/ajax/libs/c3/0.3.0/c3.min.js"></script>

+0

感謝,即反轉串行圖表(條形圖,線等) – Eugenio 2017-10-24 14:26:42

+0

https://github.com/c3js/c3/issues/ x軸默認順序的獨特方式265 – Eugenio 2017-10-24 14:28:15