Closed systemcrash closed 4 years ago
Would it be easier to load the data with fetch, parse and massage it, and then pass it to the vis as an array of objects?
The way I'm doing it now seems pretty easy, even though nothing displays yet 😄 But .data() having a .then() for some transforms seems intuitive.
I tried loading an array of objects, via a fetch/promise before, though I could never get the graph to display on page load. Certainly I was doing something wrong:
var opts = {
baseURL: "http://resource:9000/logs/",
};
var dataz = null;
var myloader = vega.loader(opts);
myloader.load('datalist.text').then(function(inData) {
dataz= vega.read(inData, {type: 'csv',
parse: 'auto' });
console.log('finished vega.read of the CSV');
});
The logs always printed finished vega.read of the CSV
last... I passed myloader in under the vl.register(vega, vegaLite, options);
So I gather I somehow need to refresh data after this load... by calling some .runAsync()
on the view? Lost.
Having a place for running a transform would be beneficial for those 'really raw data' cases.
I'm a bit lost here to be honest. Let's look at the code in more detail.
vl.register(vega, vegaLite, options);
Why do you need to register Vega/Vega-Lite here? Can you use the defaults?
vl.json( {type: "csv", url: "datafile.text"} )
What is this line supposed to do? I think the results need to be passed to a data call, no?
Can you make a small example with Vega-Lite in the editor at https://vega.github.io/editor/? Even if the transform isn't correct yet as it will help me understand what you are trying to do.
Is there a simple way to get a vega-lite config out of the vl object I run in the HTML?
Yes, omit .render
to get the spec. See the bottom of https://observablehq.com/@uwdata/introduction-to-vega-lite/.
Tried using the .toObject()
and Uncaught TypeError: plot.toObject is not a function
But if I run with plot.toString()
I get:
{"$schema":"https://vega.github.io/schema/vega-lite/v4.json","mark":{"type":"bar","tooltip":true,"filled":true},"data":{"url":"http://local-resource:9000/logs/datafile.text"},"encoding":{"y":{"field":"addr","type":"nominal"},"x":{"field":"timesSeen","type":"quantitative"}}}
Which in the editor gives either:
Blocked loading mixed active content “http://local-resource....
or Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://local-resource...
. CORS is activated on my http resource...
You can inline (sing a values data source) a small example dataset.
I guess there is no easy way to in-line CSV....
There is. You can inline a string. But I suggest you create a simple array of objects so it's easier to work with.
Got it... found an example. So waaaaaait - CSV.... there must be some kind of special newline formatting expected.
I export using the python CSV export module, into a text file. And I've tried setting content-type header to text/csv
as well as the auto content-type: application/octet-stream
, no difference. So perhaps the CSV interpreter barfs on the newline stuff.
Inlined it displays fine:
"data":
{
"values": "addr,lsf,firstSeen,lastSeen,timesSeen\r162.243.136.184,2733869240,2020-06-05T17:14:13.884582,2020-06-05T17:18:06.189240,6\r51.81.137.147,860981651,2020-06-05T17:15:10.583462,2020-06-23T12:19:08.322380,320\r5.39.19.236,86447084,2020-06-05T20:17:16.952096,2020-06-22T13:49:56.084543,99\r",
"format": {
"type": "csv"
}
}
I have some CSV files that work at https://github.com/vega/vega-datasets/tree/master/data.
So, the fix?
I MUST RENAME THE FILE TO .CSV at a URL. For it to be a CSV file, it cannot have any other extension? I could not coerce vl to accept any other of the listed params:
https://vega.github.io/vega-lite-api/api/csv https://vega.github.io/vega-lite-api/api/csvFormat
I even tried:
.data( 'datalist.text', {type: "csv"} )
No fly.
(also confirmed this behaviour in the editor)
Hmm, so Vega-Lite is supposed to automatically use the csv reader when your file extension is .csv
but you should be able to override the default. If that's not the case, please file an issue with a small example at https://github.com/vega/vega-lite.
So it sounds like this issue is somewhere else. I'm going to close the issue here but feel free to follow up and we can file the corresponding issues in the other repos.
@systemcrash, did this ever get fixed for you? I am seeing the same behaviour now, three years later. It seems that VL honours neither the application/content-type, nor the format
directive. I'm hoping this is a case of user error, but I don't think so.
I rewrote my back end to output JSON and went with that... could not get CSV to do my bidding. Have not tried since this.
Trying to get some dynamic server-side data to display. Currently 3-4000 records in JSON and CSV available. I started with:
https://observablehq.com/@vega/vega-lite-api#standalone_use
And tried getting things going.
Data
says https://vega.github.io/vega-lite-api/api/data :I set
datafile.json
e.g.vl.data( 'datafile.json' )
- also tried with CSV. Same result.Network shows that file is loaded. But graph still empty. Clues?
Also, CSV is ready for display - all columns named, but JSON data needs slight massage before processing. The key needs (re)naming. Format is:
CSV is like:
So I need to do something like:
Where would I most effectively place the above transform at load time to perform this? Is it currently possible? I tried other variants where I get a promise and do the transform in the vega.loader part of the
options
, but I could never get the data to load before render completes. Only after. 😦Some place to do deferred manipulation would be great, otherwise somewhere to name the columns in the data spec.
Here's what I have now using CSV, but graph shows no values. Just axes.
I realize now that I can supply a datasource name, e.g. https://vega.github.io/vega-lite-api/api/csv and later redraw, e.g. https://vega.github.io/vega-lite/tutorials/streaming.html but ... wanted to see what I can do with the API :)