Reputation: 57
hope you can help me. In Builder C++ create a new "Windows VCL Application". Add a "Tchart" from "Palette". Right click on the chart -> Edit Chart -> Click on Series -> Add... -> Line -> Ok to create Series1 and repeat to create Series2. Close In Unit1.cpp copy my following sample:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <vector>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
std::vector<float> x1, y1, x2, y2;
for (int i = 0; i < 2000; i++) {
float r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
float r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
x1.push_back(i+r1);
y1.push_back(r1);
x2.push_back(i+r2);
y2.push_back(r2);
}
Chart1->BottomAxis->AxisValuesFormat = "0.0";
for (unsigned i = 0; i < x1.size(); i++) {
Chart1->Series[0]->AddY(y1[i], x1[i]);
Chart1->Series[1]->AddY(y2[i], x2[i]);
}
}
//---------------------------------------------------------------------------
Compile and run. As you can see the values on the X axis appear overlapped.
In order to solve I would
For the 1st point I've tried as you see in the code but it seems not working. Thank you very much in advance.
Upvotes: 1
Views: 209
Reputation: 5039
You are adding points to the Line series with the AddY
method, passing the x values as the second parameter, which is the label.
Doing so, the bottom axis labels indeed overlap.
Instead, I'd use the AddXY
method, passing the x values as the first parameter and the y values as the second:
for (unsigned i = 0; i < x1.size(); i++) {
Chart1->Series[0]->AddXY(x1[i], y1[i]);
Chart1->Series[1]->AddXY(x2[i], y2[i]);
}
This looks fine for me here:
Upvotes: 2