Solved

Add data to a x-axis of a tchart

Posted on 2010-09-24
13
976 Views
Last Modified: 2012-06-27
Hi,

I have a listview with 5 columns and the third columns
contains dates. How can I convert these dates to a day-number.
(A few days ago I found a procedure to do that, but I can't find it anymore)
and displayed these numbers in the x-axis of a tchart.

Peter

0
Comment
Question by:peterkiers
  • 8
  • 5
13 Comments
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
set the BottomAxis.Increment value to 1 (instead of 0 by default) it will graduate your X axis every unit. If it has room it will display labels for each, otherwise will adapt to nb of labels and pixel size

all you have to do then is to set X values for each serie data (so use AddXY() instead of Add(Y) method, where X is your date number.
0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
Get the day number in its year
function FirstDayOfYear(D:TDate):TDate;

var Year, Month, Day: Word;

begin

 DecodeDate(Date, Year, Month, Day);

 Result:=EncodeDate(Year,1,1);

end;

 

function NumDayOfYear(D:TDate):Integer;

begin

 Result:=Trunc(D-FirstDayOfYear(D))+1;

end

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
I am trying but I really don't understand.
0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
this is the code for what you want (in your other post) modified with the date requirement
procedure TForm1.AddListViewValuesToSerie(NumSerie:Integer;lv:TListView;

SubColVal,SubColDat:Integer);

 procedure AddToSerie(StrVal,StrDat:String);

 Var 

  Val:Double;

  Dat:TDate;

 begin

  TryStrToFloat(StrVal,Val);

  TryStrToDate(StrDat,Dat);

  With Chart.Series[NumSerie] do AddXY(NumDayOfYear(Dat),Val);

 end;

Var

 i:integer;

begin

 Chart.Series[NumSerie].Clear;

 for i:=0 to lv.Items.Count-1 do With lv.Items[i] do

  begin

   AddToSerie(SubItems[SubColDat],SubItems[SubColVal]);

  end;

end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
Note : I simplified the code here compared to other question, because here I assume that both the value and the date are in the SubItems of each LV items, not in the caption.
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
I just don't understand what it isn't working. I understand everthing you said.
But it still doesn't work.

So, I have put the code in the code section.

And I get an error-message in this procedure:

procedure TForm1.AddListViewValuesToSerie(NumSerie: Integer; lv: TListView;
  SubColVal, SubColDat: Integer);
procedure AddToSerie(StrVal,StrDat:String);
 Var
  Val:Double;
  Dat:TDate;
 begin
  TryStrToFloat(StrVal,Val);
//  TryStrToDate(StrDat,Dat); <=============

Error: E2250 There is no overloaded version of 'TryStrToDate' that can be called with these arguments

Peter

unit Main;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, StdCtrls, Series, ComCtrls,

  xmldom, XMLIntf, msxmldom, XMLDoc;



type

  TForm1 = class(TForm)

    Chart1: TChart;

    lstvGlucose: TListView;

    XMLDoc: TXMLDocument;

    Series1: TLineSeries;

    Series2: TLineSeries;

    Series3: TLineSeries;

    Button2: TButton;

    Button1: TButton;

    procedure Button2Click(Sender: TObject);

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

    procedure AddToSerie(NumSerie:Integer;Str:String);



  public

    { Public declarations }

    procedure AddListViewValuesToSerie(NumSerie:Integer;lv:TListView;

    SubColVal,SubColDat:Integer);



  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



function FirstDayOfYear(D:TDate):TDate;

var Year, Month, Day: Word;

begin

 DecodeDate(Date, Year, Month, Day);

 Result:=EncodeDate(Year,1,1);

end;

(*---------------------------------------------*)

function NumDayOfYear(D:TDate):Integer;

begin

 Result:=Trunc(D-FirstDayOfYear(D))+1;

end;

(*---------------------------------------------*)

procedure TForm1.AddListViewValuesToSerie(NumSerie: Integer; lv: TListView;

  SubColVal, SubColDat: Integer);

procedure AddToSerie(StrVal,StrDat:String);

 Var

  Val:Double;

  Dat:TDate;

 begin

  TryStrToFloat(StrVal,Val);

//  TryStrToDate(StrDat,Dat);

  With Chart1.Series[NumSerie] do AddXY(NumDayOfYear(Dat),Val);

 end;

Var

 i:integer;

begin

 Chart1.Series[NumSerie].Clear;

 for i:=0 to lv.Items.Count-1 do With lv.Items[i] do

  begin

   AddToSerie(SubItems[SubColDat],SubItems[SubColVal]);

  end;

end;

(*---------------------------------------------*)

procedure TForm1.AddToSerie(NumSerie: Integer; Str: String);

Var Val:Double;

begin

 TryStrToFloat(Str,Val);

 With Chart1.Series[NumSerie] do Add(Val);

end;

(*---------------------------------------------*)

procedure TForm1.Button1Click(Sender: TObject);

Const

 MinX=0;

 MaxX=30;

begin

 With Chart1.Series[0] do

  begin

   AddXY(MinX,4);

   AddXY(MaxX,4);

  end;

 With Chart1.Series[1] do

  begin

   AddXY(MinX,10);

   AddXY(MaxX,10);

  end;

  With Chart1.Series[2] do

  begin

  AddListViewValuesToSerie(3,lstvGlucose,0,3);

 end;

end;

(*---------------------------------------------*)

end.



object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'Form1'

  ClientHeight = 600

  ClientWidth = 800

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Chart1: TChart

    Left = 0

    Top = 215

    Width = 800

    Height = 385

    BackWall.Brush.Color = clWhite

    BackWall.Brush.Style = bsClear

    LeftWall.Color = clWhite

    Title.Text.Strings = (

      'TChart')

    BottomAxis.Automatic = False

    BottomAxis.AutomaticMaximum = False

    BottomAxis.AutomaticMinimum = False

    BottomAxis.ExactDateTime = False

    BottomAxis.Increment = 1.000000000000000000

    BottomAxis.Maximum = 25.000000000000000000

    LeftAxis.Automatic = False

    LeftAxis.AutomaticMaximum = False

    LeftAxis.AutomaticMinimum = False

    LeftAxis.ExactDateTime = False

    LeftAxis.Increment = 2.000000000000000000

    LeftAxis.Maximum = 35.000000000000000000

    LeftAxis.Title.Caption = 'Glucose'

    View3D = False

    Align = alBottom

    TabOrder = 0

    object Series1: TLineSeries

      Marks.ArrowLength = 8

      Marks.Visible = False

      SeriesColor = clBlack

      ShowInLegend = False

      Title = 'Top baseline'

      Dark3D = False

      LinePen.Width = 2

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.DateTime = False

      XValues.Name = 'X'

      XValues.Multiplier = 1.000000000000000000

      XValues.Order = loAscending

      YValues.DateTime = False

      YValues.Name = 'Y'

      YValues.Multiplier = 1.000000000000000000

      YValues.Order = loNone

    end

    object Series2: TLineSeries

      Marks.ArrowLength = 8

      Marks.Visible = False

      SeriesColor = clBlack

      ShowInLegend = False

      Title = 'Bottom baseline'

      LinePen.Width = 2

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.DateTime = False

      XValues.Name = 'X'

      XValues.Multiplier = 1.000000000000000000

      XValues.Order = loAscending

      YValues.DateTime = False

      YValues.Name = 'Y'

      YValues.Multiplier = 1.000000000000000000

      YValues.Order = loNone

    end

    object Series3: TLineSeries

      Marks.ArrowLength = 8

      Marks.Visible = False

      SeriesColor = clRed

      Title = 'Glucose'

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.DateTime = False

      XValues.Name = 'X'

      XValues.Multiplier = 1.000000000000000000

      XValues.Order = loAscending

      YValues.DateTime = False

      YValues.Name = 'Y'

      YValues.Multiplier = 1.000000000000000000

      YValues.Order = loNone

    end

  end

  object lstvGlucose: TListView

    Left = 296

    Top = 8

    Width = 409

    Height = 185

    Columns = <

      item

        Width = 75

      end

      item

        Width = 75

      end

      item

        Width = 75

      end

      item

        Width = 75

      end

      item

        Width = 75

      end>

    GridLines = True

    TabOrder = 1

    ViewStyle = vsReport

  end

  object Button2: TButton

    Left = 24

    Top = 8

    Width = 75

    Height = 25

    Caption = 'List'

    TabOrder = 2

    OnClick = Button2Click

  end

  object Button1: TButton

    Left = 24

    Top = 39

    Width = 75

    Height = 25

    Caption = 'Graph'

    TabOrder = 3

    OnClick = Button1Click

  end

  object XMLDoc: TXMLDocument

    Left = 712

    Top = 560

    DOMVendorDesc = 'MSXML'

  end

end

Open in new window

Naamloos.jpg
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 25

Expert Comment

by:epasquier
Comment Utility
StrToDate needs a TDateTime parameter. That's a bit stupid as TDateTime = TDate, but declare Dat as TDateTime
 Var

  Val:Double;

  Dat:TDateTime; <=============

 begin

  TryStrToFloat(StrVal,Val);

  TryStrToDate(StrDat,Dat); <=============

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
I cant test to see what's wrong on your code, I don't have values in the list. probably that is what's behind the List button ?
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
This is what I got in my listbutton, its reads an xml-document:

Rename the G1839366.txt to G1839366.XML
and put the xml-file in the examples folder.

p.

procedure TForm1.Button2Click(Sender: TObject);

const

  FormatFrom: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'yyyy-mm-dd');

  FormatTo: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'dd-mm-yyyy');

  DoubleFormat: TFormatSettings = (DecimalSeparator: '.');

  Days : array[1..7] of string

    = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

var

  LoopNodes : IDOMNodeList;

  i: Integer;

  DateTime: TDateTime;

  Val: Extended;

  strVal, strValText: string;

begin

  XMLDoc.FileName := '.\G1839366.XML';

  XMLDoc.Active := True;

  try

    LoopNodes:= XMLDoc.DOMDocument.getElementsByTagName( 'BG' );

    lstvGlucose.Items.BeginUpdate;

    try

      lstvGlucose.Items.Clear;

      for i:= 0 to LoopNodes.length -1 do

        with lstvGlucose.Items.add do

        begin

          if TryStrToDate(LoopNodes[i].attributes.getNamedItem('Dt').nodeValue, DateTime, FormatFrom) then

          begin

            Caption:= Days[ DayOfWeek(DateTime) ];

            SubItems.Add( DateToStr( DateTime, FormatTo ) );

          end

          else

          begin

            Caption:= 'can''t convert date';

            SubItems.Add( LoopNodes[i].attributes.getNamedItem('Dt').nodeValue );

          end;

          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Tm').nodeValue );

          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Val').nodeValue );

          strVal:= StringReplace(LoopNodes[i].attributes.getNamedItem('Val').NodeValue, ',', '.', []);

          strValText:= '';

          if TryStrToFloat( strVal, Val, DoubleFormat ) then

            if Val < 4 then

              strValText:= 'HYPO'

            else if Val > 10 then

              strValText:= 'HYPER';

          SubItems.Add( strValText );

        end;

    finally

      lstvGlucose.Items.EndUpdate;

    end;

  finally

    XMLDoc.Active := False;

  end;

end;

Open in new window

G1839366.txt
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
Comment Utility
in BottomAxis :
- removed (set to 0) Maximum property
- set Automatic to True

code fixing :
- format conversions improved (replace the decimal separator, the date separator)
- some column index parameters errors
- improved the serie 0 & 1 creation (the 2 lines) : takes the min & max date of serie 2 into account
unit Main;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, StdCtrls, Series, ComCtrls,

  xmldom, XMLIntf, msxmldom, XMLDoc;



type

  TForm1 = class(TForm)

    Chart1: TChart;

    lstvGlucose: TListView;

    XMLDoc: TXMLDocument;

    Series1: TLineSeries;

    Series2: TLineSeries;

    Series3: TLineSeries;

    Button2: TButton;

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

    procedure AddToSerie(NumSerie:Integer;Str:String);



  public

    { Public declarations }

    procedure AddListViewValuesToSerie(NumSerie:Integer;lv:TListView;

    SubColVal,SubColDat:Integer);



  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



function FirstDayOfYear(D:TDate):TDate;

var Year, Month, Day: Word;

begin

 DecodeDate(Date, Year, Month, Day);

 Result:=EncodeDate(Year,1,1);

end;

(*---------------------------------------------*)

function NumDayOfYear(D:TDate):Integer;

begin

 Result:=Trunc(D-FirstDayOfYear(D))+1;

end;

(*---------------------------------------------*)

procedure TForm1.AddListViewValuesToSerie(NumSerie: Integer; lv: TListView;

  SubColVal, SubColDat: Integer);

procedure AddToSerie(StrVal,StrDat:String);

 Var

  Val:Double;

  Dat:TDateTime;

 begin

  StrVal:=StringReplace(StrVal,'.',',',[rfReplaceAll]);

  TryStrToFloat(StrVal,Val);

  StrDat:=StringReplace(StrDat,'-','/',[rfReplaceAll]);

  TryStrToDate(StrDat,Dat);

  With Chart1.Series[NumSerie] do AddXY(NumDayOfYear(Dat),Val);

 end;

Var

 i:integer;

begin

 Chart1.Series[NumSerie].Clear;

 for i:=0 to lv.Items.Count-1 do With lv.Items[i] do

  begin

   AddToSerie(SubItems[SubColVal],SubItems[SubColDat]);

  end;

end;

(*---------------------------------------------*)

procedure TForm1.AddToSerie(NumSerie: Integer; Str: String);

Var Val:Double;

begin

 TryStrToFloat(Str,Val);

 With Chart1.Series[NumSerie] do Add(Val);

end;

(*---------------------------------------------*)

procedure TForm1.Button1Click(Sender: TObject);

Var

 MinX,MaxX:Integer;

begin

 Chart1.Series[0].Clear;

 Chart1.Series[1].Clear;

 AddListViewValuesToSerie(2,lstvGlucose,2,0);

 MinX:=Round(Chart1.Series[2].MinXValue)-1;

 MaxX:=Round(Chart1.Series[2].MaxXValue)+1;

 With Chart1.Series[0] do

  begin

   AddXY(MinX,4);

   AddXY(MaxX,4);

  end;

 With Chart1.Series[1] do

  begin

   AddXY(MinX,10);

   AddXY(MaxX,10);

  end;

end;



procedure TForm1.Button2Click(Sender: TObject);

const

  FormatFrom: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'yyyy-mm-dd');

  FormatTo: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'dd-mm-yyyy');

  DoubleFormat: TFormatSettings = (DecimalSeparator: '.');

  Days : array[1..7] of string

    = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

var

  LoopNodes : IDOMNodeList;

  i: Integer;

  DateTime: TDateTime;

  Val: Extended;

  strVal, strValText: string;

begin

  XMLDoc.FileName := '.\G1839366.XML';

  XMLDoc.Active := True;

  try

    LoopNodes:= XMLDoc.DOMDocument.getElementsByTagName( 'BG' );

    lstvGlucose.Items.BeginUpdate;

    try

      lstvGlucose.Items.Clear;

      for i:= 0 to LoopNodes.length -1 do

        with lstvGlucose.Items.add do

        begin

          if TryStrToDate(LoopNodes[i].attributes.getNamedItem('Dt').nodeValue, DateTime, FormatFrom) then

          begin

            Caption:= Days[ DayOfWeek(DateTime) ];

            SubItems.Add( DateToStr( DateTime, FormatTo ) );

          end

          else

          begin

            Caption:= 'can''t convert date';

            SubItems.Add( LoopNodes[i].attributes.getNamedItem('Dt').nodeValue );

          end;

          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Tm').nodeValue );

          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Val').nodeValue );

          strVal:= StringReplace(LoopNodes[i].attributes.getNamedItem('Val').NodeValue, ',', '.', []);

          strValText:= '';

          if TryStrToFloat( strVal, Val, DoubleFormat ) then

            if Val < 4 then

              strValText:= 'HYPO'

            else if Val > 10 then

              strValText:= 'HYPER';

          SubItems.Add( strValText );

        end;

    finally

      lstvGlucose.Items.EndUpdate;

    end;

  finally

    XMLDoc.Active := False;

  end;

end;





(*---------------------------------------------*)

end.



// DFM



object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'Form1'

  ClientHeight = 600

  ClientWidth = 784

  Color = clBtnFace

  Constraints.MinHeight = 600

  Constraints.MinWidth = 800

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Chart1: TChart

    Left = 0

    Top = 215

    Width = 784

    Height = 385

    BackWall.Brush.Color = clWhite

    BackWall.Brush.Style = bsClear

    LeftWall.Color = clWhite

    Title.Text.Strings = (

      'TChart')

    BottomAxis.ExactDateTime = False

    BottomAxis.Increment = 1.000000000000000000

    LeftAxis.Automatic = False

    LeftAxis.AutomaticMaximum = False

    LeftAxis.AutomaticMinimum = False

    LeftAxis.ExactDateTime = False

    LeftAxis.Increment = 2.000000000000000000

    LeftAxis.Maximum = 35.000000000000000000

    LeftAxis.Title.Caption = 'Glucose'

    View3D = False

    Align = alBottom

    TabOrder = 0

    ExplicitWidth = 800

    object Series1: TLineSeries

      Marks.Callout.Brush.Color = clBlack

      Marks.Visible = False

      SeriesColor = clBlack

      ShowInLegend = False

      Title = 'Top baseline'

      Dark3D = False

      LinePen.Width = 2

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.Name = 'X'

      XValues.Order = loAscending

      YValues.Name = 'Y'

      YValues.Order = loNone

    end

    object Series2: TLineSeries

      Marks.Callout.Brush.Color = clBlack

      Marks.Visible = False

      SeriesColor = clBlack

      ShowInLegend = False

      Title = 'Bottom baseline'

      LinePen.Width = 2

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.Name = 'X'

      XValues.Order = loAscending

      YValues.Name = 'Y'

      YValues.Order = loNone

    end

    object Series3: TLineSeries

      Marks.Callout.Brush.Color = clBlack

      Marks.Visible = False

      Title = 'Glucose'

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.Name = 'X'

      XValues.Order = loAscending

      YValues.Name = 'Y'

      YValues.Order = loNone

    end

  end

  object lstvGlucose: TListView

    Left = 296

    Top = 8

    Width = 409

    Height = 185

    Columns = <

      item

        Width = 75

      end

      item

        Width = 75

      end

      item

        Width = 75

      end

      item

        Width = 75

      end

      item

        Width = 75

      end>

    GridLines = True

    TabOrder = 1

    ViewStyle = vsReport

  end

  object Button2: TButton

    Left = 24

    Top = 8

    Width = 75

    Height = 25

    Caption = 'List'

    TabOrder = 2

    OnClick = Button2Click

  end

  object Button1: TButton

    Left = 24

    Top = 39

    Width = 75

    Height = 25

    Caption = 'Graph'

    TabOrder = 3

    OnClick = Button1Click

  end

  object XMLDoc: TXMLDocument

    Left = 712

    Top = 560

    DOMVendorDesc = 'MSXML'

  end

end

Open in new window

TChartProject.gif
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
Thanks for you help.

Greetings, Peter kiers

0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
I have tried your example but I don't get it like that.

Could you please look at it once more what I forgot.
Even thou I allready gave the points.
Or send me your example.

Please

peter
unit Main;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls, ComCtrls, ExtCtrls,

  TeeProcs, TeEngine, Chart, Series;



type

  TMainForm = class(TForm)

    lstvGlucose: TListView;

    btnList: TButton;

    btnGraph: TButton;

    XMLDoc: TXMLDocument;

    Chart1: TChart;

    Series1: TLineSeries;

    Series2: TLineSeries;

    Series3: TLineSeries;

    procedure btnGraphClick(Sender: TObject);

    procedure btnListClick(Sender: TObject);

  private

    { Private declarations }

    procedure AddToSerie(NumSerie:Integer;Str:String);

  public

    { Public declarations }

    procedure AddListViewValuesToSerie(NumSerie:Integer;lv:TListView;

    SubColVal,SubColDat:Integer);

  end;



var

  MainForm: TMainForm;



implementation



{$R *.dfm}



function FirstDayOfYear(D:TDate):TDate;

var Year, Month, Day: Word;

begin

 DecodeDate(Date, Year, Month, Day);

 Result:=EncodeDate(Year,1,1);

end;

(*---------------------------------------------*)

function NumDayOfYear(D:TDate):Integer;

begin

 Result:=Trunc(D-FirstDayOfYear(D))+1;

end;

(*---------------------------------------------*)

procedure TMainForm.AddListViewValuesToSerie(NumSerie: Integer; lv: TListView;

  SubColVal, SubColDat: Integer);

procedure AddToSerie(StrVal,StrDat:String);

 Var

  Val:Double;

  Dat:TDateTime;

 begin

  StrVal:=StringReplace(StrVal,'.',',',[rfReplaceAll]);

  TryStrToFloat(StrVal,Val);

  StrDat:=StringReplace(StrDat,'-','/',[rfReplaceAll]);

  TryStrToDate(StrDat,Dat);

  With Chart1.Series[NumSerie] do AddXY(NumDayOfYear(Dat),Val);

 end;

Var

 i:integer;

begin

 Chart1.Series[NumSerie].Clear;

 for i:=0 to lv.Items.Count-1 do With lv.Items[i] do

  begin

   AddToSerie(SubItems[SubColVal],SubItems[SubColDat]);

  end;

end;

(*---------------------------------------------*)

procedure TMainForm.AddToSerie(NumSerie: Integer; Str: String);

Var Val:Double;

begin

 TryStrToFloat(Str,Val);

 With Chart1.Series[NumSerie] do Add(Val);

end;

(*---------------------------------------------*)

procedure TMainForm.btnGraphClick(Sender: TObject);

Var

 MinX,MaxX:Integer;

begin

 Chart1.Series[0].Clear;

 Chart1.Series[1].Clear;

 AddListViewValuesToSerie(2,lstvGlucose,2,0);

 MinX:=Round(Chart1.Series[2].MinXValue)-1;

 MaxX:=Round(Chart1.Series[2].MaxXValue)+1;

 With Chart1.Series[0] do

  begin

   AddXY(MinX,4);

   AddXY(MaxX,4);

  end;

 With Chart1.Series[1] do

  begin

   AddXY(MinX,10);

   AddXY(MaxX,10);

  end;

end;

(*---------------------------------------------*)

procedure TMainForm.btnListClick(Sender: TObject);

const

  FormatFrom: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'yyyy-mm-dd');

  FormatTo: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'dd-mm-yyyy');

  DoubleFormat: TFormatSettings = (DecimalSeparator: '.');

  Days : array[1..7] of string

    = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

var

  LoopNodes : IDOMNodeList;

  i: Integer;

  DateTime: TDateTime;

  Val: Extended;

  strVal, strValText: string;

begin

  XMLDoc.FileName := '.\G1839366.XML';

  XMLDoc.Active := True;

  try

    LoopNodes:= XMLDoc.DOMDocument.getElementsByTagName( 'BG' );

    lstvGlucose.Items.BeginUpdate;

    try

      lstvGlucose.Items.Clear;

      for i:= 0 to LoopNodes.length -1 do

        with lstvGlucose.Items.add do

        begin

          if TryStrToDate(LoopNodes[i].attributes.getNamedItem('Dt').nodeValue, DateTime, FormatFrom) then

          begin

            Caption:= Days[ DayOfWeek(DateTime) ];

            SubItems.Add( DateToStr( DateTime, FormatTo ) );

          end

          else

          begin

            Caption:= 'can''t convert date';

            SubItems.Add( LoopNodes[i].attributes.getNamedItem('Dt').nodeValue );

          end;

          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Tm').nodeValue );

          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Val').nodeValue );

          strVal:= StringReplace(LoopNodes[i].attributes.getNamedItem('Val').NodeValue, ',', '.', []);

          strValText:= '';

          if TryStrToFloat( strVal, Val, DoubleFormat ) then

            if Val < 4 then

              strValText:= 'HYPO'

            else if Val > 10 then

              strValText:= 'HYPER';

          SubItems.Add( strValText );

        end;

    finally

      lstvGlucose.Items.EndUpdate;

    end;

  finally

    XMLDoc.Active := False;

  end;

end;

(*---------------------------------------------*)

end.



object MainForm: TMainForm

  Left = 0

  Top = 0

  Caption = 'TChart Sample'

  ClientHeight = 600

  ClientWidth = 800

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object lstvGlucose: TListView

    Left = 248

    Top = 8

    Width = 529

    Height = 225

    Columns = <

      item

        Caption = 'Day'

        Width = 70

      end

      item

        Caption = 'Date'

        Width = 70

      end

      item

        Caption = 'Time'

        Width = 70

      end

      item

        Caption = 'Value'

        Width = 70

      end

      item

        Caption = 'Hypo/Hyper'

        Width = 70

      end>

    GridLines = True

    TabOrder = 0

    ViewStyle = vsReport

  end

  object btnList: TButton

    Left = 24

    Top = 16

    Width = 75

    Height = 25

    Caption = 'List'

    TabOrder = 1

    OnClick = btnListClick

  end

  object btnGraph: TButton

    Left = 24

    Top = 47

    Width = 75

    Height = 25

    Caption = 'Graph'

    TabOrder = 2

    OnClick = btnGraphClick

  end

  object Chart1: TChart

    Left = 176

    Top = 264

    Width = 601

    Height = 328

    BackWall.Brush.Color = clWhite

    BackWall.Brush.Style = bsClear

    Title.Text.Strings = (

      'TChart')

    LeftAxis.Automatic = False

    LeftAxis.AutomaticMaximum = False

    LeftAxis.AutomaticMinimum = False

    LeftAxis.ExactDateTime = False

    LeftAxis.Increment = 2.000000000000000000

    LeftAxis.Maximum = 35.000000000000000000

    View3D = False

    TabOrder = 3

    object Series1: TLineSeries

      Marks.ArrowLength = 8

      Marks.Visible = False

      SeriesColor = clBlack

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.DateTime = False

      XValues.Name = 'X'

      XValues.Multiplier = 1.000000000000000000

      XValues.Order = loAscending

      YValues.DateTime = False

      YValues.Name = 'Y'

      YValues.Multiplier = 1.000000000000000000

      YValues.Order = loNone

    end

    object Series2: TLineSeries

      Marks.ArrowLength = 8

      Marks.Visible = False

      SeriesColor = clBlack

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.DateTime = False

      XValues.Name = 'X'

      XValues.Multiplier = 1.000000000000000000

      XValues.Order = loAscending

      YValues.DateTime = False

      YValues.Name = 'Y'

      YValues.Multiplier = 1.000000000000000000

      YValues.Order = loNone

    end

    object Series3: TLineSeries

      Marks.ArrowLength = 8

      Marks.Visible = False

      SeriesColor = clRed

      Pointer.InflateMargins = True

      Pointer.Style = psRectangle

      Pointer.Visible = False

      XValues.DateTime = False

      XValues.Name = 'X'

      XValues.Multiplier = 1.000000000000000000

      XValues.Order = loAscending

      YValues.DateTime = False

      YValues.Name = 'Y'

      YValues.Multiplier = 1.000000000000000000

      YValues.Order = loNone

    end

  end

  object XMLDoc: TXMLDocument

    Left = 24

    Top = 80

    DOMVendorDesc = 'MSXML'

  end

end

Open in new window

mmmmmm.jpg
0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
You have all the code AND dfm of my solution. I guess you have a problem with the strings formats for float/date, I made modifications to make it work in my system, but it's possible you don't need those.
It's in :
procedure AddToSerie(StrVal,StrDat:String);

try without the StringReplace code lines, and debug to see if that is working properly
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now