|
Zoom with FinanceChart issuse |
Posted by hondodo on Jul-05-2016 10:49 |
|
Hi, now i have the problem that when i zoom and drag the finace chart the shown dots count are not fixed, it will be less or more, cause my datas are not a serial times. for example: start with 20 datas in 2016-7-2 and next 95 datas in 2016-7-4.
how can i zoom and drag with fixed dot no mater what the data is.
void onMouseWheelChart(QWheelEvent *event)
{
if (!m_ChartViewer->isMouseOnPlotArea())
{
event->ignore();
return;
}
double newVpWidth = m_ChartViewer->getViewPortWidth() * (event->delta() > 0 ? 0.9 : 1 / 0.9);
double newVpHeight = m_ChartViewer->getViewPortHeight() * (event->delta() > 0 ? 0.9 : 1 / 0.9);
newVpWidth = max(m_ChartViewer->getZoomInWidthLimit(), min(newVpWidth,
m_ChartViewer->getZoomOutWidthLimit()));
newVpHeight = max(m_ChartViewer->getZoomInHeightLimit(), min(newVpWidth,
m_ChartViewer->getZoomOutHeightLimit()));
if ((newVpWidth != m_ChartViewer->getViewPortWidth()) ||
(newVpHeight != m_ChartViewer->getViewPortHeight()))
{
double deltaX = (m_ChartViewer->getPlotAreaMouseX() - m_ChartViewer->getPlotAreaLeft()) *
(m_ChartViewer->getViewPortWidth() - newVpWidth) / m_ChartViewer->getPlotAreaWidth();
m_ChartViewer->setViewPortLeft(m_ChartViewer->getViewPortLeft() + deltaX);
m_ChartViewer->setViewPortWidth(newVpWidth);
double deltaY = (m_ChartViewer->getPlotAreaMouseY() - m_ChartViewer->getPlotAreaTop()) *
(m_ChartViewer->getViewPortHeight() - newVpHeight) / m_ChartViewer->getPlotAreaHeight();
m_ChartViewer->setViewPortTop(m_ChartViewer->getViewPortTop() + deltaY);
m_ChartViewer->setViewPortHeight(newVpHeight);
m_ChartViewer->updateViewPort(true, false);
}
}
void drawChart()
{
int extraPoints = 10;
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) floor(Chart::bSearch(m_timeStamps, viewPortEndDate));
if(startIndex < extraPoints)
{
startIndex = extraPoints;
}
int len = endIndex - startIndex + 1;
if(len <= 1)
{
len = m_timeStamps.len;
startIndex = 0;
}
DoubleArray timeStamps = DoubleArray(m_timeStamps.data + startIndex - extraPoints, len + extraPoints);
DoubleArray volData = DoubleArray(m_values.data + startIndex - extraPoints, len+ extraPoints);
DoubleArray low = DoubleArray(m_low.data + startIndex - extraPoints, len+ extraPoints);
DoubleArray high = DoubleArray(m_high.data + startIndex - extraPoints, len+ extraPoints);
DoubleArray open = DoubleArray(m_opens.data + startIndex - extraPoints, len+ extraPoints);
DoubleArray close = DoubleArray(m_close.data + startIndex - extraPoints, len+ extraPoints);
if(timeStamps.len <= 0) return;
}
|
Re: Zoom with FinanceChart issuse |
Posted by Peter Kwan on Jul-05-2016 20:29 |
|
Hi hondodo,
The problem is because in a standard financial chart, the x-axis represents trading sessions, not date/time. The date/time labels on the x-axis are just names for human reading and have no positional meaning. For example, the candlesticks always equally spaced, even though the date/time are irregular and somewhat random.
If you use date/time as the x-coordinates, then when you scroll the chart, the x-axis will contain the same duration in date/time, which is not related to the number of candlesticks.
Instead, you may simply use the array index as the x-coordinates. In this case, the full x-range of the chart are just the full range of the array index:
viewer->setFullRange("x", 0, m_TimeStamps.len - 1);
Then in your charting code, the indexes are:
// start and end indexes
int startIndex = (int)floor(viewer->getValueAtViewPort("x", viewer->getViewPortLeft()));
int endIndex = (int)ceil(viewer->getValueAtViewPort("x", viewer->getViewPortLeft() + viewer->getViewPortWidth());
// adjust for extra points
int extraPoints = std::min(10, startIndex);
staartIndex -= extraPoints;
int noOfPoints = endIndex - startIndex + 1;
// get the data
DoubleArray timeStamps = DoubleArray(m_timeStamps.data + startIndex, noOfPoints);
DoubleArray volData = DoubleArray(m_values.data + startIndex, noOfPoints);
........
Hope this can help.
Regards
Peter Kwan |
Re: Zoom with FinanceChart issuse |
Posted by hondodo on Jul-06-2016 14:32 |
|
Hi, Peter Kwan
Thank you very much. It's what I want that replace with your code, and I don't have to cut my datas. Now it works well. Best with. |
|