java绘制图表控件_【TeeChart for Java教程】(十)图表面板上的自定义绘图

本文详细介绍了如何使用TeeChart for Java库在2D和3D图表上进行自定义绘制,包括绘制线条、调整绘图笔样式、添加2D和3D形状、定位文本以及应用实例,展示了在图表控件中实现复杂视觉效果的方法。
摘要由CSDN通过智能技术生成

TeeChart Canvas

1 绘图线

1.1 2D图表

添加一个绘图线:

private void Load() {

line1.fillSampleValues(20);

line1.setVertAxis(VerticalAxis.BOTH);

line1.setHorizAxis(HorizontalAxis.BOTH);

tChart1.getAspect.setView3D(false);

}

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

IGraphics3D g = tChart1.getGraphics3D();

Point s = new Point(tChart1.getAxes().getLeft().getPosition(), tChart1.getAxes().getTop().getPosition());

Point e = new Point(tChart1.getAxes().getRight().getPosition(), tChart1.getAxes().getBottom().getPosition());

g.MoveTo(s);

g.LineTo(e,0);

}

}

1.2 3D图表

在3D图表上,由于3D正交位移,轴位置偏离图表区域。我们可以相应地移动线路:示例(在3D图表的图表区域中从左上角到右下角对角绘制线条)

private void Load() {

line1.fillSampleValues(20);

line1.setVertAxis(VerticalAxis.BOTH);

line1.setHorizAxis(HorizontalAxis.BOTH);

tChart1.getAspect.setChart3DPercent(50);

}

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

IGraphics3D g = tChart1.getGraphics3D();

com.steema.teechart.Point3D s = new com.steema.teechart.Point3D();

s.x = tChart1.getAxes().getLeft().getPosition();

s.y = tChart1.getAxes().getTop().getPosition();

s.z = 0;

com.steema.teechart.Point3D e = new com.steema.teechart.Point3D();

e.x = tChart1.getAxes().getRight().getPosition();

e.y = tChart1.getAxes().getBottom().getPosition();

e.z = tChart1.getAspect().width3D;

g.moveTo(s);

g.lineTo(e);

}

}

2 绘图笔和画笔

上面的线是使用为绘制线之前绘制的最后一个对象定义的笔和笔刷绘制的。那可能是也可能不是你想要的笔。您可以相应地更改笔:

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

IGraphics3D g = tChart1.getGraphics3D();

Point p5 = new Point(line1.calcXPos(5), line1.calcYPos(5));

Point p15 = new Point(line1.calcXPos(15), line1.calcYPos(15));

g.getPen().setDashCap(DashCap.SQUARE);

g.getPen().setEndCap(LineCap.MITER);

g.getPen().setStyle(DashStyle.DASHDOTDOT);

g.getPen().setTransparency(70);

g.getPen().setWidth(3);

g.getPen().setColor(Color.blue);

g.moveTo(p5);

g.lineTo(p15, 0);

}

}

3 添加2D形状

以与Canvas Lines类似的方式添加2D Canvas Shapes。以下示例在图表区域的中心添加一个Rectangle:

3.1 2D图表

2D图表仅支持2D形状。

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

IGraphics3D g = tChart1.getGraphics3D();

Dimension d = new Dimension(100,100);

Point p = new Point(((int)(g.getXCenter() - (d.getWidth()/2))),((int)(g.getYCenter() - (d.getHeight()/2))));

com.steema.teechart.Rectangle r = new com.steema.teechart.Rectangle(p,d);

g.getPen().setColor(Color.cyan);

g.getBrush().setColor(Color.blue);

g.rectangle(r);

}

}

3.2 3D图表

在3D图表上,您也可以在Z平面中移动矩形。请参阅RectangleWithZ方法。此示例将矩形放置在左侧墙壁上,但将其移向图表后部的中间位置。

private void Load() {

point3D1.LinePen.Visible = false;

point3D1.fillSampleValues(20);

point3D1.setVertAxis(VerticalAxis.BOTH);

point3D1.setHorizAxis(HorizontalAxis.BOTH);

tChart1.getAspect.setChart3DPercent(50);

tChart1.getAxes().getDepth().setVisible(true);

}

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

IGraphics3D g = tChart1.getGraphics3D();

Dimension d = new Dimension(100, 100);

Point l = new Point(((int)tChart1.getAxes().getLeft().getPosition()),((int)(g.getYCenter() - (d.getHeight() / 2))));

com.steema.teechart.Rectangle r = new com.steema.teechart.Rectangle(l,d);

g.getPen().setColor(Color.cyan);

g.getBrush().setColor(Color.blue);

g.rectangle(r, tChart1.getAspect().width3D/2);

}

4 添加3D形状

您可以将3D形状添加到3D图表中。此示例在Chart矩形的中间绘制一个Cube:

private void Load() {

point3D1.getLinePen().setVisible(false);

point3D1.fillSampleValues(20);

tChart1.getAspect().setChart3DPercent(50);

tChart1.getLegend().setVisible(false);

tChart1.getAxes().getDepth().setVisible(true);

}

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

IGraphics3D g = tChart1.getGraphics3D();

Dimension d = new Dimension(50,50);

Point p = new Point(((int)(g.getXCenter() - (d.getWidth()/2))),((int)(g.getYCenter() - (d.getHeight()/2))));

com.steema.teechart.Rectangle r = new com.steema.teechart.Rectangle(p,d);

g.cube(r, 0, 20, true);

}

}

5 添加文本

5.1 2D文本位置

将文本添加到最后一个矩形:

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

String text = "My Text";

IGraphics3D g = tChart1.getGraphics3D();

Dimension d = new Dimension(150, 50);

Point p = new Point(((int)(g.getXCenter() - (d.getWidth()/2))),((int)(g.getYCenter() - (d.getHeight()/2))));

com.steema.teechart.Rectangle r = new com.steema.teechart.Rectangle(p,d);

g.getPen().setColor(Color.blue);

g.rectangle(r);

g.textOut(((int)(g.getXCenter() - (g.textWidth(text)/2))),

((int)(g.getYCenter() - (g.textHeight(text)/2))), text);

}

}

5.2 3D文本位置

通过使用带有z坐标的TextOut重载,可以将文本放置在不同的3D平面中。

private void Load() {

point3D1.fillSampleValues(20);

point3D1.getLinePen().setVisible(false);

tChart1.getAspect().setChart3DPercent(50);

}

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

String text = "My Text";

IGraphics3D g = tChart1.getGraphics3D();

g.textOut(g.getXCenter(), g.getYCenter(), tChart1.getAspect().width3D / 2, text);

}

}

5.3 应用实例

这个例子取一个Series的第3和第10个值,在它们之间绘制一条Line,并告诉我们新Line的第一个和最后一个点的值以及它们之间的差异:

'First add some data to the empty Chart

procedure TForm1.BitBtn1Click(Sender: TObject);

begin

Series1.FillSampleValues(20);

end;

private void Load() {

tChart1.getAspect().setView3D(false);

line1.fillSampleValues(20);

}

public void chartEvent(EventArgs e) {

if (e instanceof AfterDrawEventArgs) {

IGraphics3D g = tChart1.getGraphics3D();

if(tChart1.getSeriesCount() > 0){

if(tChart1.getSeries(0).getCount() > 10) {

Series s = tChart1.getSeries(0);

int h = Convert.ToInt32(g.TextHeight("H"));

Point p1 = new Point(s.calcXPos(3), s.calcYPos(3));

Point p2 = new Point(s.calcXPos(10), s.calcYPos(10));

g.getPen().setColor(Color.blue);

g.getPen().setWidth(2);

g.getPen().setStyle(com.steema.teechart.drawing.DashStyle.DASH);

g.moveTo(p1);

g.lineTo(p2, 0);

g.textOut(p1.x, p1.y - h, "Point value: " + s.getYValues(3).toString());

g.textOut(p2.x, p2.y, "Point value: " + s.getYValues(10).toString());

g.textOut(p2.x, p2.y + h, "Change is: " + s.getYValues(3).toString() -

s.getYValues(10).toString());

}

}

}

购买TeeChart for Java正版授权,请点击“咨询在线客服”哟!

96077af6848fdc4e14e957f596d19dcb.png

标签:图表Java图表库图表解决方案图表控件teechart

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,尊重他人劳动成果

a6e1590ae4b228073faff3806334194e.png0

好文不易,鼓励一下吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值