/**
 * 
 */
package org.racgupta.EasyCharts;

/**
 * @author racgupta
 * 
 */
public class XYChart extends Chart {

		Styles axisStyle;
	
	

	public XYChart() {
		super();
		axisStyle = new Styles();
		axisStyle.setFill("none");
		axisStyle.setStroke("#000");
		axisStyle.setFontSize("11");
		axisStyle.setFontFamily("sans-serif");
		xAxis = new Axis();
		yAxis = new Axis();
		xAxis.setOrient("bottom");
		yAxis.setOrient("left");
		
	}

	private Axis xAxis;
	private Axis yAxis;
	private String axisCode;
	private String dataCode;
	private String domainCode;
	private String axisTitle;
	public String getDataCode() {
		if(convertData())
			return this.dataCode;
		else
			return null;
	}

	private String xyChartCode;
	
private Boolean convertData()
{
	dataCode = "var data=[";	
	if(xAxis.data.length != yAxis.data.length )
		return false;
	for(int i =0;i<xAxis.getData().length;i++)
	{
		dataCode +="{x:"+ xAxis.getData()[i]+",y:"+yAxis.getData()[i]+"},";
	}
	dataCode+="];\n";
	return true;
	
}
	public String getXYChartCode() {
		
		xyChartCode = getRegionCode();
		xyChartCode += getDataCode();		
		xyChartCode += getDomainCode();
		xyChartCode += getAxisCode();
		xyChartCode +=getTitleCode();
		xyChartCode += "svg.append(\"g\")"+
				".attr(\"class\",\"x axis\")"+axisStyle.getStyles()+
				".attr(\"transform\", \"translate(0," + getHeight()+ ")\")"+
				".call(xAxis);\n";
		xyChartCode +="svg.append(\"text\").attr(\"x\","+(getTotalWidth()/2)+").attr(\"y\","+(getHeight()+getBottomMargin())+")"+
				axisStyle.getStyles()+".attr(\"text-anchor\", \"middle\").text(\""+xAxis.getTitle()+"\");\n";
		xyChartCode +="svg.append(\"g\")"+
				".attr(\"class\", \"y axis\")"+axisStyle.getStyles()+
				".call(yAxis);\n";
		xyChartCode +="svg.append(\"text\").attr(\"y\","+(0-getLeftMargin())+").attr(\"dy\", \"1em\").attr(\"transform\",\"rotate(-90)\")"+
				axisStyle.getStyles()+".attr(\"x\","+(0-getHeight()/2)+").attr(\"text-anchor\", \"middle\").text(\""+yAxis.getTitle()+"\");\n";
		return xyChartCode;
	}
	public void setxAxis(Axis xAxis) {
		this.xAxis = xAxis;
	}

	public void setyAxis(Axis yAxis) {
		this.yAxis = yAxis;
	}

	
	public String getDomainCode() {

		domainCode = "var xDomain=d3.extent(data, function(d) { return d.x; });\n";
		domainCode += "var yDomain=d3.extent(data, function(d) { return d.y; });\n";		
		domainCode += "x.domain(xDomain);\n";
		domainCode += "y.domain(yDomain);\n";
		return domainCode;
	}
	
	
	public String getAxisCode() {
		axisCode = "var xAxis = d3.svg.axis().scale(x).orient(\"" + xAxis.getOrient()+ "\");\n";
		axisCode += "var yAxis = d3.svg.axis().scale(y).orient(\""+ yAxis.getOrient()+ "\");\n";		
		return axisCode;
	}
	public String getAxisTitle(){
		axisTitle ="svg.append(\"text\").attr(\"x\","+(getTotalWidth()/2)+")"+
					".attr(\"y\","+(getHeight()+getBottomMargin()/2)+").attr(\"text-anchor\", \"middle\").text(\""+xAxis.getTitle()+"\");";
		axisTitle +="svg.append(\"text\").attr(\"y\","+(getTotalHeight()/2)+")"+
				".attr(\"x\","+(0-getLeftMargin()/2)+").attr(\"transform\", \"rotate(-90)\").attr(\"text-anchor\", \"middle\").text(\""+xAxis.getTitle()+"\");";

		return axisTitle;
	}

}

@@ -118,20 +118,40 @@
 %09Styles 
-axis
+lineStyle;%0A%09%09Styles text
 Style;%0A%09
@@ -183,28 +183,28 @@
 %09super();%0A%09%09
-axis
+line
 Style = new 
@@ -210,25 +210,38 @@
  Styles(
+%22path.domain%22
 );%0A%09%09
-axis
+line
 Style.se
@@ -253,28 +253,28 @@
 (%22none%22);%0A%09%09
-axis
+line
 Style.setStr
@@ -284,28 +284,56 @@
 (%22#000%22);%0A%09%09
-axis
+textStyle = new Styles();%0A%09%09text
 Style.setFon
@@ -344,19 +344,19 @@
 e(%221
-1
+2
 %22);%0A%09%09
-axis
+text
 Styl
@@ -1319,36 +1319,36 @@
 %5C%22,%5C%22x axis%5C%22)%22+
-axis
+text
 Style.getStyles(
@@ -1558,36 +1558,36 @@
 gin())+%22)%22+%0A%09%09%09%09
-axis
+text
 Style.getStyles(
@@ -1732,20 +1732,20 @@
 xis%5C%22)%22+
-axis
+text
 Style.ge
@@ -1916,20 +1916,20 @@
 )%22+%0A%09%09%09%09
-axis
+text
 Style.ge
@@ -2040,24 +2040,67 @@
 )+%22%5C%22);%5Cn%22;%0A
+%09%09xyChartCode += lineStyle.getStyleCode();%0A
 %09%09return xyC

Update styling of the XY chart