{"id":17226,"date":"2018-12-21T15:20:06","date_gmt":"2018-12-21T15:20:06","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=17226"},"modified":"2026-01-12T10:03:19","modified_gmt":"2026-01-12T10:03:19","slug":"i-am-ruby-developer-how-can-i-use-highcharts","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/integration\/i-am-ruby-developer-how-can-i-use-highcharts\/","title":{"rendered":"I am a Ruby developer. How can I use Highcharts?"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Hey Rubyist! In this article, I will show you how to plot an interactive charts with just a few lines of Ruby code, thanks to Highcharts javascript library and <a href=\"https:\/\/github.com\/SciRuby\/daru-view\">Daru-view<\/a>.<\/p>\n<p>Daru-view (by <a href=\"http:\/\/sciruby.com\/\">Ruby Science Foundation<\/a>) is a plugin gem for <a href=\"https:\/\/github.com\/SciRuby\/daru\">Daru<\/a>; that allows users to generate charts using Highcharts javascript library in <a href=\"https:\/\/github.com\/SciRuby\/iruby\">IRuby notebook<\/a> and any Ruby web application framework (Rails\/ Sinatra\/ Hanami\/ Nanoc). So let\u2019s see how to create interactive charts by setting up three different charts using Highcharts, Highstock, and Highmaps.<\/p>\n<h4>Installation<\/h4>\n<p>The first step is to install Daru, and here are the steps to do so:<\/p>\n<ol>\n<li>Add the following line to your application\u2019s Gemfile:<code>gem 'daru-view', git: 'https:\/\/github.com\/SciRuby\/daru-view'<\/code>.<\/li>\n<li>Execute this command line $ bundle to launch the automatic installation or use this command line to install it manually <code>$ gem install daru-view<\/code>.<\/li>\n<\/ol>\n<h4>Highcharts and Daru-view<\/h4>\n<p>In this demo, I will create a simple line chart to display this data <code>[43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]<\/code>.<\/p>\n<p>I am using IRuby notebook to write the code, but feel free to use any other editor that suits you. Open your editor, then set up the plotting library globally to use in Ruby application\/program, and add the Highcharts library:<code>require 'daru\/view'    Daru::View.plotting_library = :highcharts<\/code><\/p>\n<p>Now, let\u2019s create the <code>Plot<\/code> object that will contain data and chart options :<\/p>\n<pre>lineChart_demo = Daru::View::Plot.new(\r\n\tdata: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]\r\n)\r\nlineChart_demo.show_in_iruby\r\n<\/pre>\n<p>Guess what, that is it, we are done \ud83d\ude42<\/p>\n<p>The code above plots the line chart in IRuby notebook cell:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-17232\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/12\/21150650\/lineChart.gif\" alt=\"\" width=\"1253\" height=\"636\" \/><\/p>\n<p>Let\u2019s check how to customize a chart by using Highcharts options to create a <a href=\"https:\/\/www.highcharts.com\/demo\/3d-pie\">3D pie chart<\/a> for example.<\/p>\n<p>We may have few (manually written) javascript code in Highcharts options.<\/p>\n<p>All you have to do is to write or add a few lines of JavaScript in the object Highcharts options, then pass the object as a parameter into the <code>Plot<\/code> object (Check the code below):<\/p>\n<pre>data= [{\r\n    type: 'pie',\r\n    name: 'Browser share',\r\n    data: [\r\n        ['Firefox', 45.0],\r\n        ['IE', 26.8],\r\n        {\r\n            name: 'Chrome',\r\n            y: 12.8,\r\n            sliced: true,\r\n            selected: true\r\n        },\r\n        ['Safari', 8.5],\r\n        ['Opera', 6.2],\r\n        ['Others', 0.7]\r\n    ]\r\n}]\r\noptions = {\r\nchart: {\r\n    type: 'pie',\r\n    options3d: {\r\n        enabled: true,\r\n        alpha: 45,\r\n        beta: 0\r\n    }\r\n},\r\ntitle: {\r\n    text: 'Browser market shares at a specific website, 2014'\r\n},\r\ntooltip: {\r\n    pointFormat: '{series.name}: <b>{point.percentage:.1f}%<\/b>'\r\n},\r\nplotOptions: {\r\n    pie: {\r\n        allowPointSelect: true,\r\n        cursor: 'pointer',\r\n        depth: 35,\r\n        dataLabels: {\r\n            enabled: true,\r\n            format: '{point.name}',\r\n            style: {\r\n                  color: \"(Highcharts.theme &amp;&amp; Highcharts.theme.contrastTextColor) || 'red'\".js_code\r\n            },\r\n            connectorColor: 'silver'\r\n        }\r\n    }\r\n}\r\n}\r\npie_chart = Daru::View::Plot.new(data, options)\r\npie_chart.show_in_iruby\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-17233\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/12\/21150719\/3DPieChart.gif\" alt=\"\" width=\"1253\" height=\"636\" \/><\/p>\n<h4>Highstock and Daru-view<\/h4>\n<p>The process to create a Highstock chart is almost similar to Highcharts; you have to add the data and the options, then pass them as parameters to the Plot object. The only difference is to add another parameter chart_class: &#8216;stock&#8217; to indicate the chart type as a stock chart.<\/p>\n<p>The code below displays a dynamic stock chart:<\/p>\n<pre>opts = {\r\n    chart: {\r\n      events: {\r\n          load: \"function () {\r\n\r\n              \/\/ set up the updating of the chart each second\r\n              var series = this.series[0];\r\n              setInterval(function () {\r\n                  var x = (new Date()).getTime(), \/\/ current time\r\n                      y = Math.round(Math.random() * 100);\r\n                  series.addPoint([x, y], true, true);\r\n              }, 1000);\r\n          }\".js_code\r\n      }\r\n  },\r\n    rangeSelector: {\r\n        buttons: [{\r\n            count: 1,\r\n            type: 'minute',\r\n            text: '1M'\r\n        }, {\r\n            count: 5,\r\n            type: 'minute',\r\n            text: '5M'\r\n        }, {\r\n            type: 'all',\r\n            text: 'All'\r\n        }],\r\n        inputEnabled: false,\r\n        selected: 0\r\n    },\r\n    title: {\r\n        text: 'Live random data'\r\n    },\r\n    exporting: {\r\n        enabled: false\r\n    }\r\n  }\r\n  series_dt = [{\r\n    name: 'Random data',\r\n    data: \"(function () {\r\n        \/\/ generate an array of random data\r\n        var data = [],\r\n            time = (new Date()).getTime(),\r\n            i;\r\n\r\n        for (i = -999; i &lt;= 0; i += 1) {\r\n            data.push([\r\n                time + i * 1000,\r\n                Math.round(Math.random() * 100)\r\n            ]);\r\n        }\r\n        return data;\r\n    }())\".js_code\r\n  }]\r\ndynamic_data = Daru::View::Plot.new(series_dt, opts, chart_class: 'stock')\r\ndynamic_data.show_in_iruby\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-17234\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/12\/21150743\/stockChart.gif\" alt=\"\" width=\"1253\" height=\"636\" \/><\/p>\n<p>For more details and example about how to use Highstock with daru-view, feel free to check this <a href=\"https:\/\/github.com\/SciRuby\/daru-view\/wiki\/HighCharts-features#highstock\">wiki page<\/a>.<\/p>\n<h4>Highmap and Daru-view<\/h4>\n<p>Well, I guess you get it; creating <a href=\"https:\/\/www.highcharts.com\/maps\/demo\/doubleclickzoomto\">Highmaps chart<\/a> is not different than Highcharts or Highstock. And again don\u2019t forget to specify the chart type in the <code>Plot<\/code> object <code>chart_class: 'map'<\/code>.<\/p>\n<pre># To import data from json url\r\n    require 'daru\/io'\r\n    worldDataFrame = Daru::DataFrame.read_json(    'https:\/\/cdn.rawgit.com\/highcharts\/highcharts\/680f5d50a47e90f53d814b53f80ce1850b9060c0\/samples\/data\/world-population-density.json',\r\n      )\r\n    # we don\u2019t want less than 1 value\r\n    value = value.map{ |elem| elem &lt; 1 ? 1 : elem}\r\n    worldDataFrame['value'] = value\r\n    options = {  \r\n      chart: {\r\n                map: 'custom\/world'\r\n            },\r\n            title: {\r\n                text: 'Zoom in on country by double click'\r\n            },\r\n            mapNavigation: {\r\n                enabled: true,\r\n                enableDoubleClickZoomTo: true\r\n            },\r\n            colorAxis: {\r\n                min: 1,\r\n                max: 1000,\r\n                type: 'logarithmic'\r\n            }\r\n      }\r\n    data = [{\r\n                data: worldDataFrame.to_json,\r\n                joinBy: ['iso-a3', 'code3'],\r\n                name: 'Population density',\r\n                states: {\r\n                    hover: {\r\n                        color: '#a4edba'\r\n                    }\r\n                },\r\n                tooltip: {\r\n                    valueSuffix: '\/km\u00b2'\r\n                }\r\n            }]\r\n    world_map = Daru::View::Plot.new(data, options, chart_class: 'map')\r\n    world_map.show_in_iruby\r\n<\/pre>\n<p>This result is the following world map:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-17235\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2018\/12\/21150805\/mapChart.gif\" alt=\"\" width=\"1253\" height=\"636\" \/><\/p>\n<p>I hope you have enjoyed this article as much as I did writing it. Feel free to share your experience with Daru-view and Highcharts in the comment below or <a href=\"https:\/\/groups.google.com\/forum\/#!forum\/sciruby-dev\">SciRuby Group<\/a>, <a href=\"https:\/\/github.com\/SciRuby\/daru-view\/issues\">open an issue<\/a>, and contribute by <a href=\"https:\/\/github.com\/SciRuby\/daru-view\/pulls\">creating new PR<\/a>. You are welcome to visit these links to expand your knowledge about the subject:<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/SciRuby\/daru-view\/wiki\/GSoC-2018---Progress-Report#features-related-to-highcharts\">Works done in GSoC 2018 for HighChart library.<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/SciRuby\/daru-view\/wiki\/Why-to-use-daru-view-%3F\">Why should I use daru-view?<\/a><\/li>\n<li><a href=\"http:\/\/nbviewer.jupyter.org\/github\/sciruby\/daru-view\/tree\/master\/spec\/dummy_iruby\/\">IRuby notebook.<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/SciRuby\/daru-view\/wiki\">daru-view wiki page.<\/a><\/li>\n<li><a href=\"http:\/\/sciruby.com\/blog\/2017\/09\/01\/gsoc-2017-data-visualization-using-daru-view\/\">SciRuby blog about initial version of the daru-view gem.<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview of how to use daru-view and highcharts javascript library to create interactive chart easily in ruby code and IRuby notebook.<\/p>\n","protected":false},"author":234,"featured_media":17256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[1105],"tags":[1094],"coauthors":[765],"class_list":["post-17226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration","tag-highcharts-core"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/17226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/234"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=17226"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/17226\/revisions"}],"predecessor-version":[{"id":29158,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/17226\/revisions\/29158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/17256"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=17226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=17226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=17226"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=17226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}