简体   繁体   English

如何使用Json File绘制折线图

[英]How to plot line chart using Json File

I am unable to plot a line chart with the data in a json file. 我无法用json文件中的数据绘制折线图。 Please help me where i am going wrong! 请帮我哪里错了!

my code is- http://jsfiddle.net/arnica04/UVe57 我的代码是-http: //jsfiddle.net/arnica04/UVe57

<!DOCTYPE html>
<meta charset="utf-8">
<title>Mean Zero Time Series</title>
<style>
/* line style */
    path {
    stroke: red;
    stroke-width: 2;
    fill: none; /* stop it being solid area */
}
/* x axis tick */
.x.axis line {
    stroke: #000;
}
    /* x axis line */
.x.axis path {
    fill: none;
    stroke: #000;
}
 /* Y tick */
.y.axis line, .y.axis path {
    fill: none;
    stroke: #000;
}
</style>

<div id="graph" class="aGraph" style="position:absolute;top:0px;left:0; float:left;">     
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
d3.json("data.json", function(error, data) {
    plotChart(data); //load the json data
});

function plotChart(data) {
    var w = 800; //width
    var h = 400; // height
    var x = d3.scale.linear().domain([0, 20]).range([0, w]); // 24 for days
    var y = d3.scale.linear().domain([0, 58]).range([h,0]);

    var xAxis = d3.svg.axis().scale(x).orient("bottom"); //x axis at bottom

    var yAxis = d3.svg.axis().scale(y).orient("left"); // y axis on left

    var line = d3.svg.line()
    .x(function(d, i) {return x(d.date);})
    .y(function(d, i) {return y(d.close);});

    var graph = d3.select("#graph").append("svg:svg").attr("width", w )
            .attr("height", h+ 200 ).append("svg:g")
            .attr("transform", "translate(" + 100 + "," + 100 + ")"); // move chart down 
 and right

            graph.append("g").attr("class", "x axis")
             .attr("transform", "translate(0," + h + ")").call(xAxis);
    graph.append("g").attr("class", "y axis").call(yAxis);

            graph.append("text").attr("class", "x label")
            .attr("text-anchor", "end").attr("x", w - (w / 2))
            .attr("y", h + 45).text("Time in Days");

            graph.append("text").attr("class", "y label")
            .attr("text-anchor", "end").attr("y", -60).attr("x", -60)
            .attr("dy", ".75em").attr("transform", "rotate(-90)")
            .text("Number of Visits");

    graph.append("svg:path").attr("d", line(data)); // red line

 }

</script>

</html>

My dataset in a different file is named as 'data.json' and it contains: 我在另一个文件中的数据集名为“ data.json”,它包含:

[                                    
{date:"00",close:"55"},         
{date:"07",close:"10"},
{date:"10",close:"58"},
{date:"15",close:"46"},
{date:"20",close:"05"}
]

最好使用交叉过滤器而不是在D3中编写代码。

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

相关问题 使用JSON数据绘制Highchart多系列折线图 - Plot Highchart multiple series line chart using JSON data 如何使用带有ajax的chart.js绘制折线图? - How to plot line chart using chart.js with ajax? D3js:如何使用JSON数据绘制多系列折线图 - D3js: how to plot multiseries line chart with json data 如何基于Month对Json数据进行分组,并使用谷歌图表绘制它 - How to group Json data based on Month and plot it using google chart 使用NVD3的本地和远程json文件的不同折线图 - Different line chart for local & remote json file using NVD3 如何使用chart.js在没有绘图点的图表上画一条线 - How to Draw a line on chart without a plot point using chart.js 如何使用 Chart.js 在 canvas 区域之外的 plot 折线图 - How to plot a line chart which has both the start and points outside the canvas area using Chart.js 如何从外部 JSON 获取 plot 图表并使用 Chart.JS 格式化 X-AXIS 以显示时间? - How to plot chart from external JSON and format X-AXIS to show time using Chart.JS? 如何在高图中绘制带有系列数据的折线图? - How to plot line chart with series data in highcharts? 如何使用charts.js 在折线图中使用plot 单个值? - How to plot a single value with line in line chart graph using charts.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM