initial diagonal work (unfinished)

This commit is contained in:
Tim
2024-04-30 21:30:16 -04:00
parent e8dafc0732
commit 056a4e299d
11 changed files with 357 additions and 165 deletions

View File

@@ -1,9 +1,9 @@
// noinspection JSPotentiallyInvalidUsageOfThis
import {invokeCallback, mixin} from "@/common.js";
import {chart, createShape, deleteShapeId, dragging, draggingShapeIds, drawShape, widget} from "@/charts/chart.js";
import {unique} from "@/misc.js";
import {allocationText, useChartOrderStore} from "@/orderbuild.js";
import {chart, createShape, deleteShapeId, draggingShapeIds, drawShape, widget} from "@/charts/chart.js";
import {pointsToOhlcStart, unique} from "@/misc.js";
import {allocationText} from "@/orderbuild.js";
import Color from "color";
@@ -203,11 +203,11 @@ export class Shape {
// createShape(this.type, this.points, {overrides:this.props}, new ShapeTVCallbacks(this))
options = {...options}
options['overrides'] = props
this.tvPoints = [...points]
this.tvPoints = pointsToOhlcStart(points)
this.tvCallbacks = new ShapeTVCallbacks(this);
const id = createShape(this.type, points, options, this.tvCallbacks)
const id = createShape(this.type, this.tvPoints, options, this.tvCallbacks)
// todo set id?
if (this.debug) console.log('created', this.type.name, points, id)
if (this.debug) console.log('created', this.type.name, this.tvPoints, id)
}
onCreate(points, props) {
@@ -241,23 +241,25 @@ export class Shape {
// setting points to null will delete the shape from the chart. setting points to a valid value will cause the
// shape to be drawn.
if (this.debug) console.log('setPoints', this.id, this.ourPoints, points)
const oldPoints = this.ourPoints
this.ourPoints = points
if (points === null || !points.length)
this.delete()
else {
if (this.id === null)
this.create()
else if (dirtyPoints(this.tvPoints, points)) {
const s = this.tvShape();
const lbe = s._model._lineBeingEdited
const lpbc = s._model._linePointBeingChanged
const lpbe = s._model._linePointBeingEdited
if (lpbc!==null)
s._model.endChangingLinetool(lbe, lpbc)
s.setPoints(points)
if (lpbc!==null)
s._model.startChangingLinetool(lbe, lpbc, lpbe)
else {
points = pointsToOhlcStart(points)
if (dirtyPoints(this.tvPoints, points)) {
const s = this.tvShape();
const lbe = s._model._lineBeingEdited
const lpbc = s._model._linePointBeingChanged
const lpbe = s._model._linePointBeingEdited
if (lpbc!==null)
s._model.endChangingLinetool(lbe, lpbc)
s.setPoints(points)
if (lpbc!==null)
s._model.startChangingLinetool(lbe, lpbc, lpbe)
}
}
}
}
@@ -376,15 +378,15 @@ class ShapeTVCallbacks {
onPoints(shapeId, _tvShape, points) {
if (!this.enabled) return // possible when shape is deleted and re-created
if (this.shape.debug) console.log('tvcb onPoints', points)
this.shape.tvPoints = points
this.shape.onPoints(points)
this.shape.tvPoints = points
}
onProps(shapeId, _tvShape, props) {
if (!this.enabled) return // possible when shape is deleted and re-created
if (this.shape.debug) console.log('tvOnProps', props)
this.shape.tvProps = props
this.shape.onProps(props)
this.shape.tvProps = props
}
onDraw() {
@@ -492,12 +494,6 @@ export class HLine extends Line {
}
function nearestOhlcStart(time) {
const period = useChartOrderStore().intervalSecs
return Math.round(time/period) * period
}
export class VLine extends Line {
constructor(model, onModel=null, onDelete=null, props=null) {
super(ShapeType.VLine, onModel, onDelete, props)
@@ -505,23 +501,12 @@ export class VLine extends Line {
// Model
this.model.time = null
// since VLines must be on the start of an OHLC boundry, we track that value separately
this.shapeTime = null
this.setModel(model) // call setModel at the end
}
doCreate(points, props, options = {}) {
this.shapeTime = nearestOhlcStart(points[0].time);
points = [{time: this.shapeTime, price:0}]
if (this.debug) console.log('VLine.doCreate', this.shapeTime, points)
super.doCreate(points, props, options);
}
delete() {
this.model.time = null
this.shapeTime = null
super.delete()
}
@@ -537,26 +522,9 @@ export class VLine extends Line {
}
}
setPoints(points) {
// if (this.debug) console.error('VLine setPoints...', points)
if (points !== null && points.length) {
// adjust shape to ohlc boundry
const time = nearestOhlcStart(points[0].time);
if (time !== this.shapeTime) {
this.shapeTime = time
super.setPoints([{time, price:0}]);
}
}
else {
this.shapeTime = null
super.setPoints(points);
}
// if (this.debug) console.error('VLine setPoints', points)
}
onPoints(points) {
const time = points[0].time;
if ( time !== this.shapeTime ) {
if ( time !== this.tvPoints[0].time ) {
super.onPoints(points);
this.updateModel({time: time})
}
@@ -564,3 +532,50 @@ export class VLine extends Line {
}
export class DLine extends Line {
constructor(model, onModel=null, onDelete=null, props=null) {
super(ShapeType.Line, onModel, onDelete, props)
this.debug = true // todo debug
// Model
this.model.pointA = null // {time:..., price:...}
this.model.pointB = null
// since VLines must be on the start of an OHLC boundry, we track those value separately
this.shapeTimes = null
this.setModel(model) // call setModel at the end
}
delete() {
this.model.pointA = null
this.model.pointB = null
super.delete();
}
setModel(model) {
super.setModel(model)
if (model.pointA === null || model.pointB === null)
this.delete()
else if (dirtyPoints(this.model.pointA, model.pointA) || dirtyPoints(this.model.pointB, model.pointB)) {
this.model.pointA = model.pointA
this.model.pointB = model.pointB
this.setPoints([model.pointA, model.pointB])
}
}
onPoints(points) {
let dirty = this.tvPoints === null
for (let i=0; !dirty && i<points.length; i++) {
if( points[i].time !== this.tvPoints[i].time)
dirty = true
}
if (dirty) {
super.onPoints(points);
this.updateModel({pointA: points[0], pointB: points[0]})
}
}
}