1

I have retrieved data from c# web method in the form of lists, i need to populate the data in the form of a line chart using the D3 JavaScript library. How can i do that?

How do I use the list data in D3 and have the date time format in one list and the value in another list?

 $.ajax({
 url: "ServiceMessage.asmx/GetPostings",
        type: "POST",
        contentType:"application/json",
        success: function (data) {
            var myData = data.d;
            var data1;
            var data2;
            alert(myData.length + " hellos");
            for (var i = 0; i < myData.length; i++) {
               // $("#divMsg").append(myData[i].date + " " + myData[i].values);
                 data1[i] = myData[i].date; "</br>"
                  data2[i] = myData[i].values; "</br>"
                 $("#divMsg").append(data1[i]+""+data2[i])
            }

How can I use data1 data2 to populate a line graph?

1

1 Answer 1

2

You can have a look here http://www.d3noob.org/2012/12/starting-with-basic-d3-line-graph.html

Your case is even simpler because you already have your data in a list. Do not split it in two arrays, it is better to have one list [{date:... , value:...},{date:..., value:...}... ]

you have to init d3 (as you can see there is a "time" range for the x axis, if you use it you can parse the dates with the d3.time.format("%d-%b-%y").parse function):

var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;

var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

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

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

/* ---------------------- This is your line --------------------- */
var valueline = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.value); });
/* -------------------------------------------------------------- */

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

and then append the line (where data is the list of {date:..., value:...}:

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

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

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

PS: I use "value" here but in your question you use "values".

Sign up to request clarification or add additional context in comments.

2 Comments

i have datetime of c# format how do i parse that?(26-08-2014 07:14:43) in this format
oh yes, you are right you have to parse. you have to apply d3.time.format(...).parse to the date attribute to each object in your data list : data.forEach(function(d) { d.time = d3.time.format("%d-%m-%Y %H:%M:%S").parse(d.date) }); see link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.