Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming parse? #20

Open
mbostock opened this issue Sep 9, 2016 · 3 comments
Open

Streaming parse? #20

mbostock opened this issue Sep 9, 2016 · 3 comments
Labels

Comments

@mbostock
Copy link
Member

mbostock commented Sep 9, 2016

Like the new shapefile.

@mbostock mbostock self-assigned this Sep 9, 2016
@mbostock
Copy link
Member Author

mbostock commented Sep 9, 2016

This is fine for Node: https://github.com/mafintosh/csv-parser

Would still be nice to have streaming CSV parsing in the browser.

@mbostock mbostock removed their assignment Sep 19, 2016
@cpietsch
Copy link

you probably mean something else, but I wrote a little class that "streams" csv rows as they are loaded through the progress event:

import { request, csvParse, csvParseRows } from 'd3'
import EventEmitter from 'eventemitter3'

export default class CsvLoader extends EventEmitter {
  constructor (url) {
    super()
    this.loadedChars = 0
    this.header = undefined
    this.data = []
    this.xhr = undefined

    this.request = request(url)
      .mimeType('text')
      .on('progress', this.onProgress)
      .on('load', () => this.emit('loaded'))

    return this
  }

  get () {
    this.request.get()
    return this
  }

  onProgress = (p) => {
    const progress = p.total / p.loaded
    const responseText = p.target.responseText
    // console.log(responseText.length)
    const lineBreak = responseText.lastIndexOf('\n') + 1
    const char = responseText.slice(this.loadedChars, lineBreak)
    let parsed = csvParseRows(char)
    if (!this.header) {
      this.header = parsed.shift()
    }
    const obj = parsed.map(p =>
      this.header.reduce((a, b, i) => {
        a[b] = p[i]
        return a
      }, {})
    )
    this.loadedChars = lineBreak
    this.emit('row', obj)
    this.emit('progress', progress)
  }
}

@Fil
Copy link
Member

Fil commented Jun 3, 2020

https://observablehq.com/@mbostock/streaming-csv

@Fil Fil added the idea label Jul 10, 2020
@mbostock mbostock reopened this Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants