I am trying to use a library called QCustomPlot and on the X Axis I want to be able to put the users time zone of the stock market being open, from say 9:30AM to 4:00PM but the paramenters i need when setting the range of the x axis has to be a double, what is the best way about getting the time to a format I can use? Thank you in advance!
Extra: Also is there any easy way to make it where the range of the y axis will go up and down depending on where the current price of the underlying stock is?
Code:
string symbol_std = symbol.toStdString();
//Retrieves json format of data
Json::Value chartData = IEX::stocks::chartYtd(symbol_std);
//Size of json
int n = chartData.size();
//Stores x and y values
QVector<double> time(n), average(n);
QDateTime start = QDateTime(QDate(2020, 1, 1));
start.setTimeSpec(Qt::UTC);
double startTime = start.toTime_t();
double binSize = 3600*24;
time[0] = startTime;
//Reads in data from json(historical data 1 day delayed)
for(int i = 0; i < chartData.size(); i++)
{
time[i + 1] = startTime + 3600*(i+1);
average[i] = (chartData[i]["close"].asDouble());
if((average[i] == 0) && (time[i] != chartData.size() - 1))
{
average[i] = average[i-1];
}
}
//Initializes graph
ui->stockGraph->addGraph();
ui->stockGraph->graph(0)->setData(time, average);
// give the axes some labels:
ui->stockGraph->xAxis->setLabel("Time");
ui->stockGraph->yAxis->setLabel("Price");
// set axes ranges, so we see all data:
ui->stockGraph->xAxis->setRange(time[0], time[n]);
ui->stockGraph->yAxis->setRange(210, 340);
ui->stockGraph->replot();
Visual of Graph
what is the best way about getting the time to a format I can use?
You need a QwtDateScaleEngine along with a QwtDateScaleDraw. These will interpret your double
values as the time from the epoch "1970-01-01T00:00:00 UTC" in milliseconds.
ui->stockGraph->setAxisScaleDraw(QwtPlot::xBottom, new QwtDateScaleDraw());
ui->stockGraph->setAxisScaleEngine(QwtPlot::xBottom, new QwtDateScaleEngine());