使用d3库从json文件中读取?

时间:2017-05-19 18:40:08

标签: javascript html json d3.js anychart

我有两个不同的图表使用相同的确切数据。一个使用嵌入在html中的json,而另一个读取文件。

我一直在谷歌上搜索我是否找到anychart从json文件或网络服务中读取的示例。我一无所获。

由于第二个图表使用json调用来读取d3.json数据,我认为可能使用相同的函数来读取anychart中的数据1}}图表。

所以我在两个图表中都包含javascript

anychart javascript

<script type="text/javascript">
    anychart.onDocumentReady(function() {
      chart = anychart.fromJson(
        {chart: {type: "line",
          series:[{seriesType: "spline",
    data: [{x: "January", value: 10000},{x: "February", value: 12000},{x: "March", value: 18000}]}],                            
          container: "container"}} 
      ).draw();                             
    });     
</script>

d3json文件中读取该文件。也许我可以使用此调用来填充上一代码中的报告?:

d3.json("data.json", function(error, data) {
  var nest = d3.nest()
    .key(function(d) {
      return d.date;
    })
    .map(data);

    rect.filter(function(d) {
      return ("$" + d) in nest;
    })
    .attr("fill", function(d) { 
      return color(nest[("$" + d)][0].open);
    })
});

感谢任何帮助。我只是想知道这是否可行。

1 个答案:

答案 0 :(得分:0)

https://api.anychart.com/7.13.1/anychart.data#loadJsonFilehttps://api.anychart.com/7.13.1/anychart#fromJsonFile方法,它们是数据适配器脚本的一部分。

以下是样本:

<!doctype html>
<html>
  <head>
    <script src="https://cdn.anychart.com/js/7.13.1/anychart-bundle.min.js"></script>
    <script src="https://cdn.anychart.com/js/latest/data-adapter.min.js"></script>
    <link rel="stylesheet" href="https://cdn.anychart.com/css/7.13.1/anychart-ui.min.css" />
    <style>
      html, body, #container {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script>

anychart.onDocumentReady(function () {
    // To work with the data adapter you need to reference the data adapter script file from AnyChart CDN
    // (https://cdn.anychart.com/js/latest/data-adapter.min.js for latest or https://cdn.anychart.com/js/7.11.1/data-adapter.min.js for the versioned file)

    // Load JSON data and create a chart by JSON data.
    anychart.data.loadJsonFile("https://cdn.anychart.com/charts-data/data_json.json", function (data) {

        chart = anychart.bar(data);

        chart.title("Load JSON data and create a chart");
        chart.container("container");
        chart.draw();
    });
});

    </script>
  </body>
</html>

https://playground.anychart.com/api/7.13.1/modules/anychart.data.loadJsonFile-plain

<!doctype html>
<html>
  <head>
    <script src="https://cdn.anychart.com/js/7.13.1/anychart-bundle.min.js"></script>
    <script src="https://cdn.anychart.com/js/latest/data-adapter.min.js"></script>
    <link rel="stylesheet" href="https://cdn.anychart.com/css/7.13.1/anychart-ui.min.css" />
    <style>
      html, body, #container {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script>

anychart.onDocumentReady(function () {
    // To work with the data adapter you need to reference the data adapter script file from AnyChart CDN
    // (https://cdn.anychart.com/js/latest/data-adapter.min.js for latest or https://cdn.anychart.com/js/7.11.1/data-adapter.min.js for the versioned file)

    // Create a chart by JSON config.
    anychart.fromJsonFile("https://cdn.anychart.com/config-samples/line-chart.json", function (chart) {
        chart.title("Create a chart by JSON config");
        chart.container("container");
        chart.draw();
    });
});

    </script>
  </body>
</html>

https://playground.anychart.com/api/7.13.1/modules/anychart.fromJsonFile-plain