Rickshaw.namespace('Rickshaw.Graph.Renderer.DashedLine');

Rickshaw.Graph.Renderer.DashedLine = Rickshaw.Class.create(Rickshaw.Graph.Renderer, {
	name : 'dashed_line',
	
	defaults: function($super) {
		return Rickshaw.extend( $super(), {
			unstack: false,
			fill: false,
			stroke: false
		} );
	},

	seriesPathFactory: function() {
		var graph = this.graph;
		var factory = d3.svg.line()
			.x( function(d) { return graph.x(d.x); } )
			.y( function(d) { return graph.y(d.y); } )
			.interpolate(this.graph.interpolation).tension(this.tension);
		factory.defined && factory.defined( function(d) { return d.y !== null; } );
		return factory;
	},

	seriesStrokeFactory: function() {
		var graph = this.graph;
		var factory = d3.svg.line()
			.x( function(d) { return graph.x(d.x); } )
			.y( function(d) { return graph.y(d.y + d.y0); } )
			.interpolate(graph.interpolation).tension(this.tension);
		factory.defined && factory.defined( function(d) { return d.y !== null; } );
		return factory;
	},

	render: function(args) {
		args = args || {};

		var graph = this.graph;
		var series = args.series || graph.series;

		var vis = args.vis || graph.vis;
		vis.selectAll('*').remove();

		// insert or stacked areas so strokes lay on top of areas
		var method = this.unstack ? 'append' : 'insert';

		var data = series
			.filter(function(s) { return !s.disabled; })
			.map(function(s) { return s.stack; });

		var nodes = vis.selectAll("path")
			.data(data)
			.enter()[method]("svg:g", 'g');

		nodes.append("svg:path")
			.attr("d", this.seriesStrokeFactory())
			.attr("class", 'line');

		var i = 0;
		series.forEach( function(series) {
			if (series.disabled) return;
			series.path = nodes[0][i++];
			this._styleSeries(series);
		}, this );
	},

	_styleSeries: function(series) {
		if (!series.path) return;

		d3.select(series.path).select('.line')
			.attr('fill', 'none')
			.attr('stroke', series.stroke || d3.interpolateRgb(series.color, 'black')(0.125))
			.attr('stroke-width', this.strokeWidth)
			.attr("stroke-dasharray", series.dotted || '4 5');

		if (series.className) {
			series.path.setAttribute('class', series.className);
		}
	}
}); 