{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'daru/view'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " /* BEGIN google_visualr.js */\n", "\n", "if(!window['googleLT_']){window['googleLT_']=(new Date()).getTime();}if (!window['google']) {\n", "window['google'] = {};\n", "}\n", "if (!window['google']['loader']) {\n", "window['google']['loader'] = {};\n", "google.loader.ServiceBase = 'https://www.google.com/uds';\n", "google.loader.GoogleApisBase = 'https://ajax.googleapis.com/ajax';\n", "google.loader.ApiKey = 'notsupplied';\n", "google.loader.KeyVerified = true;\n", "google.loader.LoadFailure = false;\n", "google.loader.Secure = true;\n", "google.loader.GoogleLocale = 'www.google.com';\n", "google.loader.ClientLocation = null;\n", "google.loader.AdditionalParams = '';\n", "(function() {function g(a){return a in l?l[a]:l[a]=-1!=navigator.userAgent.toLowerCase().indexOf(a)}var l={};function m(a,b){var c=function(){};c.prototype=b.prototype;a.ca=b.prototype;a.prototype=new c}function n(a,b,c){var d=Array.prototype.slice.call(arguments,2)||[];return function(){return a.apply(b,d.concat(Array.prototype.slice.call(arguments)))}}function p(a){a=Error(a);a.toString=function(){return this.message};return a}\n", "function q(a,b){a=a.split(/\\./);for(var c=window,d=0;d\\x3c/script>\"):(g(\"safari\")||g(\"konqueror\"))&&window.setTimeout(B,10)),x.push(a)):y(window,\"load\",a)};t(\"google.setOnLoadCallback\",google.ba);\n", "function y(a,b,c){if(a.addEventListener)a.addEventListener(b,c,!1);else if(a.attachEvent)a.attachEvent(\"on\"+b,c);else{var d=a[\"on\"+b];a[\"on\"+b]=null!=d?C([c,d]):c}}function C(a){return function(){for(var b=0;b\\x3c/script>'):\"css\"==a&&document.write('')};\n", "t(\"google.loader.writeLoadTag\",google.loader.f);google.loader.Z=function(a){w=a};t(\"google.loader.rfm\",google.loader.Z);google.loader.aa=function(a){for(var b in a)\"string\"==typeof b&&b&&\":\"==b.charAt(0)&&!v[b]&&(v[b]=new E(b.substring(1),a[b]))};t(\"google.loader.rpl\",google.loader.aa);google.loader.$=function(a){if((a=a.specs)&&a.length)for(var b=0;b\\x3c/script>')},K.Ki=function(b){var c=K.global.document,d=c.createElement(\"script\");d.type=C;d.src=b;d.defer=!1;d.async=!1;c.head.appendChild(d)},\n", "K.Tl=function(b,c){if(K.qg()){var d=K.global.document;if(!K.De&&d.readyState==t){if(/\\bdeps.js$/.test(b))return!1;throw Error('Cannot write \"'+b+'\" after document load');}void 0===c?K.di?(K.$g=!0,c=\" onreadystatechange='goog.onScriptLoad_(this, \"+ ++K.Pg+\")' \",d.write(n+b+'\"'+c+\">\\x3c/script>\")):K.De?K.Ki(b):K.Sl(b):d.write('\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "presidents_chart = Daru::View::Plot.new(df_presidents, type: :timeline)\n", "presidents_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Labeling the bars" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[\"George Washington\", \"John Adams\", \"Thomas Jefferson\"]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_presidents['Name'] = ['George Washington', 'John Adams', 'Thomas Jefferson']" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[\"1\", \"2\", \"3\"]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_presidents['Term'] = ['1', '2', '3']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ " Daru::DataFrame(3x4) \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", "
StartEndNameTerm
01789-03-30T00:00:00+00:001797-02-04T00:00:00+00:00George Washington1
11797-02-04T00:00:00+00:001801-02-04T00:00:00+00:00John Adams2
21801-02-04T00:00:00+00:001809-02-04T00:00:00+00:00Thomas Jefferson3
" ], "text/plain": [ "#\n", " Start End Name Term\n", " 0 1789-03-30 1797-02-04 George Was 1\n", " 1 1797-02-04 1801-02-04 John Adams 2\n", " 2 1801-02-04 1809-02-04 Thomas Jef 3" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_presidents.delete_vector('President')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[\"Term\", \"Name\", \"Start\", \"End\"]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_presidents.order = ['Term', 'Name', 'Start', 'End']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ " Daru::DataFrame(3x4) \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", "
TermNameStartEnd
01George Washington1789-03-30T00:00:00+00:001797-02-04T00:00:00+00:00
12John Adams1797-02-04T00:00:00+00:001801-02-04T00:00:00+00:00
23Thomas Jefferson1801-02-04T00:00:00+00:001809-02-04T00:00:00+00:00
" ], "text/plain": [ "#\n", " Term Name Start End\n", " 0 1 George Was 1789-03-30 1797-02-04\n", " 1 2 John Adams 1797-02-04 1801-02-04\n", " 2 3 Thomas Jef 1801-02-04 1809-02-04" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_presidents" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "presidents_label_chart = Daru::View::Plot.new(df_presidents, type: :timeline)\n", "presidents_label_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### An advanced example" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = [\n", " [ 'Position', 'Name', 'Start' ,'End'],\n", " [ 'President', 'George Washington', DateTime.new(1789, 3, 30), DateTime.new(1797, 2, 4) ],\n", " [ 'President', 'John Adams', DateTime.new(1797, 2, 4), DateTime.new(1801, 2, 4) ],\n", " [ 'President', 'Thomas Jefferson', DateTime.new(1801, 2, 4), DateTime.new(1809, 2, 4) ],\n", " [ 'Vice President', 'John Adams', DateTime.new(1789, 3, 21), DateTime.new(1797, 2, 4)],\n", " [ 'Vice President', 'Thomas Jefferson', DateTime.new(1797, 2, 4), DateTime.new(1801, 2, 4)],\n", " [ 'Vice President', 'Aaron Burr', DateTime.new(1801, 2, 4), DateTime.new(1805, 2, 4)],\n", " [ 'Vice President', 'George Clinton', DateTime.new(1805, 2, 4), DateTime.new(1812, 3, 20)],\n", " [ 'Secretary of State', 'John Jay', DateTime.new(1789, 8, 25), DateTime.new(1790, 2, 22)],\n", " [ 'Secretary of State', 'Thomas Jefferson',DateTime.new(1790, 2, 22), DateTime.new(1793, 11, 30)],\n", " [ 'Secretary of State', 'Edmund Randolph', DateTime.new(1794, 1, 2), DateTime.new(1795, 7, 20)],\n", " [ 'Secretary of State', 'Timothy Pickering', DateTime.new(1795, 7, 20), DateTime.new(1800, 4, 12)],\n", " [ 'Secretary of State', 'Charles Lee', DateTime.new(1800, 4, 13), DateTime.new(1800, 5, 5)],\n", " [ 'Secretary of State', 'John Marshall', DateTime.new(1800, 5, 13), DateTime.new(1801, 2, 4)],\n", " [ 'Secretary of State', 'Levi Lincoln', DateTime.new(1801, 2, 5), DateTime.new(1801, 4, 1)],\n", " [ 'Secretary of State', 'James Madison', DateTime.new(1801, 4, 2), DateTime.new(1809, 2, 3)]\n", " \n", " ]\n", "\n", "timeline_adv_table = Daru::View::Table.new(data, pageSize: 6)\n", "timeline_adv_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "position_chart = Daru::View::Plot.new(timeline_adv_table, type: :timeline)\n", "position_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Putting bars on one row" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "presidents_label_chart = Daru::View::Plot.new(df_presidents, type: :timeline)\n", "presidents_label_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "presidents_label_chart = Daru::View::Plot.new(df_presidents, type: :timeline, timeline: { groupByRowLabel: false })\n", "presidents_label_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " cols: [{ type: 'string', id: 'Role' },\n", " { type: 'string', label: 'Name' },\n", " { type: 'date', label: 'Start' },{ type: 'date', label: 'End' }\n", " ],\n", " rows: [{c:[{v: 'President'}, {v: 'George Washington'}, {v: DateTime.new(1789, 3, 30)}, {v: DateTime.new(1797, 2, 4)}]},\n", " {c:[{v: 'President'}, {v: 'John Adams'}, {v: DateTime.new(1797, 2, 4)}, {v: DateTime.new(1801, 2, 4) }]},\n", " {c:[{v: 'President'}, {v: 'Thomas Jefferson'}, {v: DateTime.new(1801, 2, 4)}, {v: DateTime.new(1809, 2, 4)}]},\n", " ]\n", " }\n", "timeline_1row_table = Daru::View::Table.new(data, height: 300, width: 200)\n", "timeline_1row_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, type: :timeline, timeline: { groupByRowLabel: true })\n", "presidents_1row_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, type: :timeline, timeline: { groupByRowLabel: false })\n", "presidents_1row_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Controlling the colors" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = [\n", " [ 'Room', 'Name', 'Start' ,'End'],\n", " [ 'Magnolia Room', 'Beginning JavaScript', DateTime.new(2017,7,31,12,0,0), DateTime.new(2017,7,31,13,30,0) ],\n", " [ 'Magnolia Room', 'Intermediate JavaScript', DateTime.new(2017,7,31,14,0,0), DateTime.new(2017,7,31,15,30,0) ],\n", " [ 'Magnolia Room', 'Advanced JavaScript', DateTime.new(2017,7,31,16,0,0), DateTime.new(2017,7,31,17,30,0) ],\n", " [ 'Willow Room', 'Beginning Google Charts', DateTime.new(2017,7,31,12,30,0), DateTime.new(2017,7,31,14,0,0) ],\n", " [ 'Willow Room', 'Intermediate Google Charts', DateTime.new(2017,7,31,14,30,0), DateTime.new(2017,7,31,16,0,0) ],\n", " [ 'Willow Room', 'Advanced Google Charts', DateTime.new(2017,7,31,16,30,0), DateTime.new(2017,7,31,18,0,0) ]\n", " \n", " ]\n", "\n", "timeline_color_table = Daru::View::Table.new(data, pageSize: 6)\n", "timeline_color_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeline_color_chart = Daru::View::Plot.new(timeline_color_table.table, type: :timeline, timeline: { colorByRowLabel: true })\n", "timeline_color_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeline_single_color_chart = Daru::View::Plot.new(timeline_color_table.table, type: :timeline, timeline: { singleColor: '#8d8' })\n", "timeline_single_color_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "opts = {\n", " timeline: { colorByRowLabel: true },\n", " backgroundColor: '#ffd',\n", " type: :timeline\n", " }\n", "timeline_bgcolor_chart = Daru::View::Plot.new(timeline_color_table.table, opts)\n", "timeline_bgcolor_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "opts = {\n", "colors: ['#cbb69d', '#603913', '#c69c6e'],\n", " type: :timeline\n", " }\n", "timeline_bgcolor_chart = Daru::View::Plot.new(timeline_color_table.table, opts)\n", "timeline_bgcolor_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Changing the fonts" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "opts = {\n", " colors: ['#cbb69d', '#603913', '#c69c6e'],\n", " timeline: { rowLabelStyle: {fontName: 'Helvetica', fontSize: 24, color: '#603913' },\n", " barLabelStyle: { fontName: 'Garamond', fontSize: 14 } },\n", " type: :timeline\n", " }\n", "timeline_font_chart = Daru::View::Plot.new(timeline_color_table.table, opts)\n", "timeline_font_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Overlapping gridlines" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, type: :timeline, timeline: { groupByRowLabel: true })\n", "presidents_1row_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "opts = {\n", " timeline: { showRowLabels: false },\n", " # FixMe : not workin. refer : https://developers.google.com/chart/interactive/docs/gallery/timeline\n", " avoidOverlappingGridLines: false,\n", " type: :timeline\n", " }\n", "presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, opts)\n", "presidents_1row_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customizing tooltips" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " cols: [{ type: 'string', id: 'President' },\n", " { type: 'string', label: 'dummy bar label' },\n", " { type: 'string', role: 'tooltip' },\n", " { type: 'date', label: 'Start' },{ type: 'date', label: 'End' }\n", " ],\n", " rows: [{c:[{v: 'Washington'}, {v: nil},{v: 'George'}, {v: DateTime.new(1789, 3, 30)}, {v: DateTime.new(1797, 2, 4)}]},\n", " {c:[{v: 'Adams'}, {v: nil},{v: 'John'}, {v: DateTime.new(1797, 2, 4)}, {v: DateTime.new(1801, 2, 4) }]},\n", " {c:[{v: 'Jefferson'}, {v: nil},{v: 'Thomas'}, {v: DateTime.new(1801, 2, 4)}, {v: DateTime.new(1809, 2, 4)}]},\n", " ]\n", " }\n", "timeline_tooltip_table = Daru::View::Table.new(data, height: 300, width: 200)\n", "timeline_tooltip_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "\"\\n
\\n\\n\"" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "opts = {\n", " type: :timeline\n", " }\n", "presidents_tooltip_chart = Daru::View::Plot.new(timeline_tooltip_table.table, opts)\n", "presidents_tooltip_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.5.3", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }