@ -1,416 +1,25 @@
import CLASS from './class' ;
import { isValue , isFunction , isString , isEmpty , Component } from './util' ;
export var c3 _axis _fn ;
export var c3 _axis _internal _fn ;
function AxisInternal ( component , params ) {
var internal = this ;
internal . component = component ;
internal . params = params || { } ;
internal . d3 = component . d3 ;
internal . scale = internal . d3 . scaleLinear ( ) ;
internal . range ;
internal . orient = "bottom" ;
internal . innerTickSize = 6 ;
internal . outerTickSize = this . params . withOuterTick ? 6 : 0 ;
internal . tickPadding = 3 ;
internal . tickValues = null ;
internal . tickFormat ;
internal . tickArguments ;
internal . tickOffset = 0 ;
internal . tickCulling = true ;
internal . tickCentered ;
internal . tickTextCharSize ;
internal . tickTextRotate = internal . params . tickTextRotate ;
internal . tickLength ;
internal . axis = internal . generateAxis ( ) ;
}
c3 _axis _internal _fn = AxisInternal . prototype ;
c3 _axis _internal _fn . axisX = function ( selection , x , tickOffset ) {
selection . attr ( "transform" , function ( d ) {
return "translate(" + Math . ceil ( x ( d ) + tickOffset ) + ", 0)" ;
} ) ;
} ;
c3 _axis _internal _fn . axisY = function ( selection , y ) {
selection . attr ( "transform" , function ( d ) {
return "translate(0," + Math . ceil ( y ( d ) ) + ")" ;
} ) ;
} ;
c3 _axis _internal _fn . scaleExtent = function ( domain ) {
var start = domain [ 0 ] , stop = domain [ domain . length - 1 ] ;
return start < stop ? [ start , stop ] : [ stop , start ] ;
} ;
c3 _axis _internal _fn . generateTicks = function ( scale ) {
var internal = this ;
var i , domain , ticks = [ ] ;
if ( scale . ticks ) {
return scale . ticks . apply ( scale , internal . tickArguments ) ;
}
domain = scale . domain ( ) ;
for ( i = Math . ceil ( domain [ 0 ] ) ; i < domain [ 1 ] ; i ++ ) {
ticks . push ( i ) ;
}
if ( ticks . length > 0 && ticks [ 0 ] > 0 ) {
ticks . unshift ( ticks [ 0 ] - ( ticks [ 1 ] - ticks [ 0 ] ) ) ;
}
return ticks ;
} ;
c3 _axis _internal _fn . copyScale = function ( ) {
var internal = this ;
var newScale = internal . scale . copy ( ) , domain ;
if ( internal . params . isCategory ) {
domain = internal . scale . domain ( ) ;
newScale . domain ( [ domain [ 0 ] , domain [ 1 ] - 1 ] ) ;
}
return newScale ;
} ;
c3 _axis _internal _fn . textFormatted = function ( v ) {
var internal = this ,
formatted = internal . tickFormat ? internal . tickFormat ( v ) : v ;
return typeof formatted !== 'undefined' ? formatted : '' ;
} ;
c3 _axis _internal _fn . updateRange = function ( ) {
var internal = this ;
internal . range = internal . scale . rangeExtent ? internal . scale . rangeExtent ( ) : internal . scaleExtent ( internal . scale . range ( ) ) ;
return internal . range ;
} ;
c3 _axis _internal _fn . updateTickTextCharSize = function ( tick ) {
var internal = this ;
if ( internal . tickTextCharSize ) {
return internal . tickTextCharSize ;
}
var size = {
h : 11.5 ,
w : 5.5
} ;
tick . select ( 'text' ) . text ( function ( d ) { return internal . textFormatted ( d ) ; } ) . each ( function ( d ) {
var box = this . getBoundingClientRect ( ) ,
text = internal . textFormatted ( d ) ,
h = box . height ,
w = text ? ( box . width / text . length ) : undefined ;
if ( h && w ) {
size . h = h ;
size . w = w ;
}
} ) . text ( '' ) ;
internal . tickTextCharSize = size ;
return size ;
} ;
c3 _axis _internal _fn . isVertical = function ( ) {
return this . orient === 'left' || this . orient === 'right' ;
} ;
c3 _axis _internal _fn . tspanData = function ( d , i , scale ) {
var internal = this ;
var splitted = internal . params . tickMultiline ? internal . splitTickText ( d , scale ) : [ ] . concat ( internal . textFormatted ( d ) ) ;
if ( internal . params . tickMultiline && internal . params . tickMultilineMax > 0 ) {
splitted = internal . ellipsify ( splitted , internal . params . tickMultilineMax ) ;
}
return splitted . map ( function ( s ) {
return { index : i , splitted : s , length : splitted . length } ;
} ) ;
} ;
c3 _axis _internal _fn . splitTickText = function ( d , scale ) {
var internal = this ,
tickText = internal . textFormatted ( d ) ,
maxWidth = internal . params . tickWidth ,
subtext , spaceIndex , textWidth , splitted = [ ] ;
if ( Object . prototype . toString . call ( tickText ) === "[object Array]" ) {
return tickText ;
}
if ( ! maxWidth || maxWidth <= 0 ) {
maxWidth = internal . isVertical ( ) ? 95 : internal . params . isCategory ? ( Math . ceil ( scale ( 1 ) - scale ( 0 ) ) - 12 ) : 110 ;
}
function split ( splitted , text ) {
spaceIndex = undefined ;
for ( var i = 1 ; i < text . length ; i ++ ) {
if ( text . charAt ( i ) === ' ' ) {
spaceIndex = i ;
}
subtext = text . substr ( 0 , i + 1 ) ;
textWidth = internal . tickTextCharSize . w * subtext . length ;
// if text width gets over tick width, split by space index or crrent index
if ( maxWidth < textWidth ) {
return split (
splitted . concat ( text . substr ( 0 , spaceIndex ? spaceIndex : i ) ) ,
text . slice ( spaceIndex ? spaceIndex + 1 : i )
) ;
}
}
return splitted . concat ( text ) ;
}
return split ( splitted , tickText + "" ) ;
} ;
c3 _axis _internal _fn . ellipsify = function ( splitted , max ) {
if ( splitted . length <= max ) {
return splitted ;
}
var ellipsified = splitted . slice ( 0 , max ) ;
var remaining = 3 ;
for ( var i = max - 1 ; i >= 0 ; i -- ) {
var available = ellipsified [ i ] . length ;
ellipsified [ i ] = ellipsified [ i ] . substr ( 0 , available - remaining ) . padEnd ( available , '.' ) ;
remaining -= available ;
if ( remaining <= 0 ) {
break ;
}
}
return ellipsified ;
} ;
c3 _axis _internal _fn . updateTickLength = function ( ) {
var internal = this ;
internal . tickLength = Math . max ( internal . innerTickSize , 0 ) + internal . tickPadding ;
} ;
c3 _axis _internal _fn . lineY2 = function ( d ) {
var internal = this ,
tickPosition = internal . scale ( d ) + ( internal . tickCentered ? 0 : internal . tickOffset ) ;
return internal . range [ 0 ] < tickPosition && tickPosition < internal . range [ 1 ] ? internal . innerTickSize : 0 ;
} ;
c3 _axis _internal _fn . textY = function ( ) {
var internal = this , rotate = internal . tickTextRotate ;
return rotate ? 11.5 - 2.5 * ( rotate / 15 ) * ( rotate > 0 ? 1 : - 1 ) : internal . tickLength ;
} ;
c3 _axis _internal _fn . textTransform = function ( ) {
var internal = this , rotate = internal . tickTextRotate ;
return rotate ? "rotate(" + rotate + ")" : "" ;
} ;
c3 _axis _internal _fn . textTextAnchor = function ( ) {
var internal = this , rotate = internal . tickTextRotate ;
return rotate ? ( rotate > 0 ? "start" : "end" ) : "middle" ;
} ;
c3 _axis _internal _fn . tspanDx = function ( ) {
var internal = this , rotate = internal . tickTextRotate ;
return rotate ? 8 * Math . sin ( Math . PI * ( rotate / 180 ) ) : 0 ;
} ;
c3 _axis _internal _fn . tspanDy = function ( d , i ) {
var internal = this ,
dy = internal . tickTextCharSize . h ;
if ( i === 0 ) {
if ( internal . isVertical ( ) ) {
dy = - ( ( d . length - 1 ) * ( internal . tickTextCharSize . h / 2 ) - 3 ) ;
} else {
dy = ".71em" ;
}
}
return dy ;
} ;
c3 _axis _internal _fn . generateAxis = function ( ) {
var internal = this , d3 = internal . d3 , params = internal . params ;
function axis ( g , transition ) {
var self ;
g . each ( function ( ) {
var g = axis . g = d3 . select ( this ) ;
var scale0 = this . _ _chart _ _ || internal . scale ,
scale1 = this . _ _chart _ _ = internal . copyScale ( ) ;
var ticksValues = internal . tickValues ? internal . tickValues : internal . generateTicks ( scale1 ) ,
ticks = g . selectAll ( ".tick" ) . data ( ticksValues , scale1 ) ,
tickEnter = ticks . enter ( ) . insert ( "g" , ".domain" ) . attr ( "class" , "tick" ) . style ( "opacity" , 1e-6 ) ,
// MEMO: No exit transition. The reason is this transition affects max tick width calculation because old tick will be included in the ticks.
tickExit = ticks . exit ( ) . remove ( ) ,
tickUpdate = ticks . merge ( tickEnter ) ,
tickTransform , tickX , tickY ;
if ( params . isCategory ) {
internal . tickOffset = Math . ceil ( ( scale1 ( 1 ) - scale1 ( 0 ) ) / 2 ) ;
tickX = internal . tickCentered ? 0 : internal . tickOffset ;
tickY = internal . tickCentered ? internal . tickOffset : 0 ;
} else {
internal . tickOffset = tickX = 0 ;
}
internal . updateRange ( ) ;
internal . updateTickLength ( ) ;
internal . updateTickTextCharSize ( g . select ( '.tick' ) ) ;
var lineUpdate = tickUpdate . select ( "line" ) . merge ( tickEnter . append ( "line" ) ) ,
textUpdate = tickUpdate . select ( "text" ) . merge ( tickEnter . append ( "text" ) ) ;
var tspans = tickUpdate . selectAll ( 'text' ) . selectAll ( 'tspan' ) . data ( function ( d , i ) {
return internal . tspanData ( d , i , scale1 ) ;
} ) ,
tspanEnter = tspans . enter ( ) . append ( 'tspan' ) ,
tspanUpdate = tspanEnter . merge ( tspans ) . text ( function ( d ) { return d . splitted ; } ) ;
tspans . exit ( ) . remove ( ) ;
var path = g . selectAll ( ".domain" ) . data ( [ 0 ] ) ,
pathUpdate = path . enter ( ) . append ( "path" ) . merge ( path ) . attr ( "class" , "domain" ) ;
// TODO: each attr should be one function and change its behavior by internal.orient, probably
switch ( internal . orient ) {
case "bottom" :
{
tickTransform = internal . axisX ;
lineUpdate . attr ( "x1" , tickX )
. attr ( "x2" , tickX )
. attr ( "y2" , function ( d , i ) { return internal . lineY2 ( d , i ) ; } ) ;
textUpdate . attr ( "x" , 0 )
. attr ( "y" , function ( d , i ) { return internal . textY ( d , i ) ; } )
. attr ( "transform" , function ( d , i ) { return internal . textTransform ( d , i ) ; } )
. style ( "text-anchor" , function ( d , i ) { return internal . textTextAnchor ( d , i ) ; } ) ;
tspanUpdate . attr ( 'x' , 0 )
. attr ( "dy" , function ( d , i ) { return internal . tspanDy ( d , i ) ; } )
. attr ( 'dx' , function ( d , i ) { return internal . tspanDx ( d , i ) ; } ) ;
pathUpdate . attr ( "d" , "M" + internal . range [ 0 ] + "," + internal . outerTickSize + "V0H" + internal . range [ 1 ] + "V" + internal . outerTickSize ) ;
break ;
}
case "top" :
{
// TODO: rotated tick text
tickTransform = internal . axisX ;
lineUpdate . attr ( "x1" , tickX )
. attr ( "x2" , tickX )
. attr ( "y2" , function ( d , i ) { return - 1 * internal . lineY2 ( d , i ) ; } ) ;
textUpdate . attr ( "x" , 0 )
. attr ( "y" , function ( d , i ) { return - 1 * internal . textY ( d , i ) - ( params . isCategory ? 2 : ( internal . tickLength - 2 ) ) ; } )
. attr ( "transform" , function ( d , i ) { return internal . textTransform ( d , i ) ; } )
. style ( "text-anchor" , function ( d , i ) { return internal . textTextAnchor ( d , i ) ; } ) ;
tspanUpdate . attr ( 'x' , 0 )
. attr ( "dy" , function ( d , i ) { return internal . tspanDy ( d , i ) ; } )
. attr ( 'dx' , function ( d , i ) { return internal . tspanDx ( d , i ) ; } ) ;
pathUpdate . attr ( "d" , "M" + internal . range [ 0 ] + "," + - internal . outerTickSize + "V0H" + internal . range [ 1 ] + "V" + - internal . outerTickSize ) ;
break ;
}
case "left" :
{
tickTransform = internal . axisY ;
lineUpdate . attr ( "x2" , - internal . innerTickSize )
. attr ( "y1" , tickY )
. attr ( "y2" , tickY ) ;
textUpdate . attr ( "x" , - internal . tickLength )
. attr ( "y" , internal . tickOffset )
. style ( "text-anchor" , "end" ) ;
tspanUpdate . attr ( 'x' , - internal . tickLength )
. attr ( "dy" , function ( d , i ) { return internal . tspanDy ( d , i ) ; } ) ;
pathUpdate . attr ( "d" , "M" + - internal . outerTickSize + "," + internal . range [ 0 ] + "H0V" + internal . range [ 1 ] + "H" + - internal . outerTickSize ) ;
break ;
}
case "right" :
{
tickTransform = internal . axisY ;
lineUpdate . attr ( "x2" , internal . innerTickSize )
. attr ( "y1" , tickY )
. attr ( "y2" , tickY ) ;
textUpdate . attr ( "x" , internal . tickLength )
. attr ( "y" , internal . tickOffset )
. style ( "text-anchor" , "start" ) ;
tspanUpdate . attr ( 'x' , internal . tickLength )
. attr ( "dy" , function ( d , i ) { return internal . tspanDy ( d , i ) ; } ) ;
pathUpdate . attr ( "d" , "M" + internal . outerTickSize + "," + internal . range [ 0 ] + "H0V" + internal . range [ 1 ] + "H" + internal . outerTickSize ) ;
break ;
}
}
if ( scale1 . rangeBand ) {
var x = scale1 , dx = x . rangeBand ( ) / 2 ;
scale0 = scale1 = function ( d ) {
return x ( d ) + dx ;
} ;
} else if ( scale0 . rangeBand ) {
scale0 = scale1 ;
} else {
tickExit . call ( tickTransform , scale1 , internal . tickOffset ) ;
}
tickEnter . call ( tickTransform , scale0 , internal . tickOffset ) ;
self = ( transition ? tickUpdate . transition ( transition ) : tickUpdate )
. style ( 'opacity' , 1 )
. call ( tickTransform , scale1 , internal . tickOffset ) ;
} ) ;
return self ;
}
axis . scale = function ( x ) {
if ( ! arguments . length ) { return internal . scale ; }
internal . scale = x ;
return axis ;
} ;
axis . orient = function ( x ) {
if ( ! arguments . length ) { return internal . orient ; }
internal . orient = x in { top : 1 , right : 1 , bottom : 1 , left : 1 } ? x + "" : "bottom" ;
return axis ;
} ;
axis . tickFormat = function ( format ) {
if ( ! arguments . length ) { return internal . tickFormat ; }
internal . tickFormat = format ;
return axis ;
} ;
axis . tickCentered = function ( isCentered ) {
if ( ! arguments . length ) { return internal . tickCentered ; }
internal . tickCentered = isCentered ;
return axis ;
} ;
axis . tickOffset = function ( ) {
return internal . tickOffset ;
} ;
axis . tickInterval = function ( ) {
var interval , length ;
if ( params . isCategory ) {
interval = internal . tickOffset * 2 ;
}
else {
length = axis . g . select ( 'path.domain' ) . node ( ) . getTotalLength ( ) - internal . outerTickSize * 2 ;
interval = length / axis . g . selectAll ( 'line' ) . size ( ) ;
}
return interval === Infinity ? 0 : interval ;
} ;
axis . ticks = function ( ) {
if ( ! arguments . length ) { return internal . tickArguments ; }
internal . tickArguments = arguments ;
return axis ;
} ;
axis . tickCulling = function ( culling ) {
if ( ! arguments . length ) { return internal . tickCulling ; }
internal . tickCulling = culling ;
return axis ;
} ;
axis . tickValues = function ( x ) {
if ( typeof x === 'function' ) {
internal . tickValues = function ( ) {
return x ( internal . scale . domain ( ) ) ;
} ;
}
else {
if ( ! arguments . length ) { return internal . tickValues ; }
internal . tickValues = x ;
}
return axis ;
} ;
return axis ;
} ;
export default class Axis extends Component {
constructor ( owner ) {
var fn = {
fn : c3 _axis _fn ,
internal : {
fn : c3 _axis _internal _fn
}
} ;
super ( owner , 'axis' , fn ) ;
import {
isValue ,
isFunction ,
isString ,
isEmpty
} from './util' ;
import {
AxisInternal
} from './axis-internal' ;
export default class Axis {
constructor ( owner ) {
this . owner = owner ;
this . d3 = owner . d3 ;
this . internal = AxisInternal ;
}
}
c3 _axis _fn = Axis . prototype ;
c3 _axis _fn . init = function init ( ) {
var $$ = this . owner , config = $$ . config , main = $$ . main ;
Axis . prototype . init = function init ( ) {
var $$ = this . owner ,
config = $$ . config ,
main = $$ . main ;
$$ . axes . x = main . append ( "g" )
. attr ( "class" , CLASS . axis + ' ' + CLASS . axisX )
. attr ( "clip-path" , config . axis _x _inner ? "" : $$ . clipPathForXAxis )
@ -440,8 +49,9 @@ c3_axis_fn.init = function init() {
. attr ( "transform" , config . axis _rotated ? "" : "rotate(-90)" )
. style ( "text-anchor" , this . textAnchorForY2AxisLabel . bind ( this ) ) ;
} ;
c3 _axis _fn . getXAxis = function getXAxis ( scale , orient , tickFormat , tickValues , withOuterTick , withoutTransition , withoutRotateTickText ) {
var $$ = this . owner , config = $$ . config ,
Axis . prototype . getXAxis = function getXAxis ( scale , orient , tickFormat , tickValues , withOuterTick , withoutTransition , withoutRotateTickText ) {
var $$ = this . owner ,
config = $$ . config ,
axisParams = {
isCategory : $$ . isCategorized ( ) ,
withOuterTick : withOuterTick ,
@ -454,7 +64,9 @@ c3_axis_fn.getXAxis = function getXAxis(scale, orient, tickFormat, tickValues, w
axis = new this . internal ( this , axisParams ) . axis . scale ( scale ) . orient ( orient ) ;
if ( $$ . isTimeSeries ( ) && tickValues && typeof tickValues !== "function" ) {
tickValues = tickValues . map ( function ( v ) { return $$ . parseDate ( v ) ; } ) ;
tickValues = tickValues . map ( function ( v ) {
return $$ . parseDate ( v ) ;
} ) ;
}
// Set tick
@ -468,8 +80,10 @@ c3_axis_fn.getXAxis = function getXAxis(scale, orient, tickFormat, tickValues, w
return axis ;
} ;
c3 _axis _fn . updateXAxisTickValues = function updateXAxisTickValues ( targets , axis ) {
var $$ = this . owner , config = $$ . config , tickValues ;
Axis . prototype . updateXAxisTickValues = function updateXAxisTickValues ( targets , axis ) {
var $$ = this . owner ,
config = $$ . config ,
tickValues ;
if ( config . axis _x _tick _fit || config . axis _x _tick _count ) {
tickValues = this . generateTickValues ( $$ . mapTargetsToUniqueXs ( targets ) , config . axis _x _tick _count , $$ . isTimeSeries ( ) ) ;
}
@ -481,8 +95,9 @@ c3_axis_fn.updateXAxisTickValues = function updateXAxisTickValues(targets, axis)
}
return tickValues ;
} ;
c3 _axis _fn . getYAxis = function getYAxis ( scale , orient , tickFormat , tickValues , withOuterTick , withoutTransition , withoutRotateTickText ) {
var $$ = this . owner , config = $$ . config ,
Axis . prototype . getYAxis = function getYAxis ( scale , orient , tickFormat , tickValues , withOuterTick , withoutTransition , withoutRotateTickText ) {
var $$ = this . owner ,
config = $$ . config ,
axisParams = {
withOuterTick : withOuterTick ,
withoutTransition : withoutTransition ,
@ -496,15 +111,18 @@ c3_axis_fn.getYAxis = function getYAxis(scale, orient, tickFormat, tickValues, w
}
return axis ;
} ;
c3 _axis _fn . getId = function getId ( id ) {
Axis . prototype . getId = function getId ( id ) {
var config = this . owner . config ;
return id in config . data _axes ? config . data _axes [ id ] : 'y' ;
} ;
c3 _axis _fn . getXAxisTickFormat = function getXAxisTickFormat ( ) {
Axis . prototype . getXAxisTickFormat = function getXAxisTickFormat ( ) {
// #2251 previously set any negative values to a whole number,
// however both should be truncated according to the users format specification
var $$ = this . owner , config = $$ . config ;
let format = ( $$ . isTimeSeries ( ) ) ? $$ . defaultAxisTimeFormat : ( $$ . isCategorized ( ) ) ? $$ . categoryName : function ( v ) { return v ; } ;
var $$ = this . owner ,
config = $$ . config ;
let format = ( $$ . isTimeSeries ( ) ) ? $$ . defaultAxisTimeFormat : ( $$ . isCategorized ( ) ) ? $$ . categoryName : function ( v ) {
return v ;
} ;
if ( config . axis _x _tick _format ) {
if ( isFunction ( config . axis _x _tick _format ) ) {
@ -515,22 +133,26 @@ c3_axis_fn.getXAxisTickFormat = function getXAxisTickFormat() {
} ;
}
}
return isFunction ( format ) ? function ( v ) { return format . call ( $$ , v ) ; } : format ;
return isFunction ( format ) ? function ( v ) {
return format . call ( $$ , v ) ;
} : format ;
} ;
c3 _axis _fn . getTickValues = function getTickValues ( tickValues , axis ) {
Axis . prototype . getTickValues = function getTickValues ( tickValues , axis ) {
return tickValues ? tickValues : axis ? axis . tickValues ( ) : undefined ;
} ;
c3 _axis _fn . getXAxisTickValues = function getXAxisTickValues ( ) {
Axis . prototype . getXAxisTickValues = function getXAxisTickValues ( ) {
return this . getTickValues ( this . owner . config . axis _x _tick _values , this . owner . xAxis ) ;
} ;
c3 _axis _fn . getYAxisTickValues = function getYAxisTickValues ( ) {
Axis . prototype . getYAxisTickValues = function getYAxisTickValues ( ) {
return this . getTickValues ( this . owner . config . axis _y _tick _values , this . owner . yAxis ) ;
} ;
c3 _axis _fn . getY2AxisTickValues = function getY2AxisTickValues ( ) {
Axis . prototype . getY2AxisTickValues = function getY2AxisTickValues ( ) {
return this . getTickValues ( this . owner . config . axis _y2 _tick _values , this . owner . y2Axis ) ;
} ;
c3 _axis _fn . getLabelOptionByAxisId = function getLabelOptionByAxisId ( axisId ) {
var $$ = this . owner , config = $$ . config , option ;
Axis . prototype . getLabelOptionByAxisId = function getLabelOptionByAxisId ( axisId ) {
var $$ = this . owner ,
config = $$ . config ,
option ;
if ( axisId === 'y' ) {
option = config . axis _y _label ;
} else if ( axisId === 'y2' ) {
@ -540,12 +162,13 @@ c3_axis_fn.getLabelOptionByAxisId = function getLabelOptionByAxisId(axisId) {
}
return option ;
} ;
c3 _axis _fn . getLabelText = function getLabelText ( axisId ) {
Axis . prototype . getLabelText = function getLabelText ( axisId ) {
var option = this . getLabelOptionByAxisId ( axisId ) ;
return isString ( option ) ? option : option ? option . text : null ;
} ;
c3 _axis _fn . setLabelText = function setLabelText ( axisId , text ) {
var $$ = this . owner , config = $$ . config ,
Axis . prototype . setLabelText = function setLabelText ( axisId , text ) {
var $$ = this . owner ,
config = $$ . config ,
option = this . getLabelOptionByAxisId ( axisId ) ;
if ( isString ( option ) ) {
if ( axisId === 'y' ) {
@ -559,7 +182,7 @@ c3_axis_fn.setLabelText = function setLabelText(axisId, text) {
option . text = text ;
}
} ;
c3 _axis _fn . getLabelPosition = function getLabelPosition ( axisId , defaultPosition ) {
Axis . prototype . getLabelPosition = function getLabelPosition ( axisId , defaultPosition ) {
var option = this . getLabelOptionByAxisId ( axisId ) ,
position = ( option && typeof option === 'object' && option . position ) ? option . position : defaultPosition ;
return {
@ -573,28 +196,28 @@ c3_axis_fn.getLabelPosition = function getLabelPosition(axisId, defaultPosition)
isBottom : position . indexOf ( 'bottom' ) >= 0
} ;
} ;
c3 _axis _fn . getXAxisLabelPosition = function getXAxisLabelPosition ( ) {
Axis . prototype . getXAxisLabelPosition = function getXAxisLabelPosition ( ) {
return this . getLabelPosition ( 'x' , this . owner . config . axis _rotated ? 'inner-top' : 'inner-right' ) ;
} ;
c3 _axis _fn . getYAxisLabelPosition = function getYAxisLabelPosition ( ) {
Axis . prototype . getYAxisLabelPosition = function getYAxisLabelPosition ( ) {
return this . getLabelPosition ( 'y' , this . owner . config . axis _rotated ? 'inner-right' : 'inner-top' ) ;
} ;
c3 _axis _fn . getY2AxisLabelPosition = function getY2AxisLabelPosition ( ) {
Axis . prototype . getY2AxisLabelPosition = function getY2AxisLabelPosition ( ) {
return this . getLabelPosition ( 'y2' , this . owner . config . axis _rotated ? 'inner-right' : 'inner-top' ) ;
} ;
c3 _axis _fn . getLabelPositionById = function getLabelPositionById ( id ) {
Axis . prototype . getLabelPositionById = function getLabelPositionById ( id ) {
return id === 'y2' ? this . getY2AxisLabelPosition ( ) : id === 'y' ? this . getYAxisLabelPosition ( ) : this . getXAxisLabelPosition ( ) ;
} ;
c3 _axis _fn . textForXAxisLabel = function textForXAxisLabel ( ) {
Axis . prototype . textForXAxisLabel = function textForXAxisLabel ( ) {
return this . getLabelText ( 'x' ) ;
} ;
c3 _axis _fn . textForYAxisLabel = function textForYAxisLabel ( ) {
Axis . prototype . textForYAxisLabel = function textForYAxisLabel ( ) {
return this . getLabelText ( 'y' ) ;
} ;
c3 _axis _fn . textForY2AxisLabel = function textForY2AxisLabel ( ) {
Axis . prototype . textForY2AxisLabel = function textForY2AxisLabel ( ) {
return this . getLabelText ( 'y2' ) ;
} ;
c3 _axis _fn . xForAxisLabel = function xForAxisLabel ( forHorizontal , position ) {
Axis . prototype . xForAxisLabel = function xForAxisLabel ( forHorizontal , position ) {
var $$ = this . owner ;
if ( forHorizontal ) {
return position . isLeft ? 0 : position . isCenter ? $$ . width / 2 : $$ . width ;
@ -602,40 +225,41 @@ c3_axis_fn.xForAxisLabel = function xForAxisLabel(forHorizontal, position) {
return position . isBottom ? - $$ . height : position . isMiddle ? - $$ . height / 2 : 0 ;
}
} ;
c3 _axis _fn . dxForAxisLabel = function dxForAxisLabel ( forHorizontal , position ) {
Axis . prototype . dxForAxisLabel = function dxForAxisLabel ( forHorizontal , position ) {
if ( forHorizontal ) {
return position . isLeft ? "0.5em" : position . isRight ? "-0.5em" : "0" ;
} else {
return position . isTop ? "-0.5em" : position . isBottom ? "0.5em" : "0" ;
}
} ;
c3 _axis _fn . textAnchorForAxisLabel = function textAnchorForAxisLabel ( forHorizontal , position ) {
Axis . prototype . textAnchorForAxisLabel = function textAnchorForAxisLabel ( forHorizontal , position ) {
if ( forHorizontal ) {
return position . isLeft ? 'start' : position . isCenter ? 'middle' : 'end' ;
} else {
return position . isBottom ? 'start' : position . isMiddle ? 'middle' : 'end' ;
}
} ;
c3 _axis _fn . xForXAxisLabel = function xForXAxisLabel ( ) {
Axis . prototype . xForXAxisLabel = function xForXAxisLabel ( ) {
return this . xForAxisLabel ( ! this . owner . config . axis _rotated , this . getXAxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . xForYAxisLabel = function xForYAxisLabel ( ) {
Axis . prototype . xForYAxisLabel = function xForYAxisLabel ( ) {
return this . xForAxisLabel ( this . owner . config . axis _rotated , this . getYAxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . xForY2AxisLabel = function xForY2AxisLabel ( ) {
Axis . prototype . xForY2AxisLabel = function xForY2AxisLabel ( ) {
return this . xForAxisLabel ( this . owner . config . axis _rotated , this . getY2AxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . dxForXAxisLabel = function dxForXAxisLabel ( ) {
Axis . prototype . dxForXAxisLabel = function dxForXAxisLabel ( ) {
return this . dxForAxisLabel ( ! this . owner . config . axis _rotated , this . getXAxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . dxForYAxisLabel = function dxForYAxisLabel ( ) {
Axis . prototype . dxForYAxisLabel = function dxForYAxisLabel ( ) {
return this . dxForAxisLabel ( this . owner . config . axis _rotated , this . getYAxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . dxForY2AxisLabel = function dxForY2AxisLabel ( ) {
Axis . prototype . dxForY2AxisLabel = function dxForY2AxisLabel ( ) {
return this . dxForAxisLabel ( this . owner . config . axis _rotated , this . getY2AxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . dyForXAxisLabel = function dyForXAxisLabel ( ) {
var $$ = this . owner , config = $$ . config ,
Axis . prototype . dyForXAxisLabel = function dyForXAxisLabel ( ) {
var $$ = this . owner ,
config = $$ . config ,
position = this . getXAxisLabelPosition ( ) ;
if ( config . axis _rotated ) {
return position . isInner ? "1.2em" : - 25 - ( $$ . config . axis _x _inner ? 0 : this . getMaxTickWidth ( 'x' ) ) ;
@ -643,7 +267,7 @@ c3_axis_fn.dyForXAxisLabel = function dyForXAxisLabel() {
return position . isInner ? "-0.5em" : config . axis _x _height ? config . axis _x _height - 10 : "3em" ;
}
} ;
c3 _axis _fn . dyForYAxisLabel = function dyForYAxisLabel ( ) {
Axis . prototype . dyForYAxisLabel = function dyForYAxisLabel ( ) {
var $$ = this . owner ,
position = this . getYAxisLabelPosition ( ) ;
if ( $$ . config . axis _rotated ) {
@ -652,7 +276,7 @@ c3_axis_fn.dyForYAxisLabel = function dyForYAxisLabel() {
return position . isInner ? "1.2em" : - 10 - ( $$ . config . axis _y _inner ? 0 : ( this . getMaxTickWidth ( 'y' ) + 10 ) ) ;
}
} ;
c3 _axis _fn . dyForY2AxisLabel = function dyForY2AxisLabel ( ) {
Axis . prototype . dyForY2AxisLabel = function dyForY2AxisLabel ( ) {
var $$ = this . owner ,
position = this . getY2AxisLabelPosition ( ) ;
if ( $$ . config . axis _rotated ) {
@ -661,21 +285,23 @@ c3_axis_fn.dyForY2AxisLabel = function dyForY2AxisLabel() {
return position . isInner ? "-0.5em" : 15 + ( $$ . config . axis _y2 _inner ? 0 : ( this . getMaxTickWidth ( 'y2' ) + 15 ) ) ;
}
} ;
c3 _axis _fn . textAnchorForXAxisLabel = function textAnchorForXAxisLabel ( ) {
Axis . prototype . textAnchorForXAxisLabel = function textAnchorForXAxisLabel ( ) {
var $$ = this . owner ;
return this . textAnchorForAxisLabel ( ! $$ . config . axis _rotated , this . getXAxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . textAnchorForYAxisLabel = function textAnchorForYAxisLabel ( ) {
Axis . prototype . textAnchorForYAxisLabel = function textAnchorForYAxisLabel ( ) {
var $$ = this . owner ;
return this . textAnchorForAxisLabel ( $$ . config . axis _rotated , this . getYAxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . textAnchorForY2AxisLabel = function textAnchorForY2AxisLabel ( ) {
Axis . prototype . textAnchorForY2AxisLabel = function textAnchorForY2AxisLabel ( ) {
var $$ = this . owner ;
return this . textAnchorForAxisLabel ( $$ . config . axis _rotated , this . getY2AxisLabelPosition ( ) ) ;
} ;
c3 _axis _fn . getMaxTickWidth = function getMaxTickWidth ( id , withoutRecompute ) {
var $$ = this . owner , config = $$ . config ,
maxWidth = 0 , targetsToShow , scale , axis , dummy , svg ;
Axis . prototype . getMaxTickWidth = function getMaxTickWidth ( id , withoutRecompute ) {
var $$ = this . owner ,
config = $$ . config ,
maxWidth = 0 ,
targetsToShow , scale , axis , dummy , svg ;
if ( withoutRecompute && $$ . currentMaxTickWidths [ id ] ) {
return $$ . currentMaxTickWidths [ id ] ;
}
@ -694,40 +320,42 @@ c3_axis_fn.getMaxTickWidth = function getMaxTickWidth(id, withoutRecompute) {
}
dummy = $$ . d3 . select ( 'body' ) . append ( 'div' ) . classed ( 'c3' , true ) ;
svg = dummy . append ( "svg" ) . style ( 'visibility' , 'hidden' ) . style ( 'position' , 'fixed' ) . style ( 'top' , 0 ) . style ( 'left' , 0 ) ,
svg . append ( 'g' ) . call ( axis ) . each ( function ( ) {
$$ . d3 . select ( this ) . selectAll ( 'text' ) . each ( function ( ) {
var box = this . getBoundingClientRect ( ) ;
if ( maxWidth < box . width ) { maxWidth = box . width ; }
svg . append ( 'g' ) . call ( axis ) . each ( function ( ) {
$$ . d3 . select ( this ) . selectAll ( 'text' ) . each ( function ( ) {
var box = this . getBoundingClientRect ( ) ;
if ( maxWidth < box . width ) {
maxWidth = box . width ;
}
} ) ;
dummy . remove ( ) ;
} ) ;
dummy . remove ( ) ;
} ) ;
}
$$ . currentMaxTickWidths [ id ] = maxWidth <= 0 ? $$ . currentMaxTickWidths [ id ] : maxWidth ;
return $$ . currentMaxTickWidths [ id ] ;
} ;
c3 _axis _fn . updateLabels = function updateLabels ( withTransition ) {
Axis . prototype . updateLabels = function updateLabels ( withTransition ) {
var $$ = this . owner ;
var axisXLabel = $$ . main . select ( '.' + CLASS . axisX + ' .' + CLASS . axisXLabel ) ,
axisYLabel = $$ . main . select ( '.' + CLASS . axisY + ' .' + CLASS . axisYLabel ) ,
axisY2Label = $$ . main . select ( '.' + CLASS . axisY2 + ' .' + CLASS . axisY2Label ) ;
( withTransition ? axisXLabel . transition ( ) : axisXLabel )
. attr ( "x" , this . xForXAxisLabel . bind ( this ) )
. attr ( "x" , this . xForXAxisLabel . bind ( this ) )
. attr ( "dx" , this . dxForXAxisLabel . bind ( this ) )
. attr ( "dy" , this . dyForXAxisLabel . bind ( this ) )
. text ( this . textForXAxisLabel . bind ( this ) ) ;
( withTransition ? axisYLabel . transition ( ) : axisYLabel )
. attr ( "x" , this . xForYAxisLabel . bind ( this ) )
. attr ( "x" , this . xForYAxisLabel . bind ( this ) )
. attr ( "dx" , this . dxForYAxisLabel . bind ( this ) )
. attr ( "dy" , this . dyForYAxisLabel . bind ( this ) )
. text ( this . textForYAxisLabel . bind ( this ) ) ;
( withTransition ? axisY2Label . transition ( ) : axisY2Label )
. attr ( "x" , this . xForY2AxisLabel . bind ( this ) )
. attr ( "x" , this . xForY2AxisLabel . bind ( this ) )
. attr ( "dx" , this . dxForY2AxisLabel . bind ( this ) )
. attr ( "dy" , this . dyForY2AxisLabel . bind ( this ) )
. text ( this . textForY2AxisLabel . bind ( this ) ) ;
} ;
c3 _axis _fn . getPadding = function getPadding ( padding , key , defaultValue , domainLength ) {
Axis . prototype . getPadding = function getPadding ( padding , key , defaultValue , domainLength ) {
var p = typeof padding === 'number' ? padding : padding [ key ] ;
if ( ! isValue ( p ) ) {
return defaultValue ;
@ -738,13 +366,14 @@ c3_axis_fn.getPadding = function getPadding(padding, key, defaultValue, domainLe
// assume padding is pixels if unit is not specified
return this . convertPixelsToAxisPadding ( p , domainLength ) ;
} ;
c3 _axis _fn . convertPixelsToAxisPadding = function convertPixelsToAxisPadding ( pixels , domainLength ) {
Axis . prototype . convertPixelsToAxisPadding = function convertPixelsToAxisPadding ( pixels , domainLength ) {
var $$ = this . owner ,
length = $$ . config . axis _rotated ? $$ . width : $$ . height ;
return domainLength * ( pixels / length ) ;
} ;
c3 _axis _fn . generateTickValues = function generateTickValues ( values , tickCount , forTimeSeries ) {
var tickValues = values , targetCount , start , end , count , interval , i , tickValue ;
Axis . prototype . generateTickValues = function generateTickValues ( values , tickCount , forTimeSeries ) {
var tickValues = values ,
targetCount , start , end , count , interval , i , tickValue ;
if ( tickCount ) {
targetCount = isFunction ( tickCount ) ? tickCount ( ) : tickCount ;
// compute ticks according to tickCount
@ -766,11 +395,16 @@ c3_axis_fn.generateTickValues = function generateTickValues(values, tickCount, f
tickValues . push ( end ) ;
}
}
if ( ! forTimeSeries ) { tickValues = tickValues . sort ( function ( a , b ) { return a - b ; } ) ; }
if ( ! forTimeSeries ) {
tickValues = tickValues . sort ( function ( a , b ) {
return a - b ;
} ) ;
}
return tickValues ;
} ;
c3 _axis _fn . generateTransitions = function generateTransitions ( duration ) {
var $$ = this . owner , axes = $$ . axes ;
Axis . prototype . generateTransitions = function generateTransitions ( duration ) {
var $$ = this . owner ,
axes = $$ . axes ;
return {
axisX : duration ? axes . x . transition ( ) . duration ( duration ) : axes . x ,
axisY : duration ? axes . y . transition ( ) . duration ( duration ) : axes . y ,
@ -778,7 +412,7 @@ c3_axis_fn.generateTransitions = function generateTransitions(duration) {
axisSubX : duration ? axes . subx . transition ( ) . duration ( duration ) : axes . subx
} ;
} ;
c3 _axis _fn . redraw = function redraw ( duration , isHidden ) {
Axis . prototype . redraw = function redraw ( duration , isHidden ) {
var $$ = this . owner ,
transition = duration ? $$ . d3 . transition ( ) . duration ( duration ) : null ;
$$ . axes . x . style ( "opacity" , isHidden ? 0 : 1 ) . call ( $$ . xAxis , transition ) ;