Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal line to show average in dygraph

I am trying to add a horizontal line to dygraph which already show my time series data.

I have average of the complete data which I want to show as a static horizontal line on dygraph.

Does anyone know how to do that simply.

I already checked following links: http://dygraphs.com/tests/highlighted-region.html view-source:http://dygraphs.com/tests/crosshair.html

There must be some simple solution to this

like image 821
Chirag Tayal Avatar asked Apr 03 '13 00:04

Chirag Tayal


People also ask

How to add a horizontal average line to a chart?

Add a horizontal average line to a chart with a helper column. Note: Click to know more about applying the same formla to entire column, or applying the exact same formula/value to entire column without cell number incrementing. 2. And then select this range and choose one chart format that you want to insert,...

How do I set a rolling average in dygraphs?

A rolling average can be set using the text box in the lower left-hand corner of the graph (the showRoller attribute is what makes this appear). Also note that we've explicitly set the size of the chart div. Error Bars Another significant feature of the dygraphs library is the ability to display error bars around data series.

How to calculate standard deviation in dygraphs?

If a moving average is being displayed, dygraphs will compute the standard deviation of the average at each point. I.E. σ = sqrt( (σ12+ σ22+ ... + σn2) / n )

How does dygraph choose the color of the data?

The Dygraph automatically chose two different, easily-distinguishable colors for the two data series. The labels on the x-axis have switched from days to months. If you zoom in, they'll switch to weeks and then days. Some heuristics are used to determine a good vertical range for the data.


2 Answers

As danvk mentioned before, try specifying the "underlayCallback" option within your "new Dygraph()" call. Use HTML Canvas context to draw the line.

Example below: (xmin and xmax are your unix epoch time in milliseconds)

var yavg= 50, xmin=1357016400000, xmax=1359694800000;
new Dygraph(document.getElementById('graph1'), data,{
  (....other options....),
  underlayCallback:function(ctx,area,dygraph){      
    var xl = dygraph.toDomCoords(xmin,yavg);
    var xr = dygraph.toDomCoords(xmax,yavg);
    ctx.strokeStyle= 'green';
    ctx.beginPath();
    ctx.moveTo(xl[0],xl[1]);
    ctx.lineTo(xr[0],xr[1]);
    ctx.closePath();
    ctx.stroke();       
 }});
like image 150
user2916847 Avatar answered Oct 19 '22 23:10

user2916847


Your options are either to use an underlay callback (ala http://dygraphs.com/tests/highlighted-region.html) or to add a second, constant data series.

like image 39
danvk Avatar answered Oct 20 '22 00:10

danvk