So what I’m trying to do is for this particular data
data = [
{ date: "2022-01-01", name: "Microsoft", score 20 },
{ date: "2022-01-02", name: "Microsoft", score 22 },
{ date: "2022-01-03", name: "Microsoft", score 24 },
{ date: "2022-01-01", name: "Apple", score 20 },
{ date: "2022-01-02", name: "Apple", score 22 },
{ date: "2022-01-03", name: "Apple", score 24 },
{ date: "2022-01-01", name: "IBM", score 10 },
{ date: "2022-01-02", name: "IBM", score 12 },
]
I want to display in table Inputs.table
only the latest score… something like this…
THIS IS NOT WORKING
Inputs.table(
data,
Plot.groupX({ x: "last" }, { x: "companyName" })
)
While I find your question to be a bit unclear, I’m guessing that you want a table to display just the most recent value for each company. That is, I guess you want to display the following data in a table:
[
{ date: "2022-01-03", name: "Microsoft", score 24 },
{ date: "2022-01-03", name: "Apple", score 24 },
{ date: "2022-01-02", name: "IBM", score 12 },
]
If I’m off, then I suppose it’s some variant and the following advice might help.
First, it looks like you have some confusion surrounding a couple of different group
functions. The Plot.groupX
function is part of the Plot
package and should really be used only in that context. It’s not going to create an appropriate options object for Inputs.table
.
You probably need to use d3.group
like so:
d3.group(data, (o) => o.name)
That will return a Javascript Map object that you can iterate over to extract just the rows you want:
table = {
let last_elements = [];
d3.group(data, (o) => o.name).forEach(function (v) {
last_elements.push(v.slice(-1)[0]);
});
return Inputs.table(last_elements);
}
Here’s the result:
1 Like
There is currently no quick, supported way to do this within Plot, but you can see some examples of seeing how Plot transforms your data in this notebook: Plot data transformations / Observable / Observable
Here is an example with your data above: Plot transform in an Inputs.table / Duane Millar Barlow / Observable