简体   繁体   English

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

[英]Using techanJS D3 examples with JSON data

I what to use a couple of the techanJS examples in my application but am struggling use my own data.我想在我的应用程序中使用几个 techanJS 示例,但正在努力使用我自己的数据。

Here is the example code for the close chart which I want to use.这是我要使用的收盘图表示例代码

I have a play!我有戏! framework application which passes in a List[CloseList] array, this is then converted to a JSON object using:传入 List[CloseList] 数组的框架应用程序,然后使用以下方法将其转换为 JSON 对象:

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

I am then assuming that I will be able to replace d3.csv() with d3.json() but cannot find a working example and my hacking hasn't got me anywhere so far.然后我假设我将能够用 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 ($)");
});

Working code here, this renders:这里的工作代码,这呈现:

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 ($)");
});

I you can stock your data in a json file, you have just to use the function我可以将数据存储在 json 文件中,您只需使用该功能

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

after, you have to change the name of the attributes in the code (the attirbutes on his csv are probably differents to your attributes in json)之后,您必须更改代码中的属性名称(他的 csv 上的属性可能与您在 json 中的属性不同)

callback, is usually a function but i don't think you have any other change to do in your code.回调,通常是一个函数,但我认为您在代码中没有任何其他更改。

Verify your json (use a website like http://jsonprettyprint.com/ ), test the code on your computer ( http://bl.ocks.org/andredumas/af8674d57980790137a0 ) with the default csv to see if it's work.验证您的 json(使用http://jsonprettyprint.com/ 之类的网站),使用默认 csv 在您的计算机上测试代码( http://bl.ocks.org/andredumas/af8674d57980790137a0 )以查看它是否有效。 It it's work, change to use json (like i told you).它的工作,改变使用json(就像我告诉你的那样)。

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

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