{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions.\n", "\n", "Install the mechanize gem version ~>2.7.5 for using mechanize functions.\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'daru/view'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "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.Mi=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.Wl=function(b,c){if(K.tg()){var d=K.global.document;if(!K.Ge&&d.readyState==t){if(/\\bdeps.js$/.test(b))return!1;throw Error('Cannot write \"'+b+'\" after document load');}void 0===c?K.fi?(K.eh=!0,c=\" onreadystatechange='goog.onScriptLoad_(this, \"+ ++K.Sg+\")' \",d.write(n+b+'\"'+c+\">\\x3c/script>\")):K.Ge?K.Mi(b):K.Vl(b):d.write('\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_basic_table = Daru::View::Table.new(df_city_pop)\n", "bar_basic_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_basic_options = {\n", " title: 'Population of Largest U.S. Cities',\n", " type: :bar,\n", "}\n", "bar_basic_chart = Daru::View::Plot.new(bar_basic_table.table, bar_basic_options)\n", "bar_basic_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_basic_options = {\n", " title: 'Population of Largest U.S. Cities',\n", " chartArea: {width: '50%'},\n", " hAxis: {\n", " title: 'Total Population',\n", " minValue: 0\n", " },\n", " vAxis: {\n", " title: 'City'\n", " },\n", " type: :bar,\n", " height: 400\n", "}\n", "bar_basic_chart = Daru::View::Plot.new(bar_basic_table.table, bar_basic_options)\n", "bar_basic_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bar chart With Styling " ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Note: If youwant to use role, label, pattern, etc column, then directly use the google datatable \n", "# (don't create the daru dataframe/vector, since daru dataframe vectors can be string or number. So can't be a hash )\n", "# Refer this link for more datatable options : https://developers.google.com/chart/interactive/docs/reference\n", "data = {\n", " cols: [{id: 'Element', label: 'Element', type: 'string'},\n", " {id: 'Density', label: 'Density', type: 'number'},\n", " {type: 'string', role: \"style\" },\n", " ],\n", " rows: [{c:[{v: 'Copper'}, {v: 8.94}, {v: '#b87333'}]},\n", " {c:[{v: 'Silver'}, {v: 10.49}, {v: 'silver'}]},\n", " {c:[{v: 'Gold'}, {v: 19.30}, {v: 'gold'}]},\n", " {c:[{v: 'Platinum'}, {v: 21.45}, {v: '#e5e4e2'}]}]\n", " }\n", "bar_table = Daru::View::Table.new(data, height: 300, width: 200)\n", "bar_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "(pry):173: warning: key :height is duplicated and overwritten on line 177\n" ] }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_options = {\n", "title: \"Density of Precious Metals, in g/cm^3\",\n", " width: 600,\n", " height: 400,\n", " bar: {groupWidth: \"95%\"},\n", " legend: { position: \"none\" },\n", " type: :bar,\n", " height: 400\n", "}\n", "bar_chart = Daru::View::Plot.new(bar_table, bar_options)\n", "bar_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " cols: [{id: 'Element', label: 'Element', type: 'string'},\n", " {id: 'Density', label: 'Density', type: 'number'},\n", " {type: 'string', role: \"style\" },\n", " ],\n", " rows: [\n", " {c:[{v: 'Copper'}, {v: 8.94}, {v: 'color: gray'}]},\n", " {c:[{v: 'Silver'}, {v: 10.49}, {v: 'color: #76A7FA'}]},\n", " {c:[{v: 'Gold'}, {v: 19.30}, {v: 'opacity: 0.2'}]},\n", " {c:[{v: 'Platinum'}, {v: 21.45}, {v: 'stroke-color: #703593; stroke-width: 4; fill-color: #C5A5CF'}]},\n", " {c:[{v: 'Other'}, {v: 31.45}, {v: 'stroke-color: #871B47; stroke-opacity: 0.6; stroke-width: 8; fill-color: #BC5679; fill-opacity: 0.2'}]}\n", " ]\n", " }\n", "bar_table = Daru::View::Table.new(data, height: 300, width: 200)\n", "bar_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "(pry):222: warning: key :height is duplicated and overwritten on line 226\n" ] }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_options = {\n", "title: \"Density of Precious Metals, in g/cm^3\",\n", " width: 600,\n", " height: 400,\n", " bar: {groupWidth: \"95%\"},\n", " legend: { position: \"none\" },\n", " type: :bar,\n", " height: 400\n", "}\n", "bar_chart = Daru::View::Plot.new(bar_table, bar_options)\n", "bar_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " cols: [{id: 'Element', label: 'Element', type: 'string'},\n", " {id: 'Density', label: 'Density', type: 'number'},\n", " {type: 'string', role: \"style\" },{type: 'string', role: \"annotation\" }\n", " ],\n", " rows: [{c:[{v: 'Copper'}, {v: 8.94}, {v: '#b87333'}, {v: 'Cu'}]},\n", " {c:[{v: 'Silver'}, {v: 10.49}, {v: 'silver'}, {v: 'Ag'}]},\n", " {c:[{v: 'Gold'}, {v: 19.30}, {v: 'gold'}, {v: 'Au'}]},\n", " {c:[{v: 'Platinum'}, {v: 21.45}, {v: '#e5e4e2'}, {v: 'Pt'}]}]\n", " }\n", "bar_table = Daru::View::Table.new(data)\n", "bar_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "(pry):268: warning: key :height is duplicated and overwritten on line 272\n" ] }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_options = {\n", "title: \"Density of Precious Metals, in g/cm^3\",\n", " width: 600,\n", " height: 400,\n", " bar: {groupWidth: \"95%\"},\n", " legend: { position: \"none\" },\n", " type: :bar,\n", " height: 400\n", "}\n", "bar_chart = Daru::View::Plot.new(bar_table, bar_options)\n", "bar_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stacked bar charts" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " cols: [{id: 'Genre', label: 'Genre', type: 'string'},\n", " {id: 'Fantasy & Sci Fi', label: 'Fantasy & Sci Fi', type: 'number'},\n", " {id: 'Romance', label: 'Romance', type: 'number'},\n", " {id: 'Mystery/Crime', label: 'Mystery/Crime', type: 'number'},\n", " {id: 'General', label: 'General', type: 'number'},\n", " {id: 'Western', label: 'Western', type: 'number'},\n", " {id: 'Literature', label: 'Literature', type: 'number'},\n", " {type: 'string', role: \"annotation\" }\n", " ],\n", " rows: [{c:[{v: '2010'}, {v: 10}, {v: 24}, {v: 20}, {v: 32}, {v: 18}, {v: 5}, {v: ''}]},\n", " {c:[{v: '2020'}, {v: 16}, {v: 22}, {v: 23}, {v: 30}, {v: 16}, {v: 9}, {v: ''}]},\n", " {c:[{v: '2030'}, {v: 28}, {v: 19}, {v: 29}, {v: 30}, {v: 12}, {v: 13}, {v: ''}]},\n", " ]\n", " \n", " }\n", "bar_stacked_table = Daru::View::Table.new(data)\n", "bar_stacked_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_stacked_options = {\n", " width: 600,\n", " height: 400,\n", " legend: { position: 'top', maxLines: 3 },\n", " bar: { groupWidth: '75%' },\n", " isStacked: true,\n", " type: :bar,\n", "}\n", "bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)\n", "bar_stacked_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Same data in array of rows. Not including annotation role\n", "data = [\n", " ['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',\n", " 'Western', 'Literature' ],\n", " ['2010', 10, 24, 20, 32, 18, 5],\n", " ['2020', 16, 22, 23, 30, 16, 9],\n", " ['2030', 28, 19, 29, 30, 12, 13]\n", " ]\n", "bar_stacked_table = Daru::View::Table.new(data)\n", "bar_stacked_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_stacked_options = {\n", " width: 600,\n", " height: 400,\n", " isStacked: true,\n", " type: :bar,\n", "}\n", "bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)\n", "bar_stacked_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Percent\n", "bar_stacked_options = {\n", " width: 600,\n", " height: 400,\n", " isStacked: 'percent',\n", " type: :bar,\n", "}\n", "bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)\n", "bar_stacked_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can manage the x axis ticks\n", "bar_stacked_options = {\n", " width: 600,\n", " height: 400,\n", " isStacked: 'percent',\n", " type: :bar,\n", " hAxis: {\n", " minValue: 0,\n", " ticks: [0, 0.3, 0.6, 0.9, 1]\n", " }\n", "}\n", "bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)\n", "bar_stacked_chart.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TODO : Work on Material bar charts . It is defined in google_visualr: https://developers.google.com/chart/interactive/docs/gallery/barchart**" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Same data in array of rows. Not including annotation role\n", "data = [\n", " ['Year', 'Sales', 'Expenses', 'Profit'],\n", " ['2014', 1000, 400, 200],\n", " ['2015', 1170, 460, 250],\n", " ['2016', 660, 1120, 300],\n", " ['2017', 1030, 540, 350]\n", " ]\n", "bar_multiple_table = Daru::View::Table.new(data)\n", "bar_multiple_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can manage the x axis ticks\n", "bar_multiple_options = {\n", "chart: {\n", " title: 'Company Performance',\n", " subtitle: 'Sales, Expenses, and Profit: 2014-2017',\n", " },\n", " bars: 'horizontal',\n", " type: :bar, height: 400, widht: 400\n", "}\n", "bar_multiple_chart = Daru::View::Plot.new(bar_multiple_table.table, bar_multiple_options)\n", "bar_multiple_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Same data in array of rows. Not including annotation role\n", "data = [\n", " ['Galaxy', 'Distance', 'Brightness'],\n", " ['Canis Major Dwarf', 8000, 230.3],\n", " ['Sagittarius Dwarf', 24000, 4000.5],\n", " ['Ursa Major II Dwarf', 30000, 1412.3],\n", " ['Lg. Magellanic Cloud', 50000, 120.9],\n", " ['Bootes I', 60000, 1223.1]\n", " ]\n", "bar_multiple_table = Daru::View::Table.new(data)\n", "bar_multiple_table.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\"
\\n\\n\"" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can manage the x axis ticks\n", "bar_multiple_options = {\n", "width: 800,\n", "\n", " title: 'Nearby galaxies',\n", " subtitle: 'distance on the left, brightness on the right',\n", " bars: 'horizontal', # Required for Material Bar Charts.\n", " series: {\n", " 0 => { axis: 'brightness' }, # Bind series 0 to an axis named 'distance'.\n", " 1 => { axis: 'distance' } # Bind series 1 to an axis named 'brightness'.\n", " },\n", " axes: {\n", " x: {\n", " distance: {label: 'parsecs'}, # Bottom x-axis.\n", " brightness: {side: 'top', label: 'apparent magnitude'} # Top x-axis.\n", " }\n", " },\n", " type: :bar, height: 400\n", "}\n", "bar_multiple_chart = Daru::View::Plot.new(bar_multiple_table.table, bar_multiple_options)\n", "bar_multiple_chart.show_in_iruby" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.3.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.3.1" } }, "nbformat": 4, "nbformat_minor": 2 }