0

I'm trying to use d3js graph. In the example, it gets the data like this :

d3.tsv("data.tsv", function(d) {
  d.frequency = +d.frequency;
  return d;
}, function(error, data) {
  if (error) throw error;

  x.domain(data.map(function(d) { return d.letter; }));
  y.domain([0, d3.max(data, function(d) { return d.frequency; })]);

And the data.tsv file is something like this :

letter  frequency
A   .08167
B   .01492
C   .02782
D   .04253
E   .12702
F   .02288
G   .02015

Suppose I have a List of tuples like this :

public class myData {
public string x {get; set;}
public int y {get; set;}
}

myData data1 = new myData("A", 1);
myData data2 = new myData("B", 3);

List<myData> list1 = new List<myData>()
        {
         data1, data2
        };

How can I pass this list1 to d3js? Thanks in advance.

1 Answer 1

2

There are many ways... Is this data dyamically generated?

If not - you could just put it in a file.

If so - you can return as JSON using the Json() method in a controller, then consume with an AJAX call, or you could just spit it out in your view in the javascript like var data = @(Model.Data)

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

2 Comments

Is passing data like var data = @(Model.Data) enough? Because in above js, there are lines which does mapping or things like that. Thanks.
Well yes you will obviously still have to map the data... otherwise how will it know which fields to use? We can't do that bit for you - it depends what you want to graph.... Your question seemed to be about how to use data from the backend (C#) in the frontend (javascript) which my questions answers. For help with D3JS look at their website, they have great documentation and examples: d3js.org

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.