|
About x-axis negative coordinate setting problem |
Posted by lanis on Oct-21-2021 15:29 |
|
hello
I have an MFC program that uses ChartDirector, it can’t display negative X axis, how to setting the chart?
void CViewportcontroldemoDlg::loadData()
{
double* x_axis = new double[100];
memset(x_axis, 0, 100 * sizeof(double));
double* y_axis = new double[100];
memset(y_axis, 0, 100 * sizeof(double));
int u = 0;
for (int i = 0; i < 100; i++) // set range -50~+50
{
u = i - 50;
x_axis[i] = u;
}
m_timeStamps.len = 100;
m_dataSeriesA.len = 100;
m_timeStamps.data = x_axis;
m_dataSeriesA.data = y_axis;
}
void CViewportcontroldemoDlg::drawChart(CChartViewer *viewer)
{
double viewPortStartDate = viewer->getValueAtViewPort("x", viewer->getViewPortLeft());
double viewPortEndDate = viewer->getValueAtViewPort("x", viewer->getViewPortLeft() +
viewer->getViewPortWidth());
int startIndex = (int)floor(Chart::bSearch(m_timeStamps, viewPortStartDate));
int endIndex = (int)ceil(Chart::bSearch(m_timeStamps, viewPortEndDate));
int noOfPoints = endIndex - startIndex + 1;
DoubleArray viewPortTimeStamps = DoubleArray(m_timeStamps.data + startIndex, noOfPoints);
DoubleArray viewPortDataSeriesA = DoubleArray(m_dataSeriesA.data + startIndex, noOfPoints);
XYChart *c = new XYChart(640, 400);
c->setPlotArea(55, 55, c->getWidth() - 80, c->getHeight() - 92, c->linearGradientColor(0, 55, 0,
c->getHeight() - 35, 0xf0f6ff, 0xa0c0ff), -1, Chart::Transparent, 0xffffff, 0xffffff);
c->setClipping();
c->getLegend()->setLineStyleKey();
c->getLegend()->setFontSize(10);
c->xAxis()->setLinearScale(-50,50,10,0); //Q_Q
c->xAxis()->setColors(Chart::Transparent);
c->yAxis()->setColors(Chart::Transparent);
LineLayer *layer = c->addLineLayer();
layer->setLineWidth(2);
layer->setFastLineMode();
layer->setXData(-50,50);
layer->addDataSet(viewPortDataSeriesA, 0xff3333, "Alpha");
viewer->syncDateAxisWithViewPort("x", c->xAxis());
if (!viewer->isInMouseMoveEvent())
{
trackLineLegend(c, (0 == viewer->getChart()) ? c->getPlotArea()->getRightX() :
viewer->getPlotAreaMouseX());
}
delete viewer->getChart();
viewer->setChart(c);
}
|
Re: About x-axis negative coordinate setting problem |
Posted by Peter Kwan on Oct-21-2021 22:47 |
|
Hi Ianis,
Your chart is zoomable and scrollable, so the visible x-axis scale and labels are controlled by the user. (The user can zoom in and out to change the scale.) Your code can only control the "full range" of the axis.
In your code, the line:
viewer->syncDateAxisWithViewPort("x", c->xAxis());
means to set the x-axis scale according to zooming and scrolling, with the "x" full range.
The "x" full range can be configured with CChartViewer::setFullRange. In our sample code, there is usually a "initChartViewer" method that configures the "x" full range.
For your case, instead of
c->xAxis()->setLinearScale(-50,50,10,0); //Q_Q
please use:
viewer->setFullRange("x", -50, 50);
Also, as your x-axis is not a date/time axis, instead of:
viewer->syncDateAxisWithViewPort("x", c->xAxis());
please use:
viewer->syncLinearAxisWithViewPort("x", c->xAxis());
Hope this can help.
Regards
Peter Kwan |
|