2013-03-26 57 views
1

我正在寻找一种方式,使用cubism.js 这里JSON数据源是我的代码:使用JSON数据源简单cubism.js

<script> 
var context = cubism.context() 
    .serverDelay(0) 
    .clientDelay(0) 
    .step(1e3) 
    .size(960); 


var dataset = [ [-1, -3], [2, 4], [3, -4], [-3, 1]]; 
//var foo = random("foo"); 
var foo = context.metric(function(start, stop, step, callback) { 
    d3.json(dataset, function(data) { 
    if (!data) return callback(new Error("unable to load data")); 
    callback(null, data); 
    }); 
}); 
var bar = random("bar"); 

d3.select("#kpi2").call(function(div) { 

    div.append("div") 
     .attr("class", "axis") 
     .call(context.axis().orient("top")); 

    div.selectAll(".horizon") 
     .data([foo]) 
    .enter().append("div") 
     .attr("class", "horizon") 
     .call(context.horizon().extent([-20, 20])); 

    div.append("div") 
     .attr("class", "rule") 
     .call(context.rule()); 

}); 

context.on("focus", function(i) { 
    d3.selectAll(".value").style("right", i == null ? null : context.size() - i + "px"); 
}); 

</script> 

我听说过使用context.metric但我不知道如何使用,因为这是一个新手。 感谢

+0

你看过这个链接吗? http://stackoverflow.com/questions/10526058/using-other-data-sources-for-cubism-js?rq=1 – 2013-04-19 05:44:39

回答

-2
function stock(name) { 
    return context.metric(function(start, stop, step, callback) { 
     d3.json("/data" + name + ".json", function(rows) { 
      var compare = rows[0][1], value = rows[0][1], values = [value]; 

      // Creates an array of the price differences throughout the day 
      rows.forEach(function(d) { 
       values.push(value = (d[1] - compare)/compare); 
      }); 
     callback(null, values); }); 
    }, name); 
} 

你可以看看这个例子了解更多详情:http://www.kshitijaranke.com/author/

Can we use custom JSON Data on Cubism?

它应该给你一个良好的开端就如何做到这一点。

+0

虽然这可能在理论上回答这个问题,[这将是优选的](/ /元.stackoverflow.com/q/8259)在这里包含答案的基本部分,并提供供参考的链接。 – Tunaki 2016-01-31 11:18:46

+0

你的意思是我想添加一个代码片段来表明我作为答案并说明我在右上方的链接引用了吗? – edwin 2016-02-01 02:03:37