11function donutChart ( ) {
22 var width ,
33 height ,
4+ darkMode ,
45 margin = { top : 10 , right : 10 , bottom : 10 , left : 10 } ,
56 colour = d3 . scaleOrdinal ( d3 . schemeCategory20 ) , // colour scheme
67 variable , // value in data that will dictate proportions on chart
@@ -78,20 +79,35 @@ function donutChart() {
7879 } )
7980 . attr ( "class" , "legend" ) ;
8081
82+ if ( darkMode )
83+ legendG . style ( "fill" , "#ffffff" ) ;
84+ else legendG . style ( "fill" , "black" ) ;
85+
8186 legendG . append ( "rect" ) // make a matching color rect
8287 . attr ( "width" , 10 )
8388 . attr ( "height" , 10 )
8489 . attr ( "fill" , function ( d , i ) {
8590 return colour ( d . data [ category ] ) ;
8691 } ) ;
8792
88- legendG . append ( "text" ) // add the text
89- . text ( function ( d ) {
90- return d . data . url ;
91- } )
92- . style ( "font-size" , 13 )
93- . attr ( "y" , 10 )
94- . attr ( "x" , 13 ) ;
93+ if ( darkMode )
94+ legendG . append ( "text" ) // add the text
95+ . text ( function ( d ) {
96+ return d . data . url ;
97+ } )
98+ . style ( "font-size" , 13 )
99+ . style ( 'fill' , '#ffffff' )
100+ . attr ( "y" , 10 )
101+ . attr ( "x" , 13 ) ;
102+ else
103+ legendG . append ( "text" ) // add the text
104+ . text ( function ( d ) {
105+ return d . data . url ;
106+ } )
107+ . style ( "fill" , "black" )
108+ . style ( "font-size" , 13 )
109+ . attr ( "y" , 10 )
110+ . attr ( "x" , 13 ) ;
95111
96112 // ===========================================================================================
97113 // add tooltip to mouse events on slices and labels
@@ -110,12 +126,21 @@ function donutChart() {
110126 // add tooltip (svg circle element) when mouse enters label or slice
111127 selection . on ( 'mouseenter' , function ( data ) {
112128 d3 . selectAll ( '.toolCircle' ) . remove ( ) ;
113- svg . append ( 'text' )
114- . attr ( 'class' , 'toolCircle' )
115- . attr ( 'dy' , - 15 ) // hard-coded. can adjust this to adjust text vertical alignment in tooltip
116- . html ( toolTipHTML ( data ) ) // add text to the circle.
117- . style ( 'font-size' , '.9em' )
118- . style ( 'text-anchor' , 'middle' ) ; // centres text in tooltip
129+ if ( darkMode )
130+ svg . append ( 'text' )
131+ . attr ( 'class' , 'toolCircle' )
132+ . attr ( 'dy' , - 15 ) // hard-coded. can adjust this to adjust text vertical alignment in tooltip
133+ . html ( toolTipHTML ( data ) ) // add text to the circle.
134+ . style ( 'font-size' , '.9em' )
135+ . style ( 'fill' , '#ffffff' )
136+ . style ( 'text-anchor' , 'middle' ) ; // centres text in tooltip
137+ else
138+ svg . append ( 'text' )
139+ . attr ( 'class' , 'toolCircle' )
140+ . attr ( 'dy' , - 15 )
141+ . html ( toolTipHTML ( data ) )
142+ . style ( 'font-size' , '.9em' )
143+ . style ( 'text-anchor' , 'middle' ) ;
119144
120145 svg . append ( 'circle' )
121146 . attr ( 'class' , 'toolCircle' )
@@ -180,6 +205,12 @@ function donutChart() {
180205 return chart ;
181206 } ;
182207
208+ chart . darkMode = function ( value ) {
209+ if ( ! arguments . length ) return darkMode ;
210+ darkMode = value ;
211+ return chart ;
212+ } ;
213+
183214 chart . margin = function ( value ) {
184215 if ( ! arguments . length ) return margin ;
185216 margin = value ;
@@ -323,17 +354,33 @@ function drawIntervalChart(data) {
323354
324355 var tickDistance = 4.38 ;
325356
326- var tooltip = d3 . select ( "#timeChart" )
327- . append ( "div" )
328- . style ( "opacity" , 0 )
329- . style ( "display" , "none" )
330- . style ( "position" , "absolute" )
331- . attr ( "class" , "tooltip" )
332- . style ( "background-color" , "white" )
333- . style ( "border" , "solid" )
334- . style ( "border-width" , "1px" )
335- . style ( "border-radius" , "5px" )
336- . style ( "padding" , "5px" )
357+ var tooltip ;
358+ if ( document . body . classList . contains ( 'night-mode' ) )
359+ tooltip = d3 . select ( "#timeChart" )
360+ . append ( "div" )
361+ . style ( "opacity" , 0 )
362+ . style ( "display" , "none" )
363+ . style ( "position" , "absolute" )
364+ . attr ( "class" , "tooltip" )
365+ . style ( "background-color" , "#cbcbcb" )
366+ . style ( "color" , "black" )
367+ . style ( "border" , "solid" )
368+ . style ( "border-width" , "1px" )
369+ . style ( "border-radius" , "5px" )
370+ . style ( "padding" , "5px" )
371+ else
372+ tooltip = d3 . select ( "#timeChart" )
373+ . append ( "div" )
374+ . style ( "opacity" , 0 )
375+ . style ( "display" , "none" )
376+ . style ( "position" , "absolute" )
377+ . attr ( "class" , "tooltip" )
378+ . style ( "background-color" , "white" )
379+ . style ( "color" , "black" )
380+ . style ( "border" , "solid" )
381+ . style ( "border-width" , "1px" )
382+ . style ( "border-radius" , "5px" )
383+ . style ( "padding" , "5px" )
337384
338385 // Three function that change the tooltip when user hover / move / leave a cell
339386 var mouseover = function ( d ) {
@@ -361,11 +408,20 @@ function drawIntervalChart(data) {
361408 }
362409
363410 //create the svg
364- var svg = d3 . select ( "#timeChart" ) . append ( "svg" )
365- . attr ( "width" , width + margin . left + margin . right )
366- . attr ( "height" , height + margin . top + margin . bottom )
367- . append ( "g" )
368- . attr ( "transform" , "translate(" + margin . left + "," + margin . top + ")" ) ;
411+ var svg ;
412+ if ( document . body . classList . contains ( 'night-mode' ) )
413+ svg = d3 . select ( "#timeChart" ) . append ( "svg" )
414+ . style ( 'background-color' , '#383838' )
415+ . attr ( "width" , width + margin . left + margin . right )
416+ . attr ( "height" , height + margin . top + margin . bottom )
417+ . append ( "g" )
418+ . attr ( "transform" , "translate(" + margin . left + "," + margin . top + ")" ) ;
419+ else
420+ svg = d3 . select ( "#timeChart" ) . append ( "svg" )
421+ . attr ( "width" , width + margin . left + margin . right )
422+ . attr ( "height" , height + margin . top + margin . bottom )
423+ . append ( "g" )
424+ . attr ( "transform" , "translate(" + margin . left + "," + margin . top + ")" ) ;
369425
370426 //draw the axis.
371427 svg . append ( "g" )
@@ -419,7 +475,7 @@ function drawIntervalChart(data) {
419475 } )
420476 . attr ( "height" , function ( d ) {
421477 var offset = getMinutesTo ( d . interval ) - getMinutesFrom ( d . interval ) ;
422- if ( offset == 0 ) {
478+ if ( offset == 0 ) {
423479 var offsetSeconds = getSecondsTo ( d . interval ) - getSecondsFrom ( d . interval ) ;
424480 if ( offsetSeconds <= 3 )
425481 return 0 ;
0 commit comments