2015-05-27 18:28:00 +02:00
( function ( ) {
2015-06-13 15:51:28 +02:00
"use strict" ;
2014-06-29 19:36:25 +02:00
2015-06-13 15:51:28 +02:00
var root = this ,
Chart = root . Chart ,
helpers = Chart . helpers ;
2014-06-29 19:36:25 +02:00
2015-06-16 08:17:26 +02:00
Chart . defaults . radar = {
scale : {
type : "radialLinear" ,
2015-06-13 15:51:28 +02:00
} ,
2015-06-16 08:17:26 +02:00
elements : {
line : {
tension : 0 , // no bezier in radar
}
} ,
} ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
Chart . controllers . radar = function ( chart , datasetIndex ) {
this . initialize . call ( this , chart , datasetIndex ) ;
} ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
helpers . extend ( Chart . controllers . radar . prototype , {
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
initialize : function ( chart , datasetIndex ) {
this . chart = chart ;
this . index = datasetIndex ;
this . linkScales ( ) ;
this . addElements ( ) ;
} ,
2015-06-17 04:04:52 +02:00
updateIndex : function ( datasetIndex ) {
this . index = datasetIndex ;
} ,
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
linkScales : function ( ) {
// No need. Single scale only
} ,
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
getDataset : function ( ) {
return this . chart . data . datasets [ this . index ] ;
} ,
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
getScaleForId : function ( scaleID ) {
return this . chart . scales [ scaleID ] ;
} ,
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
addElements : function ( ) {
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
this . getDataset ( ) . metaData = this . getDataset ( ) . metaData || [ ] ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
this . getDataset ( ) . metaDataset = this . getDataset ( ) . metaDataset || new Chart . elements . Line ( {
_chart : this . chart . chart ,
_datasetIndex : this . index ,
_points : this . getDataset ( ) . metaData ,
_loop : true
} ) ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
helpers . each ( this . getDataset ( ) . data , function ( value , index ) {
this . getDataset ( ) . metaData [ index ] = this . getDataset ( ) . metaData [ index ] || new Chart . elements . Point ( {
_chart : this . chart . chart ,
_datasetIndex : this . index ,
2015-06-13 15:51:28 +02:00
_index : index ,
_model : {
2015-06-16 08:17:26 +02:00
x : 0 , //xScale.getPixelForValue(null, index, true),
y : 0 , //this.chartArea.bottom,
2015-06-13 15:51:28 +02:00
} ,
} ) ;
} , this ) ;
2015-06-16 08:17:26 +02:00
} ,
2015-06-19 20:36:22 +02:00
addElementAndReset : function ( index ) {
this . getDataset ( ) . metaData = this . getDataset ( ) . metaData || [ ] ;
var point = new Chart . elements . Point ( {
_chart : this . chart . chart ,
_datasetIndex : this . index ,
_index : index ,
} ) ;
// Reset the point
this . updateElement ( point , index , true ) ;
// Add to the points array
this . getDataset ( ) . metaData . splice ( index , 0 , point ) ;
// Make sure bezier control points are updated
this . updateBezierControlPoints ( ) ;
} ,
removeElement : function ( index ) {
this . getDataset ( ) . metaData . splice ( index , 1 ) ;
} ,
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
reset : function ( ) {
this . update ( true ) ;
} ,
2015-06-13 15:51:28 +02:00
2015-09-22 02:59:53 +02:00
buildOrUpdateElements : function buildOrUpdateElements ( ) {
// Handle the number of data points changing
var numData = this . getDataset ( ) . data . length ;
var numPoints = this . getDataset ( ) . metaData . length ;
// Make sure that we handle number of datapoints changing
if ( numData < numPoints ) {
// Remove excess bars for data points that have been removed
2015-10-21 02:33:51 +02:00
this . getDataset ( ) . metaData . splice ( numData , numPoints - numData ) ;
2015-09-22 02:59:53 +02:00
} else if ( numData > numPoints ) {
// Add new elements
for ( var index = numPoints ; index < numData ; ++ index ) {
this . addElementAndReset ( index ) ;
}
}
} ,
update : function update ( reset ) {
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
var line = this . getDataset ( ) . metaDataset ;
var points = this . getDataset ( ) . metaData ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
var scale = this . chart . scale ;
var scaleBase ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
if ( scale . min < 0 && scale . max < 0 ) {
scaleBase = scale . getPointPositionForValue ( 0 , scale . max ) ;
} else if ( scale . min > 0 && scale . max > 0 ) {
scaleBase = scale . getPointPositionForValue ( 0 , scale . min ) ;
} else {
scaleBase = scale . getPointPositionForValue ( 0 , 0 ) ;
}
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
helpers . extend ( this . getDataset ( ) . metaDataset , {
// Utility
_datasetIndex : this . index ,
// Data
_children : this . getDataset ( ) . metaData ,
// Model
_model : {
// Appearance
tension : this . getDataset ( ) . tension || this . chart . options . elements . line . tension ,
backgroundColor : this . getDataset ( ) . backgroundColor || this . chart . options . elements . line . backgroundColor ,
borderWidth : this . getDataset ( ) . borderWidth || this . chart . options . elements . line . borderWidth ,
borderColor : this . getDataset ( ) . borderColor || this . chart . options . elements . line . borderColor ,
fill : this . getDataset ( ) . fill !== undefined ? this . getDataset ( ) . fill : this . chart . options . elements . line . fill , // use the value from the this.getDataset() if it was provided. else fall back to the default
// Scale
scaleTop : scale . top ,
scaleBottom : scale . bottom ,
scaleZero : scaleBase ,
} ,
2015-06-13 15:51:28 +02:00
} ) ;
2015-06-16 08:17:26 +02:00
this . getDataset ( ) . metaDataset . pivot ( ) ;
// Update Points
helpers . each ( points , function ( point , index ) {
2015-06-19 20:36:22 +02:00
this . updateElement ( point , index , reset ) ;
2015-06-13 15:51:28 +02:00
} , this ) ;
2015-06-16 08:17:26 +02:00
// Update bezier control points
2015-06-19 20:36:22 +02:00
this . updateBezierControlPoints ( ) ;
} ,
updateElement : function ( point , index , reset ) {
var pointPosition = this . chart . scale . getPointPositionForValue ( index , this . getDataset ( ) . data [ index ] ) ;
helpers . extend ( point , {
// Utility
_datasetIndex : this . index ,
_index : index ,
2015-10-31 01:35:36 +01:00
_scale : this . chart . scale ,
2015-06-19 20:36:22 +02:00
// Desired view properties
_model : {
x : reset ? this . chart . scale . xCenter : pointPosition . x , // value not used in dataset scale, but we want a consistent API between scales
y : reset ? this . chart . scale . yCenter : pointPosition . y ,
// Appearance
tension : point . custom && point . custom . tension ? point . custom . tension : this . chart . options . elements . line . tension ,
radius : point . custom && point . custom . radius ? point . custom . pointRadius : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . pointRadius , index , this . chart . options . elements . point . radius ) ,
backgroundColor : point . custom && point . custom . backgroundColor ? point . custom . backgroundColor : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . pointBackgroundColor , index , this . chart . options . elements . point . backgroundColor ) ,
borderColor : point . custom && point . custom . borderColor ? point . custom . borderColor : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . pointBorderColor , index , this . chart . options . elements . point . borderColor ) ,
borderWidth : point . custom && point . custom . borderWidth ? point . custom . borderWidth : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . pointBorderWidth , index , this . chart . options . elements . point . borderWidth ) ,
// Tooltip
hitRadius : point . custom && point . custom . hitRadius ? point . custom . hitRadius : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . hitRadius , index , this . chart . options . elements . point . hitRadius ) ,
} ,
} ) ;
2015-10-24 00:36:00 +02:00
point . _model . skip = point . custom && point . custom . skip ? point . custom . skip : ( isNaN ( point . _model . x ) || isNaN ( point . _model . y ) ) ;
2015-06-19 20:36:22 +02:00
} ,
updateBezierControlPoints : function ( ) {
2015-06-16 08:17:26 +02:00
helpers . each ( this . getDataset ( ) . metaData , function ( point , index ) {
2015-06-13 15:51:28 +02:00
var controlPoints = helpers . splineCurve (
2015-06-16 08:17:26 +02:00
helpers . previousItem ( this . getDataset ( ) . metaData , index , true ) . _model ,
2015-06-13 15:51:28 +02:00
point . _model ,
2015-06-16 08:17:26 +02:00
helpers . nextItem ( this . getDataset ( ) . metaData , index , true ) . _model ,
2015-06-13 15:51:28 +02:00
point . _model . tension
) ;
point . _model . controlPointPreviousX = controlPoints . previous . x ;
point . _model . controlPointNextX = controlPoints . next . x ;
// Prevent the bezier going outside of the bounds of the graph
2015-06-19 20:36:22 +02:00
// Cap outer bezier handles to the upper/lower scale bounds
2015-06-16 08:17:26 +02:00
if ( controlPoints . next . y > this . chart . chartArea . bottom ) {
point . _model . controlPointNextY = this . chart . chartArea . bottom ;
} else if ( controlPoints . next . y < this . chart . chartArea . top ) {
point . _model . controlPointNextY = this . chart . chartArea . top ;
2015-06-13 15:51:28 +02:00
} else {
point . _model . controlPointNextY = controlPoints . next . y ;
}
// Cap inner bezier handles to the upper/lower scale bounds
2015-06-16 08:17:26 +02:00
if ( controlPoints . previous . y > this . chart . chartArea . bottom ) {
point . _model . controlPointPreviousY = this . chart . chartArea . bottom ;
} else if ( controlPoints . previous . y < this . chart . chartArea . top ) {
point . _model . controlPointPreviousY = this . chart . chartArea . top ;
2015-06-13 15:51:28 +02:00
} else {
point . _model . controlPointPreviousY = controlPoints . previous . y ;
}
// Now pivot the point for animation
point . pivot ( ) ;
} , this ) ;
} ,
draw : function ( ease ) {
var easingDecimal = ease || 1 ;
2015-06-16 08:17:26 +02:00
// Transition Point Locations
helpers . each ( this . getDataset ( ) . metaData , function ( point , index ) {
point . transition ( easingDecimal ) ;
} , this ) ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
// Transition and Draw the line
this . getDataset ( ) . metaDataset . transition ( easingDecimal ) . draw ( ) ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
// Draw the points
helpers . each ( this . getDataset ( ) . metaData , function ( point ) {
point . draw ( ) ;
} ) ;
2015-06-13 15:51:28 +02:00
} ,
2015-06-16 08:17:26 +02:00
setHoverStyle : function ( point ) {
// Point
var dataset = this . chart . data . datasets [ point . _datasetIndex ] ;
var index = point . _index ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
point . _model . radius = point . custom && point . custom . radius ? point . custom . radius : helpers . getValueAtIndexOrDefault ( dataset . pointHoverRadius , index , this . chart . options . elements . point . hoverRadius ) ;
point . _model . backgroundColor = point . custom && point . custom . hoverBackgroundColor ? point . custom . hoverBackgroundColor : helpers . getValueAtIndexOrDefault ( dataset . pointHoverBackgroundColor , index , helpers . color ( point . _model . backgroundColor ) . saturate ( 0.5 ) . darken ( 0.1 ) . rgbString ( ) ) ;
point . _model . borderColor = point . custom && point . custom . hoverBorderColor ? point . custom . hoverBorderColor : helpers . getValueAtIndexOrDefault ( dataset . pointHoverBorderColor , index , helpers . color ( point . _model . borderColor ) . saturate ( 0.5 ) . darken ( 0.1 ) . rgbString ( ) ) ;
point . _model . borderWidth = point . custom && point . custom . hoverBorderWidth ? point . custom . hoverBorderWidth : helpers . getValueAtIndexOrDefault ( dataset . pointBorderWidth , index , point . _model . borderWidth ) ;
} ,
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
removeHoverStyle : function ( point ) {
var dataset = this . chart . data . datasets [ point . _datasetIndex ] ;
var index = point . _index ;
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
point . _model . radius = point . custom && point . custom . radius ? point . custom . radius : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . radius , index , this . chart . options . elements . point . radius ) ;
point . _model . backgroundColor = point . custom && point . custom . backgroundColor ? point . custom . backgroundColor : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . pointBackgroundColor , index , this . chart . options . elements . point . backgroundColor ) ;
point . _model . borderColor = point . custom && point . custom . borderColor ? point . custom . borderColor : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . pointBorderColor , index , this . chart . options . elements . point . borderColor ) ;
point . _model . borderWidth = point . custom && point . custom . borderWidth ? point . custom . borderWidth : helpers . getValueAtIndexOrDefault ( this . getDataset ( ) . pointBorderWidth , index , this . chart . options . elements . point . borderWidth ) ;
}
2015-06-13 15:51:28 +02:00
2015-06-16 08:17:26 +02:00
} ) ;
2015-06-13 15:51:28 +02:00
2015-02-24 20:19:01 +01:00
} ) . call ( this ) ;