用Win32::OLE和Microsoft Graph画图
use strict;
use Win32::OLE qw( with in );
use Win32::OLE::Const "Microsoft Graph 9.0 Object Library";
my (%t,@CELLS,$Value,$Point);
$t{TIME} = time();
$t{WIDTH} = 640;
$t{HEIGHT} = 400;
( @CELLS ) = ( 'a'..'zz' );
$t{File} = "c:\\temp\\test.gif";
srand( time() );
$t{Class} = "MSGraph.Application";
$t{Chart} = new Win32::OLE( $t{Class} ) || die "GO Away. Can not create '$t{Class}'\n";
$t{Chart}->{Visible} = 1;
$t{Data} = $t{Chart}->DataSheet();
$t{Graph} = $t{Chart}->Chart();
$t{Graph}->{Width} = $t{WIDTH};
$t{Graph}->{Height} = $t{HEIGHT};
$t{Graph}->{HasLegend} = 0;
$t{Graph}->{Type} = xlLine;
# Align the chart so it starts on the origin
$t{Graph}->ChartGroups(1)->{HasUpDownBars} = 1 ;
$t{Graph}->ChartGroups(1)->{HasUpDownBars} = 0;
# Add data to the graph
foreach $Value ( 0 .. 22 )
{
$t{Date} = localtime( $t{TIME} + 3600 * $Value );
$t{Data}->Range( "$CELLS[$Value]0" )->{Value} = $t{Date};
$t{Data}->Range( "$CELLS[$Value]1" )->{Value} = rand( 50 );
}
# Config the x-axis
if( $t{Axis} = $t{Graph}->Axes( xlCategory ) )
{
$t{Axis}->{HasMajorGridlines} = 0;
$t{Axis}->{TickLabels}->{orientation} = xlUpward;
with( $t{Axis}->{TickLabels}->{Font},
Name => "Tahoma",
Bold => 0,
Italic => 0
);
}
# Config the y-axis
if( $t{Axis} = $t{Graph}->Axes( xlValue ) )
{
$t{Axis}->{HasMajorGridlines} = 1;
$t{Axis}->{MajorGridlines}->{Border}->{Weight} = 1;
# The color index 48 == 40% gray
$t{Axis}->{MajorGridlines}->{Border}->{ColorIndex} = 48;
$t{Axis}->{MajorGridlines}->{Border}->{LineStyle} = xlContinuous;
with( $t{Graph}->Axes( xlValue )->{TickLabels}->{Font},
Name => "Tahoma",
Bold => 0,
Italic => 0
);
}
# Configure the data point labels for the series collection
$t{Graph}->SeriesCollection( 1 )->{HasDataLabels} = 1;
if( $t{Labels} = $t{Graph}->SeriesCollection(1)->DataLabels() )
{
with( $t{Labels},
NumberFormat => "#.0",
Type => 'xlDataLabelsShowValues'
);
with( $t{Labels}->{Font},
Name => "Tahoma",
Bold => 0,
Italic => 0,
);
}
# Remove any data point labels if they are redundant
foreach $Point (in( $t{Graph}->SeriesCollection( 1 )->Points()))
{
$t{Text} = $Point->{DataLabel}->{Text};
$Point->{MarkerStyle} = xlMarkerStyleDot;
$Point->{DataLabel}->{Font}->{Background} = xlBackgroundOpaque;
$Point->{HasDataLabel} = 0 if( $t{Text} eq $t{PrevText} );
$t{PrevText} = $t{Text};
}
$t{Graph}->Export( $t{File}, "GIF", 0 );
`start $t{File}`;
返回