【问题标题】:simple cubism.js with json datasource带有 json 数据源的简单 cubism.js
【发布时间】:2013-03-26 09:36:00
【问题描述】:

我正在寻找一种在 cubism.js 中使用 json 数据源的方法 这是我的代码:

<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,但我不知道如何使用,因为我是这个领域的新手。 谢谢

【问题讨论】:

标签: d3.js cubism.js


【解决方案1】:
    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?

它应该让你在如何做这件事上有一个良好的开端。

【讨论】:

  • 虽然这在理论上可以回答问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
  • 你的意思是我想添加一个代码 sn-p 来表明我在上面的链接中引用的答案和状态?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 2013-09-02
  • 2023-04-07
相关资源
最近更新 更多