Line Chart with Compare to Past data

1,389 views
Skip to first unread message

KWeb

unread,
Feb 25, 2014, 5:20:12 AM2/25/14
to google-visua...@googlegroups.com
I'm trying to create a line chart which has this current year's (or months) data and then to compare it to last year's (or months) data - similar to the functionality you have in Google Analytics where you can compare site traffic for this year against last year (see below screen shot)

I can't seem to work out how to plot these 2 sets of data on the same chart because one would have dates from a different range to the other set.

Any pointers/advice would be really appreciated.

Thanks

Daniel LaLiberte

unread,
Feb 25, 2014, 10:34:25 AM2/25/14
to google-visua...@googlegroups.com
I believe what you want would work if you use the 'domain' role (see https://developers.google.com/chart/interactive/docs/roles ). For each date regardless of year, you would have two domain columns, one for one year, and one for the prior year, and two columns for the values on those dates.

Here is an example of how this can be done:

function drawVisualization({
  // Create and populate the data table.
        var data new google.visualization.DataTable();
        data.addColumn({type'date'role'domain'}'2009 Quarter');
        data.addColumn('number''2009 Sales');
        data.addColumn({type'date'role'domain'}'2008 Quarter');
        data.addColumn('number''2008 Sales');
        data.addRows([
          [new Date(200901)1000new Date(200801)800],
          [new Date(200911)800new Date(200811)600],
          [new Date(200921)765new Date(200821)987],
          [new Date(200931)555new Date(200831)1500]
        ]);

  // Create and draw the visualization.
  new google.visualization.LineChart(document.getElementById('visualization')).
      draw(data{width500height400,
                  focusTarget'category'}
          );
}



--
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 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/groups/opt_out.



--
dlaliberte@Google.com   5CC, Cambridge MA
daniel.laliberte@GMail.com 9 Juniper Ridge Road, Acton MA

James Yao

unread,
Aug 14, 2017, 6:40:55 AM8/14/17
to Google Visualization API
Hi Daniel, 

I saw your post.. even 3 years after. It helps for me, thank you.

I have a question, how can I put two lines into one graph but this two lines have different date range, I mean, for example, one line has 7 days but another only have 2 days.

I tried it in your way, but got error message for column number. Do you have a solution for this.

Thanks,
James
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@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/groups/opt_out.



--
dlali...@Google.com   5CC, Cambridge MA
daniel.l...@GMail.com 9 Juniper Ridge Road, Acton MA

Daniel LaLiberte

unread,
Aug 14, 2017, 8:35:52 AM8/14/17
to Google Visualization API
Can you show us some code that tries to do what you want?  

To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsubscr...@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/groups/opt_out.



--
dlali...@Google.com   5CC, Cambridge MA
daniel.l...@GMail.com 9 Juniper Ridge Road, Acton MA

--
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 google-visualization-api+unsub...@googlegroups.com.

James Yao

unread,
Aug 14, 2017, 9:58:12 PM8/14/17
to google-visua...@googlegroups.com
Hi Daniel,

Thanks for reply.

In GA, if you choose different date range, it will like below:
Inline image 1


But in my code for Google Chart, I must fill the all rest data for different range. Otherwise I'll get an column differ error.
Inline image 2

Do you have solution for this?

Thank you very much!

Regards,
James




--

--
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/Ko9HcJ-wtZ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-visualization-api+unsub...@googlegroups.com.

Francesco Pepe

unread,
Sep 15, 2018, 8:37:23 PM9/15/18
to Google Visualization API
It's possible to group two datatables?

var cols = [{"id":"ga:date","label":"date","type":"date"},{"id":"ga:sessions","label":"sessions","type":"number"},{"id":"ga:pageviews","label":"pageviews","type":"number"}];
var cols2 = [{"id":"ga:date","label":"date","type":"date"},{"id":"ga:sessions","label":"sessions","type":"number"},{"id":"ga:pageviews","label":"pageviews","type":"number"}];
var rows= [
{"c":[{"v":"Date(2018, 07, 16)"},{"v":"149"},{"v":"559"}]},
{"c":[{"v":"Date(2018, 07, 17)"},{"v":"158"},{"v":"529"}]},
{"c":[{"v":"Date(2018, 07, 18)"},{"v":"110"},{"v":"404"}]},
{"c":[{"v":"Date(2018, 07, 19)"},{"v":"96"},{"v":"350"}]},
{"c":[{"v":"Date(2018, 07, 20)"},{"v":"242"},{"v":"883"}]}
];

var rows2= [
{"c":[{"v":"Date(2018, 08, 16)"},{"v":"119"},{"v":"229"}]},
{"c":[{"v":"Date(2018, 08, 17)"},{"v":"118"},{"v":"319"}]},
{"c":[{"v":"Date(2018, 08, 18)"},{"v":"110"},{"v":"414"}]},
{"c":[{"v":"Date(2018, 08, 19)"},{"v":"161"},{"v":"310"}]},
{"c":[{"v":"Date(2018, 08, 20)"},{"v":"212"},{"v":"813"}]}
];

    var data = new google.visualization.DataTable({cols:cols,rows:rows});
    var data2 = new google.visualization.DataTable({cols:cols2,rows:rows2});
      
    var joinedData = google.visualization.data.join(data, data2, 'full', [[0,0], [1,1], [2,2]], [1], [1]);


Here's a fiddle: https://jsfiddle.net/Tropicalista/94f8bmL0/16/
Reply all
Reply to author
Forward
0 new messages