[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppVclTChart

(C++ VCL) TChart

Visual Component for visualizing data series.

Can be forward declarated with:
namespace Chart { class TChart; };


In the implementation file, #include chart.hpp:
  1. include <Chart.hpp>


The TChart Component cannot plot a surface plot. You can use Bilderbikkel's SurfacePlotter class for this.

Note that the CLX library does NOT contain an equivalent of this Component.

How-to's overview:
  • How to: read the values from a TChart?
  • How to: calculate a best-fitting line of the values in a TChart?
  • How to: draw a best fitting line of a ChartSeries?

How to: read the values from a TChart?

  1. include <assert>
//Fill the Chart with values for (double x = 0.0; x < 10.0; x+=0.1234) { const double y = x * x; Chart1->Series[0]->AddXY(x,y); } //Copy the pointers to the values TChartValueList * xValues(Chart1->Series[0]->XValues); TChartValueList * yValues(Chart1->Series[0]->YValues); //Assume there are as many X's as Y's assert(xValues->Count()==yValues->Count()); //Loop through them and put them in a TRichEdit const int count = xValues->Count(); for (int i=0; i!=count; ++i) { String myString = FloatToStr(xValues->operator [](i)) + " " + FloatToStr(yValues->operator [](i)); RichEdit1->Lines->Add(myString); }


How to: calculate a best-fitting line of the values in a TChart ?

void getTrendLine(const TChartSeries * series, double& offset, double& slope)
{
  const int nValues = (const_cast<TChartSeries*>(series))->Count(); //VCL is not const-correct
  //Calculate meanX and meanY
  double sumX = 0.0;
  double sumY = 0.0;
  for (int i=0; i!=nValues; ++i)
  {
    sumX += series->XValues->operator [](i);
    sumY += series->YValues->operator [](i);
  }
  const double meanX = sumX / static_cast<double>(nValues);
  const double meanY = sumY / static_cast<double>(nValues);
  double sumBup = 0.0;
  double sumBdown = 0.0;
  for (int i=0; i<nValues; ++i)
  {
    sumBup += ((series->XValues->operator [](i) - meanX)
             * (series->YValues->operator [](i) - meanY));
    sumBdown+= ((series->XValues->operator [](i) - meanX)
             *  (series->XValues->operator [](i) - meanX));
  }
  slope = sumBup/sumBdown;
  offset = meanY - (slope*meanX);
}


How to: draw a best fitting line of a ChartSeries?

Using the function getTrendLine above:

void drawTrendLine(TChart * chart,const int& seriesFrom,const int& seriesTo)
{
  double offset, slope;
  const double minX = chart->Series[seriesFrom]->XValues->MinValue;
  const double maxX = chart->Series[seriesFrom]->XValues->MaxValue;
  if (minX!=maxX)
  {
    getTrendLine(chart->Series[seriesFrom],offset,slope);
    chart->Series[seriesTo]->AddXY(minX, offset + (minX * slope));
    chart->Series[seriesTo]->AddXY(maxX, offset + (maxX * slope));
  }
}


Code links

TChart links

TChart is not present in the CLX
  • Pascal VCL TChart


last edited (November 18, 2006) by bilderbikkel, Number of views: 2595, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.