require 'daru/view' Daru::View.plotting_library = :highcharts # bar-basic chart : basic opts = { chart: { type: 'bar', }, title: { text: 'Historic World Population by Region' }, subtitle: { text: 'Source: Wikipedia.org' }, xAxis: { categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'], title: { text: nil } }, yAxis: { min: 0, title: { text: 'Population (millions)', align: 'high' }, labels: { overflow: 'justify' } }, tooltip: { valueSuffix: ' millions' }, plotOptions: { bar: { dataLabels: { enabled: true } } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'top', x: -40, y: 80, floating: true, borderWidth: 1, backgroundColor: "((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF')".js_code, shadow: true }, credits: { enabled: false }, } series_dt = [ { name: 'Year 1800', data: [107, 31, 635, 203, 2] }, { name: 'Year 1900', data: [133, 156, 947, 408, 6] }, { name: 'Year 2012', data: [1052, 954, 4250, 740, 38] } ] bar_basic = Daru::View::Plot.new(series_dt, opts) bar_basic.show_in_iruby # bar-basic chart : stacked opts = { chart: { type: 'bar' }, title: { text: 'Stacked bar chart' }, xAxis: { categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas'] }, yAxis: { min: 0, title: { text: 'Total fruit consumption' } }, legend: { reversed: true }, plotOptions: { series: { stacking: 'normal' } }, } series_dt = [ { name: 'John', data: [5, 3, 4, 7, 2] }, { name: 'Jane', data: [2, 2, 3, 2, 1] }, { name: 'Joe', data: [3,4,4,2,5] } ] bar_stacked = Daru::View::Plot.new(series_dt, opts) bar_stacked.show_in_iruby # bar-basic chart : bar-negative-stack/ categories = ['0-4', '5-9', '10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40-44', '45-49', '50-54', '55-59', '60-64', '65-69', '70-74', '75-79', '80-84', '85-89', '90-94', '95-99', '100 + '] opts = { chart: { type: 'bar' }, title: { text: 'Population pyramid for Germany, 2015' }, subtitle: { text: 'Source: Population Pyramids of the World from 1950 to 2100' }, xAxis: [{ categories: categories, reversed: false, labels: { step: 1 } }, { # mirror axis on right side opposite: true, reversed: false, categories: categories, linkedTo: 0, labels: { step: 1 } }], yAxis: { title: { text: nil }, labels: { formatter: "function () { return Math.abs(this.value) + '%'; }".js_code } }, plotOptions: { series: { stacking: 'normal' } }, tooltip: { formatter: "function () { return '' + this.series.name + ', age ' + this.point.category + '
' + 'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0); }".js_code }, } series_dt = [ { name: 'Male', data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2, -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4, -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0] }, { name: 'Female', data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9, 3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9, 1.8, 1.2, 0.6, 0.1, 0.0] } ] bar_neg_stacked = Daru::View::Plot.new(series_dt, opts) bar_neg_stacked.show_in_iruby