home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / TeeChartPro / TeeChart5Delphi5Eval.exe / %MAINDIR% / Examples / Features / TeeCross.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-10  |  4.5 KB  |  164 lines

  1. {**********************************************}
  2. {   TeeChart Database Cross-Tab routines       }
  3. {   Copyright (c) 1996-2000 by David Berneda   }
  4. {**********************************************}
  5. unit TeeCross;
  6.  
  7. interface
  8.  
  9. { The procedure below creates an array of Chart Series
  10.   and fills them using the DataSet parameter.
  11.  
  12.   The Series are created using the "AGroupField" parameter.
  13.  
  14.   The "ASeries" parameter will be used to duplicate it many times,
  15.   one for each "group", thus using it as a template.
  16.  
  17.   Example of use:
  18.   ---------------
  19.  
  20.   Imagine you have a table with "Product sales".
  21.  
  22.   In this table you have the following fields:
  23.  
  24.   Product    ( Cars, Bikes, Trucks... )
  25.   Country    ( USA, UK, Germany, Australia... )
  26.   Amount     ( $1234... )
  27.  
  28.   Now we want to create a crosstab Chart consisting of one Bar
  29.   Series for each "Product", each one showing the sum of
  30.   "Amount" for each "Country".
  31.  
  32.   So,
  33.   our "GroupField" is "Product",
  34.   our "LabelField" is "Country" and
  35.   our "ValueField" is "Amount".
  36.  
  37.   Usage is:
  38.  
  39.   FillDataSet( Table1, BarSeries1, "Product", "Country", "Amount", gfSum );
  40.  
  41.   After calling this procedure, the Chart will show several Series,
  42.   one for each "Product".
  43.   Each series will show the "Sum of Amount" by "Country".
  44.  
  45.   You can access and modify these Series as usually, like for example
  46.   changing the Series Color, Title, etc.
  47.  
  48. }
  49. uses DB, Chart, TeEngine, TeeProcs;
  50.  
  51. type TGroupFormula = (gfCount, gfSum);
  52.  
  53.  
  54. Procedure FillDataSet( ADataSet:TDataSet;
  55.                        ASeries:TChartSeries;
  56.                        Const AGroupField,ALabelField,AValueField:String;
  57.                        GroupFormula:TGroupFormula);
  58.  
  59. implementation
  60.  
  61.  
  62.  
  63. Procedure FillDataSet( ADataSet:TDataSet;
  64.                        ASeries:TChartSeries;
  65.                        Const AGroupField,ALabelField,AValueField:String;
  66.                        GroupFormula:TGroupFormula);
  67.  
  68.    Function LocateSeries(Const ATitle:String):TChartSeries;
  69.    var t : Integer;
  70.    begin
  71.      With ASeries.ParentChart do
  72.      for t:=0 to SeriesCount-1 do
  73.      if Series[t].Title=ATitle then
  74.      begin
  75.        result:=Series[t];
  76.        exit;
  77.      end;
  78.      result:=nil;
  79.    end;
  80.  
  81.    Function LocateLabel(tmpSeries:TChartSeries; Const ALabel:String):Integer;
  82.    var t : Integer;
  83.    begin
  84.      With tmpSeries do
  85.      for t:=0 to Count-1 do
  86.      if XLabel[t]=ALabel then
  87.      begin
  88.        result:=t;
  89.        exit;
  90.      end;
  91.      result:=-1;
  92.    end;
  93.  
  94. var tmpGroup   : String;
  95.     tmpSeries  : TChartSeries;
  96.     tmpLabel   : String;
  97.     tmpValue   : Double;
  98.     t,tt,
  99.     tmpPoint   : Integer;
  100.     tmpBookMark   : TBookMark;
  101. begin
  102.   With ASeries.ParentChart do
  103.   begin
  104.     While SeriesCount>1 do
  105.           if Series[SeriesCount-1]<>ASeries then Series[SeriesCount-1].Free;
  106.     ASeries.Clear;
  107.     ASeries.Title:='';
  108.     With ADataSet do
  109.     begin
  110.       tmpBookMark:=GetBookMark;
  111.       DisableControls;
  112.       try
  113.         First;
  114.         While not eof do
  115.         begin
  116.           tmpGroup:=FieldByName(AGroupField).AsString;
  117.           tmpSeries:=LocateSeries(tmpGroup);
  118.           if tmpSeries=nil then
  119.           begin
  120.             if ASeries.Title='' then
  121.                tmpSeries:=ASeries
  122.             else
  123.             begin
  124.               tmpSeries:=CloneChartSeries(ASeries);
  125.               tmpSeries.SeriesColor:=GetDefaultColor(SeriesCount);
  126.             end;
  127.             tmpSeries.Title:=tmpGroup;
  128.           end;
  129.           tmpLabel:=FieldByName(ALabelField).AsString;
  130.  
  131.           tmpValue:=FieldByName(AValueField).AsFloat;
  132.           if GroupFormula=gfCount then tmpValue:=1;
  133.  
  134.           tmpPoint:=LocateLabel(tmpSeries,tmpLabel);
  135.           if tmpPoint=-1 then
  136.           begin
  137.             tmpSeries.Add(tmpValue,tmpLabel,clTeeColor);
  138.             for t:=0 to SeriesCount-1 do
  139.             if Series[t]<>tmpSeries then
  140.                if tmpSeries.Count>Series[t].Count then
  141.                for tt:=1 to (tmpSeries.Count-Series[t].Count) do
  142.                    Series[t].Add(0,tmpLabel,clTeeColor);
  143.           end
  144.           else
  145.           begin
  146.             With tmpSeries.MandatoryValueList do
  147.             case GroupFormula of
  148.               gfCount,
  149.               gfSum: Value[tmpPoint]:=Value[tmpPoint]+tmpValue;
  150.             end;
  151.           end;
  152.           Next;
  153.         end;
  154.       finally
  155.         GotoBookMark(tmpBookMark);
  156.         FreeBookmark(tmpBookMark);
  157.         EnableControls;
  158.       end;
  159.     end;
  160.   end;
  161. end;
  162.  
  163. end.
  164.