Recent

Author Topic: [SOLVED] How to avoid null data?  (Read 2655 times)

madepabloh

  • Full Member
  • ***
  • Posts: 160
[SOLVED] How to avoid null data?
« on: August 18, 2014, 09:00:56 pm »
I am writting a simple application what read a csv file, shows the data into an stringgrid, and plot the values in a chart.

Since the numbers of columns is unknown, i add the series in runtime by counting the humber of columns in the stringgrid. Everything works fine except when the cells are empty.

I know there is an example in TAChart component about to assign "NaN" to that component, and after explore the example and this forum, i still can´t figure out how could i use this information to my case.

This is the procedure i create to read the stringgrid and create lineseries in the Chart:
Code: [Select]
procedure TMain.PlotData (Table: TstringGrid; Chart: TChart);
 var
  colnumber: Integer;
  rownumber: integer;
  ls: Tlineseries;
  x: TDateTime;
  y: double;

begin
  Chart.ClearSeries;
  colnumber := Table.ColCount;
  rownumber := Table.RowCount;
  for i := 0 to colnumber-2 do
    begin
      ls := Tlineseries.Create(Chart);
      chart.addSeries(ls);
      ls.Legend.Format := Table.Cells[i+2, 1];
      for n := 2 to Rownumber-1 do
        begin
          x := StrToDateTime(Table.Cells[1, n]);
          y := StrToFloat(Table.Cells[i + 2, n]);
          ls.AddXY(x, y);
        end;
    end;
end;

How could i solve this issue? Thanks!
« Last Edit: August 18, 2014, 11:15:34 pm by madepabloh »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to avoid null data?
« Reply #1 on: August 18, 2014, 09:29:52 pm »
Code: [Select]
procedure TMain.PlotData (Table: TstringGrid; Chart: TChart);
 var
  colnumber: Integer;
  rownumber: integer;
  ls: Tlineseries;
  x: TDateTime;
  y: double;

begin
  Chart.ClearSeries;
  colnumber := Table.ColCount;
  rownumber := Table.RowCount;
  for i := 0 to colnumber-2 do
    begin
      ls := Tlineseries.Create(Chart);
      chart.addSeries(ls);
      ls.Legend.Format := Table.Cells[i+2, 1];
      for n := 2 to Rownumber-1 do
        begin
          if TryStrToFloat(Table.Cells[i + 2, n], y) then begin
            x := StrToDateTime(Table.Cells[1, n]);
            ls.AddXY(x, y);
          end;
        end;
    end;
end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: How to avoid null data?
« Reply #2 on: August 18, 2014, 11:15:12 pm »
OMG! It was so simple!
I had on mind really complex procedures...

Thanks so much @taazz for to modify my code. It work nice now! Thanks!!

 

TinyPortal © 2005-2018