There are two different approaches you can take with your dates.  Either 
you can add a new column to your DataTable and populate it with dates (then 
you can format those dates), or you can use a DataView to calculate the 
dates.  In the view, you can call on a formatter to format the dates if you 
want to:

var dataView = new google.visualization.DataView(data);
dataView.setColumns([{
    type: 'date',
    label: data.getColumnLabel(0),
    calc: function (dt, row) {
        var dateString = dt.getValue(row, 0).toString();
        var year = dateString.substring(0, 4);
        var month = dateString.substring(4, 2); // javascript uses 
0-indexed months (ie, January is 0 not 1), so you may need to subtract 1 
from this if you haven't already accounted for it
        var day = dateString.substring(6, 2);
        var date = new Date(year, month, day);
        var formatter = new google.visualization.DateFormat({pattern: 
'dd/MM/yyyy'});
        return {v: date, f: formatter.formatValue(date)};
    }
}, 1, 2]);

The formatters do change the formatted values stored in the DataTable (most 
of them, anyway); they call the #setFormattedValue method in the 
background, so there's not much difference, except the formatters tend to 
handle formatting aspects that can be difficult to handle (especially 
localization).

PieCharts cannot use dates.


On Monday, April 7, 2014 4:44:40 PM UTC-4, cyb wrote:
>
> Hi,
>
> there are so many different ways i can do things in google charts.
>
> I think if i do date parsing and formatting i should do it directly in the 
> dataTable and not the view, so that the view that i create later has 
> immediately the right format !?
>
> How can i do this:
>
> var dataView = new google.visualization.DataView(data);
>> dataView.setColumns([{
>>     type: 'date',
>>     label: data.getColumnLabel(0),
>>     calc: function (dt, row) {
>>         var dateString = dt.getValue(row, 0).toString();
>>         var year = dateString.substring(0, 4);
>>         var month = dateString.substring(4, 2); // javascript uses 
>> 0-indexed months (ie, January is 0 not 1), so you may need to subtract 1 
>> from this if you haven't already accounted for it
>>         var day = dateString.substring(6, 2);
>>         return new Date(year, month, day);        
>>     }
>> }, 1, 2]);
>>
>
> with a dataTable ? I need a dynamicly way that i can say for example parse 
>  column 0 and 1 to a Date Object..
>
> and then i think i can use a formatter(Date Formatter) to formate the the 
> dates directly in the DataTable.. so that all DataViews has then Date 
> Objects and the right format.
> The Formatters did change the data directly in the DataTable ?
>
> and what is the different between the setFormattedValue option from a 
> DataTable and the formatters that google offers ?
>
> and last question, a pie chart needs a string object for the first column, 
> what is if the first column is a date object can a pie chart handle this ?
>
>  
>
> Am Montag, 24. März 2014 17:03:13 UTC+1 schrieb cyb:
>>
>> Hi,
>>
>> i have the following code:
>>
>>
>> var data = new google.visualization.arrayToDataTable([
>>                     ['date','column1','column2'],
>>                     [20111001,2,3],
>>                     [20111002,5,6],
>>                     [20111003,8,9],
>>                     [20111004,11,12]  ]);
>>
>>
>> var dataView = new google.visualization.DataView(data);
>>
>> var chart = new 
>> google.visualization.LineChart(document.getElementById('chart_div'));
>>
>> chart.draw(dataView, options); 
>>
>>  
>> This draws me a line chart but if i replace the LineChart with this: 
>> chart = new 
>> google.visualization.PieChart(document.getElementById('chart_div'));
>> i get this error: "Pie chart should have a first column of type string"
>> What is now the best an easiest way to convert the data from a line in a 
>> pie chart? I want switch between a pie and a line chart and all should use 
>> the same data-array. Or can i disable the check that the first value must 
>> be a string !?
>> And then i want select different values with dataView.setColumns(); so 
>> that i can determine which data is showing in my chart.
>>
>> And second question can i convert the date- column to a real date !? 
>> 20111001 is Year 2011 Month: 10 and Day: 01 ?
>>
>>
>>
>> Best regards cyb
>>
>>
>>
>>
>>              
>>
>>
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.

Reply via email to