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

  1. {******************************************}
  2. {   Netscape Plugin for TChart Components  }
  3. { Copyright (c) 1995-1998 by David Berneda }
  4. {    All Rights Reserved                   }
  5. {******************************************}
  6. unit NPTee;
  7.  
  8. interface
  9.  
  10. uses
  11.   Windows, Messages, SysUtils, Classes, Graphics, Controls,
  12.   StdCtrls,NPForm, NPPlugin, TeeProcs, TeEngine, Chart, ExtCtrls;
  13.  
  14. type
  15.   TTeeChartPluginForm = class(TPluginForm)
  16.     Chart1: TChart;
  17.     Panel1: TPanel;
  18.     Button1: TButton;
  19.     Timer1: TTimer;
  20.     Button2: TButton;
  21.     ComboBox1: TComboBox;
  22.     Label1: TLabel;
  23.     procedure Button1Click(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure Timer1Timer(Sender: TObject);
  26.     procedure Button2Click(Sender: TObject);
  27.     procedure ComboBox1Change(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.   public
  31.     { Public declarations }
  32.     TheType:TChartSeriesClass;
  33.     Procedure LoadChart(FileName:String);
  34.     procedure LoadURL(URL:String);
  35.   end;
  36.  
  37. var
  38.   TeeChartPluginForm: TTeeChartPluginForm;
  39.  
  40. implementation
  41.  
  42. {$R *.DFM}
  43. Uses Series,EditChar,TeeStore,Dialogs,TeePrevi;
  44.  
  45. procedure TTeeChartPluginForm.Button1Click(Sender: TObject);
  46. begin
  47.   EditChart(Self,Chart1);
  48. end;
  49.  
  50. type TChartType=Record
  51.         Name:String;
  52.         AType:TChartSeriesClass;
  53.      end;
  54.  
  55. Const NumTeeTypes=7;
  56.       TeeTypes:Array[1..NumTeeTypes] of TChartType=(
  57.         ( Name: 'Bar';      AType: TBarSeries ),
  58.         ( Name: 'Line';     AType: TLineSeries ),
  59.         ( Name: 'Point';    AType: TPointSeries ),
  60.         ( Name: 'Pie';      AType: TPieSeries ),
  61.         ( Name: 'HorizBar'; AType: THorizBarSeries ),
  62.         ( Name: 'Area';     AType: TAreaSeries ),
  63.         ( Name: 'FastLine'; AType: TFastLineSeries )
  64.           );
  65.  
  66. Function WhatChartType(AParam:String; Var Index:Integer):TChartSeriesClass;
  67. var t:Integer;
  68. begin
  69.   AParam:=UpperCase(AParam);
  70.   for t:=1 to NumTeeTypes do
  71.   if UpperCase(TeeTypes[t].Name)=AParam then
  72.   begin
  73.     Index:=t;
  74.     result:=TeeTypes[t].AType;
  75.     exit;
  76.   end;
  77.   Index:=-1;
  78.   result:=TBarSeries;
  79. end;
  80.  
  81. type TPluginColor=Record
  82.           Name:String;
  83.           Color:TColor;
  84.      end;
  85.  
  86. Const NumColors=16;
  87.       Colors:Array[1..NumColors] of TPluginColor=(
  88.           ( Name:'Red'; Color:clRed),
  89.           ( Name:'Green'; Color:clGreen),
  90.           ( Name:'Yellow'; Color:clYellow),
  91.           ( Name:'Blue'; Color:clBlue),
  92.           ( Name:'White'; Color:clWhite),
  93.           ( Name:'Gray'; Color:clGray),
  94.           ( Name:'Fuchsia'; Color:clFuchsia),
  95.           ( Name:'Teal'; Color:clTeal),
  96.           ( Name:'Navy'; Color:clNavy),
  97.           ( Name:'Maroon'; Color:clMaroon),
  98.           ( Name:'Lime'; Color:clLime),
  99.           ( Name:'Olive'; Color:clOlive),
  100.           ( Name:'Purple'; Color:clPurple),
  101.           ( Name:'Silver'; Color:clSilver),
  102.           ( Name:'Aqua'; Color:clAqua),
  103.           ( Name:'Black'; Color:clBlack)
  104.         );
  105.  
  106.   Function WhatColor(Const AColor:String):TColor;
  107.   var t:Integer;
  108.       tmp:String;
  109.   begin
  110.     tmp:=Uppercase(AColor);
  111.     for t:=1 to NumColors do
  112.     begin
  113.       if Uppercase(Colors[t].Name)=tmp then
  114.       begin
  115.         result:=Colors[t].Color;
  116.         exit;
  117.       end;
  118.     end;
  119.     result:=clWhite;
  120.   end;
  121.  
  122. Const NumTeeParams=23;
  123.       TeeParams:Array[1..NumTeeParams] of String=(
  124. {1}         'View3D',
  125. {2}         'TitleText',
  126. {3}         'ChartType',
  127. {4}         'Color',
  128. {5}         'Chart3DPercent',
  129. {6}         'Animate',
  130. {7}         'LegendVisible',
  131. {8}         'URLChart',
  132. {9}         'ToolbarVisible',
  133. {10}         'Labels',
  134. {11}         'Series1',
  135. {12}         'Series1Title',
  136. {13}         'Series2',
  137. {14}         'Series2Title',
  138. {15}         'Series3',
  139. {16}         'Series3Title',
  140. {17}         'Series4',
  141. {18}         'Series4Title',
  142. {19}         'Series5',
  143. {20}         'Series5Title',
  144. {21}         'LabelsTitle',
  145. {22}         'ValuesTitle',
  146. {23}         'GradientColor'
  147.           );
  148.  
  149. Function WhatParam(AParam:String):Integer;
  150. var t:Integer;
  151. begin
  152.   AParam:=UpperCase(AParam);
  153.   for t:=1 to NumTeeParams do
  154.   if UpperCase(TeeParams[t])=AParam then
  155.   begin
  156.     result:=t;
  157.     exit;
  158.   end;
  159.   result:=0;
  160. end;
  161.  
  162. Function ParamBool(Const AParam:String):Boolean;
  163. begin
  164.   result:=UpperCase(AParam)='TRUE';
  165. end;
  166.  
  167. Function ParamLong(AParam:String):Longint;
  168. begin
  169.   result:=StrToInt(AParam);
  170. end;
  171.  
  172. Procedure TTeeChartPluginForm.LoadChart(FileName:String);
  173. var tmpChart:TCustomChart;
  174. begin
  175.   Chart1.Free;
  176.   tmpChart:=TChart.Create(Self);
  177.   LoadChartFromFile(tmpChart,FileName);
  178.   Chart1:=tmpChart as TChart;
  179.   Chart1.Align:=alClient;
  180.   Chart1.Parent:=Self;
  181. end;
  182.  
  183. procedure TTeeChartPluginForm.LoadURL(URL:String);
  184. var tmp:SmallInt;
  185.     St,LocalFile:Array[0..255] of char;
  186. begin
  187.   if GetTempPath(SizeOf(St),St)=0 then
  188.      raise Exception.Create('Cannot get the Windows TEMP path');
  189.   if GetTempFileName(St,'TMPTEE',0,LocalFile)=0 then
  190.      raise Exception.Create('Cannot get a TEMP file name');
  191.   tmp:=NPN_GetURL( Plugin.Instance, PChar(URL), LocalFile );
  192.   if tmp <> NPERR_NO_ERROR then
  193.     ShowMessage('Error loading: '+URL+' ('+IntToStr(tmp)+')')
  194.   else
  195.     LoadChart(LocalFile);
  196. end;
  197.  
  198. procedure TTeeChartPluginForm.FormCreate(Sender: TObject);
  199.  
  200.    Procedure CheckNumSeries(SeriesNum:Integer);
  201.    var tmpType:TChartSeriesClass;
  202.        t:Integer;
  203.        tmp:TChartSeries;
  204.    begin
  205.      if Chart1.SeriesCount<=SeriesNum then
  206.      begin
  207.        if Chart1.SeriesCount>0 then tmpType:=TChartSeriesClass(Chart1[0].ClassType)
  208.                                else tmpType:=TheType;
  209.        for t:=Chart1.SeriesCount to SeriesNum-1 do
  210.        begin
  211.          tmp:=tmpType.Create(Self);
  212.          tmp.Marks.Style:=smsValue;
  213.          Chart1.AddSeries(tmp);
  214.        end;
  215.      end;
  216.    end;
  217.  
  218.    Procedure ProcessLabels(Const AText:String);
  219.    var tmp:TStringList;
  220.        t:Integer;
  221.    begin
  222.      CheckNumSeries(1);
  223.      With Chart1[Chart1.SeriesCount-1] do
  224.      begin
  225.        Clear;
  226.        tmp:=TStringList.Create;
  227.        try
  228.          tmp.CommaText:=AText;
  229.          if tmp.Count>0 then
  230.          begin
  231.            for t:=0 to tmp.Count-1 do
  232.            begin
  233.              if Count<=t then
  234.                 Add(0,tmp[t],clTeeColor)
  235.              else
  236.                 XLabel[t]:=tmp[t];
  237.            end;
  238.          end;
  239.        finally
  240.          tmp.Free;
  241.        end;
  242.      end;
  243.    end;
  244.  
  245.    Procedure AddValues(SeriesNum:Integer; Const AText:String);
  246.    var t:Integer;
  247.        tmp:TStringList;
  248.    begin
  249.      CheckNumSeries(SeriesNum);
  250.      With Chart1[SeriesNum-1] do
  251.      begin
  252.        tmp:=TStringList.Create;
  253.        try
  254.          tmp.CommaText:=AText;
  255.          if tmp.Count>0 then
  256.          begin
  257.            for t:=0 to tmp.Count-1 do
  258.            begin
  259.              if Count<=t then
  260.                 Add(StrToFloat(tmp[t]),'',clTeeColor)
  261.              else
  262.                 MandatoryValueList[t]:=StrToFloat(tmp[t]);
  263.            end;
  264.          end;
  265.        finally
  266.          tmp.Free;
  267.        end;
  268.      end;
  269.    end;
  270.  
  271.    Procedure SeriesTitle(SeriesNum:Integer; Const AText:String);
  272.    begin
  273.      CheckNumSeries(SeriesNum);
  274.      Chart1[SeriesNum-1].Title:=AText;
  275.    end;
  276.  
  277. Var i,t:Integer;
  278.     tmp1,tmp2:TColor;
  279.     tmpType:TChartSeriesClass;
  280. begin
  281.   TheType:=TBarSeries;
  282.   ComboBox1.ItemIndex:=0;
  283.   with Plugin do
  284.   for t:=0 to ParamNames.Count-1 do
  285.   Case WhatParam(ParamNames[t]) of
  286.     1: Chart1.View3D:=ParamBool(ParamValues[t]);
  287.     2: Begin
  288.          Chart1.Title.Text.Clear;
  289.          Chart1.Title.Text.Add(ParamValues[t]);
  290.        end;
  291.     3: begin
  292.          tmpType:=WhatChartType(ParamValues[t],i);
  293.          ChangeAllSeriesType(Chart1,tmpType);
  294.          ComboBox1.ItemIndex:=i-1;
  295.        end;
  296.     4: Chart1.Color:=ParamLong(ParamValues[t]);
  297.     5: Chart1.Chart3DPercent:=ParamLong(ParamValues[t]);
  298.     6: Timer1.Enabled:=ParamBool(ParamValues[t]);
  299.     7: Chart1.Legend.Visible:=ParamBool(ParamValues[t]);
  300.     8: begin
  301.          Timer1.Enabled:=False;
  302.          LoadURL(ParamValues[t]);
  303.        end;
  304.     9: Panel1.Visible:=ParamBool(ParamValues[t]);
  305.    10: ProcessLabels(ParamValues[t]);
  306.    11: AddValues(1,ParamValues[t]);
  307.    12: SeriesTitle(1,ParamValues[t]);
  308.    13: AddValues(2,ParamValues[t]);
  309.    14: SeriesTitle(2,ParamValues[t]);
  310.    15: AddValues(3,ParamValues[t]);
  311.    16: SeriesTitle(3,ParamValues[t]);
  312.    17: AddValues(4,ParamValues[t]);
  313.    18: SeriesTitle(4,ParamValues[t]);
  314.    19: AddValues(5,ParamValues[t]);
  315.    20: SeriesTitle(5,ParamValues[t]);
  316.    21: Chart1.BottomAxis.Title.Caption:=ParamValues[t];
  317.    22: Chart1.LeftAxis.Title.Caption:=ParamValues[t];
  318.    23: begin
  319.          i:=Pos(',',ParamValues[t]);
  320.          if i>0 then
  321.          begin
  322.            tmp1:=WhatColor(Copy(ParamValues[t],1,i-1));
  323.            tmp2:=WhatColor(Copy(ParamValues[t],i+1,255));
  324.          end
  325.          else
  326.          begin
  327.            tmp1:=WhatColor(ParamValues[t]);
  328.            tmp2:=tmp1;
  329.          end;
  330.          With Chart1.Gradient do
  331.          begin
  332.            Visible:=tmp1<>tmp2;
  333.            StartColor:=tmp1;
  334.            EndColor:=tmp2;
  335.            if not Visible then Chart1.Color:=tmp1;
  336.          end;
  337.        end;
  338.   end;
  339. end;
  340.  
  341. procedure TTeeChartPluginForm.Timer1Timer(Sender: TObject);
  342. begin
  343.   if Chart1.SeriesCount>0 then
  344.   begin
  345.     if Chart1[0] is TPieSeries then
  346.     With TPieSeries(Chart1[0]) do RotationAngle:=RotationAngle+5;
  347.   end;
  348. end;
  349.  
  350.  
  351. procedure TTeeChartPluginForm.Button2Click(Sender: TObject);
  352. begin
  353.   ChartPreview(Self,Chart1);
  354. end;
  355.  
  356. procedure TTeeChartPluginForm.ComboBox1Change(Sender: TObject);
  357. var i:Integer;
  358. begin
  359.   ChangeAllSeriesType(Chart1,WhatChartType(ComboBox1.Items[ComboBox1.ItemIndex],i));
  360. end;
  361.  
  362. initialization
  363.   RegisterPluginForm( '', TTeeChartPluginForm ) ;
  364. end.
  365.