TAChart: Add TDateTimeIntervalChartSource component

git-svn-id: trunk@26949 -
This commit is contained in:
ask 2010-08-01 03:12:12 +00:00
parent df930dcc3f
commit 518cb42e0e
2 changed files with 166 additions and 3 deletions

View File

@ -53,6 +53,34 @@ LazarusResources.Add('tcharttoolset','PNG',[
+'k'#209#191#237#154#175#137'Vr'#224#208#4#170#229#230'?'#207#214#205#191#132
+#224#11'M'#1#188'+SC'#237'5'#0#0#0#0'IEND'#174'B`'#130
]);
LazarusResources.Add('tdatetimeintervalchartsource','PNG',[
#137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#0#24#0#0#0#24#8#2#0#0#0'o'#21#170#175
+#0#0#0#4'gAMA'#0#0#177#143#11#252'a'#5#0#0#0#25'tEXtSoftware'#0'Paint.NET v3'
+'.5.4>'#141#204'v'#0#0#2#8'IDAT8O'#173#148#205'K'#27'A'#24#198#211'JhO9'#22
+#165#208#155#133#226#7#133'^"T'#15#214#180#7#15'AAi'#169#169#10#162'F'#165
+#196#4'kl'#226'GP'#188'm'#210#216#180'$'#209#172'D'#189#148'&1'#238'jL'#168
+#174#237'6'#179#206#236#236#244#149#149'HkLv'#163#195#195'2,'#179#191'y'#222
+'wg'#158'{'#140'1'#203#157#12#0#221#201#176#232#20#187'}'#164'>'#149'M\'#129
+#224#21'B'#232'P'#204#243#252#174#199#19#240#249#2#28#199#185'\'#174'*~a'#239
+#207#161#176#190#224#10#148#203#229#230'}'#139#179#222#165#181#245'P'#146#223
+#203#29#28#18#140#131#171'k'#177'X<'#147#249'~]'#128#168#12#154#157#251'('#22
+#138#197#19'I'#250#245'['#150'e'#132#20'U'#211#246#5#161#199#254#218#28#232
+#131'w'#238#172#132#148's'#172#230#243#148#223#166#209'(I'#167#21'B'#28#131
+'oR'#169#148#9'G'#206#209#177't'#225#148'P'#138#243#199#10'B$'#153#164#28'G4'
+'mc{oh'#200'i'#2#20#253#26#243#135'y'#172'1DY'#169#132'p('#172#186#221'$'#181
+'#)'#164#239#221#176#9#144'('#136#3#147#139'G'#152')'#132#201'T'#147'U'#149
+#184#221#216#31#160#170':'#245'i%'#158'H'#232',6'#211#10#130#201#141#205#198
+#24#191#29#157#158#231'%'#25'L'#197#226#212#235'QW'#131'''?'#165#31#18#243'~'
+#203#188#234'u'#24#5#193'Y'#216#136'D'#218#251#23#14#254#176'3'#229'<Q,M'#239
+#210#206#160#214'<,4'#217'f'#158'<mcV'#171'!G'#0#130'u'#207#187#28#29#254'B'
+#243#164#248#224#229#151#134#150#169'G-'#189'/:'#187''''#198#199#178#217'l'
+#25'd'#25#180'U+M'#7#133#194#145#251'M'#182#135#143#159'9'#223#143'omn'#10
+#130#0'%_'#24#177'Z'#245#239#225'i'#8#4'A'#0#171#237#203#13' '#152'462P'#25
+#164'Sj4[wT'#5#164#155#170#253#215#140#128'L4'#219'`'#198#213'nv'#197#251'i'
+#238#210'BipXM'#169'r'#140#212#151#144#255#7'['#29'v'#202#222#255'I'#200#219
+#231#255'e'#212#222#30#244#23#12#145#11'Z'#145#245'.'#159#0#0#0#0'IEND'#174
+'B`'#130
]);
LazarusResources.Add('tdbchartsource','PNG',[
#137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#0#24#0#0#0#24#8#6#0#0#0#224'w='#248#0
+#0#0#4'gAMA'#0#0#177#143#11#252'a'#5#0#0#0#24'tEXtSoftware'#0'Paint.NET v3.3'

View File

@ -184,6 +184,34 @@ type
var AValues: TDoubleDynArray; var ATexts: TStringDynArray); override;
end;
TDateTimeStep = (
dtsCentury, dtsDecade, dtsYear, dtsQuarter, dtsMonth, dtsWeek, dtsDay,
dtsHour, dtsTenMinutes, dtsMinute, dtsTenSeconds, dtsSecond, dtsMillisecond
);
TDateTimeSteps = set of TDateTimeStep;
const
DATE_TIME_STEPS_ALL = [Low(TDateTimeStep) .. High(TDateTimeStep)];
type
{ TDateTimeIntervalChartSource }
TDateTimeIntervalChartSource = class(TIntervalChartSource)
private
FDateTimeFormat: String;
FSteps: TDateTimeSteps;
public
constructor Create(AOwner: TComponent); override;
procedure ValuesInRange(
AMin, AMax: Double; const AFormat: String; AUseY: Boolean;
var AValues: TDoubleDynArray; var ATexts: TStringDynArray); override;
published
property DateTimeFormat: String read FDateTimeFormat write FDateTimeFormat;
property Steps: TDateTimeSteps
read FSteps write FSteps default DATE_TIME_STEPS_ALL;
end;
TUserDefinedChartSource = class;
TGetChartDataItemEvent = procedure (
@ -220,7 +248,7 @@ procedure SetDataItemDefaults(var AItem: TChartDataItem);
implementation
uses
Math, StrUtils;
DateUtils, Math, StrUtils;
{$IFOPT R+}{$DEFINE RangeChecking}{$ELSE}{$UNDEF RangeChecking}{$ENDIF}
{$IFOPT Q+}{$DEFINE OverflowChecking}{$ELSE}{$UNDEF OverflowChecking}{$ENDIF}
@ -246,8 +274,10 @@ type
procedure Register;
begin
RegisterComponents(
CHART_COMPONENT_IDE_PAGE,
[TListChartSource, TRandomChartSource, TUserDefinedChartSource]);
CHART_COMPONENT_IDE_PAGE, [
TListChartSource, TRandomChartSource, TDateTimeIntervalChartSource,
TUserDefinedChartSource
]);
end;
procedure SetDataItemDefaults(var AItem: TChartDataItem);
@ -914,6 +944,111 @@ begin
ATexts[i] := Format(AFormat, [AValues[i], 0.0, '', 0.0, 0.0]);
end;
{ TDateTimeIntervalChartSource }
constructor TDateTimeIntervalChartSource.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FSteps := DATE_TIME_STEPS_ALL;
end;
procedure TDateTimeIntervalChartSource.ValuesInRange(AMin, AMax: Double;
const AFormat: String; AUseY: Boolean; var AValues: TDoubleDynArray;
var ATexts: TStringDynArray);
const
YEAR = 365.25;
STEP_INTERVALS: array [TDateTimeStep] of Double = (
100 * YEAR, 10 * YEAR, YEAR, YEAR / 4, YEAR / 12, 7, 1,
OneHour, 10 * OneMinute, OneMinute, 10 * OneSecond, OneSecond, OneMillisecond
);
MIN_STEPS = 4;
MAX_STEPS = 20;
var
s: TDateTimeStep;
si, x, start: TDateTime;
prevSt: TSystemTime;
function FormatLabel: String;
var
st: TSystemTime;
begin
if DateTimeFormat <> '' then
exit(FormatDateTime(DateTimeFormat, x));
DateTimeToSystemTime(x, st);
case s of
dtsCentury, dtsDecade, dtsYear:
Result := FormatDateTime('yyyy', x);
dtsQuarter:
Result := FormatDateTime('yyyy/', x) + IntToStr(Floor(x / si) mod 4 + 1);
dtsMonth:
Result := FormatDateTime(
IfThen(st.Year = prevSt.Year, 'mm', 'mm/yyyy'), x);
dtsWeek:
Result := FormatDateTime('dd/mm', x);
dtsDay:
Result := FormatDateTime(
IfThen(st.Month = prevSt.Month, 'dd', 'dd/mm'), x);
dtsHour:
Result := FormatDateTime(
IfThen(st.Day = prevSt.Day, 'hh:00', 'dd hh:00'), x);
dtsTenMinutes, dtsMinute:
Result := FormatDateTime(
IfThen(st.Hour = prevSt.Hour, 'nn', 'hh:nn'), x);
dtsTenSeconds, dtsSecond:
Result := FormatDateTime(
IfThen(st.Minute = prevSt.Minute, 'ss', 'nn:ss'), x);
dtsMillisecond:
Result := IntToStr(st.Millisecond) + 'ms';
end;
prevSt := st;
end;
var
i, cnt: Integer;
begin
if (AMax - AMin) / STEP_INTERVALS[dtsCentury] > MAX_STEPS then begin
inherited ValuesInRange(
AMin / STEP_INTERVALS[dtsYear], AMax / STEP_INTERVALS[dtsYear],
AFormat, AUseY, AValues, ATexts);
exit;
end;
s := Low(s);
while s < High(s) do begin
si := STEP_INTERVALS[s];
if (s in Steps) and ((AMax - AMin) / si > MIN_STEPS) then
break;
Inc(s);
end;
start := Int(AMin / si - 1) * si;
x := start;
cnt := 0;
while x <= AMax do begin
if x >= AMin then
cnt += 1;
x += si;
end;
i := Length(AValues);
SetLength(AValues, i + cnt);
SetLength(ATexts, i + cnt);
FillChar(prevSt, SizeOf(prevSt), $FF);
x := start;
while x <= AMax do begin
if x >= AMin then begin
AValues[i] := x;
ATexts[i] := Format(AFormat, [x, 0.0, FormatLabel, 0.0, 0.0]);
i += 1;
end;
case s of
dtsCentury: x := IncYear(x, 100);
dtsDecade: x := IncYear(x, 10);
dtsYear: x := IncYear(x);
dtsMonth: x := IncMonth(x);
otherwise x += si;
end;
end;
end;
{ TUserDefinedChartSource }
function TUserDefinedChartSource.GetCount: Integer;