require 'daru/view'
Daru::View.plotting_library = :highcharts
# dynamic-update/
opts = {
chart: {
type: 'spline',
animation: "Highcharts.svg".js_code, # don't animate in old IE
marginRight: 10,
events: {
load: "function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}".js_code
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: "function () {
return '' + this.series.name + '
' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '
' +
Highcharts.numberFormat(this.y, 2);
}".js_code
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
}
series_dt = [
{
name: 'Random data',
data: "(function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
}())".js_code
}
]
dyn_update = Daru::View::Plot.new
dyn_update.chart.options = opts;
dyn_update.chart.series_data = series_dt
dyn_update.show_in_iruby
# dynamic-click-to-add/
opts = {
chart: {
type: 'scatter',
margin: [70, 50, 60, 80],
events: {
click: "function (e) {
// find the clicked values and the series
var x = Math.round(e.xAxis[0].value),
y = Math.round(e.yAxis[0].value),
series = this.series[0];
// Add it
series.addPoint([x, y]);
}".js_code
}
},
title: {
text: 'User supplied data'
},
subtitle: {
text: 'Click the plot area to add a point. Click a point to remove it.'
},
xAxis: {
gridLineWidth: 1,
minPadding: 0.2,
maxPadding: 0.2,
maxZoom: 60
},
yAxis: {
title: {
text: 'Value'
},
minPadding: 0.2,
maxPadding: 0.2,
maxZoom: 60,
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
plotOptions: {
series: {
lineWidth: 1,
point: {
events: {
'click': "function () {
if (this.series.data.length > 1) {
this.remove();
}
}".js_code
}
}
}
},
}
series_dt = [
{data: [[20, 20], [80, 80]]}
]
dyn_click_add = Daru::View::Plot.new
dyn_click_add.chart.options = opts;
dyn_click_add.chart.series_data = series_dt
dyn_click_add.show_in_iruby