SVG xlink

15 篇文章 0 订阅

http://www.w3.org/TR/SVG/linking.html

Modify the code from : http://blog.csdn.net/solaris_navi/article/details/7467480


// draw a circle then add hyperlink on it
			String point ="<a xlink:title='"+String.valueOf(numbers[i])+"'>" +
					"<circle cx='"+String.valueOf((int)(startx + i*rw))+"' " +
							"cy='"+ String.valueOf((starty+height-(float)numbers[i]*(float)rh))+"' " +
							"r='3' fill='black'/>" +
							"</a>";
			circlepoint += point;


Main function:

private double getMax(double[] intarr)
	{
		double m = intarr[0];
		double c = 0;
		for(int i=1;i<intarr.length;i++)
		{
			c = intarr[i];
			
			if(c > m)
				m = c;
		}
		
		return m;
	}
	
	private String getSVGDrawLine(int width, int height, int startx, int starty, double[] data)
	{
		String htmlstr;
		
		// if width height ==0 then => error
		
		double maxdatavalue = this.getMax(data);	
		int datapoints = data.length;
		
		String coordi = "<path d='M"+String.valueOf(startx)+" "+String.valueOf(starty)+" L"+String.valueOf(startx)+" "+ 
						String.valueOf(height + starty)+" L"+ String.valueOf(width + startx)+" "+  String.valueOf(height + starty) +
						"' fill='none' stroke='black' stroke-width='2'/>" +
						"<text x='"+String.valueOf(startx - 10)+"' y='"+String.valueOf(starty -10)+"'>y=max:"+String.valueOf(maxdatavalue)+"</text>" +
						"<text x='"+String.valueOf(startx + width + 10)+"' y='"+String.valueOf(starty + height + 10)+"'>x=max:"+String.valueOf(datapoints-1)+"</text>" +
						"<text x='"+String.valueOf(startx - 10)+"' y='"+String.valueOf(height + starty + 10)+"'>0</text>";


		float rw = (float)width / (float)datapoints;
		float rh = (float)height / (float)maxdatavalue;
		
		String points = "";
		String circlepoint = "";
		// System.out.println(height+","+maxdatavalue+","+rh);
		
		for(int i=0;i<datapoints;i++)
		{
			points += " " + String.valueOf((int)(startx + i*rw)) + "," + String.valueOf((starty+height-(float)data[i]*(float)rh));
			//System.out.println( " " + String.valueOf(startx + (int)(i*rw)) + "," + String.valueOf((float)starty+(((float)numbers[i])*rh)));
			
			// draw a circle then add hyperlink on it
			String point ="<a xlink:title='"+String.valueOf(data[i])+"'>" +
					"<circle cx='"+String.valueOf((int)(startx + i*rw))+"' " +
							"cy='"+ String.valueOf((starty+height-(float)data[i]*(float)rh))+"' " +
							"r='3' fill='black'/>" +
							"</a>";
			circlepoint += point;
			
		}
		
		int nhl = (int)(height/25); // num of horizontal line
		int nvl = (int)(width/100); // num of vertical line
		
		if(nhl >= maxdatavalue)
			nhl = (int)maxdatavalue;
		
		if(nvl >= datapoints)
			nvl = datapoints;
		
		boolean isdrawdotlinehor = false;
		boolean isdrawdotlinevor = false;
		
		if(nhl >= 1)
			isdrawdotlinehor = true;
		if(nvl >= 1)
			isdrawdotlinevor = true;
						
		String dotLinehor = "";
		String dotLinevor = "";
		
		if(isdrawdotlinehor)
		{
			for(int i=1; i<nhl;i++)
			{
				dotLinehor += "<text x='"+String.valueOf(startx - 10)+"' y='"+String.valueOf(starty + height/nhl*i)+"' text-anchor='end'>"+String.valueOf(maxdatavalue - (int)((float)maxdatavalue/(float)nhl*(float)i))+"</text>"+
								"<line x1='"+String.valueOf(startx)+"' y1='"+String.valueOf(starty + height/nhl*i)+"' x2='"+ String.valueOf(width + startx)+
								"' y2='"+String.valueOf(starty + height/nhl*i)+"' stroke='gray' stroke-dasharray='2'/>";
			}
		}
		
		//System.out.println(nvl);
		
		if(isdrawdotlinevor)
		{
			for(int i=1; i<nvl;i++)
			{
				dotLinehor += "<text x='"+String.valueOf(startx + width/nvl*i)+"' y='"+String.valueOf(starty + height + 20)+"' text-anchor='middle'>"+String.valueOf((int)((float)datapoints/(float)nvl*(float)i))+"</text>"+
								"<line x1='"+String.valueOf(startx + width/nvl*i)+"' y1='"+String.valueOf(starty)+"' x2='"+String.valueOf(startx + width/nvl*i)+
								"' y2='"+String.valueOf(starty + height)+"' stroke='gray' stroke-dasharray='2'/>";
			}
		}
		
		String drawline = "<polyline points='"+points+"' fill='none' stroke='red' />";
		
		String svgstr = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"  width='"+String.valueOf(startx + width + 100)+
						"px' height='"+String.valueOf(starty + height + 100)+"px' xmlns:xlink='http://www.w3.org/1999/xlink'>" + 
						coordi +
						dotLinehor +
						dotLinevor +
						drawline +
						circlepoint +
						"</svg>" ; 
		
		htmlstr = " <html>" + 
					"<body>" +
					svgstr +
					"</body>" + 
					"</html>";
		
		return htmlstr;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值