Hi folks,
The original d3 example of a Multi-Series Line Chart utilizes the variable "name", which then allows one to specify labels and colors by "name". In my example above (
http://bl.ocks.org/natemiller/20f9bd99d1795e3a0b1c) I have explored how to make the same plot if the data were in a different (long) format, because often my own data is in this format. In my example, I nested the temperature data within city, but the method in which I did this left me without a "name" variable, so that I had to instead plot lines and colors using the "key". This works, however, as I've played with it more its not ideal as I can't use "d.key" in the same way as I can "
d.name" to apply the appropriate colors, or to specify labels (as in the d3 example) or a legend, such as with
https://gist.github.com/ZJONSSON/3918369.
I think this problem arises from my incomplete understanding of this section of code (below) and how it might be modified for the dataset I present above (where data from each city is not in a separate column).
Would someone be willing explain in greater detail what this section of code is doing? I'll try to describe how I think it works and you can see where my mistakes are. As I see it, this section is using a function that takes a "name" value and returns an object containing that name and an associated date and temperature (though I'm not perfectly clear on what exactly the +d[name] portion should be translated into words). Then the objects with a common name are mapped to a particular color and the color/data combination is placed in the variable cities.
var cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, temperature: +d[name]};
})
};
});
Can a similar type of code be used for the long format data shown in my example? How can I map the city name to the corresponding date and temperature when in "long" format? Can you add city labels to my example above?
I'll point out that while in this example dataset, all temperature measurements for each city is recorded at the same date/time, typically my datasets consist of measurements from several sites, but not at exactly the same date/time. Thus the data really must be stored and accessed in a "long" format, unless I were to include NAs or (nulls) in regions where measurements from different sites did not share a common date/time.
I'm still new to using d3 and Javascript and so have been mostly building upon and modify existing code to accomplish my visualization goals. I've learned a lot in the past month, but at times, like now, I'm not sure if the existing code can be modified to accomplish what I would like or if something new is needed. I appreciate any constructive advice.
Thank you,
Nate