Coder Social home page Coder Social logo

Streaming parse? about d3-dsv HOT 3 OPEN

d3 avatar d3 commented on May 2, 2024
Streaming parse?

from d3-dsv.

Comments (3)

Fil avatar Fil commented on May 2, 2024 4

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

from d3-dsv.

mbostock avatar mbostock commented on May 2, 2024 2

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

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

from d3-dsv.

cpietsch avatar cpietsch commented on May 2, 2024

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)
  }
}

from d3-dsv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.