Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time scatter plot w/ chart.js

I'm trying to render a scatter plot in chart.js of (x,y) data where x is a date string. I've seen many examples and tutorials online where the instructor uses a function to generate the time stamp for an example chart, but I haven't found any examples that use real data like one might collect.

I have data that looks like this (collected from cron):

2017-07-08T06:15:02-0600,23.375
2017-07-08T06:20:02-0600,23.312
2017-07-08T06:25:02-0600,23.312
2017-07-08T06:30:02-0600,23.25

I tried a data like this in chart.js (both with and without "quotes" around the data string):

data: [{
  x: 2017-07-08T06:15:02-0600,
  y: 23.375
},{
  x: 2017-07-08T06:20:02-0600,
  y: 23.312
},{
  x: 2017-07-08T06:25:02-0600,
  y: 23.312
},{
  x: 2017-07-08T06:30:02-0600,
  y: 23.25

}],

Nothing renders. What am I doing wrong?

like image 727
user2711286 Avatar asked Jul 13 '17 05:07

user2711286


People also ask

How do you draw a time in a scatter plot?

To create a time series plot in Excel, first select the time (DateTime in this case) Column and then the data series (streamflow in this case) column. Next, click on the Insert ribbon, and then select Scatter. From scatter plot options, select Scatter with Smooth Lines as shown below.

Are time plots scatter plots?

While a timeplot can resemble a scatter plot, with a series of dots, you will often see these plots with the dots connected, especially in financial publications like The Wall Street Journal.

What are the 3 types of scatter plots?

The kind of correlation can be interpreted through the patterns revealed on a Scatterplot. These are: positive (values increase together), negative (one value decreases as the other increases) or null (no correlation). The shape of the correlation can be described as: linear, exponential and U-shaped.


3 Answers

According to the documentation of scatter charts:

Unlike the line chart where data can be supplied in two different formats, the scatter chart only accepts data in a point format.

So you can't use values like 2017-07-08T06:15:02-0600. You can convert dates into numbers and use them in your data.

In your case:

data: [{
        x: 1499516102000,
        y: 23.375
    }, {
        x: 1499516402000,
        y: 23.312
    }, {
        x: 1499516702000,
        y: 23.312
    }, {
        x: 1499517002000,
        y: 23.25
    }
]

Now your xAxes will be with numbers, so you can use a callback to modify xAxes labels.

like image 60
mugimugi Avatar answered Nov 06 '22 11:11

mugimugi


That advice isn't quite right. The javascript moment.js makes it possible to plat scatter data using dates as the x axis value. For some reason the bundled version in Chart.bundle.js wasn't working for me, so I downloaded moment.js directly. I'm using this to setup:

<script src="js/moment.js"></script>
<script src="js/Chart.min.js"></script>

and this for my chart.js data details:

data: [
  {x: moment("2017-07-08T06:15:02-0600"), y: 23.375},
  {x: moment("2017-07-08T06:20:02-0600"),y: 23.312},
  {x: moment("2017-07-08T06:25:02-0600"),y: 23.312},
  {x: moment("2017-07-08T06:30:02-0600"),y: 23.25}
],

It's working great!

like image 31
user2711286 Avatar answered Nov 06 '22 09:11

user2711286


Another solution that worked great for me, was to just use the line type for the chart, configure it for using dates for the x axis, and addtionally disable the lines, so that it just looks like a scatterplot.

new Chart(ctx, {
        type: 'line',
        data: {
            datasets: [{
                x: 2017-07-08T06:15:02-0600,
                y: 23.375
              },{
                x: 2017-07-08T06:20:02-0600,
                y: 23.312
              },{
                x: 2017-07-08T06:25:02-0600,
                y: 23.312
              },{
                x: 2017-07-08T06:30:02-0600,
                y: 23.25
            }]
          },
          options: {
            showLine: false,
            scales: {
              x:{
                type: 'time',
                display: true,
                title: {
                  display: true,
                  text: 'Date'
                },
              },
            }
          }
       }
    );

I see this as a quite elegant solution. The documentation even specifies:

The scatter chart supports all of the same properties as the line chart. By default, the scatter chart will override the showLine property of the line chart to false.

like image 2
chillichief Avatar answered Nov 06 '22 09:11

chillichief