繁体   English   中英

使用带有 JSON 数据的 techanJS D3 示例

[英]Using techanJS D3 examples with JSON data

我想在我的应用程序中使用几个 techanJS 示例,但正在努力使用我自己的数据。

这是我要使用的收盘图表示例代码

我有戏! 传入 List[CloseList] 数组的框架应用程序,然后使用以下方法将其转换为 JSON 对象:

var closesJSON = @{Html(new Gson().toJson(closes))};

然后我假设我将能够用 d3.json() 替换 d3.csv() 但找不到一个有效的例子,我的黑客到目前为止还没有让我在任何地方。

d3.csv("data.csv", function(error, data) {
    var accessor = close.accessor();

    data = data.slice(0, 200).map(function(d) {
        return {
            date: parseDate(d.Date),
            open: +d.Open,
            high: +d.High,
            low: +d.Low,
            close: +d.Close,
            volume: +d.Volume
        };
    }).sort(function(a, b) { return d3.ascending(accessor.d(a), accessor.d(b)); });

    x.domain(data.map(accessor.d));
    y.domain(techan.scale.plot.ohlc(data, accessor).domain());

    svg.append("g")
            .datum(data)
            .attr("class", "close")
            .call(close);

    svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);

    svg.append("g")
            .attr("class", "y axis")
            .call(yAxis)
            .append("text")
            .attr("transform", "rotate(-90)")
            .attr("y", 6)
            .attr("dy", ".71em")
            .style("text-anchor", "end")
            .text("Price ($)");
});

这里的工作代码,这呈现:

d3.json("/api/closedata/@equity.getId", function(error, data) {
    var accessor = close.accessor();

    data = data.map(function(d,i) {
        console.log(d.high);
        return {
            date : parseDate(d.closeDate),
            open : d.openPrice,
            high : d.high,
            low : d.low,
            close : d.closePrice,
            volume : d.volume
        }
    }).sort(function(a, b) { return d3.ascending(accessor.d(a), accessor.d(b)); });


    x.domain(data.map(accessor.d));
    y.domain(techan.scale.plot.ohlc(data, accessor).domain());

    svg.append("g")
            .datum(data)
            .attr("class", "close")
            .call(close);

    svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);

    svg.append("g")
            .attr("class", "y axis")
            .call(yAxis)
            .append("text")
            .attr("transform", "rotate(-90)")
            .attr("y", 6)
            .attr("dy", ".71em")
            .style("text-anchor", "end")
            .text("Price ($)");
});

我可以将数据存储在 json 文件中,您只需使用该功能

d3.json("file.json",callback);

之后,您必须更改代码中的属性名称(他的 csv 上的属性可能与您在 json 中的属性不同)

回调,通常是一个函数,但我认为您在代码中没有任何其他更改。

验证您的 json(使用http://jsonprettyprint.com/ 之类的网站),使用默认 csv 在您的计算机上测试代码( http://bl.ocks.org/andredumas/af8674d57980790137a0 )以查看它是否有效。 它的工作,改变使用json(就像我告诉你的那样)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM