import QtQuick 2.0
import QtQuick.Layouts 1.1
import "."
Rectangle {
id: chart
property var stockModel: null
property var startDate: new Date()
property var endDate: new Date()
property string activeChart: "week"
property var settings
property int gridSize: 4
property real gridStep: gridSize ? (canvas.width - canvas.tickMargin) / gridSize : canvas.xGridStep
function update() {
endDate = new Date(stockModel.newest);
if (chart.activeChart === "month") {
chart.startDate = new Date(stockModel.newest.getFullYear(),
stockModel.newest.getMonth() - 1,
stockModel.newest.getDate());
gridSize = 4;
}
else if (chart.activeChart === "quarter") {
chart.startDate = new Date(stockModel.newest.getFullYear(),
stockModel.newest.getMonth() - 3,
stockModel.newest.getDate());
gridSize = 3;
}
else if (chart.activeChart === "halfyear") {
chart.startDate = new Date(stockModel.newest.getFullYear(),
stockModel.newest.getMonth() - 6,
stockModel.newest.getDate());
gridSize = 6;
}
else {
chart.startDate = new Date(stockModel.newest.getFullYear(),
stockModel.newest.getMonth(),
stockModel.newest.getDate() - 7);
gridSize = 0;
}
canvas.requestPaint();
}
GridLayout {
anchors.fill: parent
columns: 6
rows: 3
columnSpacing: 4
Button {
id: weekButton
text: "Week"
buttonEnabled: chart.activeChart === "week"
onClicked: {
chart.activeChart = "week";
chart.update();
}
}
Button {
id: monthButton
text: "Month"
buttonEnabled: chart.activeChart === "month"
onClicked: {
chart.activeChart = "month";
chart.update();
}
}
Button {
id: quarterlyButton
text: "3 Months"
buttonEnabled: chart.activeChart === "quarter"
onClicked: {
chart.activeChart = "quarter";
chart.update();
}
}
Button {
id: halfYearlyButton
text: "6 Months"
buttonEnabled: chart.activeChart === "halfyear"
onClicked: {
chart.activeChart = "halfyear";
chart.update();
}
}
Canvas {
id: canvas
Layout.fillWidth: true
Layout.fillHeight: true
Layout.columnSpan: 6
property int pixelSkip: 1
property int numPoints: 1
property int tickMargin: 34
property real xGridStep: (canvas.width - tickMargin) / numPoints
property real yGridOffset: canvas.height / 26
property real yGridStep: canvas.height / 12
function drawBackground(ctx) {
ctx.save();
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#d7d7d7";
ctx.beginPath();
for (var i = 0; i < 12; i++) {
ctx.moveTo(0, canvas.yGridOffset + i * canvas.yGridStep);
ctx.lineTo(canvas.width, canvas.yGridOffset + i * canvas.yGridStep);
}
var height = 35 * canvas.height / 36;
var yOffset = canvas.height - height;
var xOffset = 0;
for (i = 0; i < chart.gridSize; i++) {
ctx.moveTo(xOffset + i * chart.gridStep, yOffset);
ctx.lineTo(xOffset + i * chart.gridStep, height);
}
ctx.stroke();
ctx.strokeStyle = "#666666";
ctx.beginPath();
var xStart = canvas.width - tickMargin;
ctx.moveTo(xStart, 0);
ctx.lineTo(xStart, canvas.height);
for (i = 0; i < 12; i++) {
ctx.moveTo(xStart, canvas.yGridOffset + i * canvas.yGridStep);
ctx.lineTo(canvas.width, canvas.yGridOffset + i * canvas.yGridStep);
}
ctx.moveTo(0, canvas.yGridOffset + 9 * canvas.yGridStep);
ctx.lineTo(canvas.width, canvas.yGridOffset + 9 * canvas.yGridStep);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
function volumeToString(value) {
if (value < 1000)
return value;
var exponent = parseInt(Math.log(value) / Math.log(1000));
var shortVal = parseFloat(parseFloat(value) / Math.pow(1000, exponent)).toFixed(1);
if (shortVal >= 100.0) {
shortVal = parseFloat(shortVal).toFixed(0);
}
return shortVal + "KMBTG".charAt(exponent - 1);
}
function drawScales(ctx, high, low, vol)
{
ctx.save();
ctx.strokeStyle = "#888888";
ctx.font = "10px Open Sans"
ctx.beginPath();
var x = canvas.width - tickMargin + 3;
var priceStep = (high - low) / 9.0;
for (var i = 0; i < 10; i += 2) {
var price = parseFloat(high - i * priceStep).toFixed(1);
ctx.text(price, x, canvas.yGridOffset + i * yGridStep - 2);
}
for (i = 0; i < 3; i++) {
var volume = volumeToString(vol - (i * (vol/3)));
ctx.text(volume, x, canvas.yGridOffset + (i + 9) * yGridStep + 10);
}
ctx.closePath();
ctx.stroke();
ctx.restore();
}
function drawPrice(ctx, from, to, color, price, points, highest, lowest)
{
ctx.save();
ctx.globalAlpha = 0.7;
ctx.strokeStyle = color;
ctx.lineWidth = numPoints > 200 ? 1 : 3
ctx.beginPath();
var end = points.length;
var range = highest - lowest;
if (range == 0) {
range = 1;
}
for (var i = 0; i < end; i += pixelSkip) {
var x = points[i].x;
var y = points[i][price];
var h = 9 * yGridStep;
y = h * (lowest - y)/range + h + yGridOffset;
if (i == 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.stroke();
ctx.restore();
}
function drawVolume(ctx, from, to, color, price, points, highest)
{
ctx.save();
ctx.fillStyle = color;
ctx.globalAlpha = 0.8;
ctx.lineWidth = 0;
ctx.beginPath();
var end = points.length;
var margin = 0;
if (chart.activeChart === "month" || chart.activeChart === "week") {
margin = 8;
ctx.shadowOffsetX = 4;
ctx.shadowBlur = 3.5;
ctx.shadowColor = Qt.darker(color);
}
for (var i = 1; i < end; i += pixelSkip) {
var x = points[i - 1].x;
var y = points[i][price];
y = canvas.height * (y / highest);
y = 3 * y / 12;
ctx.fillRect(x, canvas.height - y + yGridOffset,
canvas.xGridStep - margin, y);
}
ctx.stroke();
ctx.restore();
}
function drawError(ctx, msg)
{
ctx.save();
ctx.strokeStyle = "#888888";
ctx.font = "24px Open Sans"
ctx.textAlign = "center"
ctx.shadowOffsetX = 4;
ctx.shadowOffsetY = 4;
ctx.shadowBlur = 1.5;
ctx.shadowColor = "#aaaaaa";
ctx.beginPath();
ctx.fillText(msg, (canvas.width - tickMargin) / 2,
(canvas.height - yGridOffset - yGridStep) / 2);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
onPaint: {
numPoints = stockModel.indexOf(chart.startDate);
if (chart.gridSize == 0)
chart.gridSize = numPoints
var ctx = canvas.getContext("2d");
ctx.globalCompositeOperation = "source-over";
ctx.lineWidth = 1;
drawBackground(ctx);
if (!stockModel.ready) {
drawError(ctx, "No data available.");
return;
}
var highestPrice = 0;
var highestVolume = 0;
var lowestPrice = -1;
var points = [];
for (var i = numPoints, j = 0; i >= 0 ; i -= pixelSkip, j += pixelSkip) {
var price = stockModel.get(i);
if (parseFloat(highestPrice) < parseFloat(price.high))
highestPrice = price.high;
if (parseInt(highestVolume, 10) < parseInt(price.volume, 10))
highestVolume = price.volume;
if (lowestPrice < 0 || parseFloat(lowestPrice) > parseFloat(price.low))
lowestPrice = price.low;
points.push({
x: j * xGridStep,
open: price.open,
close: price.close,
high: price.high,
low: price.low,
volume: price.volume
});
}
if (settings.drawHighPrice)
drawPrice(ctx, 0, numPoints, settings.highColor, "high", points, highestPrice, lowestPrice);
if (settings.drawLowPrice)
drawPrice(ctx, 0, numPoints, settings.lowColor, "low", points, highestPrice, lowestPrice);
if (settings.drawOpenPrice)
drawPrice(ctx, 0, numPoints,settings.openColor, "open", points, highestPrice, lowestPrice);
if (settings.drawClosePrice)
drawPrice(ctx, 0, numPoints, settings.closeColor, "close", points, highestPrice, lowestPrice);
drawVolume(ctx, 0, numPoints, settings.volumeColor, "volume", points, highestVolume);
drawScales(ctx, highestPrice, lowestPrice, highestVolume);
}
}
Text {
id: fromDate
color: "#000000"
font.family: Settings.fontFamily
font.pointSize: 8
Layout.alignment: Qt.AlignLeft
text: "| " + startDate.toDateString()
}
Text {
id: toDate
color: "#000000"
font.family: Settings.fontFamily
font.pointSize: 8
Layout.alignment: Qt.AlignRight
Layout.rightMargin: canvas.tickMargin
Layout.columnSpan: 5
text: endDate.toDateString() + " |"
}
}
}