Marco's Web Center

Menu for Development

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old)Tools
(Old) Conferences

My Other Sites
Italian Site (www.marcocantu.it)
the delphi search

Spirit of delphi

Advertising
Home My Blog Books Object Pascal Marco

Home: Code Repository: Mastering Delphi 5

Project WEBCHART

Project Structure


WEBCHART.DPR

library WebChart;

uses
  WebBroker,
  ISAPIApp,
  ChartMod in 'ChartMod.pas' {WebModule1: TWebModule};

{$R *.RES}

exports
  GetExtensionVersion,
  HttpExtensionProc,
  TerminateExtension;

begin
  Application.Initialize;
  Application.CreateForm(TWebModule1, WebModule1);
  Application.Run;
end.

CHARTMOD.PAS

unit ChartMod;

interface

uses
  Windows, Messages, SysUtils, Classes, HTTPApp, Db, DBTables,
  DbChart, Series, Jpeg, ExtCtrls;

type
  TWebModule1 = class(TWebModule)
    Table1: TTable;
    procedure WebModule1ActionAreaAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    procedure WebModule1ActionPopulationAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    procedure WebModule1Create(Sender: TObject);
    procedure WebModule1Destroy(Sender: TObject);
    procedure WebModule1AfterDispatch(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  private
    Chart: TDBChart;
    Series: TPieSeries;
    Image: TImage;
  public
    { Public declarations }
  end;

var
  WebModule1: TWebModule1;

implementation

{$R *.DFM}

procedure TWebModule1.WebModule1ActionAreaAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  // set specific values
  Chart.Title.Text.Clear;
  Chart.Title.Text.Add ('Area of Countries');
  Chart.LeftAxis.Title.Caption := 'Area';
  Series.Title := 'Area';
  Series.PieValues.ValueSource := 'Area';
end;

procedure TWebModule1.WebModule1ActionPopulationAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  // set specific values
  Chart.Title.Text.Clear;
  Chart.Title.Text.Add ('Population of Countries');
  Chart.LeftAxis.Title.Caption := 'Population';
  Series.Title := 'Population';
  Series.PieValues.ValueSource := 'Population';
end;

procedure TWebModule1.WebModule1Create(Sender: TObject);
begin
  // open the database table
  Table1.Open;
  // create the chart
  Chart := TDBChart.Create (nil);
  Chart.Width := 600;
  Chart.Height := 400;
  Chart.AxisVisible := False;
  Chart.Legend.Visible := False;
  Chart.BottomAxis.Title.Caption := 'Name';
  // create the pie series
  Series := TPieSeries.Create (Chart);
  Series.ParentChart := Chart;
  Series.DataSource := Table1;
  Series.XLabelsSource := 'Name';
  Series.OtherSlice.Style := poBelowPercent;
  Series.OtherSlice.Text := 'Others';
  Series.OtherSlice.Value := 2;
  Chart.AddSeries (Series);
  // create the memory bitmap
  Image := TImage.Create (nil);
  Image.Width := Chart.Width;
  Image.Height := Chart.Height;
end;

procedure TWebModule1.WebModule1Destroy(Sender: TObject);
begin
  Chart.Free;
  Series.Free;
  Image.Free;
end;

procedure TWebModule1.WebModule1AfterDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  Jpeg: TJpegImage;
  MemStr: TMemoryStream;
begin
  // paint the chart on the memory bitmap
  Chart.Draw (Image.Canvas, Image.BoundsRect);
  // create the jpeg and copy the iamge to it
  Jpeg := TJpegImage.Create;
  try
    Jpeg.Assign (Image.Picture.Bitmap);
    MemStr := TMemoryStream.Create;
    // save to a stream and return it
    Jpeg.SaveToStream (MemStr);
    MemStr.Position := 0;
    Response.ContentType := 'image/jpeg';
    Response.ContentStream := MemStr;
    Response.SendResponse;
  finally
    Jpeg.Free;
  end;
end;

end.

CHARTMOD.DFM

object WebModule1: TWebModule1
  OldCreateOrder = False
  OnCreate = WebModule1Create
  OnDestroy = WebModule1Destroy
  Actions = <
    item
      Default = True
      Name = 'ActionPopulation'
      PathInfo = '/population'
      OnAction = WebModule1ActionPopulationAction
    end
    item
      Name = 'ActionArea'
      PathInfo = '/area'
      OnAction = WebModule1ActionAreaAction
    end>
  AfterDispatch = WebModule1AfterDispatch
  Left = 194
  Top = 107
  Height = 150
  Width = 215
  object Table1: TTable
    DatabaseName = 'DBDEMOS'
    TableName = 'country.db'
    Left = 24
    Top = 24
  end
end