Series point values


Q: How can I copy values from one Series to another ?


One solution is to make the second Series DataSource to be the first Series:

DELPHI CODE : 

 Series2.DataSource := Series1 ;

CBUILDER CODE

 Series2->DataSource=Series1;

Another solution is to call the AssignValues method:

DELPHI CODE : 

 Series2.AssignValues( Series1 );

CBUILDER CODE : 

 Series2->AssignValues(Series1);

Q: How can I display the sum of all the values in a Chart Series?

Example: There is a Pie chart series with three pie slices, the values are 10, 20, 30. Now I want to see the sum (=60) somewhere in the chart.

You can use the Series.YValues Total and TotalABS properties

DELPHI CODE : 

 ShowMessage( FloatToStr( Series1.YValues.Total ) );

CBUILDER CODE : 

 ShowMessage(FloatToStr(Series1->YValues->Total));

Q: How can I group and plot values by month or by week ?


If your database does not accept "Group by Month()" clauses in your SQL statements, you might use the "TeeMonth.pas" unit included in TeeChart Pro 3.0
This unit can be found at this TeeChart Pro 3.0 folder:

c:\TeeChart\TeeKit\Extras\Monthly


Q: How to add NULL values to a chart ?


Use the Series AddNull or the Series AddNullXY method. Null points are not displayed.

DELPHI CODE : 

 Series1.AddNull( '' );
 Series1.AddNullXY(2,3,'');

CBUILDER CODE : 

 Series1->AddNull("");
 Series1->AddNullXY(2,3,"");

Q: How to remove gaps after deleting points in a Series ?


You can call the Series values FillSequence method to assign a new different X coordinate for each point.

DELPHI CODE : 

Series1.Delete( 3 );
Series1.XValues.FillSequence;

CBUILDER CODE : 

 Series1->Delete(3);
 Series1->XValues->FillSequence();

Q: Is there a way to specify a range of points to be shown on chart? For example, if we add 50 points to a Series, have the chart show points 34 to 41 only.

The Series OnBeforeAdd event can be used to limit which points do we actually add (see the code bellow):

DELPHI CODE : 

var tmpCount:integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.Clear;  //force clear, just in case
  Series1.FillSampleValues(50);
end;

function TForm1.Series1BeforeAdd(Sender: TChartSeries): Boolean;
begin
  Inc(tmpCount);
  result:= (tmpCount>34) and (tmpCount<41); //add only desired values
end;

procedure TForm1.Series1ClearValues(Sender: TChartSeries);
begin
  tmpCount:=0; //reset counter
end;

CBUILDER CODE : 

TForm1 *Form1;
int tmpCount;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
        Series1->Clear();  //force clear, just in case
        Series1->FillSampleValues(50);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Series1ClearValues(TChartSeries *Sender)
{
        tmpCount=0; // reset counter
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::Series1BeforeAdd(TChartSeries *Sender)
{
  tmpCount++;
  bool result= (tmpCount>34) && (tmpCount<41); //add only desired values
  return result;
}
//---------------------------------------------------------------------------

Q: I'd like to connect points in "chornological" order i.e. the way I add them. But TeeChart seems to connect points by ascending XValues.

Well, the Series.XValues.Order:=loAscending; is the default setting for connecting points by line. If you require something else, you can easily change the "line drawing" order by:

DELPHI CODE : 

  Series.XValues.Order:=loNone;

CBUILDER CODE : 

 Series1->XValues->Order=loNone;

Q: If I use a Series, then the Null values are always displayed as 0. How can I avoid this?

The AddNull (or the AddNullXY in TeeChart4) method is actually only the standard Add (or AddXY in TeeChart4) method with the Color=clNone; I took this out of the source code:

Function TChartSeries.AddNull(Const ALabel:String):Longint;
begin
result:=Add( 0, ALabel, clNone );
end;
Function TChartSeries.AddNullXY(Const X,Y:Double; Const ALabel:String):Longint;
begin
result:=AddXY(X,Y,ALabel,clNone );
end;

The solution:

a) if you know the range in advance, use the AddNullXY(x,y,'') to add Null points, where y is in [min,max] interval.

b) if you don't know the y-range in advance, utilize the Series OnAfterAdd event to "shift" null values up or down, depending if they are min or max values (see the code bellow):

DELPHI CODE : 

procedure TForm1.Series1AfterAdd(Sender: TChartSeries;
ValueIndex: Integer);
var i:integer;
begin
for i:=Sender.FirstValueIndex to Sender.LastValueIndex do
 if Sender.IsNull(ValueIndex) then
   if Sender.YValue[ValueIndex]=Sender.YValues.MinValue then
    Sender.YValue[ValueIndex]:=Sender.YValues.MaxValue
  else
  if Sender.YValue[ValueIndex]=Sender.YValues.MaxValue then
   Sender.YValue[ValueIndex]:=Sender.YValues.MinValue
end;

CBUILDER CODE : 

void __fastcall TForm1::Series1AfterAdd(TChartSeries *Sender,
      int ValueIndex)
{
   for (int i=Sender->FirstValueIndex;i<=Sender->LastValueIndex;i++)
   {
    if (Sender->IsNull(ValueIndex))
     if (Sender->YValues->Value[ValueIndex]==Sender->YValues->MinValue)
       Sender->YValues->Value[ValueIndex]=Sender->YValues->MaxValue;
     else
        if (Sender->YValues->Value[ValueIndex]==Sender->YValues->MaxValue)
          Sender->YValues->Value[ValueIndex]=Sender->YValues->MinValue;
   }
}