2017-10-05 95 views
0

我實施在我的柱形圖滾動功能,但我無法設置程度屬性,因爲我的要求是顯示在x軸,而不是索引的時間爲[0, 1,2,...]。 代碼:設置在C3圖表程度屬性

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Occupancy</title> 
<link rel="stylesheet" type="text/css" href="css/c3.css"> 
<link rel="stylesheet" type="text/css" href="css/c3.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src = "js/c3.js"></script> 
<script src = "js/c3.min.js"></script> 
<script src = "js/d3.min.js"></script> 
<script src = "js/d3.js"></script> 
</head> 
<body> 
<div id="testDate1"></div> 
<script type="text/javascript"> 
var listDate = []; 
m = [ "Jan", "Feb", "Mar", "Apr", "May","Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]; 
var listDate = []; 
var startDate ='2017-04-12'; 
var endDate = '2017-07-06'; 
var dateMove = new Date(startDate); 
var strDate = startDate; 
while (strDate < endDate){ 
    var strDate = dateMove.toISOString().slice(0,10); 
    listDate.push(strDate); 
    dateMove.setDate(dateMove.getDate()+1); 
}; 

var val = [10,90,80,85,60,75,25,10,95,70,82,65,80,20,9,95,80,89,70,60,15,11,80,90,75,60,78,16,5,98,85,89,70,65,25,15,75,80,85,68,78,20,9,90,96,83,91,76,18,7,99,90,85,86,65,14,6,95,94,85,75,83,19,8,93,95,83,86,90,23,10,100,95,97,83,65,25,14,96,100,85,75,69,26,15,55,96,75,85,90,30,20,55]; 
var date = ['x'].concat(listDate); 
var occu = ['Occupancy'].concat(val);   

//Bar line Chart 
var axisLabel = "Occupancy(%)" 
var chart = c3.generate({    
    bindto : '#testDate1', 
    data: { 
     x: 'x', 
     columns: [date,occu], 
     type: 'bar' 
    }, 
    bar: { 
     /* width: { 
      ratio: 0.05 // this makes bar width 50% of length between ticks 
     }  */ 
     width: 9 
    }, 
    subchart: { 
     show: true 
    }, 
    axis: { 
     x: { 
      extent: [4/12, 5/24] 
     } 
    }, 
    axis : { 
     x: { 
       type: 'timeseries',      
       tick: { 
       /* count: 8, 
       format: '%m-%d'  */ 
       values: ['2017-04-12', '2017-04-19','2017-04-26','2017-05-03','2017-05-10','2017-05-17','2017-05-24','2017-05-31','2017-06-07','2017-06-07','2017-06-14','2017-06-21','2017-06-28','2017-07-06']  
      } 
      }, 
     y : { 
      label : { 
       text : axisLabel, 
       position : 'outer-middle' 
      }, 
      min : 10, 
      max : 100 
     }, 

    },    
    grid: { 
     x: { 
      show: true 
     }, 
     y: { 
      show: true 
     } 
    }, 
    color: { 
     pattern: ['#140e7f'] 
    } 
}); 

</script> 
</body> 
</html> 

像,在該小提琴:http://jsfiddle.net/y6tns4mt/1/

我已經設置的程度屬性作爲[-1,14]即滾動條的長度設置。 我想通過傳遞日期以實施。

回答

0

兩件事情:

  1. 您已經聲明軸的兩倍。不管在第一個(的程度)由以下聲明

  2. 你聲明的範圍覆蓋是一對數字,沒有日期。 4/12 = 0.333

因此,在你的代碼,你需要聲明如下軸:

axis : { 
     x: { 
      extent: ['2017-04-12', '2017-05-24'], 
       type: 'timeseries',      
       tick: { 
       /* count: 8, 
       format: '%m-%d'  */ 
       values: ['2017-04-12', '2017-04-19','2017-04-26','2017-05-03','2017-05-10','2017-05-17','2017-05-24','2017-05-31','2017-06-07','2017-06-07','2017-06-14','2017-06-21','2017-06-28','2017-07-06']  
      } 
      }, 
     y : { 
      label : { 
       text : axisLabel, 
       position : 'outer-middle' 
      }, 
      min : 10, 
      max : 100 
     }, 

    },