# Time series change rate calculations for displaying trend line chart

I'm struggling to find a solution to produce a line chart which shows a trend from a Time Series metric.

I try to make my line chart look similar to the following:

Basically, I want to make use of relative change rates, but I struggle to find a calculation which makes a visualization as seen above possible. The graph should always be relative to a specific time window, meaning that I can query my data dynamically with a start and end timestamp.

My (incomplete) sample data set is the following:

timestamp,value,change_rate,change_rate_absolute_sum,change_rate_delta,change_rate_sum
1426587900000,778;0.00129,0.00129;0.00129,0.00129
1426588200000,778;0,0.00129;-0.00129,0
1426588500000,1189;0.52828,0.52957;0.52828,0.52828
1426588800000,1195;0.00505,0.53462;-0.52323,0.00505
1426589100000,1195;0,0.53462;-0.00505,0
1426589400000,1196;0.00084,0.53546;0.00084,0.00084
1426589700000,1286;0.07525,0.61071;0.07441,0.07525
1426590000000,1290;0.00311,0.61382;-0.07214,0.00311
1426590300000,1294;0.0031,0.61692;-0.00001,0.0031
1426590600000,1296;0.00155,0.61847;-0.00155,0.00155
1426590900000,1356;0.0463,0.66477;0.04475,0.0463
1426591200000,1358;0.00147,0.66624;-0.04483,0.00147
1426591500000,1358;0,0.66624;-0.00147,0
1426591800000,1360;0.00147,0.66771;0.00147,0.00147
1426592100000,1408;0.03529,0.703;0.03382,0.03529
1426592400000,1390;-0.01278,0.69022;-0.04807,-0.01278
1426592700000,1391;0.00072,0.69094;0.0135,0.00072
1426593000000,1410;0.01366,0.7046;0.01294,0.01366
1426593300000,1414;0.00284,0.70744;-0.01082,0.00284
1426593600000,1410;-0.00283,0.70461;-0.00567,-0.00283
1426593900000,1414;0.00284,0.70745;0.00567,0.00284
1426594200000,1420;0.00424,0.71169;0.0014,0.00424
1426594500000,1417;-0.00211,0.70958;-0.00635,-0.00211


Any ideas warmly appreciated. Thanks a lot in advance!

While it is not very clear to me what specific relationships between your data you want to display, I can give some general advice. I think that this time series visualization calls for so-called diverging color scheme (as opposed to sequential, categorical and other ones). ColorBrewer is nice online tool for selecting an appropriate color scheme and other parameters (note that ColorBrewer's color schemes have built-in support in major data analysis and visualization software, such as R, Python, d3.js, plot.ly and others).

If you would use R environment, it would be quite easy to produce the time series trend line chart that you want by using ggplot2 package and its scale_color_gradient2() function:

... + scale_color_gradient2(midpoint=midValue, low="blue", mid="white", high="orange" )

A quick glance at your profile suggested me that you would prefer a JavaScript solution. In that case, I would advise to use d3.js library or one of multiple other JavaScript visualization libraries or packages. For more details, check this relevant answer of mine.

• Thanks a lot for your answer! Maybe my question is not very clear, but I struggle more with the calculation of the Y value than with the diverging color scheme. I have the absolute values of the metric, but that doesn't really help me showing the "speed" of change, and if the "speed" is positive compared to the last "step"/"time bucket". – Tobi Mar 17 '15 at 14:55
• A line gradient solution with D3 can be found at bl.ocks.org/d3noob/3e72cafd95e1834f599b and (better) codepen.io/ekrembk/pen/ights for example... – Tobi Mar 17 '15 at 15:03
• You're very welcome! I couldn't display the result of your most recently referred code, as it's anon. Anyway, I don't see calculation of speed/rate of change as a problem. Rate of chage $R_i$ (for a time delta $\Delta t_i$) = $\frac{Y_{i+1} - Y_i}{\Delta t_i}$. So your $Y$ for the trend chart should be the $R_i$. What is the problem with that? – Aleksandr Blekh Mar 17 '15 at 15:45
• @Tobi: Very easy (if I understand you correctly) - you just need to calculate second derivative (rate of change of rate of change) at your predefined intervals. Those will be your $Y$ values. – Aleksandr Blekh Mar 17 '15 at 16:04
• @Tobi: No need to apologize. Nobody knows everything :-). Yes, I believe that's the quadratic approximation. There are lots of resources on the topic. Have a look at this discussion, for example. Good luck and let me know, if I can be of more help! – Aleksandr Blekh Mar 17 '15 at 16:42