سؤال

The data: A 'premiums.tsv' file, with two columns, 'BiddingDate' and 'CategoryA'

What I'm trying to do: Using d3, display in a table or divs just the latest BiddingDate and the CategoryA value that corresponds to that latest BiddingDate.

I've got a chart on the page, that works fine. I can also get a table of all the values. That works fine too. But I just can't figure out how to isolate the data corresponding to the latest date value and then display it. Would really appreciate any help. Thanks!

هل كانت مفيدة؟

المحلول

I solved it by using data.map to map the data into a new array, then simply calling the last item in the array.

I only ever have 241 data points, but for datasets of variable size, you can just use .length to figure out the latest data point.

var allDates = [];
allDates = data.map(function(d) {return d.BiddingMonth;});

var latestDate = allDates[240];

var allCatA = [];
allCatA = data.map(function(d) {return d.CategoryAPremium;});

var latestCatA = allCatA[240];

Then all I had to do was to print latestDate and latestCatA wherever I wanted.

d3.select("#lateDate").text("$ " + latestDate);
d3.select("#lateA").text("$ " + latestCatA);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top