在使用charDirector画图时, 要确定setPlotArea的位置. setPlotArea方法的参数如下.
public PlotArea setPlotArea(int x, int y, int width, int height [, int bgColor [, int altBgColor [, int edgeColor [, int hGridColor [, int vGridColor ]]]]])
其中参数解释如下:
[table]
|x| The x coordinate of the left of the plot area.|
|y| The y coordinate of the top of the plot area.|
|width| The width of the plot area in pixels.|
|height| The height of the plot area in pixels.|
[/table]
因为画图的时候, y轴的值有大有小, 所以我们不能固定一个值. 应该要根据y轴值的最长宽度的计算出点的位置.
找了一下, 在chartDirtect中, 可以使用以下代码计算文本的宽度和高度:
其实, 它的原理跟java.awt.FontMetrics很类似的.
关于java.awt.FontMetrics请参考:
[url=http://fonter.iteye.com/blog/474526]androd之绘制文本[/url]
[url=http://www.cnblogs.com/KeenLeung/archive/2012/05/31/2527208.html]JAVA:图形之利用FontMetrics类居中[/url]
附上个关于java里font的图解
FontMetrics对象
它以四个基本坐标为基准,分别为:
・FontMetrics.top
・FontMetrics.ascent
・FontMetrics.descent
・FontMetrics.bottom
该图片将如下
[img]http://dl.iteye.com/upload/picture/pic/121048/96dc310a-49a3-3f20-91ad-f08c3b7eabcb.gif[/img]
public PlotArea setPlotArea(int x, int y, int width, int height [, int bgColor [, int altBgColor [, int edgeColor [, int hGridColor [, int vGridColor ]]]]])
其中参数解释如下:
[table]
|x| The x coordinate of the left of the plot area.|
|y| The y coordinate of the top of the plot area.|
|width| The width of the plot area in pixels.|
|height| The height of the plot area in pixels.|
[/table]
因为画图的时候, y轴的值有大有小, 所以我们不能固定一个值. 应该要根据y轴值的最长宽度的计算出点的位置.
找了一下, 在chartDirtect中, 可以使用以下代码计算文本的宽度和高度:
//这里得到一个TTFText文本对象
TTFText testTTF = c.getDrawArea().text3(ylabelStr[0], 字体名称, 字体大小);
int plotLeft = testTTF.getWidth() + 5; //计算出最宽的
int plotWith = picWith - plotLeft - plotRight; //画图的宽度
int plotTop = testTTF.getHeight();
plotHeight = picHeiht - plotTop - plotBottom; //画图的高度
其实, 它的原理跟java.awt.FontMetrics很类似的.
关于java.awt.FontMetrics请参考:
[url=http://fonter.iteye.com/blog/474526]androd之绘制文本[/url]
[url=http://www.cnblogs.com/KeenLeung/archive/2012/05/31/2527208.html]JAVA:图形之利用FontMetrics类居中[/url]
附上个关于java里font的图解
FontMetrics对象
它以四个基本坐标为基准,分别为:
・FontMetrics.top
・FontMetrics.ascent
・FontMetrics.descent
・FontMetrics.bottom
该图片将如下
[img]http://dl.iteye.com/upload/picture/pic/121048/96dc310a-49a3-3f20-91ad-f08c3b7eabcb.gif[/img]
Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize( 35);
textPaint.setColor( Color.WHITE);
// FontMetrics对象
FontMetrics fontMetrics = textPaint.getFontMetrics();
String text = "abcdefghijklmnopqrstu";
// 计算每一个坐标
float baseX = 0;
float baseY = 100;
float topY = baseY + fontMetrics.top;
float ascentY = baseY + fontMetrics.ascent;
float descentY = baseY + fontMetrics.descent;
float bottomY = baseY + fontMetrics.bottom;
// 绘制文本
canvas.drawText( text, baseX, baseY, textPaint);
// BaseLine描画
Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);>
baseLinePaint.setColor( Color.RED);
canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);
// Base描画
canvas.drawCircle( baseX, baseY, 5, baseLinePaint);
// TopLine描画
Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
topLinePaint.setColor( Color.LTGRAY);
canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);
// AscentLine描画
Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
ascentLinePaint.setColor( Color.GREEN);
canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);
// DescentLine描画
Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
descentLinePaint.setColor( Color.YELLOW);
canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);
// ButtomLine描画
Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
bottomLinePaint.setColor( Color.MAGENTA);
canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);