home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Delphi1_And_Delphi2 / EXAMPLES / DATABASE / TEECROSS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  4.2 KB  |  158 lines

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