2017-07-19 114 views
-1

我在我的csv文件中有一些值,并且我在y轴和日期x轴上显示了一个图形。d3.js x轴日期范围

对于第一图表我具有以下值

date,close 
13-Jul-16,0.8736701869033555 
15-Jul-16,0.3631761567983922 
17-Jul-16,0.4795564555162078 
19-Jul-16,0.3754827857186281 
21-Jul-16,0.4355941951068847 
23-Jul-16,0.34393804366457353 
25-Jul-16,0.40967947088135176 
27-Jul-16,0.2707818657230363 
29-Jul-16,0.34430251610420176 
31-Jul-16,0.28089496856221585 

对于第二图表我具有以下值

date,close 
11-Jul-16,0.766705419439816 
15-Jul-16,0.7353651170975812 
17-Jul-16,0.41531502169603063 
19-Jul-16,0.5927871032351933 
21-Jul-16,0.7986419920511857 
23-Jul-16,0.7904979990272231 
25-Jul-16,0.817690401573838 
27-Jul-16,0.8433545168648027 
29-Jul-16,0.8612307965742473 
31-Jul-16,0.806498303188971 

但在第二图表的x轴不包含所有的日期。作为一个例子,我放我的输出图表myoutput的打印屏幕在这里。

这是我的代码,它从csv文件中获取数据并将其可视化。

var selectedMonth = document.getElementById('selectedMonth')。value; var selectedTopic = document.getElementById('selectedTopic').value;

var userFileDirectory="../documents/"; 

userFileDirectory=userFileDirectory+selectedMonth+"/"+selectedTopic+"/"+"dataCs.csv"; 



// Set the dimensions of the canvas/graph 
var margin = {top: 30, right: 20, bottom: 30, left: 50}, 
    width = 600 - margin.left - margin.right, 
    height = 270 - margin.top - margin.bottom; 

// Parse the date/time 
var parseDate = d3.time.format("%d-%b-%y").parse; 
// Set the ranges 
var x = d3.time.scale().range([0, width]); 
var y = d3.scale.linear().range([height, 0]); 

// Define the axes 
var xAxis = d3.svg.axis().scale(x) 
    .orient("bottom").ticks(5); 

var yAxis = d3.svg.axis().scale(y) 
    .orient("left").ticks(5); 

// Define the line 
var valueline = d3.svg.line() 
    .x(function(d) { return x(d.date); }) 
    .y(function(d) { return y(d.close); }); 

// Adds the svg canvas 
var svg = d3.select("body") 
    .append("svg") 
     .attr("width", width + margin.left + margin.right) 
     .attr("height", height + margin.top + margin.bottom) 
    .append("g") 
     .attr("transform", 
       "translate(" + margin.left + "," + margin.top + ")"); 

// Get the data 
d3.csv(userFileDirectory, function(error, data) { 


    data.forEach(function(d) { 

     d.date = parseDate(d.date); 

     d.close = +d.close; 
    }); 

    // Scale the range of the data 
    x.domain(d3.extent(data, function(d) { return d.date; })); 


    y.domain([0, d3.max(data, function(d) { return d.close; })]); 

    // Add the valueline path. 
    svg.append("path") 
     .attr("class", "line") 
     .attr("d", valueline(data)); 

    // Add the X Axis 
    svg.append("g") 
     .attr("class", "x axis") 
     .attr("transform", "translate(0," + height + ")") 
     .call(xAxis); 

    // Add the Y Axis 
    svg.append("g") 
     .attr("class", "y axis") 
     .call(yAxis); 

    svg.append("text") 
     .text("("+selectedMonth+" "+selectedTopic+")"); 


}); 
+1

嗨 - 请分享代码,特别是建立比例尺(域和范围),轴和线功能的部分。另外,两个图表是否都使用相同的比例,坐标轴和直线功能? –

+0

@TomShanley感谢您的关注,我编辑了我的文章。 – mstfky

回答

0

我会尝试明确设置刻度值,使用tickValues: https://github.com/d3/d3-3.x-api-reference/blob/master/SVG-Axes.md#tickValues

蜱(5)将建议5个蜱,但会根据规模的域进行调整。作为tickValues()的替代方法,您可以尝试使用tick(d3.time.day,2)每2天打一个tick。

+0

谢谢你太多 – mstfky

+0

如何在y轴上的特定点上绘制线条,我使用这个,但是线条存在于图形的顶部,我想指出一些点高于平均值,有些低于平均值,或者我应该找到平台。我的目标是衡量门槛你有什么想法吗? svg.append(“line”)//附加一行 \t .style(“stroke”,“red”)//颜色线 \t .style(“stroke-dasharray”,(“2,2”) ) \t .attr(“x1”,300)// x行的第一端的位置 \t .attr(“y1”,0.4); // y线的第一端的位置 – mstfky

+0

这应该作为一个新问题提出来,欢呼汤姆 –