1. 程式人生 > >【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 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()); 
            }             
        }
    }