json - set date without time in D3 js -


i working on d3 js. lerner , trying render multi line graph not able set proper date format.i wanted remove 12 pm .example april 12 pm wed 02

below code

var margin = { top: 40, right: 40, bottom: 35, left: 85 },     width = 800 - margin.left - margin.right,     height = 350 - margin.top - margin.bottom;         var parsedate = d3.time.format("%y%m%d").parse;      var x = d3.time.scale()     .range([0, width]);      var y = d3.scale.linear()     .range([height, 0]);      var color = d3.scale.category10();      var xaxis = d3.svg.axis()     .scale(x)     .orient("bottom");      var yaxis = d3.svg.axis()     .scale(y)     .orient("left");      var line = d3.svg.line()     .interpolate("basis")     .x(function (d) { return x(d.date); })     .y(function (d) { return y(d.request); });      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 + ")");      //d3.tsv("data.tsv", function (error, data) {     color.domain(d3.keys(data.listweekly[0]).filter(function (key) { return key !== "date"; }));          data.listweekly.foreach(function (d) {             d.date = parsedate(d.date);          });          var cities = color.domain().map(function (name) {             return {                 name: name,                 values: data.listweekly.map(function (d) {                     return { date: d.date, request: +d[name] };                 })             };         });          x.domain(d3.extent(data.listweekly, function (d) { return d.date; }));          y.domain([     d3.min(cities, function (c) { return d3.min(c.values, function (v) { return v.request; }); }),     d3.max(cities, function (c) { return d3.max(c.values, function (v) { return v.request; }); })   ]);          svg.append("g")       .attr("class", "x axis")       .attr("transform", "translate(0," + height + ")")       .call(xaxis);          svg.append("g")       .attr("class", "y axis")       .call(yaxis)     .append("text")       .attr("transform", "rotate(-90)")       .attr("y", 6)       .attr("dy", ".71em")       .style("text-anchor", "end")       .text("request");          var city = svg.selectall(".city")       .data(cities)     .enter().append("g")       .attr("class", "city");          city.append("path")       .attr("class", "line")       .attr("d", function (d) { return line(d.values); })       .style("stroke", function (d) { return color(d.name); });          city.append("text")       .datum(function (d) { return { name: d.name, value: d.values[d.values.length - 1] }; })       .attr("transform", function (d) { return "translate(" + x(d.value.date) + "," + y(d.value.request) + ")"; })       .attr("x", 3)       .attr("dy", ".35em")       .text(function (d) { return d.name; });     //}); 

my json data

var list = new list<object>(); list.add(new { date = "20140401", weekday = "163", weekend = "263" });                 list.add(new { date = "20140402", weekday = "153", weekend = "253" });                 list.add(new { date = "20140403", weekday = "133", weekend = "233" });                 list.add(new { date = "20140404", weekday = "167", weekend = "373" });                 list.add(new { date = "20140405", weekday = "123", weekend = "183" });                 list.add(new { date = "20140406", weekday = "178", weekend = "123" });                 list.add(new { date = "20140407", weekday = "32", weekend = "223" });    return json(new { listweekly = list }); 

you can set time format using d3.time.format helper , set format of tick labels of axis using axis tickformat method. have dates formatted 2014-04-30, can use following code:

// create time format generator var datefmt = d3.time.format('%y-%m-%d');  // 2014-04-30  // datefmt function returns string formatted date console.log(datefmt(new date('2014/04/04')); // 2014-04-04  // set tick format xaxis var xaxis = d3.svg.axis()    .scale(x)    .orient("bottom")    .tickformat(function(d) { return datefmt(d.date); }); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -