-1

i have some values in my csv file and i show a graph with values on y axis and dates on x axis.

For first graph i have following values

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

For second graph i have following values

 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

But in second graph x axis does not contain all dates.. As an example i put a printscreen of my output graphs myoutput to here.

This is my code which takes datas from csv file and visualize it.

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+")");


});
mstfky
  • 87
  • 1
  • 10
  • 1
    Hi - please share the code, especially the parts that establish the scales (domain and range), axis and line function. Also, do both charts use the same scale, axes and line functions? – Tom Shanley Jul 19 '17 at 21:24
  • @TomShanley thank you for your interest i edited my post. – mstfky Jul 20 '17 at 07:26

1 Answers1

0

I would try setting the tick values explicitly, using tickValues: https://github.com/d3/d3-3.x-api-reference/blob/master/SVG-Axes.md#tickValues

ticks(5) will suggest 5 ticks, but will be adapted based on the scale's domain. Alternative to tickValues(), you could try ticks(d3.time.day, 2) to have a tick every 2 days.

Tom Shanley
  • 1,757
  • 1
  • 7
  • 9
  • how can i draw line on a specific point on y axis , i use this but line exists on top of the graph , i want to point out that some points are above average and some are below, or i should find plateu. My aim is to measure threshold Do you have any idea ? svg.append("line") // attach a line .style("stroke", "red") // colour the line .style("stroke-dasharray", ("2,2")) .attr("x1", 300) // x position of the first end of the line .attr("y1",0.4 ) ; // y position of the first end of the line – mstfky Aug 05 '17 at 09:01