我有两个不同的图表使用相同的精确数据。一个使用嵌入在html中的json,另一个则读取文件。
我一直在google上寻找anychart
读取json文件或web服务的例子。我什么也没找到。
由于第二个图表使用json
调用读取d3.json
数据,我想也许可以使用相同的函数读取anychart
图表中的数据。
所以我把两个图表的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>
d3
从json
文件读取。也许我可以使用这个调用在前面的代码中填充报表?: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);
})
});
如有任何帮助,我们将不胜感激。我只想知道这是否可能。
最佳答案
有https://api.anychart.com/7.13.1/anychart.data#loadJsonFile和https://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