Line chart doesn't work with type time chart.js

Arthur

I use chart.js with React and I can't figure out why the line chart doesn't work with type: 'time', maybe I could be missing something:

Chart.js CodeSandbox

import React from "react";
import { Line } from "react-chartjs-2";

const startDate = new Date(2020, 4, 20);

const json = '{"responses":[{"count":"73","rows":[{"values":["1"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["1"]},{"values":["6"]},{"values":["7"]},{"values":["5"]},{"values":["8"]},{"values":["9"]},{"values":["2"]},{"values":["1"]},{"values":["1"]},{"values":["1"]},{"values":["6"]},{"values":["3"]},{"values":["0"]},{"values":["20"]},{"values":["9"]},{"values":["3"]},{"values":["2"]},{"values":["1"]},{"values":["13"]},{"values":["3"]},{"values":["13"]},{"values":["13"]},{"values":["7"]},{"values":["12"]},{"values":["0"]}]}]}';

const values = JSON.parse(json).responses[0].rows.map(row => row.values[0]);

const options = {
  legend: {
    display: false
  },
  hover: {
    mode: "index",
    intersect: false,
    animationDuration: 0
  },
  scales: {
    yAxes: [{ position: "right" }],
    xAxes: [
      {
        gridLines: { display: false },
        type: "time",
        time: {
          parser: "MMM D",
          unit: "day",
          unitStepSize: 5,
          displayFormats: {
            day: "MMM D"
          }
        },
        ticks: {
          min: startDate, //May 20
          max: new Date()
        }
      }
    ]
  },
  tooltips: {
    mode: "x-axis"
  }
};

const data = {
  datasets: [
    {
      label: "data",
      fill: false,
      data: values,
      backgroundColor: "pink",
      borderWidth: 2,
      lineTension: 0,
      borderColor: "pink",
      hoverBorderWidth: 2,
      pointBorderColor: "rgba(0, 0, 0, 0)",
      pointBackgroundColor: "rgba(0, 0, 0, 0)",
      pointHoverBackgroundColor: "#fff",
      pointHoverBorderColor: "pink",
      showLine: true
    }
  ]
};

const LineChart = () => {
  return (
    <div style={{ maxWidth: 1024, margin: "32px auto" }}>
      <Line data={data} options={options} />
    </div>
  );
};

export default LineChart;

ordman

You should generate specific dataset like this:

const values = JSON.parse(json).responses[0].rows.map((row, index) => {
  let date = new Date(2020, 4, 20);
  date.setDate(startDate.getDate() + index)
  return {
    y: row.values[0],
    x: date
  };
});

example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Chart.js Line chart legend label font size doesn't work

Chart js - Chart doesn't show when x axis has type 'time'

Chart JS onclick doesn't work

Chart js doesn't show chart

Chart.js line chart tooltip shows wrong label when line doesn't start at first label

chart.js toggleDatasetVisibility and getDatasetVisibility doesn't work

Updating chart.js with array index 0 doesn't work

Code doesn't work after updating chart.js versioning

stacked bar chart doesn't work - vuechart.js

Charts' line chart doesn't render line chart properly

Chart.js Radar chart legend label font size doesn't work

Changing Chart.js chart type doesn't remove older axis

Chart.js won't draw the line on chart line

Drawing Real Time Timeseries Line Chart with Lightning Chart JS?

Line chart iOS doesn't display chart entries

dc.js time series line chart

Chart.js: Dynamic Changing of Chart Type (Line to Bar as Example)

Tooltip doesn't work in Google Scatter Chart

Google Chart Tooltip Doesn't Work

Merge scatter and line type with chart.js

Chart js Bar Time Chart

date and time chart With Chart js

chart.js - Pie Chart doesn't display all data

Vue-Chart.js: a chart doesn't begin from zero

Line chart in Excel using C# : seriesCollection doesn't work properly

XY Scatter Chart with Smooth Line doesn't consider Time in DateTime X-Axis

Chart JS in React doesn't fetch data

Time in x-axis and data for the y-axis for line chart in d3.js doesn't match/show with what (data) is on JSON

Remove the vertical line in the chart js line chart