Adding a Date cell to TableRow

62 views
Skip to first unread message

amirmiller

unread,
Dec 12, 2011, 12:32:40 PM12/12/11
to Google Visualization API
Hi,

I am creating a table using Google Visualization Data Source library.
I created the column like this:
ColumnDescription colDesc = new ColumnDescription("Date",
ValueType.DATE, "Date");

Now I am trying to create a row:
TableRow tr = new TableRow();
tr.addCell(new Date(2011, 11, 10));
However it seems the addCell only supports boolean, double and String
(along with TableCell and Value).

Trying to run it as string or something else produces an exception:
"Cell type does not match column type, at index: 0. Should be of type:
DATE"

How can I make the value a date so it can produce this: {v:new
Date(2011,11,29)} ?

Thanks in advanced,

Amir.

Roni Biran

unread,
Dec 13, 2011, 8:21:01 AM12/13/11
to google-visua...@googlegroups.com
Are you intending to use this for rendering JavaScript datatable or JSP etc.? If it's for JS purposes, you can render a regular string (all the way) and just call the eval function in your JS file. If it's for JSP you can use a TableCell definition with value and formattedValue

TableCell(Value value, java.lang.String formattedValue)


 


--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.


amirmiller

unread,
Dec 14, 2011, 4:35:28 AM12/14/11
to Google Visualization API
It's for regular js.
When I use the string value it treats the date as string and doesn't
sort properly.

Amir.

On Dec 13, 3:21 pm, Roni Biran <roni.bi...@gmail.com> wrote:
> Are you intending to use this for rendering JavaScript datatable or JSP
> etc.? If it's for JS purposes, you can render a regular string (all the
> way) and just call the eval function in your JS file. If it's for JSP you
> can use a TableCell definition with value and formattedValue
>
> TableCell(Value value, java.lang.String formattedValue)

> (http://code.google.com/apis/chart/interactive/docs/dev/dsl_javadocs/i...

Roni Biran

unread,
Dec 14, 2011, 4:44:42 AM12/14/11
to google-visua...@googlegroups.com
If you're using a JS file, why are you using the Data Source Library? This is a Java library.
Use a regular DataTable definition, with date cell type.

Amir Miller

unread,
Dec 14, 2011, 7:58:56 AM12/14/11
to google-visua...@googlegroups.com
I'm creating the data on the server side using the data library and reading it on the client with js.
I might not have understood the question correctly before.
Anyhow, java creates the DataTable json object and the client draws the chart.

Amir.

Roni Biran

unread,
Dec 14, 2011, 8:12:08 AM12/14/11
to google-visua...@googlegroups.com
This is exactly what I thought at start. Any way, you can create a JSON string in the server and convert it to a datatable very simply in your JS file.

Let's say your server renders a JSON string like this:

{
  cols: [
    {id: 'task', label: 'Task', type: 'string'},
    {id: 'hours', label: 'Hours per Day', type: 'number'},
    {id: 'timestamp', label: 'My Date', type: 'date' }
  ], rows: [
    {c:[{v: 'Work'}, {v: 11}, { v: new Date(2011,1,1) }]},
    {c:[{v: 'Eat'}, {v: 2}, { v: new Date(2011,1,1) }]},
    {c:[{v: 'Commute'}, {v: 2}, { v: new Date(2011,1,1) }]},
    {c:[{v: 'Watch TV'}, {v:2}, { v: new Date(2011,1,1) }]},
    {c:[{v: 'Sleep'}, {v:7, f:'7.000'}, { v: new Date(2011,1,1) }]}
  ]
}

You can load the datatable in the following way:

var data = new google.visualization.DataTable(eval('('+serverJSONString+')'), 0.6)





Amir.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

Amir Miller

unread,
Dec 14, 2011, 8:43:00 AM12/14/11
to google-visua...@googlegroups.com
This is exactly my issue.
When creating the column there is no issue in creating it as type:date.
But when creating the row , you can't create it as "new Date() because date is not a possible value for a cell (only string, number or boolean).
If I create it as string it just appears as "new Date(2011,1,1)".

Amir.

Roni Biran

unread,
Dec 14, 2011, 8:45:06 AM12/14/11
to google-visua...@googlegroups.com
The thing is that you don't create a row in the form of "new TableRow()", you create a JSON string and let the JS serialize it.



Amir.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

Amir Miller

unread,
Dec 14, 2011, 9:34:07 AM12/14/11
to google-visua...@googlegroups.com
I'm not sure that's possible.
I played with it for a while but they have such a specific JSON format that I could not reproduce it using XStream and creating it just as text is not an option (too ugly for someone else to look at this code).

Roni Biran

unread,
Dec 14, 2011, 9:49:13 AM12/14/11
to google-visua...@googlegroups.com
in that case, why not just create your own JSON string and send it to the client?


On Wed, Dec 14, 2011 at 4:34 PM, Amir Miller <amir....@gmail.com> wrote:
I'm not sure that's possible.
I played with it for a while but they have such a specific JSON format that I could not reproduce it using XStream and creating it just as text is not an option (too ugly for someone else to look at this code).

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

Amir Miller

unread,
Dec 14, 2011, 10:06:13 AM12/14/11
to google-visua...@googlegroups.com
I guess that's an option.
And then turn that into the client DataTable using js?
That would also solve the lack of support for annotations (a new experimental feature) which is not available on the java side.

Roni Biran

unread,
Dec 14, 2011, 10:13:16 AM12/14/11
to google-visua...@googlegroups.com
Precisely my point.


--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

Alan Carlos Junior Rossetto

unread,
Dec 14, 2011, 8:10:43 AM12/14/11
to google-visua...@googlegroups.com
Thanks for this explanation!

:D

2011/12/14 Amir Miller <amir....@gmail.com>

Amir.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.



--
"No esforço para entender a realidade, somos um homem que tenta
compreender o mecanismo de um relógio fechado. Ele vê o mostrador
e os ponteiros, escuta o tique-taque, mas não tem como abrir a caixa.
Sendo habilidoso, poderá imaginar o mecanismo responsável pelo que
ele observa, mas nunca poderá estar completamente seguro de que a
sua explicação é, realmente a única possível." - Albert Einstein

Alan Carlos Junior Rossetto

Dale Angus

unread,
Apr 30, 2015, 12:50:58 PM4/30/15
to google-visua...@googlegroups.com
I read the solution below. I don't think it is a good one. Creating a JSON by yourself?
Isn't there a fix yet to allow Date in row.addCell()?

Amir Miller

unread,
May 1, 2015, 5:33:47 AM5/1/15
to google-visua...@googlegroups.com
This question is from 2011
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Google Visualization API" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/google-visualization-api/u9-Y-59DrlI/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> google-visualizati...@googlegroups.com.
> To post to this group, send email to
> google-visua...@googlegroups.com.
> Visit this group at
> http://groups.google.com/group/google-visualization-api.
> For more options, visit https://groups.google.com/d/optout.
>
Reply all
Reply to author
Forward
0 new messages