aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick
diff options
context:
space:
mode:
authorVenugopal Shivashankar <Venugopal.Shivashankar@qt.io>2017-09-06 16:04:39 +0200
committerVenugopal Shivashankar <Venugopal.Shivashankar@qt.io>2017-09-12 09:57:54 +0000
commit1f77d1b36d3b28abcb53fc7df400bd095ea14c9d (patch)
treee607eb7693a761fde6904cc4b9dfee0af3750340 /examples/quick
parent6fef83c37b2e4c1369a1f5cf7c68c54c9c5a27e9 (diff)
Example: Switch to google finance URL
The yahoo finance URL that was used earlier is not available anymore Task-number: QTBUG-60630 Change-Id: Id3302a60c0f39c34ab0053806c95c28c6aec5b91 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'examples/quick')
-rw-r--r--examples/quick/demos/stocqt/content/StockChart.qml3
-rw-r--r--examples/quick/demos/stocqt/content/StockListModel.qml30
-rw-r--r--examples/quick/demos/stocqt/content/StockModel.qml51
-rw-r--r--examples/quick/demos/stocqt/content/stocqt.js72
-rw-r--r--examples/quick/demos/stocqt/stocqt.qrc1
5 files changed, 94 insertions, 63 deletions
diff --git a/examples/quick/demos/stocqt/content/StockChart.qml b/examples/quick/demos/stocqt/content/StockChart.qml
index e90aba3aef..ec65f1501a 100644
--- a/examples/quick/demos/stocqt/content/StockChart.qml
+++ b/examples/quick/demos/stocqt/content/StockChart.qml
@@ -86,7 +86,7 @@ Rectangle {
gridSize = 3;
}
else {
- chart.startDate = new Date(2005, 3, 25);
+ chart.startDate = new Date(2011, 4, 25);
gridSize = 4;
}
@@ -345,7 +345,6 @@ Rectangle {
onPaint: {
numPoints = stockModel.indexOf(chart.startDate);
-
if (chart.gridSize == 0)
chart.gridSize = numPoints
diff --git a/examples/quick/demos/stocqt/content/StockListModel.qml b/examples/quick/demos/stocqt/content/StockListModel.qml
index 02ece32a49..f9bb8755b3 100644
--- a/examples/quick/demos/stocqt/content/StockListModel.qml
+++ b/examples/quick/demos/stocqt/content/StockListModel.qml
@@ -37,9 +37,8 @@
** $QT_END_LICENSE$
**
****************************************************************************/
-
import QtQuick 2.0
-
+import "stocqt.js" as JSLibrary
ListModel {
id: stocks
@@ -50,28 +49,13 @@ ListModel {
}
}
- function requestUrl(stockId) {
- var endDate = new Date(""); // today
- var startDate = new Date()
- startDate.setDate(startDate.getDate() - 5);
-
- var request = "http://ichart.finance.yahoo.com/table.csv?";
- request += "s=" + stockId;
- request += "&g=d";
- request += "&a=" + startDate.getMonth();
- request += "&b=" + startDate.getDate();
- request += "&c=" + startDate.getFullYear();
- request += "&d=" + endDate.getMonth();
- request += "&e=" + endDate.getDate();
- request += "&f=" + endDate.getFullYear();
- request += "&g=d";
- request += "&ignore=.csv";
- return request;
- }
-
function getCloseValue(index) {
- var req = requestUrl(get(index).stockId);
+ var endDate = new Date(); // today
+ var startDate = new Date();
+ startDate.setDate(endDate.getDate() - 7);
+
+ var req = JSLibrary.requestUrl(get(index).stockId, startDate, endDate);
if (!req)
return;
@@ -87,12 +71,14 @@ ListModel {
if (records.length > 0 && xhr.status == 200) {
var r = records[1].split(',');
var today = parseFloat(r[4]);
+
if (!isNaN(today))
setProperty(index, "value", today.toFixed(2));
if (records.length > 2) {
r = records[2].split(',');
var yesterday = parseFloat(r[4]);
var change = today - yesterday;
+
if (change >= 0.0)
setProperty(index, "change", "+" + change.toFixed(2));
else
diff --git a/examples/quick/demos/stocqt/content/StockModel.qml b/examples/quick/demos/stocqt/content/StockModel.qml
index 035d9454d0..5807e34c81 100644
--- a/examples/quick/demos/stocqt/content/StockModel.qml
+++ b/examples/quick/demos/stocqt/content/StockModel.qml
@@ -39,12 +39,12 @@
****************************************************************************/
import QtQuick 2.0
+import "stocqt.js" as JSLibrary
ListModel {
id: model
property string stockId: ""
property string stockName: ""
- property string stockDataCycle: "d"
property bool ready: false
property real stockPrice: 0.0
property real stockPriceChanged: 0.0
@@ -57,6 +57,7 @@ ListModel {
var newest = new Date(model.get(0).date);
var oldest = new Date(model.get(model.count - 1).date);
+
if (newest <= date)
return -1;
@@ -71,7 +72,7 @@ ListModel {
currDiff = Math.abs(d.getTime() - date.getTime());
if (currDiff < bestDiff) {
bestDiff = currDiff;
- retval = i;
+ retval = i + 1;
}
if (currDiff > bestDiff)
return retval;
@@ -80,52 +81,25 @@ ListModel {
return -1;
}
- function requestUrl() {
- if (stockId === "")
- return;
-
- var startDate = new Date(2011, 4, 25);
-
- var endDate = new Date(); //today
-
- if (stockDataCycle !== "d" && stockDataCycle !== "w" && stockDataCycle !== "m")
- stockDataCycle = "d";
-
- /*
- Fetch stock data from yahoo finance:
- url: http://ichart.finance.yahoo.com/table.csv?s=NOK&a=5&b=11&c=2010&d=7&e=23&f=2010&g=d&ignore=.csv
- s:stock name/id, a:start day, b:start month, c:start year default: 25 April 1995, oldest c= 1962
- d:end day, e:end month, f:end year, default:today (data only available 3 days before today)
- g:data cycle(d daily, w weekly, m monthly, v Dividend)
- */
- var request = "http://ichart.finance.yahoo.com/table.csv?";
- request += "s=" + stockId;
- request += "&a=" + startDate.getMonth();
- request += "&b=" + startDate.getDate();
- request += "&c=" + startDate.getFullYear();
- request += "&d=" + endDate.getMonth();
- request += "&e=" + endDate.getDate();
- request += "&f=" + endDate.getFullYear();
- request += "&g=" + stockDataCycle;
- request += "&ignore=.csv";
- return request;
- }
-
function createStockPrice(r) {
return {
- "date": r[0],
+ "date": JSLibrary.parseDate(r[0]),
"open":r[1],
"high":r[2],
"low":r[3],
"close":r[4],
"volume":r[5],
- "adjusted":r[6]
};
}
function updateStock() {
- var req = requestUrl();
+ if (stockId === "")
+ return;
+ var startDate = new Date(2011, 4, 25);
+ var endDate = new Date(); //today
+
+ var req = JSLibrary.requestUrl(stockId, startDate, endDate);
if (!req)
return;
@@ -139,17 +113,16 @@ ListModel {
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.LOADING || xhr.readyState === XMLHttpRequest.DONE) {
var records = xhr.responseText.split('\n');
-
for (;i < records.length; i++ ) {
var r = records[i].split(',');
- if (r.length === 7)
+ if (r.length === 6)
model.append(createStockPrice(r));
}
if (xhr.readyState === XMLHttpRequest.DONE) {
if (model.count > 0) {
model.ready = true;
- model.stockPrice = model.get(0).adjusted;
+ model.stockPrice = model.get(0).close;
model.stockPriceChanged = model.count > 1 ? (Math.round((model.stockPrice - model.get(1).close) * 100) / 100) : 0;
} else {
model.stockPrice = 0;
diff --git a/examples/quick/demos/stocqt/content/stocqt.js b/examples/quick/demos/stocqt/content/stocqt.js
new file mode 100644
index 0000000000..cc8ad7a9ef
--- /dev/null
+++ b/examples/quick/demos/stocqt/content/stocqt.js
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+.pragma library
+
+function requestUrl(stockId, startDate, endDate) {
+ var request = ("http://www.google.com/finance/historical?"
+ + "q=" + stockId
+ + "&startdate=" + encodeURIComponent(startDate.toLocaleDateString(Qt.locale("C"),"MMM dd yyyy"))
+ + "&enddate=" + encodeURIComponent(endDate.toLocaleDateString(Qt.locale("C"),"MMM dd yyyy"))
+ + "&output=csv");
+ return request;
+}
+
+function parseDate(date) {
+ // Map Google Finance's date format to one that Date can parse.
+ var parts = date.split("-");
+ var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ if (parts[2].length == 2)
+ parts[2] = "20" + parts[2];
+
+ if (typeof parts[1] == "string") {
+ var monthIdx = months.indexOf(parts[1]) + 1;
+ if (monthIdx)
+ parts[1] = (monthIdx <= 9 ? "0" : "") + monthIdx;
+ }
+
+ if (parseInt(parts[0]) < 10)
+ parts[0] = "0" + parts[0];
+
+ var dateString = parts[2] + "-" + parts[1] + "-" + parts[0];
+ return dateString;
+}
diff --git a/examples/quick/demos/stocqt/stocqt.qrc b/examples/quick/demos/stocqt/stocqt.qrc
index ab7772a62a..629c4025b4 100644
--- a/examples/quick/demos/stocqt/stocqt.qrc
+++ b/examples/quick/demos/stocqt/stocqt.qrc
@@ -18,5 +18,6 @@
<file>content/+windows/Settings.qml</file>
<file>content/StockListDelegate.qml</file>
<file>content/Banner.qml</file>
+ <file>content/stocqt.js</file>
</qresource>
</RCC>