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

【下载TeeChart for Java最新版本】

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 Chartprocedure 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正版授权,请点击“咨询在线客服”哟!

FastReport2018.4

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

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2018年8月18日
下一篇 2018年8月19日

相关推荐

发表回复

登录后才能评论