score:1

Accepted answer

yes, this is possible by using the time axis. you'll need to remodel your data to fit the required input format; an example of this is in the snippet below using foreach.

it's important to note that chart.js requires moment.js for datetime handling. make sure to include moment.js before chart.js, or use the chart.js bundled build.

let j = {
    "site1": [{
        "datetime": "2019-01-09 14:43:58",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 14:44:17",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:02:59",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:05:43",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:08:52",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:16:51",
        "price": "649.99"
      }
    ],
    "site2": [{
        "datetime": "2019-01-09 15:03:05",
        "price": "0"
      },
      {
        "datetime": "2019-01-09 15:05:52",
        "price": "804.91"
      },
      {
        "datetime": "2019-01-09 15:09:00",
        "price": "804.91"
      },
      {
        "datetime": "2019-01-09 15:16:58",
        "price": "804.91"
      }
    ]
  },
  k = object.keys(j),
  datasets = [];

// remodel the data so it can be provided direct to chart.js
k.foreach(function(val) {
  let d = {
    label: val,
    data: []
  };

  j[val].foreach(function(val2) {
    d.data.push({
      x: val2.datetime,
      y: val2.price
    });
  });

  datasets.push(d);
});

// create the chart.
new chart(document.getelementbyid('canvas'), {
  type: 'line',
  data: {
    datasets: datasets
  },
  options: {
    scales: {
      xaxes: [{
        type: 'time'
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.7.3/chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>

score:1

this can be achieved using chart.js + moment.js when you're plotting a chart against time.

see this below code or fiddle -> http://jsfiddle.net/goz3jehy/4/

var config = {
  type: 'line',
  data: {
    datasets: [{
        label: "site1",
        backgroundcolor: 'red',
        bordercolor: 'pink',
        fill: false,
        data: [{
            x: '2019-01-09 14:43:58',
            y: 649.99
          },
          {
            x: '2019-01-09 14:44:17',
            y: 649.99
          },
          {
            x: "2019-01-09 15:02:59",
            y: 649.99
          },
          {
            x: "2019-01-09 15:05:43",
            y: 649.99
          },
          {
            x: "2019-01-09 15:08:52",
            y: 649.99
          },
          {
            x: "2019-01-09 15:16:51",
            y: 700
          }
        ],
      },
      {
        label: "site2",
        backgroundcolor: 'orange',
        bordercolor: 'yellow',
        fill: false,
        data: [{
            x: "2019-01-09 15:03:05",
            y: 0
          },
          {
            x: "2019-01-09 15:05:52",
            y: 804.91
          },
          {
            x: "2019-01-09 15:09:00",
            y: 804.91
          },
          {
            x: "2019-01-09 15:16:58",
            y: 804.91
          }
        ],
      }
    ]
  },
  options: {
    scales: {
      xaxes: [{
        type: 'time',
        time: {          
        }
      }],
    },
  }
};

var ctx = document.getelementbyid("mychart").getcontext("2d");
new chart(ctx, config);

Related Query

More Query from same tag