猿问

如何修复我的图表,以便 x 轴的比例由一组标签定义?

js,我用的是v3.js 我很确定我的图表的问题出在我的 x 轴上。


我有这样的结构:


[

 {date: "OCT 2020", close: 57370}

 {date: "SEP 2020", close: 60100}

 {date: "AGO 2020", close: 62530}

 {date: "JUL 2020", close: 68840}

 {date: "MAY 2020", close: 91470}

 {date: "ABR 2020", close: 54130}

 {date: "MAR 2020", close: 57960}

 {date: "FEB 2020", close: 55720}

 {date: "ENE 2020", close: 54360}

]

轴x将是键“日期”,并且 是y-axis,close我不知道如何定义 ,x-axis以便每个表示 x 轴的值将是一个标签。(我的x轴上不会重复任何值,并且我的数据已排序)

我依赖这个例子: https://bl.ocks.org/d3noob/4414436

我该如何修复它?


阿晨1998
浏览 98回答 1
1回答

蝴蝶不菲

这是工作代码::&nbsp;http://plnkr.co/edit/pdjNBWCYd91drndL ?open=lib%2Fscript.js这是使用 D3v3,您可能需要升级 D3 版本并浏览 D3docs,因为最新的 D3 版本中有很多更改。以下是使用您的数据的基本工作示例<!DOCTYPE html><meta charset="utf-8"><style> /* set the CSS */&nbsp;body { font: 12px Arial;}&nbsp;path {&nbsp;&nbsp; stroke: steelblue;&nbsp; stroke-width: 2;&nbsp; fill: none;}&nbsp;.axis path,.axis line {&nbsp; &nbsp; fill: none;&nbsp; &nbsp; stroke: grey;&nbsp; &nbsp; stroke-width: 1;&nbsp; &nbsp; shape-rendering: crispEdges;}&nbsp;</style><body>&nbsp;<!-- load the d3.js library -->&nbsp;<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>&nbsp;<script>&nbsp;// Set the dimensions of the canvas / graphvar margin = {top: 30, right: 20, bottom: 30, left: 50},&nbsp; &nbsp; width = 600 - margin.left - margin.right,&nbsp; &nbsp; height = 270 - margin.top - margin.bottom;&nbsp;&nbsp;// Set the rangesvar x = d3.scale.ordinal().domain(["OCT 2020","SEP 2020","AGO 2020","JUL 2020","MAY 2020","ABR 2020","MAR 2020","FEB 2020","ENE 2020"]).rangePoints([0, width]);var y = d3.scale.linear().range([height, 0]);&nbsp;// Define the axesvar xAxis = d3.svg.axis().scale(x)&nbsp; &nbsp; .orient("bottom").ticks(100);&nbsp;var yAxis = d3.svg.axis().scale(y)&nbsp; &nbsp; .orient("left").ticks(10);&nbsp;// Define the linevar valueline = d3.svg.line()&nbsp; &nbsp; .x(function(d) {&nbsp; return x(d.date); })&nbsp; &nbsp; .y(function(d) {&nbsp; return y(d.close); });&nbsp;// Adds the svg canvasvar svg = d3.select("body")&nbsp; &nbsp; .append("svg")&nbsp; &nbsp; &nbsp; &nbsp; .attr("width", width + margin.left + margin.right)&nbsp; &nbsp; &nbsp; &nbsp; .attr("height", height + margin.top + margin.bottom)&nbsp; &nbsp; .append("g")&nbsp; &nbsp; &nbsp; &nbsp; .attr("transform", "translate(" + margin.left + "," + margin.top + ")");&nbsp;// Get the data&nbsp;let data=[&nbsp;{date: "OCT 2020", close: 57370},&nbsp;{date: "SEP 2020", close: 60100},&nbsp;{date: "AGO 2020", close: 62530},&nbsp;{date: "JUL 2020", close: 68840},&nbsp;{date: "MAY 2020", close: 91470},&nbsp;{date: "ABR 2020", close: 54130},&nbsp;{date: "MAR 2020", close: 57960},&nbsp;{date: "FEB 2020", close: 55720},&nbsp;{date: "ENE 2020", close: 54360}]&nbsp; &nbsp; // Scale the range of the data&nbsp; &nbsp; // x.domain(d3.extent(data, function(d) { return d.date; }));&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; y.domain([0, d3.max(data, function(d) { return d.close; })]);&nbsp; &nbsp; // Add the valueline path.&nbsp; &nbsp; svg.append("path")&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .attr("class", "line")&nbsp; &nbsp; &nbsp; &nbsp; .attr("d", valueline(data));&nbsp;&nbsp; &nbsp; // Add the X Axis&nbsp; &nbsp; svg.append("g")&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .attr("class", "x axis")&nbsp; &nbsp; &nbsp; &nbsp; .attr("transform", "translate(0," + height + ")")&nbsp; &nbsp; &nbsp; &nbsp; .call(xAxis);&nbsp;&nbsp; &nbsp; // Add the Y Axis&nbsp; &nbsp; svg.append("g")&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .attr("class", "y axis")&nbsp; &nbsp; &nbsp; &nbsp; .call(yAxis);</script></body>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答