示例字符串输出
[{v:5, f:'Week 1'}, {v:10, f:'Week 2'},{v:15, f:'Week 3'},{v:20, f:'Week 4'},{v:25, f:'Week 5'}]
段
function weekAxis(){
var ticks = '';
var week_count = <?php echo $chart ?>; //count number of weeks
for(i=0; i<=week_count; i++){
var tick_str[] = '{v:'+week_count, f:'Week' +week_count}';
}
return ticks;
}
基于周数的所需输出
如果week_count
= 5
,
var ticks = [{v:5, f:'Week 1'}, {v:10, f:'Week 2'},{v:15, f:'Week 3'},{v:20, f:'Week 4'},{v:25, f:'Week 5'}]
如果week_count
= 4
,
var ticks = [{v:4, f:'Week 1'}, {v:8, f:'Week 2'},{v:12, f:'Week 3'},{v:16, f:'Week 4'}]
如果week_count
= 6
,
var ticks = [{v:6, f:'Week 1'}, {v:12, f:'Week 2'},{v:18, f:'Week 3'}, {v:24, f:'Week 4'}, {v:30, f:'Week 5'}, {v:36, f:'Week 6'}]
如所观察到的,v
的值与计数本身相乘,并将循环直至达到周计数。同样,Week
文本将增加计数器值。
如何根据周数计算上面的字符串?
答案 0 :(得分:0)
试试这个:你可以初始化一个数组并将地图推入其中。在for循环中,使用count和i
变量创建地图,如下所示。
function weekAxis(){
var ticks = new Array();
var week_count = 4; //count number of weeks
for(i=1; i<=week_count; i++){
var tick_str = {};
tick_str['v'] = ( week_count * i );
tick_str['f'] = 'Week ' + i ;
ticks.push(tick_str);
}
return ticks;
}
alert(JSON.stringify(weekAxis()));
&#13;
答案 1 :(得分:0)
这应该可以解决问题
function weekAxis(){
var ticks = '',week_count = 5,tick_str = [];
for(i=0; i<week_count; i++){
var ex = {
v:week_count*(i+1),
f:'Week'+week_count
};
tick_str.push(ex);
}
return JSON.stringify(tick_str);
}