📄 chart2unit.~pas
字号:
unit Chart2Unit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleCtrls, OWC_TLB, StdCtrls, ExtCtrls, MSDATASRC_TLB;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
ChartSpace1: TChartSpace;
DataSourceControl1: TDataSourceControl;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const
DBPath : String = 'C:\DATA\NORTHWIND.MDB';
procedure TForm1.Button1Click(Sender: TObject);
var
RSD : RecordsetDef; // Datasource
BarChart : WCChart; // Chart
PieChart : WCChart; // Chart
begin
//
// Define the data source itself and the SQL query used to extract data
//
DataSourceControl1.ConnectionString :=
'DRIVER={Microsoft Access Driver (*.mdb)}; ' +
'DBQ=' + DBPath;
RSD := DataSourceControl1.RecordsetDefs.AddNew(
'Select * from [Category Sales for 1997]', 3, 'Sales');
//
// Clear the contents of the chart
//
With ChartSpace1 do
Begin
Clear;
Refresh;
// Specify the datasource
DataSource := DataSourceControl1.DefaultInterface as MSDATASRC_TLB.DataSource;
DataMember := RSD.Name;
End;
// Set the horizontal layout for the Chart
ChartSpace1.ChartLayout := chChartLayoutHorizontal;
// Add new Chart
BarChart := ChartSpace1.Charts.Add(0);
With BarChart do
Begin
// Set the type of Chart - Bar Chart
Type_ := chChartTypeBarClustered;
// The first field if Categories
SetData(chDimCategories, 0, 0);
// The second field is values
SetData(chDimValues, 0, 1);
// Let's format the axes
With Axes[chAxisPositionBottom] do
Begin
NumberFormat := '0,';
MajorUnit := 25000;
HasMajorGridlines := False;
End;
End;
// Add new Chart
PieChart := ChartSpace1.Charts.Add(1);
With PieChart do
Begin
// Set the type of Chart - Pie Chart
Type_ := chChartTypePie;
// The first field if Categories
SetData(chDimCategories, 0, 0);
// The second field is values
SetData(chDimValues, 0, 1);
// "Explode" segments of the pie
SeriesCollection.Item[0].Explosion := 20;
// Let's add the legend
HasLegend := True;
Legend.Position := chLegendPositionBottom;
// and title
HasTitle := True;
Title.Caption := 'Sales by Category for 1997';
Title.Font.Set_Bold(True);
Title.Font.Set_Size(11);
WidthRatio := 75;
// Data will be shown as percents
With SeriesCollection.Item[0].DataLabelsCollection.Add do
Begin
HasValue := False;
HasPercentage := True;
Font.Set_Size(7);
End
End;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -