unit u_chart;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, Series, DB, Grids, DBGrids, DBTables, ExtCtrls,
  TeeProcs, Chart, DbChart, StdCtrls, ComCtrls, Buttons, jpeg;

type
  TF_chart = class(TForm)
    Query1: TQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Memo1: TMemo;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    DBGrid2: TDBGrid;
    Table1: TTable;
    DataSource2: TDataSource;
    ListBox1: TListBox;
    DBChart1: TDBChart;
    BitBtn1: TBitBtn;
    Image1: TImage;
    Label4: TLabel;
    Series1: TLineSeries;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  F_chart: TF_chart;

implementation

{$R *.dfm}

procedure TF_chart.FormCreate(Sender: TObject);
Var
   MyStringList: TStringList;
   i: Integer;
begin
   try
      MyStringList:= TStringList.Create;
      Session.GetTableNames('dados','*.*',False, False, MyStringList);
      For i:= 1 To MyStringList.Count-1 do
         ListBox1.Items.Add(MyStringList.Strings[i]);
      finally
         MyStringList.Free;
      end;
end;

procedure TF_chart.ListBox1DblClick(Sender: TObject);
var
	tabela:integer;
begin
	tabela := listbox1.ItemIndex;
//	showmessage(listbox1.Items[tabela])
	table1.close;
	table1.TableName := listbox1.Items[tabela];
   table1.Open;
end;

procedure TF_chart.BitBtn1Click(Sender: TObject);
var
	s : TBARSeries;
begin
	query1.Active := false;
	query1.SQL.clear;
	query1.sql.add(memo1.text);
	query1.active := true;
   //
	try
		with DBChart1.SeriesList.Series[0] do begin
  		   active := false;
   	   ParentChart := nil;
   	   Free;
      end;
   except
      s := TBARSeries.create(dbchart1);
      s.ParentChart := dbchart1;
      s.DataSource := query1;
      s.XLabelsSource := edit1.text;
      s.XValues.ValueSource := edit1.text;
      s.YValues.ValueSource := edit2.text;
      s.CheckDataSource;
   end;
end;

procedure TF_chart.Button1Click(Sender: TObject);
begin
	dbchart1.Print;
end;

end.
