Search code examples
javascriptnode.jsc3.jspapaparse

PapaParse doesn't handle my date correctly


I am having an issue creating a chart with some JSON that papaparse gives me. It continually gives me this error.

c3.js:2100 Failed to parse x '10-18-2018' to Date object

I've attempted to change the date format in csv to no avail. I have followed examples from c3 website, papaparse examples, and some stack overflow questions. Hopefully someone can tell me what I'm doing wrong so I can move forward with my project. Here is the code and the csv lines

app.js

"use strict";
$(function () {
  $.get("108jbdata.csv") // Use HTTP GET (via Live-server) to retreive data from the static CSV file that we have included with our web assets.
    .then(function (response) {
      // Callback is executed when data is asynchronously received.
      var parseOptions = {
        // Parse options given to PapaParse.
        header: true, // Allow PapaParse to derive field names from the CSV header line.
        dynamicTyping: true, // Tell PapaParse to parse CSV string fields to the correct types for us.
      };
      var parsed = Papa.parse(response, parseOptions); // Parse the CSV data retreived from Live-server.
      console.log(parsed);
      var chart = c3.generate({
        // Generate our chart.
        bindto: "#chart",
        data: {
          json: parsed.data, // Plug the parsed CSV data into the chart.
          keys: {
            x: "Date",
            xFormat: "%m-%d-%Y",
            value: [
              "KFISH", // Specify which column from the CSV file that we want to appear in the chart.
              "WATER",
            ],
          },
        },
        axis: {
          x: {
            type: "timeseries",
            tick: {
              format: "%m-%d-%Y",
            },
          },
        },
      });
    })
    .catch(function (err) {
      // Handle any error that might have occurred.
      console.error(err);
    });
});

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>C3 chart template</title>

    <link href="bower_components/c3/c3.css" rel="stylesheet" />
  </head>
  <body>
    <div id="chart"></div>

    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/d3/d3.js"></script>
    <script src="bower_components/c3/c3.js"></script>
    <script src="bower_components/papaparse/papaparse.js"></script>
    <script src="app.js"></script>
  </body>
</html>

csv

Date,Iron,Chromium,Lead,Copper,Tin,Aluminum,Nickel,Silver,Silicon,boron,Sodium,Magnesium,Calcium,Barium,Phosphorous,Zinc,Molybdenum,Tin1,Vandium,W,Potassium,Antimony,Lithium,Maganese,Cadmium,VISC40,TAN,KFISH,WATER,PC0,PC1,pc2,pc3,pc4,pc5,PCISO0,PCISO1,PCISO2
"10-18-2018",0,0,3,0,0,0,0,0,2,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,65.03,0.37,15,0.0015,374,229,52,19,2,0,16,15,13
"11-2-2018",0,0,0,0,3,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,8,0,0,0,64.63,0.5,24,0.0024,2915,388,15,3,0,0,19,16,11
"11-29-2018",0,0,0,0,0,0,0,0,2,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,64.13,0.93,23,0.0023,3292,527,16,4,1,0,19,16,11
"12-13-2018",0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,63.95,0.91,20,0.002,3076,517,14,5,1,1,19,16,11
"1-14-2019",0,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64.74,0.84,13,0.0013,4007,698,32,9,1,0,19,17,12

Solution

  • I got past being unable to parse the string date from the csv as a Date by going through each element and parsing it as a Date before I sent it off to the generate function.

    parsed.data.forEach((h) => {
            h.Date = Date.parse(h.Date);
          });