Tanvir Shaikh
Tanvir Shaikh

Reputation: 709

GVIZ: Custom label for continous axis(datetime)

I have a line chart where we plot some numbers against a datetime value on x-axis. Since, using datetime value on column type renders it a continuous axis, the column labels are auto generated.

We get only the time component as the column labels but not the date. Is there anything I can do to get the entire date time as the label?

(NOTE: I do not want to change the data type to string as I do not want even spacing in data points.)

Upvotes: 0

Views: 281

Answers (1)

jmac
jmac

Reputation: 7128

Yes. You need to look at formatters which use a subset of the ICU SimpleDateFormat.

Here is an example of how they work:

function drawVisualization() {
  // Create and populate the data table.
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Date');
  data.addRows([
    [new Date(2012,1,5)],
    [new Date(2012,2,10)],
    [new Date(2012,3,15)],
    [new Date(2012,4,20)]
  ]);

  alert(data.getFormattedValue(3,0));

  var formatter1 = new google.visualization.DateFormat({pattern: 'yyyy, MMM'});

  formatter1.format(data,0);

  alert(data.getFormattedValue(3,0));
}

Adjust to fit your data and needs, and presto! You have it working.

Upvotes: 1

Related Questions