1+ // We need to override Rickshaw _frequentInterval detection for bar charts due to this issue:
2+ // https://github.com/shutterstock/rickshaw/issues/461
3+ SmartResolutionBarRenderer = Rickshaw . Class . create ( Rickshaw . Graph . Renderer . Bar , {
4+ defaults : function ( $super ) {
5+
6+ var defaults = Rickshaw . extend ( $super ( ) , {
7+ gapSize : 0.05 ,
8+ unstack : false ,
9+ resolution : 'minute'
10+ } ) ;
11+
12+ delete defaults . tension ;
13+ return defaults ;
14+ } ,
15+
16+ initialize : function ( $super , args ) {
17+ args = args || { } ;
18+ this . resolution = args . resolution || this . resolution ;
19+ $super ( args ) ;
20+ } ,
21+
22+ _frequentInterval : function ( data ) {
23+ var resolutionDuration = moment . duration ( 1 , this . resolution ) ;
24+ return { count : 100 , magnitude : resolutionDuration . asSeconds ( ) } ;
25+ }
26+ } ) ;
27+
28+ rickshawHelper = {
29+ getRenderer : function ( renderer ) {
30+ if ( renderer == "bar" ) {
31+ return SmartResolutionBarRenderer ;
32+ }
33+ return renderer ;
34+ } ,
35+
36+ // Add two points before the actual data if needed to ensure charts look
37+ // good while we show the whole time range.
38+ _correctDataLeftBoundary : function ( data , boundary , resolution ) {
39+ var firstDataPoint = data [ 0 ] ;
40+ if ( boundary < firstDataPoint . x ) {
41+ var previousPointInTime = moment . unix ( firstDataPoint . x ) . subtract ( 1 , resolution ) . unix ( ) ;
42+ if ( previousPointInTime > boundary ) {
43+ data . unshift ( { "x" : previousPointInTime , "y" : 0 } ) ;
44+ }
45+
46+ data . unshift ( { "x" : boundary , "y" : 0 } ) ;
47+ }
48+ } ,
49+
50+ // Add two points after the actual data if needed to ensure charts look
51+ // good while we show the whole time range.
52+ _correctDataRightBoundary : function ( data , boundary , resolution ) {
53+ var lastDataPoint = data [ data . length - 1 ] ;
54+ if ( boundary > lastDataPoint . x ) {
55+ var nextPointInTime = moment . unix ( lastDataPoint . x ) . add ( 1 , resolution ) . unix ( ) ;
56+ if ( nextPointInTime < boundary ) {
57+ data . push ( { "x" : nextPointInTime , "y" : 0 } ) ;
58+ }
59+ data . push ( { "x" : boundary , "y" : 0 } ) ;
60+ }
61+ } ,
62+
63+ // Show the whole search time range on charts, even if no data is available.
64+ correctDataBoundaries : function ( data , from , to , resolution ) {
65+ var fromMoment = moment . utc ( from ) ;
66+ var toMoment = moment . utc ( to ) ;
67+
68+ var formattedResolution = momentHelper . getFormattedResolution ( resolution ) ;
69+ var fromFormatted = fromMoment . startOf ( formattedResolution ) . unix ( ) ;
70+ var toFormatted = toMoment . startOf ( formattedResolution ) . unix ( ) ;
71+
72+ // Correct left boundary
73+ this . _correctDataLeftBoundary ( data , fromFormatted , resolution ) ;
74+
75+ if ( toFormatted != fromFormatted ) {
76+ this . _correctDataRightBoundary ( data , toFormatted , resolution ) ;
77+ }
78+
79+ return data ;
80+ }
81+ } ;
0 commit comments