@millistream/millistream-widgets 0.0.14 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/millistream-widgets.js +462 -755
  2. package/package.json +1 -1
@@ -1,3 +1,5 @@
1
+ 'use strict';
2
+
1
3
  function Milli_Chart(settings) {
2
4
  var _this = this;
3
5
  _this.mdf_fields = [];
@@ -5,10 +7,6 @@ function Milli_Chart(settings) {
5
7
  _this.scaleinfoY = {};
6
8
  _this.scaleinfoY2 = {};
7
9
  _this.instruments = [];
8
- var m_modules = {
9
- quantity: null
10
- };
11
-
12
10
  _this.settings = {
13
11
  autodraw: true,
14
12
  instrument: null,
@@ -44,6 +42,16 @@ function Milli_Chart(settings) {
44
42
  yearLabelsPos: 'bottom'
45
43
  };
46
44
  _this.unsubscriptions = {};
45
+ var m_chartspaces = {
46
+ chart: {
47
+ percent: 100,
48
+ height: 0
49
+ },
50
+ lowerChart: {
51
+ percent: 0,
52
+ height: 0
53
+ }
54
+ };
47
55
  var m_dummyDiv = null; // dummy div For chartclasses
48
56
  var m_requestid = 0;
49
57
  var m_canvas = null;
@@ -99,6 +107,7 @@ function Milli_Chart(settings) {
99
107
  div: null,
100
108
  isZooming: false
101
109
  };
110
+ var m_lastDrawnInstrument = -1;
102
111
  var m_months = ['Jan', 'Feb', 'Mar', 'Apr', 'Maj', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'];
103
112
  var m_xLegendCss = {
104
113
  'font-size': '10px',
@@ -204,7 +213,6 @@ function Milli_Chart(settings) {
204
213
  }
205
214
 
206
215
  function findFirstWeekDay(date) {
207
- // console.log('DATE', date);
208
216
  var tmp = new Date(date);
209
217
  var count = 0;
210
218
  while (tmp.getDay() == 0 || tmp.getDay() == 6) {
@@ -394,9 +402,7 @@ function Milli_Chart(settings) {
394
402
  minValue *= 0.9;
395
403
  }
396
404
  var value = (maxValue - minValue) / num_labels;
397
- // console.log(value);
398
405
  var x = numDigits(value);
399
- // console.log(x);
400
406
  value = value / Math.pow(10, x);
401
407
  for (var i = 0; i < 3; i++) {
402
408
  value = getTickNextTickValue(value);
@@ -404,26 +410,16 @@ function Milli_Chart(settings) {
404
410
  if (nt <= num_labels) break;
405
411
  value *= 2;
406
412
  }
407
- /* var prec = (value < 0.000009 ? 7 :
408
- (value < 0.000009 ? 6 :
409
- (value < 0.00009 ? 5 :
410
- (value < 0.0009 ? 4 :
411
- (value < 0.009 ? 3 :
412
- (value < 0.09 ? 2 :
413
- (value < 0.9 ? 1 : 0)))))));
414
- m_yAxisPrecision = m_yAxisPrecision < prec ? prec : m_yAxisPrecision;
415
- */
416
- // console.log('ticksize', value, Math.pow(10, x), x);
417
-
418
413
  return value * Math.pow(10, x);
419
414
  }
420
415
 
421
416
  function calcHighLow() {
422
- var datalen = 0;
423
417
  _this.scaleinfoY.lowValue = null;
424
418
  _this.scaleinfoY.highValue = null;
425
419
  _this.scaleinfoY2.lowValue = null;
426
420
  _this.scaleinfoY2.highValue = null;
421
+ _this.scaleinfoY.lowLowerChart = null;
422
+ _this.scaleinfoY.highLowerChart = null;
427
423
  var firstvalue = null;
428
424
 
429
425
  for (var s = 0; s < _this.instruments.length; s++) {
@@ -432,34 +428,26 @@ function Milli_Chart(settings) {
432
428
  if (_this.scaleinfoY.type != 'history') {
433
429
  if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d' && !m_zoom.mousedown.timestamp) {
434
430
  firstvalue = parseFloat(_this.instruments[s].closeprice1d);
435
- //console.log('closeprice1d', firstvalue);
436
431
  }
437
432
  }
438
- //console.log(_this.scaleinfoY.type, data, firstvalue);
439
433
  for (var i = 0; i < data.length; i++) {
440
434
  // only calc on visible data
441
435
  var price = data[i][_this.settings.graphvalue] * _this.instruments[s].factor;
436
+ var quantity = 0;
437
+ if (data[i].quantity !== 'undefined')
438
+ quantity = data[i].quantity;
442
439
  if (data[i].timestamp < _this.scaleinfoX.startTimeStamp) {
443
- // if (_this.settings.chartlen != '1d' && _this.settings.chartlen != '0d') firstvalue = data[i][_this.settings.graphvalue] * _this.instruments[s].factor;
444
- // continue; // only calc high low on data we are showing
445
440
  continue;
446
441
  }
447
442
  if (data[i].timestamp > _this.scaleinfoX.endTimeStamp) {
448
- break; // continue?
443
+ break;
449
444
  }
450
445
 
451
446
  if (_this.scaleinfoY.type != 'history' && (data[i].timestamp % 86400000 < _this.instruments[0].opentimestamp || data[i].timestamp % 86400000 > _this.instruments[0].closetimestamp)) {
452
447
  // stämmer detta kan det bli överlapp vid sommartid/vintertid?
453
448
  continue;
454
449
  }
455
- if (data[i].timestamp > _this.scaleinfoX.endTimeStamp) break;
456
450
 
457
- if (data[i].timestamp < _this.scaleinfoX.startTimeStamp) { // hit kommer vi ju aldrig
458
- //if (_this.settings.chartlen != '1d' && _this.settings.chartlen != '0d') firstvalue = price
459
- //console.log(new Date(data[i].timestamp),price,firstvalue);
460
- continue; // only calc high low on data we are showing
461
- }
462
- datalen++;
463
451
  if (firstvalue == null) {
464
452
  if (_this.scaleinfoY.type == 'history')
465
453
  firstvalue = price;
@@ -476,6 +464,8 @@ function Milli_Chart(settings) {
476
464
  data[i].diff = diff;
477
465
  if (_this.scaleinfoY2.lowValue == null || _this.scaleinfoY2.lowValue > diff) _this.scaleinfoY2.lowValue = diff;
478
466
  if (_this.scaleinfoY2.highValue == null || _this.scaleinfoY2.highValue < diff) _this.scaleinfoY2.highValue = diff;
467
+ if (_this.scaleinfoY.lowLowerChart == null || _this.scaleinfoY.lowLowerChart > quantity) _this.scaleinfoY.lowLowerChart = quantity;
468
+ if (_this.scaleinfoY.highLowerChart == null || _this.scaleinfoY.highLowerChart < quantity) _this.scaleinfoY.highLowerChart = quantity;
479
469
  }
480
470
  if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') { // if closeprice is used calch high/low on it
481
471
  var cp = parseFloat(_this.instruments[s].closeprice1d);
@@ -494,11 +484,113 @@ function Milli_Chart(settings) {
494
484
  _this.scaleinfoY.lowValue -= 1;
495
485
  _this.scaleinfoY.highValue += 1;
496
486
  }
497
- // console.log(_this.scaleinfoY);
498
- // console.log('CALCHHIGHLOW', _this.scaleinfoY);
499
487
  return 1;
500
488
  }
501
489
 
490
+ function plotLower(valuePerPixel, lineLength) {
491
+ m_ctx.save();
492
+ m_ctx.strokeStyle = m_instrumentCss[0].color;
493
+ m_ctx.lineWidth = parseInt(m_instrumentCss[0].width);
494
+ m_ctx.beginPath();
495
+ var width = Math.round(m_chartspaces.lowerChart.width / (m_datapoints.length + 1) / 2);
496
+ console.log(m_chartspaces.lowerChart.width, m_datapoints.length);
497
+ if (width < 2) width = 2;
498
+ else if (width > 20) width = 20;
499
+ if (width * (m_datapoints.length + 1) >= m_chartspaces.lowerChart.width) console.log('test');
500
+ m_ctx.lineWidth = width;
501
+ console.log(width);
502
+ for (var i = 0; i < m_datapoints.length; i++) {
503
+ var x = Math.round(m_datapoints[i].x);
504
+ if (x - width < m_chartspaces.lowerChart.left) {
505
+ x += width / 4;
506
+ m_ctx.lineWidth = width / 2;
507
+ } else
508
+ if (x + width > m_chartspaces.lowerChart.right) {
509
+ x -= width / 4;
510
+ m_ctx.lineWidth = width / 2;
511
+ }
512
+
513
+ m_ctx.moveTo(x + 0.5, m_chartspaces.lowerChart.bottom);
514
+ var y = Math.round(m_chartspaces.lowerChart.bottom - (m_datapoints[i].quantity * valuePerPixel));
515
+ m_ctx.lineTo(x + 0.5, y);
516
+ m_ctx.stroke();
517
+ }
518
+ m_ctx.closePath();
519
+ m_ctx.restore();
520
+ }
521
+
522
+ function drawYLegendLower(x, numticks, lineLength, markers) {
523
+ m_ctx.strokeStyle = m_gridHorizontalCss.color;
524
+ m_ctx.fillStyle = m_yLegendCss.color;
525
+
526
+ m_ctx.textAlign = 'right';
527
+ var tickSize = getTickValue(_this.scaleinfoY.lowLowerChart, _this.scaleinfoY.highLowerChart, numticks);
528
+ console.log(_this.scaleinfoY.highLowerChart, tickSize, lineLength);
529
+ var maxValue = _this.scaleinfoY.highLowerChart == 0 ? 100 : _this.scaleinfoY.highLowerChart + (tickSize * 0.2);
530
+ var valuePerPixel = lineLength / maxValue;
531
+ if (isNaN(valuePerPixel) || !isFinite(valuePerPixel)) {
532
+ console.log('cant draw valuePerPixel' + valuePerPixel);
533
+ return false;
534
+ }
535
+ var value = 0;
536
+ for (;;) {
537
+ var y = Math.round(m_chartspaces.lowerChart.bottom - (value * valuePerPixel));
538
+ console.log(value, y, tickSize, maxValue);
539
+
540
+ if (y <= m_chartspaces.lowerChart.top) break;
541
+ if (y <= m_chartspaces.lowerChart.bottom) {
542
+ if (_this.settings.gridHorizontalLines == true) {
543
+ m_ctx.save();
544
+ if (_this.settings.gridHorizontalLinesStyle == 'dash') {
545
+ m_ctx.setLineDash([3, 3]);
546
+ }
547
+ m_ctx.moveTo(m_chartspaces.lowerChart.left, y + 0.5);
548
+ m_ctx.lineTo(m_chartspaces.lowerChart.right, y + 0.5);
549
+ m_ctx.stroke();
550
+ m_ctx.restore();
551
+ } else
552
+ if (_this.settings.drawyaxis == true && markers == true) { // draw legenditem markers for price
553
+ m_ctx.moveTo(m_chartspaces.lowerChart.left, y + 0.5);
554
+ m_ctx.lineTo(m_chartspaces.lowerChart.left + 3, y + 0.5);
555
+ m_ctx.stroke();
556
+ }
557
+
558
+ if (markers == true && _this.settings.drawyaxis == true) {
559
+ var label = formatLargeNumber(value, 0, _this);
560
+ var textpos = x - 5;
561
+ if (m_yLegendCss['vertical-align'] == 'top') {
562
+ if (y - (getFontSize(m_yLegendCss)) > 0) // dont draw if cropped
563
+ m_ctx.fillText(label, textpos, y - ((getFontSize(m_yLegendCss) + 2)));
564
+ } else
565
+ m_ctx.fillText(label, textpos, y - (getFontSize(m_yLegendCss) / 2));
566
+ }
567
+ }
568
+ value += tickSize;
569
+ }
570
+ plotLower(valuePerPixel, lineLength);
571
+ return true;
572
+ }
573
+
574
+ function drawYAxisLower() {
575
+ //m_ctx.save();
576
+ m_ctx.strokeStyle = m_gridHorizontalCss.color;
577
+ m_ctx.font = m_yLegendCss['font-weight'] + ' ' + m_yLegendCss['font-size'] + ' ' + m_yLegendCss['font-family'];
578
+ console.log(m_yLegendCss.color, m_gridHorizontalCss.color);
579
+ m_ctx.fillStyle = m_yLegendCss.color;
580
+ var lineLen = m_chartspaces.lowerChart.bottom - m_chartspaces.lowerChart.top;
581
+ var numticks = lineLen / (getFontSize(m_yLegendCss) * 2);
582
+ if (numticks > 8) numticks = 8; // limit to 8 items on Y legend ( this is not an absolut count, since we calculate nice legend numbers
583
+
584
+ m_ctx.moveTo(m_chartspaces.lowerChart.left + 0.5, m_chartspaces.lowerChart.top);
585
+ m_ctx.lineTo(m_chartspaces.lowerChart.left + 0.5, m_chartspaces.lowerChart.bottom);
586
+ m_ctx.stroke();
587
+ var x = m_chartspaces.lowerChart.left - 3;
588
+
589
+ drawYLegendLower(x, numticks, lineLen, true);
590
+ //m_ctx.restore();
591
+
592
+ }
593
+
502
594
  function drawYLegend(si, x, side, gridHorizontalLines, number) {
503
595
  var value;
504
596
  if (si.highValue == si.lowValue) console.log(si.tickSize);
@@ -523,25 +615,25 @@ function Milli_Chart(settings) {
523
615
  return false;
524
616
  }
525
617
  for (;;) {
526
- var y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - ((value - si.minValue) * si.valuePerPixel));
527
- if (y <= parseInt(m_chartCss['margin-top'])) break;
528
- if (y <= m_canvas.height - parseInt(m_chartCss['margin-bottom'])) {
618
+ var y = Math.round(m_chartspaces.chart.bottom - ((value - si.minValue) * si.valuePerPixel));
619
+ if (y <= m_chartspaces.chart.top) break;
620
+ if (y <= m_chartspaces.chart.bottom) {
529
621
  if (gridHorizontalLines == true) {
530
622
  m_ctx.save();
531
623
  if (_this.settings.gridHorizontalLinesStyle == 'dash') {
532
624
  m_ctx.setLineDash([3, 3]);
533
625
  }
534
626
  m_ctx.beginPath();
535
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), y + 0.5);
536
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), y + 0.5);
627
+ m_ctx.moveTo(m_chartspaces.chart.left, y + 0.5);
628
+ m_ctx.lineTo(m_chartspaces.chart.right, y + 0.5);
537
629
  m_ctx.stroke();
538
630
  m_ctx.closePath();
539
631
  m_ctx.restore();
540
632
  } else
541
633
  if (_this.settings.drawyaxis == true && number == 1) { // draw legenditem markers for price
542
634
  m_ctx.beginPath();
543
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), y + 0.5);
544
- m_ctx.lineTo(parseInt(m_chartCss['margin-left']) + 3, y + 0.5);
635
+ m_ctx.moveTo(m_chartspaces.chart.left, y + 0.5);
636
+ m_ctx.lineTo(m_chartspaces.chart.left + 3, y + 0.5);
545
637
  m_ctx.stroke();
546
638
  m_ctx.closePath();
547
639
  } else
@@ -570,23 +662,20 @@ function Milli_Chart(settings) {
570
662
  }
571
663
  value += si.tickSize;
572
664
  }
573
- // console.log('drawYLegend out');
574
665
  return true;
575
666
  }
576
667
 
577
668
  function drawYAxis() {
578
- // console.log(m_chartCss['margin-left']);
579
669
  m_ctx.save();
580
670
  m_ctx.strokeStyle = m_gridHorizontalCss.color;
581
671
  m_ctx.font = m_yLegendCss['font-weight'] + ' ' + m_yLegendCss['font-size'] + ' ' + m_yLegendCss['font-family'];
582
672
  m_ctx.fillStyle = m_yLegendCss.color;
583
- if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') firstvalue = _this.instruments[0].closeprice1d;
673
+ //if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') firstvalue = _this.instruments[0].closeprice1d;
584
674
 
585
675
  if (0 == calcHighLow()) {
586
- // console.log('no data to draw on');
587
676
  return;
588
677
  }
589
- _this.scaleinfoY.lineLength = m_canvas.height - parseInt(m_chartCss['margin-top']) - parseInt(m_chartCss['margin-bottom']);
678
+ _this.scaleinfoY.lineLength = m_chartspaces.chart.bottom - m_chartspaces.chart.top;
590
679
  _this.scaleinfoY2.lineLength = _this.scaleinfoY.lineLength;
591
680
  var numticks = _this.scaleinfoY.lineLength / (getFontSize(m_yLegendCss) * 4);
592
681
  if (numticks > 8) numticks = 8; // limit to 8 items on Y legend ( this is not an absolut count, since we calculate nice legend numbers
@@ -595,12 +684,13 @@ function Milli_Chart(settings) {
595
684
  _this.scaleinfoY.decimals = _this.scaleinfoY.decimals > 4 ? 4 : _this.scaleinfoY.decimals;
596
685
  _this.scaleinfoY.decimals = _this.scaleinfoY.decimals < 2 ? 2 : _this.scaleinfoY.decimals;
597
686
  var label = formatNiceNumber(_this.scaleinfoY.highValue, _this.settings.thousandseparator, _this.settings.decimalseparator, _this.scaleinfoY.decimals);
598
- // console.log('SCALE', _this.scaleinfoY);
599
687
  if (m_yLegendCss.float != 'right') {
600
- m_chartCss['margin-left'] = 10 + Math.round(m_ctx.measureText(label).width) + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
688
+ m_chartspaces.chart.left = 10 + Math.round(m_ctx.measureText(label).width); // + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
689
+ m_chartspaces.lowerChart.left = m_chartspaces.chart.left;
601
690
  } else {
602
691
  if (m_yLegendCss['text-align'] == 'right') {
603
- m_chartCss['margin-right'] = 10 + Math.round(m_ctx.measureText(label).width) + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
692
+ m_chartspaces.chart.right = m_canvas.width - (10 + Math.round(m_ctx.measureText(label).width)); // + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
693
+ m_chartspaces.lowerChart.right = m_chartspaces.chart.right;
604
694
  }
605
695
  // TODO: kolla setting om den skall vara "i diagrammet"
606
696
  }
@@ -612,61 +702,53 @@ function Milli_Chart(settings) {
612
702
  _this.scaleinfoY2.decimals = _this.scaleinfoY2.decimals < 0 ? 0 : _this.scaleinfoY2.decimals;
613
703
 
614
704
  label = formatNiceNumber(_this.scaleinfoY2.highValue, _this.settings.thousandseparator, _this.settings.decimalseparator, _this.scaleinfoY2.decimals) + ' %';
615
- if (m_y2LegendCss.float != 'right')
616
- m_chartCss['margin-left'] = 10 + Math.round(m_ctx.measureText(label).width) + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
617
- else {
705
+ if (m_y2LegendCss.float != 'right') {
706
+ m_chartspaces.chart.left = 10 + Math.round(m_ctx.measureText(label).width); // + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
707
+ m_chartspaces.lowerChart.left = m_chartspaces.chart.left
708
+ } else {
618
709
  // kolla setting om den skall vara "i diagrammet"
619
- m_chartCss['margin-right'] = 10 + Math.round(m_ctx.measureText(label).width) + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
710
+ m_chartspaces.chart.right = m_canvas.width - (10 + Math.round(m_ctx.measureText(label).width)); // + 'px'; // räkna fram hur hur mycket plats Y värdena tar och sätt margin till det, skall vi göra så?
711
+ m_chartspaces.lowerChart.right = m_chartspaces.chart.right;
620
712
  }
621
713
  }
622
- // console.log(m_chartCss['margin-left']);
623
714
  if (m_yLegendCss.float != 'right' && _this.settings.drawyaxis) {
624
715
  m_ctx.save();
625
716
  m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
626
- if (getStringWidth(m_ctx, _this.settings.dateformat) / 2 + 5 > parseInt(m_chartCss['margin-left'])) m_chartCss['margin-left'] = getStringWidth(m_ctx, _this.settings.dateformat) / 2 + 5;
717
+ if (getStringWidth(m_ctx, _this.settings.dateformat) / 2 + 5 > m_chartspaces.chart.left) m_chartspaces.chart.left = getStringWidth(m_ctx, _this.settings.dateformat) / 2 + 5;
627
718
  m_ctx.restore();
628
719
  }
629
- // console.log('drawYXis 1');
630
-
631
- //if (_this.settings.drawyaxis == true) { // draw left y legend line
632
720
  m_ctx.beginPath();
633
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']) + 0.5, parseInt(m_chartCss['margin-top']));
634
- m_ctx.lineTo(parseInt(m_chartCss['margin-left']) + 0.5, m_canvas.height - parseInt(m_chartCss['margin-bottom']));
721
+ m_ctx.moveTo(m_chartspaces.chart.left + 0.5, m_chartspaces.chart.top);
722
+ m_ctx.lineTo(m_chartspaces.chart.left + 0.5, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']));
635
723
  m_ctx.stroke();
636
724
  m_ctx.closePath();
637
- //}
638
- // console.log('drawYXis 2');
725
+
639
726
  var x;
640
727
  if (m_yLegendCss.float == 'right') {
641
728
  if (m_yLegendCss['text-align'] == 'right')
642
- x = m_canvas.width - parseInt(m_chartCss['margin-right']);
729
+ x = m_chartspaces.chart.right;
643
730
  else
644
- x = m_canvas.width - parseInt(m_chartCss['margin-right']) + 3;
731
+ x = m_chartspaces.chart.right + 3;
645
732
  } else {
646
- x = parseInt(m_chartCss['margin-left']) - 3;
733
+ x = m_chartspaces.chart.left - 3;
647
734
  }
648
- // console.log('drawYXis 3');
649
735
 
650
736
  m_ctx.textAlign = 'right';
651
- //if (_this.settings.drawyaxis) {
652
737
  if (m_yLegendCss['text-align'] == 'right') {
653
738
  m_ctx.textAlign = 'left';
654
739
  drawYLegend(_this.scaleinfoY, x, 'right', _this.settings.gridHorizontalLines, 1);
655
740
  } else
656
741
  drawYLegend(_this.scaleinfoY, x, 'left', _this.settings.gridHorizontalLines, 1);
657
- //}
658
- // console.log('drawYXis 4');
659
742
 
660
743
  if (_this.settings.drawy2axis) {
661
744
  x = 0;
662
745
  if (m_y2LegendCss.float == 'right') {
663
- x = m_canvas.width - parseInt(m_chartCss['margin-right']) - 3;
746
+ x = m_chartspaces.chart.right - 3;
664
747
  m_ctx.textAlign = 'left';
665
748
  }
666
749
  drawYLegend(_this.scaleinfoY2, x, 'right', _this.settings.gridHorizontalLines2, 2);
667
750
  }
668
751
  m_ctx.restore();
669
- // console.log('draw Y axis out');
670
752
  }
671
753
 
672
754
  function drawXAxisGridlines(p, newday) {
@@ -674,7 +756,7 @@ function Milli_Chart(settings) {
674
756
  m_ctx.save();
675
757
  m_ctx.strokeStyle = m_gridHorizontalCss.color;
676
758
  if (_this.settings.gridVerticalLines > 0) {
677
- if (_this.settings.drawy2axis == false || p.x != parseInt(m_chartCss['margin-left'])) {
759
+ if (_this.settings.drawy2axis == false || p.x != m_chartspaces.chart.left) {
678
760
  if (_this.settings.gridHorizontalLinesStyle == 'dash') {
679
761
  m_ctx.setLineDash([3, 3]);
680
762
  }
@@ -682,7 +764,7 @@ function Milli_Chart(settings) {
682
764
 
683
765
  m_ctx.beginPath();
684
766
  m_ctx.moveTo(p.x + 0.5, p.y + 3);
685
- m_ctx.lineTo(p.x + 0.5, parseInt(m_chartCss['margin-top']));
767
+ m_ctx.lineTo(p.x + 0.5, m_chartspaces.chart.top);
686
768
  m_ctx.stroke();
687
769
  m_ctx.closePath();
688
770
  } else if (_this.settings.drawxaxis == 0) {
@@ -708,7 +790,7 @@ function Milli_Chart(settings) {
708
790
  _this.scaleinfoX.startDate = new Date(starttime);
709
791
  _this.scaleinfoX.endDate = new Date(endtime);
710
792
  _this.scaleinfoX.days = getNumberOfDays(starttime, endtime);
711
- _this.scaleinfoX.lineLength = m_canvas.width - parseInt(m_chartCss['margin-right']) - parseInt(m_chartCss['margin-left']);
793
+ _this.scaleinfoX.lineLength = m_chartspaces.chart.right - m_chartspaces.chart.left;
712
794
  var datesize = new Date('2888-12-28'); // bredaste datum jag kan komma på
713
795
  //_this.scaleinfoX.itemwidth = getStringWidth(m_ctx, '88:88') * 2; // kolla rätt format en 8a för varje tecken
714
796
  _this.scaleinfoX.itemwidth = getStringWidth(m_ctx, formatDate(datesize, _this.settings.dateformat)) * 2; // kolla rätt format en 8a för varje tecken
@@ -747,15 +829,13 @@ function Milli_Chart(settings) {
747
829
  if (xx >= x) return timestamp;
748
830
  timestamp += 86400000;
749
831
  }
750
- //var timestamp = (x - Math.round(parseInt(m_chartCss['margin-left'] + offset)) * _this.scaleinfoX.timePerPixel) - _this.scaleinfoX.startDate.getTime();
751
-
752
832
  return 0;
753
833
  }
754
834
 
755
835
  function getXPosition(timestamp) {
756
836
  var offset = (timestamp.getTime() - _this.scaleinfoX.startDate.getTime()) / (86400000 * 7); // veckor
757
837
  offset = offset * 86400000 * 2 / _this.scaleinfoX.timePerPixel;
758
- return Math.round(parseInt(m_chartCss['margin-left']) + ((timestamp.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
838
+ return Math.round(m_chartspaces.chart.left + ((timestamp.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
759
839
  }
760
840
 
761
841
  function drawXAxisYears(starttime, endtime) {
@@ -768,8 +848,8 @@ function Milli_Chart(settings) {
768
848
  calcXScale(starttime, endtime);
769
849
  if (_this.settings.drawxaxis != 0) {
770
850
  m_ctx.beginPath();
771
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
772
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
851
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
852
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
773
853
  m_ctx.stroke();
774
854
  m_ctx.closePath();
775
855
  }
@@ -787,20 +867,23 @@ function Milli_Chart(settings) {
787
867
  }
788
868
  }
789
869
  if (draw) {
790
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
870
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
871
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
791
872
  text = year;
792
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - m_ctx.measureText(text).width) { // not to far right?
873
+ if (_this.scaleinfoX.lineLength + m_chartspaces.chart.left > x - m_ctx.measureText(text).width) { // not to far right?
793
874
  if (_this.settings.yearLabelsPos == 'top') {
794
875
  m_ctx.save(); // flip and write new years on top
795
876
  var fontMetrix = m_ctx.measureText(text);
796
877
  x = x + fontMetrix.actualBoundingBoxAscent + fontMetrix.actualBoundingBoxDescent + 2;
797
- var y = parseInt(m_chartCss['margin-top']); // + m_ctx.measureText(datum).width;
878
+ var y = m_chartspaces.chart.top; // + m_ctx.measureText(datum).width;
798
879
  m_ctx.translate(x, y);
799
880
  m_ctx.rotate(90 * Math.PI / 180);
800
881
  m_ctx.fillText(text, 0, 0);
801
882
  m_ctx.restore();
802
- } else
803
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
883
+ } else {
884
+ //m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
885
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 10);
886
+ }
804
887
  }
805
888
  numItems++;
806
889
  legendItems.push({ text: year.toString(), timestamp: new Date(year + '-01-01T00:00:00Z'), 'x': x });
@@ -823,10 +906,12 @@ function Milli_Chart(settings) {
823
906
  }
824
907
  }
825
908
  if (draw) {
826
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
909
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
910
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
827
911
  text = formatDate(year.getFullYear() + '-07-01', _this.settings.dateformat);
828
912
  // ingen if som på year???
829
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
913
+ //m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
914
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 10);
830
915
  numItems++;
831
916
  legendItems.push({ 'text': text, timestamp: new Date(year + '-07-01T00:00:00Z'), 'x': x });
832
917
  }
@@ -851,9 +936,11 @@ function Milli_Chart(settings) {
851
936
  }
852
937
  if (dontPrint) break;
853
938
  if (draw) {
854
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
939
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
940
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
855
941
  text = formatDate(year.getFullYear() + '-' + (year.getMonth() + 1) + '-01', _this.settings.dateformat);
856
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
942
+ //m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
943
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.height - parseInt(m_chartCss['margin-bottom']) + 10);
857
944
  }
858
945
  legendItems.push({ 'text': text, timestamp: new Date(year + '-04-01T00:00:00Z'), 'x': x });
859
946
  year = new Date((year.getFullYear() + 1) + '-04-01T00:00:00Z');
@@ -872,9 +959,11 @@ function Milli_Chart(settings) {
872
959
  }
873
960
  }
874
961
  if (draw) {
875
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
962
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
963
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
876
964
  text = formatDate(year.getFullYear() + '-' + (year.getMonth() + 1) + '-01', _this.settings.dateformat);
877
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
965
+ //m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
966
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 10);
878
967
  }
879
968
  legendItems.push({ 'text': text, timestamp: new Date(year + '-10-01T00:00:00Z'), 'x': x });
880
969
  year = new Date((year.getFullYear() + 1) + '-10-01T00:00:00Z');
@@ -895,8 +984,8 @@ function Milli_Chart(settings) {
895
984
  calcXScale(starttime, endtime);
896
985
  if (_this.settings.drawxaxis != 0) { // draw line
897
986
  m_ctx.beginPath();
898
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
899
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
987
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
988
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
900
989
  m_ctx.stroke();
901
990
  m_ctx.closePath();
902
991
  }
@@ -912,122 +1001,43 @@ function Milli_Chart(settings) {
912
1001
  currentDate = new Date(currentDate.getTime() + 86400000);
913
1002
  offset += 86400000 / _this.scaleinfoX.timePerPixel;
914
1003
  }
915
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
916
- if (lastx == 0 && parseInt(m_chartCss['margin-left']) > (x - (getMaxDateWidth() / 2))) { // do not print left of y legend
1004
+ x = Math.round(m_chartspaces.chart.left + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1005
+ if (lastx == 0 && m_chartspaces.chart.left > (x - (getMaxDateWidth() / 2))) { // do not print left of y legend
917
1006
  currentDate = new Date(currentDate.getTime() + 86400000);
918
1007
  continue;
919
1008
  }
920
- console.log(x, lastx);
921
1009
  if (lastx + (getMaxDateWidth() / 2) > (x - getMaxDateWidth())) {
922
1010
  draw = false;
923
1011
  }
924
1012
  if (draw) {
925
1013
  lastx = x;
926
1014
  var text = formatDate(currentDate.toISOString().substring(0, 10), _this.settings.dateformat);
927
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxDateWidth()) // not to far right?
928
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
929
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1015
+ if (_this.scaleinfoX.lineLength + m_chartspaces.chart.left > x - getMaxDateWidth()) { // not to far right?
1016
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.bottom + 10);
1017
+ }
1018
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom });
930
1019
  }
931
1020
  currentDate = new Date(currentDate.getTime() + 86400000);
932
1021
  }
933
1022
  // vad är detta?
934
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
935
- var boxshadow = m_chartCss['box-shadow'].split(' ');
936
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1023
+ if (typeof m_chartCss.boxShadow !== 'undefined') {
1024
+ var boxshadow = m_chartCss.boxShadow.split(' ');
1025
+ if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
937
1026
 
938
- } else
939
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
940
- m_ctx.restore();
941
- }
942
-
943
- function drawXAxisMonth2(starttime, endtime) {
944
- // month axis
945
- starttime -= starttime % 8640000;
946
- endtime -= starttime % 8640000;
947
- m_ctx.save();
948
- m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
949
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
950
- m_ctx.fillStyle = m_xLegendCss.color;
951
- m_ctx.textAlign = "left";
952
-
953
- calcXScale(starttime, endtime);
954
- if (_this.settings.drawxaxis != 0) { // draw line
955
- m_ctx.beginPath();
956
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
957
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
958
- m_ctx.stroke();
959
- m_ctx.closePath();
960
- }
961
- var currentDate = new Date(starttime);
962
- var lastDate = new Date(starttime);
963
- var endDate = new Date(endtime);
964
- var p = { x: 0, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) };
965
- var offset = 0; // timeoffset for closed hours,and weekends
966
- var last = 0;
967
- var olddate = '';
968
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) { // oklart om det skall vara så här 2021-06-01
969
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
970
- currentDate = new Date(currentDate.getTime() + 86400000);
971
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
972
- }
973
- p.x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
974
- var datum;
975
- var dash = false;
976
- datum = formatDate(currentDate.toISOString().substring(0, 10), _this.settings.dateformat);
977
- offset += ((86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel); // * dateDiffInDays(lastdate, currentDate); // varför?
978
- currentDate = new Date(currentDate.getTime() + _this.scaleinfoX.ticksize);
979
- if (parseInt(m_chartCss['margin-left']) + (getMaxDateWidth() / 2) > (p.x - (getMaxDateWidth()))) {
980
- continue;
981
- }
982
-
983
- lastDate = new Date(currentDate);
984
- lastDate.setHours(endDate.getHours());
985
- lastDate.setMinutes(endDate.getMinutes());
986
- lastDate.setSeconds(endDate.getSeconds());
987
- var printLegendItem = true;
988
-
989
- if (p.x != parseInt(m_chartCss['margin-left']) && p.x - last < (Math.floor(_this.scaleinfoX.itemwidth))) {
990
- printLegendItem = false;
991
- }
992
- if (printLegendItem) {
993
- if (olddate != datum) {
994
- last = p.x;
995
- if (_this.scaleinfoX.lineLength > (p.x - (m_ctx.measureText(datum).width / 2) + m_ctx.measureText(datum).width / 2))
996
- m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
997
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
998
- olddate = datum;
999
- }
1000
- }
1027
+ } else {
1028
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
1001
1029
  }
1002
- // print end x legend if not to close to last
1003
- /*if (_this.scaleinfoY.type == 'history') {
1004
- p.x = parseInt(m_chartCss['margin-left']) + _this.scaleinfoX.lineLength; // - (getStringWidth(m_ctx, 'HH:MM'));
1005
- if (p.x - last > (Math.floor(_this.scaleinfoX.itemwidth))) {
1006
- var datum = formatDate(_this.scaleinfoX.endDate, _this.settings.dateformat);
1007
- m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1008
- }
1009
- }*/
1010
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1011
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1012
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines(p, false);
1013
-
1014
- } else
1015
- drawXAxisGridlines(p, false);
1016
1030
  m_ctx.restore();
1017
- return;
1018
-
1019
-
1020
1031
  }
1021
1032
 
1022
1033
  function calcXScaleTick(starttime, endtime) {
1023
1034
  _this.scaleinfoX.startDate = new Date(starttime);
1024
1035
  _this.scaleinfoX.endDate = new Date(endtime);
1025
1036
  _this.scaleinfoX.days = getNumberOfDays(starttime, endtime); // kan vi nog skita i
1026
- _this.scaleinfoX.lineLength = m_canvas.width - parseInt(m_chartCss['margin-right']) - parseInt(m_chartCss['margin-left']); // daybreaks can have 88/88
1037
+ _this.scaleinfoX.lineLength = m_chartspaces.chart.right - m_chartspaces.chart.left; // daybreaks can have 88/88
1027
1038
  _this.scaleinfoX.itemwidth = getStringWidth(m_ctx, '88:88') * 2; // kolla rätt format en 8a för varje tecken
1028
1039
  var maxLegendItems = Math.floor(_this.scaleinfoX.lineLength / (_this.scaleinfoX.itemwidth * 2));
1029
1040
 
1030
- //if (_this.scaleinfoX.days < 1) {
1031
1041
  if (_this.scaleinfoY.type != 'history') {
1032
1042
  var arr = [];
1033
1043
  var tmptime = starttime;
@@ -1079,7 +1089,6 @@ function Milli_Chart(settings) {
1079
1089
  else factor = 1;
1080
1090
  _this.scaleinfoX.ticksize = factor * 60000; // time to add to next legenditem
1081
1091
  }
1082
- //console.log('calcx out',numticks,_this.scaleinfoX.ticksize,factor,totalmilli);
1083
1092
  return;
1084
1093
  }
1085
1094
  }
@@ -1108,6 +1117,34 @@ function Milli_Chart(settings) {
1108
1117
  return datetime.toTimeString().substring(0, 8);
1109
1118
  }
1110
1119
 
1120
+ function calcTimeSpanInDays(startdate, enddate) {
1121
+ var s = Math.ceil(startdate / 86400000) * 86400000;
1122
+ var e = Math.ceil(enddate / 86400000) * 86400000;
1123
+ var days = 1;
1124
+ for (s; s < e; s += 86400000) {
1125
+ var d = new Date(s);
1126
+ if (d.getDay() != 0 && d.getDay() != 6)
1127
+ days++;
1128
+ }
1129
+ return Math.min(days, 5);
1130
+ }
1131
+
1132
+ function drawXAxisTickLowerChart() {
1133
+ m_ctx.save();
1134
+ m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1135
+
1136
+ m_ctx.strokeStyle = m_gridHorizontalCss.color;
1137
+ m_ctx.fillStyle = m_xLegendCss.color;
1138
+ if (_this.settings.drawxaxis != 0) { // draw line for legend
1139
+ m_ctx.beginPath();
1140
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.lowerChart.bottom + 0.5);
1141
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.lowerChart.bottom + 0.5);
1142
+ m_ctx.stroke();
1143
+ m_ctx.closePath();
1144
+ }
1145
+ m_ctx.restore();
1146
+ }
1147
+
1111
1148
  function drawXAxisTick(starttime, endtime) {
1112
1149
  m_ctx.save();
1113
1150
  m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
@@ -1118,82 +1155,114 @@ function Milli_Chart(settings) {
1118
1155
  calcXScaleTick(starttime, endtime);
1119
1156
  if (_this.settings.drawxaxis != 0) { // draw line for legend
1120
1157
  m_ctx.beginPath();
1121
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1122
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1158
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.chart.bottom + 0.5);
1159
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.chart.bottom + 0.5);
1123
1160
  m_ctx.stroke();
1124
1161
  m_ctx.closePath();
1125
1162
  }
1126
- // if(startime.date == endtime.date && starttime > open || endtime < close ) // no date
1127
-
1128
1163
  var currentDate = new Date(starttime);
1129
- console.log('startdate', currentDate);
1130
1164
  var x;
1131
1165
  var lastx = 0;
1132
1166
  var draw = true;
1133
1167
  var offset = 0;
1134
1168
  var legendItems = [];
1135
- // add days (daychanges)
1136
- //currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1169
+ var openhour = new Date(new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z')).getTime() % 86400000;
1170
+ // add daysstarts
1137
1171
  if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1138
1172
  currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1139
- console.log(currentDate);
1140
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1141
- draw = true;
1142
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1143
- currentDate = new Date(currentDate.getTime() + 86400000);
1144
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1145
- }
1146
- //currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1147
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1148
- if (lastx != 0 && lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1149
- draw = false;
1150
- }
1151
- if (draw) {
1152
- lastx = x;
1153
- x += 0.5;
1154
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1155
- // if day change and setting print on top as well
1156
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxTimeWidth()) { // not to far right?
1157
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1158
- legendItems.push({ x: x, type: 0, text: text });
1173
+
1174
+ var firstDate = new Date(currentDate);
1175
+ var days = calcTimeSpanInDays(starttime, endtime);
1176
+ if (m_zoom.mouseup.timestamp == null || (m_zoom.mouseup.timestamp != null && days > 1)) {
1177
+ while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1178
+ draw = true;
1179
+ while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1180
+ currentDate = new Date(currentDate.getTime() + 86400000);
1181
+ offset += 86400000 / _this.scaleinfoX.timePerPixel;
1182
+ }
1183
+ x = Math.round(m_chartspaces.chart.left + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1184
+ if (lastx != 0 && lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1185
+ draw = false;
1186
+ } else
1187
+ if (m_zoom.mouseup.timestamp != null && firstDate.getTime() === currentDate.getTime())
1188
+ draw = false;
1189
+ if (draw) {
1190
+ lastx = x + 0.5;
1191
+ var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1192
+ // if day change and setting print on top as well
1193
+ if (_this.scaleinfoX.lineLength + m_chartspaces.chart.left > x - getMaxTimeWidth()) { // not to far right?
1194
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.bottom + 10);
1195
+ legendItems.push({ x: x, type: 0, text: text });
1196
+ }
1197
+ if (x != m_chartspaces.chart.left + 0.5) { // do not draw the first line, it is already drawn
1198
+ if (openhour == currentDate.getTime() % 86400000) {
1199
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, true); // dash?
1200
+ } else {
1201
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false); // dash?
1202
+ }
1203
+ }
1159
1204
  }
1160
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }); // dash?
1205
+ // Move to next day and set starting hour correct if we have open at half hour
1206
+ currentDate = new Date(currentDate.getTime() + 86400000);
1207
+ currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1208
+ if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1209
+ currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1210
+ offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1161
1211
  }
1162
- currentDate = new Date(currentDate.getTime() + 86400000);
1163
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1164
- //offset += (86400000 - _this.scaleinfoX.milliPerDay);
1165
1212
  }
1213
+ // add timestamps
1166
1214
  var currentDate = new Date(starttime);
1167
- console.log(currentDate);
1168
- //currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1169
1215
  if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1170
1216
  currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1171
- console.log(currentDate);
1172
1217
 
1173
1218
  var maxHourLegends;
1174
- console.log(legendItems);
1175
1219
  if (legendItems.length == 0)
1176
- maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - parseInt(m_chartCss['margin-left']) - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1220
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - m_chartspaces.chart.left - getMaxTimeWidth()) / (getMaxTimeWidth() * 3)); // varför tar vi bort margin???
1177
1221
  else
1178
- if (legendItems.length == 1)
1179
- maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1180
- else
1181
- maxHourLegends = Math.floor((legendItems[1].x - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1182
- var tickSize;
1183
- if (m_zoom.isZooming) {
1184
- console.log(starttime, endtime);
1222
+ if (legendItems.length == 1) {
1223
+ if (m_zoom.mouseup.timestamp && days == 2) {
1224
+ // ta den största delen av linjen och mät på
1225
+ if (_this.scaleinfoX.lineLength / 2 < legendItems[0].x + m_chartspaces.chart.left)
1226
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - (_this.scaleinfoX.lineLength - legendItems[0].x) - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1227
+ else
1228
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1229
+ } else
1230
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1231
+ } else {
1232
+ maxHourLegends = Math.floor((legendItems[legendItems.length - 1].x - legendItems[legendItems.length - 2].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3)); // calculate with the last days length
1233
+ }
1234
+
1235
+ var tickSize; // per day
1236
+ if (days == 1) {
1237
+ // use start and endtime as ticksize
1185
1238
  tickSize = new Date(endtime - starttime);
1186
- console.log(tickSize.getTime());
1187
- } else
1239
+ } else if (m_zoom.mouseup.timestamp && days == 2) { // zoom with more than 2 days
1240
+ // use the day with most time as tickSize
1241
+ var tmpDate = new Date(starttime);
1242
+ tickSize = new Date(new Date(tmpDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z') - new Date(starttime)).getTime();
1243
+ var firstDay = tickSize;
1244
+ tmpDate = new Date(endtime);
1245
+ var secondDay = new Date(endtime - new Date(tmpDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z').getTime()).getTime();
1246
+ tickSize += new Date(endtime - new Date(tmpDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z').getTime()).getTime();
1247
+ tickSize = new Date(Math.max(firstDay, secondDay));
1248
+ } else {
1249
+ // use full day as tickSize
1188
1250
  tickSize = new Date(new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z') - new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z'));
1251
+ }
1189
1252
  var interval;
1190
1253
  var modularvalue = 3600000;
1191
- if (tickSize.getTime() < 3600000) {
1254
+
1255
+ // calc the ticksize to add to time(interval) when drawing and the modulu value
1256
+ if (tickSize.getTime() < 1800000) {
1257
+ tickSize = tickSize.getTime() / 60000;
1258
+ interval = Math.floor(tickSize / (maxHourLegends + 1)) * 60000;
1259
+ if (interval < 60000) interval = 60000;
1260
+ modularvalue = 60000;
1261
+ } else if (tickSize.getTime() < 3600000) {
1192
1262
  tickSize = tickSize.getTime() / 300000;
1193
1263
  interval = Math.floor(tickSize / (maxHourLegends + 1)) * 300000;
1194
1264
  modularvalue = 300000;
1195
- } else
1196
- if (tickSize.getTime() < 7200000 * 3) {
1265
+ } else if (tickSize.getTime() < 7200000 * 3) {
1197
1266
  tickSize = tickSize.getTime() / 600000;
1198
1267
  interval = Math.floor(tickSize / (maxHourLegends + 1)) * 600000;
1199
1268
  modularvalue = 600000;
@@ -1202,40 +1271,37 @@ function Milli_Chart(settings) {
1202
1271
  interval = Math.floor(tickSize / (maxHourLegends + 1)) * 3600000;
1203
1272
  }
1204
1273
  if (interval == 0) interval = 300000;
1205
-
1206
- console.log('MAX', maxHourLegends, tickSize, interval);
1274
+ if (interval % 60000 != 0)
1275
+ interval = (interval - interval % 60000) + 60000; // remove sekunder
1207
1276
  // print other times
1208
1277
  offset = 0;
1209
1278
  lastx = 0;
1210
1279
  var closeTime = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1211
1280
  var workDate = new Date(currentDate);
1212
- var currentDate = new Date(startdate);
1281
+ currentDate = new Date(starttime);
1213
1282
  if (currentDate.getTime() % modularvalue != 0) // if not full hour, move up to next full hour
1214
1283
  currentDate = new Date(currentDate.getTime() - currentDate.getTime() % modularvalue + modularvalue); // START om full hour (if modulo == 0 then + 3600000 kanske)
1215
1284
 
1216
- console.log('workdate', workDate);
1217
1285
  var count = 0;
1218
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1219
- console.log('workdate', workDate);
1220
- if (count++ > 30) break;
1286
+ while (currentDate.getTime() <= _this.scaleinfoX.endTimeStamp) {
1287
+ if (count++ > 100) {
1288
+ break; // just make sure we dont do an infinity loop
1289
+
1290
+ }
1221
1291
  draw = true;
1222
1292
  while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1223
- console.log('Helg', currentDate);
1224
1293
  closeTime = new Date(closeTime.getTime() + 86400000);
1225
1294
  currentDate = new Date(currentDate.getTime() + 86400000);
1226
- //currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1227
1295
  if (currentDate.getTime() % modularvalue != 0) // if not full hour, move up to next full hour
1228
1296
  currentDate = new Date(currentDate.getTime() - currentDate.getTime() % modularvalue + modularvalue); // START om full hour (if modulo == 0 then + 3600000 kanske)
1229
1297
  var workDate = new Date(workDate.getTime() + 86400000);
1230
1298
  offset += 86400000 / _this.scaleinfoX.timePerPixel;
1231
1299
  }
1232
- console.log(currentDate, closeTime);
1233
1300
  if (currentDate.getTime() > closeTime.getTime()) {
1234
1301
  // draw DayEnd(start) dash line
1235
- /*var dayStart = new Date(workDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z')
1236
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((dayStart.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1237
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, true); // dash?*/
1238
-
1302
+ var dayStart = new Date(workDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z')
1303
+ x = Math.round(m_chartspaces.chart.left + ((dayStart.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1304
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, true); // dash?
1239
1305
 
1240
1306
  closeTime = new Date(closeTime.getTime() + 86400000);
1241
1307
  workDate = new Date(workDate.getTime() + 86400000);
@@ -1243,11 +1309,9 @@ function Milli_Chart(settings) {
1243
1309
  if (currentDate.getTime() % modularvalue != 0) // if not full hour, move up to next full hour
1244
1310
  currentDate = new Date(currentDate.getTime() - currentDate.getTime() % modularvalue + modularvalue); // START om full hour (if modulo == 0 then + 3600000 kanske)
1245
1311
  offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1246
- console.log('CLOSED', currentDate, closeTime);
1247
1312
  continue;
1248
1313
  }
1249
- console.log(currentDate, closeTime);
1250
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1314
+ x = Math.round(m_chartspaces.chart.left + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1251
1315
  for (var i = 0; i < legendItems.length; i++) {
1252
1316
  if (x < legendItems[i].x && x + (getMaxTimeWidth() / 2) > (legendItems[i].x - getMaxTimeWidth())) {
1253
1317
  draw = false;
@@ -1257,413 +1321,36 @@ function Milli_Chart(settings) {
1257
1321
  draw = false;
1258
1322
  break;
1259
1323
  }
1260
- }
1261
- console.log(x, lastx, offset);
1262
- if (lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1263
- //draw = false;
1264
- //console.log('false');
1265
- }
1266
- if (draw) {
1267
- lastx = x;
1268
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1269
- // if day change and setting print on top as well
1270
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxTimeWidth()) { // not to far right?
1271
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1272
- legendItems.push({ x: x, type: 0 });
1273
- }
1274
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1275
- }
1276
- currentDate = new Date(currentDate.getTime() + interval);
1277
- //currentDate = new Date(currentDate.getTime() + 3600000);
1278
- }
1279
-
1280
- // vad är detta?
1281
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1282
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1283
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1284
-
1285
- } else
1286
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1287
- m_ctx.restore();
1288
-
1289
- }
1290
-
1291
- function drawXAxisTickBra(starttime, endtime) {
1292
- m_ctx.save();
1293
- m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1294
-
1295
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
1296
- m_ctx.fillStyle = m_xLegendCss.color;
1297
- m_ctx.textAlign = "left";
1298
- calcXScaleTick(starttime, endtime);
1299
- if (_this.settings.drawxaxis != 0) { // draw line for legend
1300
- m_ctx.beginPath();
1301
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1302
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1303
- m_ctx.stroke();
1304
- m_ctx.closePath();
1305
- }
1306
- // if(startime.date == endtime.date && starttime > open || endtime < close ) // no date
1307
-
1308
- var currentDate = new Date(starttime);
1309
- console.log('startdate', currentDate);
1310
- var x;
1311
- var lastx = 0;
1312
- var draw = true;
1313
- var offset = 0;
1314
- var legendItems = [];
1315
- // add days (daychanges)
1316
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1317
- console.log(currentDate);
1318
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1319
- draw = true;
1320
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1321
- // console.log('Helg', currentDate);
1322
- currentDate = new Date(currentDate.getTime() + 86400000);
1323
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1324
- }
1325
- //currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1326
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1327
- if (lastx != 0 && lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1328
- draw = false;
1329
- }
1330
- if (draw) {
1331
- lastx = x;
1332
- x += 0.5;
1333
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1334
- // if day change and setting print on top as well
1335
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxTimeWidth()) { // not to far right?
1336
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1337
- legendItems.push({ x: x, type: 0, text: text });
1338
- }
1339
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }); // dash?
1340
- }
1341
- currentDate = new Date(currentDate.getTime() + 86400000);
1342
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1343
- }
1344
- var currentDate = new Date(starttime);
1345
- console.log(currentDate);
1346
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1347
-
1348
- var maxHourLegends;
1349
- console.log(legendItems);
1350
- if (legendItems.length == 0)
1351
- maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - parseInt(m_chartCss['margin-left']) - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1352
- else
1353
- if (legendItems.length == 1)
1354
- maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1355
- else
1356
- maxHourLegends = Math.floor((legendItems[1].x - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1357
- var tickSize = new Date(new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z') - new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z'));
1358
- var tickSize = tickSize.getTime() / 3600000;
1359
- var interval = Math.floor(tickSize / (maxHourLegends + 1)) * 3600000;
1360
-
1361
- // print other times
1362
- offset = 0;
1363
- lastx = 0;
1364
- var closeTime = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1365
- var workDate = new Date(currentDate);
1366
- console.log('workdate', workDate);
1367
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1368
- console.log('workdate', workDate);
1369
- draw = true;
1370
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1371
- console.log('Helg', currentDate);
1372
- closeTime = new Date(closeTime.getTime() + 86400000);
1373
- currentDate = new Date(currentDate.getTime() + 86400000);
1374
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1375
- var workDate = new Date(workDate.getTime() + 86400000);
1376
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1377
- }
1378
- console.log(currentDate, closeTime);
1379
- if (currentDate.getTime() > closeTime.getTime()) {
1380
- closeTime = new Date(closeTime.getTime() + 86400000);
1381
- workDate = new Date(workDate.getTime() + 86400000);
1382
- currentDate = new Date(workDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1383
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1384
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1385
- console.log('CLOSED', currentDate, closeTime);
1386
- continue;
1387
- }
1388
- console.log(currentDate, closeTime);
1389
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1390
- for (var i = 0; i < legendItems.length; i++) {
1391
- console.log('loop', x, legendItems[i].x);
1392
- if (x < legendItems[i].x && x + (getMaxTimeWidth() / 2) > (legendItems[i].x - getMaxTimeWidth())) {
1393
- draw = false;
1394
- console.log('False1');
1395
- break;
1396
- }
1397
- if (x > legendItems[i].x && legendItems[i].x + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1324
+ if (x == legendItems[i].x) {
1398
1325
  draw = false;
1399
- console.log('False2');
1400
1326
  break;
1401
1327
  }
1402
1328
  }
1403
- console.log(x, lastx, offset);
1404
1329
  if (lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1405
1330
  //draw = false;
1406
- console.log('false');
1407
1331
  }
1332
+ var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1408
1333
  if (draw) {
1409
1334
  lastx = x;
1410
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1411
1335
  // if day change and setting print on top as well
1412
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxTimeWidth()) { // not to far right?
1413
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1336
+ if (_this.scaleinfoX.lineLength + m_chartspaces.chart.left > x - getMaxTimeWidth()) { // not to far right?
1337
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.bottom + 10);
1414
1338
  legendItems.push({ x: x, type: 0 });
1415
1339
  }
1416
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1340
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
1417
1341
  }
1418
1342
  currentDate = new Date(currentDate.getTime() + interval);
1419
- //currentDate = new Date(currentDate.getTime() + 3600000);
1420
1343
  }
1421
1344
 
1422
1345
  // vad är detta?
1423
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1424
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1425
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1346
+ if (typeof m_chartCss.boxShadow !== 'undefined') {
1347
+ var boxshadow = m_chartCss.boxShadow.split(' ');
1348
+ if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
1426
1349
 
1427
- } else
1428
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1429
- m_ctx.restore();
1430
-
1431
- }
1432
-
1433
- function drawXAxisTickTest(starttime, endtime) {
1434
- m_ctx.save();
1435
- m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1436
-
1437
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
1438
- m_ctx.fillStyle = m_xLegendCss.color;
1439
- m_ctx.textAlign = "left";
1440
- calcXScaleTick(starttime, endtime);
1441
- if (_this.settings.drawxaxis != 0) { // draw line for legend
1442
- m_ctx.beginPath();
1443
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1444
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1445
- m_ctx.stroke();
1446
- m_ctx.closePath();
1447
- }
1448
- // if(startime.date == endtime.date && starttime > open || endtime < close ) // no date
1449
-
1450
- var currentDate = new Date(starttime);
1451
- console.log('startdate', currentDate);
1452
- var x;
1453
- var lastx = 0;
1454
- var draw = true;
1455
- var offset = 0;
1456
- var legendItems = [];
1457
- // add days (daychanges)
1458
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1459
- draw = true;
1460
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1461
- console.log('Helg', currentDate);
1462
- currentDate = new Date(currentDate.getTime() + 86400000);
1463
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1464
- }
1465
- var closeTime = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1466
- if (currentDate.getTime() > closeTime.getTime()) {
1467
- currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1468
- currentDate = new Date(currentDate.getTime() + 86400000);
1469
- // offset += 86400000 / _this.scaleinfoX.timePerPixel;
1470
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1471
- console.log('STÄNGT', currentDate);
1472
- continue;
1473
- }
1474
- console.log(currentDate);
1475
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1476
- /*if (lastx == 0 && parseInt(m_chartCss['margin-left']) > (x - (getMaxTimeWidth() / 2))) { // do not print left of y legend
1477
- console.log('ftst', currentDate);
1478
- currentDate = new Date(currentDate.getTime() + 86400000);
1479
- continue;
1480
- }*/
1481
- console.log(x, lastx, offset);
1482
- if (lastx + (getMaxDateWidth() / 2) > (x - getMaxDateWidth())) {
1483
- draw = false;
1484
- console.log('false');
1485
- }
1486
- if (draw) {
1487
- lastx = x;
1488
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1489
- // if day change and setting print on top as well
1490
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxDateWidth()) // not to far right?
1491
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1492
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1493
- }
1494
- //currentDate = new Date(currentDate.getTime() + 86400000);
1495
- currentDate = new Date(currentDate.getTime() + _this.scaleinfoX.ticksize);
1496
- }
1497
- // vad är detta?
1498
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1499
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1500
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1501
-
1502
- } else
1503
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1504
- m_ctx.restore();
1505
-
1506
- }
1507
-
1508
- function drawXAxisTick2(starttime, endtime) {
1509
- // day and month axis
1510
- m_ctx.save();
1511
- m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1512
-
1513
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
1514
- m_ctx.fillStyle = m_xLegendCss.color;
1515
- m_ctx.textAlign = "left";
1516
- calcXScaleTick(starttime, endtime);
1517
- if (_this.settings.drawxaxis != 0) { // draw line for legend
1518
- m_ctx.beginPath();
1519
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1520
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1521
- m_ctx.stroke();
1522
- m_ctx.closePath();
1523
- }
1524
- var currentDate = new Date(starttime);
1525
- lastDate = new Date(new Date(starttime).toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1526
-
1527
- var p = { x: 0, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) };
1528
- var offset = 0; // timeoffset for closed hours,and weekends
1529
- var oldx = 1;
1530
- var newday = true;
1531
- var last = 0;
1532
- var nextYear = currentDate.getFullYear() + 1;
1533
- var drawDate = currentDate;
1534
- var lastDrawX = -100;
1535
- var datum;
1536
- var date;
1537
- var y, x;
1538
- while (p.x < (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) - (_this.scaleinfoX.itemwidth / 2))) { // && oldx != p.x) {
1539
- var dayChange = false;
1540
- if (currentDate.getTime() > endtime) {
1541
- break;
1542
- }
1543
- oldx = p.x;
1544
- p.x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1545
- var dash = false;
1546
- if (newday) {
1547
- //datum = currentDate.getDate() + '/' + (currentDate.getMonth() + 1); // format date
1548
- date = currentDate.getDate() + '/' + (currentDate.getMonth() + 1); // format date
1549
- dash = true;
1550
- dayChange = true;
1551
- }
1552
- //datum = currentDate.toTimeString().substring(0, 5); // Format time
1553
- _this.settings.timeformat = 'HH:mm'; // jonas får fixa
1554
- datum = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1555
- drawDate = currentDate;
1556
- newday = false;
1557
- currentDate = new Date(currentDate.getTime() + _this.scaleinfoX.ticksize);
1558
- var printLegendItem = true;
1559
- var drawGridline = true;
1560
- if (currentDate.getTime() >= lastDate.getTime() && _this.settings.chartlen != '1d' && _this.settings.chartlen != '0d') { // om nästa ticket är större än stängning idag, flytta till nästa dag
1561
- //console.log('hmm', currentDate, lastDate, _this.scaleinfoX.ticksize, newday);
1562
- currentDate = new Date((currentDate.getTime() + 86400000) - (currentDate.getTime() % 86400000));
1563
- currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1564
-
1565
- // next day att starttime
1566
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) {
1567
- currentDate = new Date(currentDate.getTime() + 86400000);
1568
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1569
- }
1570
- //console.log(currentDate, _this.scaleinfoX.endDate);
1571
- if (currentDate.getTime() > _this.scaleinfoX.endDate.getTime()) {
1572
- break;
1573
- }
1574
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1575
- lastDate = new Date(currentDate);
1576
- lastDate = new Date(lastDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1577
- newday = true;
1578
- }
1579
- var fontMetrix;
1580
- var nextx = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1581
- //if ((nextx - p.x < Math.floor(_this.scaleinfoX.itemwidth)) && new Date(currentDate.getTime() + _this.scaleinfoX.ticksize).getDate() != drawDate.getDate()) { // skriv bara ut om utrymmet får plats innan nästa punkt kommer skrivas (dagbrytningar)
1582
- console.log(nextx, p.x, currentDate);
1583
- if ((nextx - p.x < m_ctx.measureText('88:88').width) && new Date(currentDate.getTime() + _this.scaleinfoX.ticksize).getDate() != drawDate.getDate()) { // skriv bara ut om utrymmet får plats innan nästa punkt kommer skrivas (dagbrytningar)
1584
- //console.log('1', datum, 'Skall nog skrivas ut men för nära sista....',nextx,_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) - (_this.scaleinfoX.itemwidth / 1));
1585
- if (nextx < _this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) - (_this.scaleinfoX.itemwidth / 1)) { // is it too close to next?
1586
- //console.log('HIDE');
1587
- printLegendItem = false;
1588
- } else
1589
- if (p.x - lastDrawX < Math.floor(_this.scaleinfoX.itemwidth)) { // is it to close to previous?
1590
- // console.log('to close to previous',dash,datum);
1591
- printLegendItem = false;
1592
- }
1593
- if (dash) {
1594
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1595
- }
1596
- } else
1597
- if (p.x - lastDrawX < Math.floor(_this.scaleinfoX.itemwidth)) { // to close to previous?
1598
- //console.log('to close to previous',dash,datum);
1599
- printLegendItem = false;
1600
- if (dash) {
1601
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1602
- printLegendItem = true;
1603
- }
1604
- } else {
1605
- if (dash && _this.settings.intradaydates) { // lutande text
1606
- m_ctx.save();
1607
- fontMetrix = m_ctx.measureText(date);
1608
- x = p.x + fontMetrix.actualBoundingBoxAscent + fontMetrix.actualBoundingBoxDescent + 2;
1609
- y = parseInt(m_chartCss['margin-top']); // + m_ctx.measureText(datum).width;
1610
- m_ctx.translate(x, y);
1611
- m_ctx.rotate(90 * Math.PI / 180);
1612
- m_ctx.fillText(date, 0, 0);
1613
- m_ctx.restore();
1614
- }
1615
- if (drawGridline) {
1616
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1617
- drawGridline = false;
1618
- }
1619
- }
1620
- if (printLegendItem) {
1621
- last = p.x;
1622
- //console.log(newday,datum,dayChange);
1623
- if (dayChange) {
1624
- m_ctx.save();
1625
- fontMetrix = m_ctx.measureText(date);
1626
- x = p.x + fontMetrix.actualBoundingBoxAscent + fontMetrix.actualBoundingBoxDescent + 2;
1627
- y = parseInt(m_chartCss['margin-top']); // + m_ctx.measureText(datum).width;
1628
- m_ctx.translate(x, y);
1629
- m_ctx.rotate(90 * Math.PI / 180);
1630
- m_ctx.fillText(date, 0, 0);
1631
- m_ctx.restore();
1632
- //m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), parseInt(m_chartCss['margin-top']) + 5);
1633
- if (drawGridline) {
1634
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1635
- }
1636
- lastDrawX = p.x;
1637
- }
1638
- //else
1639
- if (p.x < m_canvas.width - m_ctx.measureText(datum).width) { // if text will be cropped do not print
1640
- m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1641
- if (drawGridline) {
1642
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1643
- }
1644
- lastDrawX = p.x;
1645
- } else {
1646
- //console.log('else', p.x, _this.scaleinfoX.itemwidth, m_canvas.width, _this.scaleinfoX.lineLength, parseInt(m_chartCss['margin-left']), _this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) - _this.scaleinfoX.itemwidth);
1647
- }
1648
- }
1350
+ } else {
1351
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
1649
1352
  }
1650
- // print end x legend if not to close to last
1651
- /* p.x = parseInt(m_chartCss['margin-left']) + _this.scaleinfoX.lineLength; // - (getStringWidth(m_ctx, 'HH:MM'));
1652
- if (p.x - last > (Math.floor(_this.scaleinfoX.itemwidth))) {
1653
- var datum = _this.scaleinfoX.endDate.toTimeString().substring(0, 5);
1654
- m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1655
- }
1656
-
1657
- */
1658
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1659
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1660
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines(p, false);
1661
-
1662
- } //else
1663
- //drawXAxisGridlines(p, false);
1664
-
1665
1353
  m_ctx.restore();
1666
- return;
1667
1354
  }
1668
1355
 
1669
1356
  function onMouseOut(evt) {
@@ -1707,15 +1394,15 @@ function Milli_Chart(settings) {
1707
1394
  m_canvas.parentNode.appendChild(m_zoom.div);
1708
1395
  m_zoom.div.setAttribute('class', 'millistream-chart-zoombox');
1709
1396
  }
1710
- if (x < parseInt(m_chartCss['margin-left'])) x = parseInt(m_chartCss['margin-left']);
1711
- m_zoom.div.style.top = parseInt(m_chartCss['margin-top']) + 'px';
1397
+ if (x < m_chartspaces.chart.left) x = m_chartspaces.chart.left;
1398
+ m_zoom.div.style.top = m_chartspaces.chart.top + 'px';
1712
1399
  m_zoom.div.style.left = (m_zoom.mousedown.pos > x ? x : m_zoom.mousedown.pos) + 'px';
1713
- if (x > m_canvas.width - parseInt(m_chartCss['margin-right'])) x = m_canvas.width - parseInt(m_chartCss['margin-right']);
1714
- m_zoom.div.style.height = m_canvas.height - parseInt(m_chartCss['margin-bottom']) - parseInt(m_chartCss['margin-top']) + 'px';
1400
+ if (x > m_chartspaces.chart.right) x = m_chartspaces.chart.right;
1401
+ m_zoom.div.style.height = m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) - m_chartspaces.chart.top + 'px';
1715
1402
  m_zoom.div.style.width = (m_zoom.mousedown.pos > x ? m_zoom.mousedown.pos - x : x - m_zoom.mousedown.pos) + 'px';
1716
1403
  }
1717
1404
  if (_this.settings.enablehover == false) return;
1718
- if (x < parseInt(m_chartCss['margin-left']) || x > m_canvas.width - parseInt(m_chartCss['margin-right']) || y < parseInt(m_chartCss['margin-top']) || y > m_canvas.height - parseInt(m_chartCss['margin-bottom'])) {
1405
+ if (x < m_chartspaces.chart.left || x > m_chartspaces.chart.right || y < m_chartspaces.chart.top || y > m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom'])) {
1719
1406
  onMouseOut();
1720
1407
  return;
1721
1408
  }
@@ -1732,7 +1419,6 @@ function Milli_Chart(settings) {
1732
1419
  }
1733
1420
  var obj = m_dataPoints.map.get(m_dataPoints.arr[i]);
1734
1421
  if (obj == null) {
1735
- //console.log('not found', m_dataPoints, obj);
1736
1422
  return;
1737
1423
  }
1738
1424
  // datetime
@@ -1794,7 +1480,6 @@ function Milli_Chart(settings) {
1794
1480
  //posy = parseFloat(highy) + 20;
1795
1481
  }
1796
1482
  m_toolTip.instrDiv[k].innerHTML = formatNiceNumber(obj.instruments[k].price, _this.settings.thousandseparator, _this.settings.decimalseparator, obj.instruments[k].price.countDecimals());
1797
- //console.log()
1798
1483
  if (typeof obj.instruments[k].diff !== 'undefined') m_toolTip.instrDiv[k].innerHTML += ', diff:' + formatNiceNumber(obj.instruments[k].diff, ' ', ',', m_y2settings.decimals);
1799
1484
  m_toolTip.instrDiv[k].setAttribute('class', 'millistream-chart-tooltip'); // set class so we can measure it might change below depending on position
1800
1485
  m_toolTip.instrDiv[k].style.top = posy + 'px'; // TODO: räkna ut 7 px beroende på hur hoverpilen ser ut från höjden på diven depending on position
@@ -1868,8 +1553,8 @@ function Milli_Chart(settings) {
1868
1553
 
1869
1554
  function setTimeSpanData() {
1870
1555
  if (typeof _this.settings.timespanelement === 'undefined' || _this.settings.timespanelement == null) return;
1871
- startdate = new Date(_this.scaleinfoX.startTimeStamp);
1872
- enddate = new Date(_this.scaleinfoX.endTimeStamp);
1556
+ var startdate = new Date(_this.scaleinfoX.startTimeStamp);
1557
+ var enddate = new Date(_this.scaleinfoX.endTimeStamp);
1873
1558
  if (startdate.getTime() - (startdate.getTime() % 86400000) == enddate.getTime() - (enddate.getTime() % 86400000)) {
1874
1559
  _this.settings.timespanelement.innerHTML = startdate.toTimeString().substring(0, 8) + ' - ' + enddate.toTimeString().substring(0, 8);
1875
1560
  } else {
@@ -1929,42 +1614,19 @@ function Milli_Chart(settings) {
1929
1614
  }
1930
1615
  }
1931
1616
 
1932
- function checkTickChartData() {
1933
- var count = 0;
1934
- for (var i = 0; i < _this.instruments[0].trades.length; i++) {
1935
- if (_this.instruments[0].trades[i].timestamp > _this.scaleinfoX.startTimeStamp && _this.instruments[0].trades[i].timestamp < _this.scaleinfoX.endTimeStamp) {
1936
- if (count++ > 0) {
1937
- return true;
1938
- }
1939
- }
1940
- }
1941
- var y = (m_canvas.height - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-top']);
1942
- var x = (m_canvas.width - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-left']);
1943
- m_ctx.save();
1944
- m_ctx.font = 'bold ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1945
- m_ctx.font = 'bold 12px ' + m_xLegendCss['font-family'];
1946
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
1947
- m_ctx.fillStyle = m_xLegendCss.color;
1948
- m_ctx.textAlign = "left";
1949
-
1950
- m_ctx.fillText(_this.settings.nochartlabel, x - (m_ctx.measureText(_this.settings.nochartlabel).width / 2), y);
1951
- m_ctx.restore();
1952
- return false;
1953
- }
1954
-
1955
1617
  function checkChartData(data) {
1956
1618
  // _this.instruments[0].history / _this.instruments[0].trades
1957
1619
  var count = 0;
1958
1620
  for (var i = 0; i < data.length; i++) {
1959
- if (data[i].timestamp > _this.scaleinfoX.startTimeStamp && data[i].timestamp < _this.scaleinfoX.endTimeStamp) {
1960
- //console.log(new Date(data[i].timestamp), new Date(_this.scaleinfoX.startTimeStamp), new Date(_this.scaleinfoX.endTimeStamp), data[i]);
1621
+ if (data[i].timestamp >= _this.scaleinfoX.startTimeStamp && data[i].timestamp <= _this.scaleinfoX.endTimeStamp) {
1961
1622
  if (count++ > 0) {
1962
1623
  return true;
1963
1624
  }
1964
1625
  }
1965
1626
  }
1966
- var y = (m_canvas.height - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-top']);
1967
- var x = (m_canvas.width - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-left']);
1627
+ console.log('NO data to draw on');
1628
+ var y = m_chartspaces.chart.bottom - m_chartspaces.chart.top / 2 + m_chartspaces.chart.top;
1629
+ var x = m_chartspaces.chart.right - m_chartspaces.chart.left / 2 + m_chartspaces.chart.left;
1968
1630
  m_ctx.save();
1969
1631
  m_ctx.font = 'bold ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1970
1632
  m_ctx.font = 'bold 12px ' + m_xLegendCss['font-family'];
@@ -1977,8 +1639,34 @@ function Milli_Chart(settings) {
1977
1639
  return false;
1978
1640
  }
1979
1641
 
1642
+ function calcChartSpaces() {
1643
+ m_chartspaces.chart.height = Math.round(m_canvas.height * (m_chartspaces.chart.percent / 100));
1644
+ m_chartspaces.chart.top = parseInt(m_chartCss['margin-top']);
1645
+ m_chartspaces.chart.left = parseInt(m_chartCss['margin-left']);
1646
+ m_chartspaces.chart.right = m_canvas.width - parseInt(m_chartCss['margin-right']);
1647
+ m_chartspaces.chart.bottom = m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']);
1648
+ m_chartspaces.chart.width = m_canvas.width;
1649
+ m_chartspaces.lowerChart.height = m_canvas.height * (m_chartspaces.lowerChart.percent / 100);
1650
+ m_chartspaces.lowerChart.top = Math.round(m_chartspaces.chart.bottom + parseInt(m_chartCss['margin-bottom']) + m_chartspaces.chart.top);
1651
+ m_chartspaces.lowerChart.left = m_chartspaces.chart.left;
1652
+ m_chartspaces.lowerChart.right = m_chartspaces.chart.right;
1653
+ m_chartspaces.lowerChart.bottom = m_canvas.height - parseInt(m_chartCss['margin-bottom']);
1654
+ m_chartspaces.lowerChart.width = m_canvas.width;
1655
+ }
1656
+
1980
1657
  _this.drawChart = function() {
1658
+ if (m_lastDrawnInstrument != _this.instruments[0].insref) {
1659
+ m_zoom.mousedown.pos = 0;
1660
+ m_zoom.mousedown.i = 0;
1661
+ m_zoom.mousedown.timestamp = null;
1662
+ m_zoom.mouseup.timestamp = null;
1663
+ if (m_zoom.div) m_canvas.parentNode.removeChild(m_zoom.div);
1664
+ m_zoom.div = null;
1665
+ m_zoom.isZooming = false;
1666
+ }
1667
+ m_lastDrawnInstrument = _this.instruments[0].insref;
1981
1668
  if (m_ctx == null) return;
1669
+ //console.clear();
1982
1670
  m_datapoints = [];
1983
1671
  if (m_dataPoints.map) m_dataPoints.map.clear();
1984
1672
  m_dataPoints.map = new Map();
@@ -1987,6 +1675,8 @@ function Milli_Chart(settings) {
1987
1675
  console.log('no chartdata');
1988
1676
  return;
1989
1677
  }
1678
+ calcChartSpaces();
1679
+
1990
1680
  var period = _this.settings.chartlen.substring(_this.settings.chartlen.length - 1);
1991
1681
  var len = parseInt(_this.settings.chartlen.substring(0, _this.settings.chartlen.length - 1));
1992
1682
  m_ctx.clearRect(0, 0, m_canvas.width, m_canvas.height);
@@ -2001,11 +1691,8 @@ function Milli_Chart(settings) {
2001
1691
  _this.scaleinfoX.endTimeStamp = m_zoom.mouseup.timestamp > m_zoom.mousedown.timestamp ? m_zoom.mouseup.timestamp : m_zoom.mousedown.timestamp;
2002
1692
  _this.scaleinfoX.startTimeStamp = m_zoom.mouseup.timestamp < m_zoom.mousedown.timestamp ? m_zoom.mouseup.timestamp : m_zoom.mousedown.timestamp;
2003
1693
  } else {
2004
- //console.log('len', len);
2005
1694
  _this.scaleinfoX.startTimeStamp = getTickStartDate(len); // calc not to start with weekends
2006
1695
  // back up x days from quotedate if needed
2007
- //console.log(new Date(_this.scaleinfoX.startTimeStamp), new Date(_this.instruments[0].quotedate));
2008
- //console.log(new Date(_this.scaleinfoX.startTimeStamp), new Date(_this.instruments[0].quotedate - (86400000 * (len - 1))));
2009
1696
  // TODO if startTimeStamp < quotedate -len borde det vara
2010
1697
  var quoteDate = businessDaysSubtraction(_this.instruments[0].quotedate, len);
2011
1698
  if (_this.scaleinfoX.startTimeStamp - _this.scaleinfoX.startTimeStamp % 86400000 > quoteDate) { // if startdate > quotedate back startdate
@@ -2019,7 +1706,6 @@ function Milli_Chart(settings) {
2019
1706
  _this.scaleinfoX.endTimeStamp = new Date(_this.scaleinfoX.endTimeStamp.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z').getTime();
2020
1707
  else if (len == 1) {
2021
1708
  // check if startdate is not today (due to exchange is closed f ex DJ)
2022
- //console.log(new Date(_this.scaleinfoX.endTimeStamp), new Date(_this.scaleinfoX.startTimeStamp));
2023
1709
  if (_this.scaleinfoX.startTimeStamp % 86400000 != new Date().getTime() % 86400) {
2024
1710
  _this.scaleinfoX.endTimeStamp = new Date(new Date(_this.scaleinfoX.startTimeStamp).toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z').getTime();
2025
1711
  } else
@@ -2037,8 +1723,6 @@ function Milli_Chart(settings) {
2037
1723
  _this.scaleinfoX.endTimeStamp -= _this.scaleinfoX.endTimeStamp - _this.scaleinfoX.endTimeStamp % 86400000;
2038
1724
  _this.scaleinfoX.endTimeStamp += _this.instruments[0].quotedate; // set enddate = last Quotedate
2039
1725
  }
2040
- //console.log('Starttimestamp',new Date(_this.scaleinfoX.startTimeStamp));
2041
- //console.log('endtimestammp',new Date(_this.scaleinfoX.endTimeStamp), 'quotedate',new Date(_this.instruments[0].quotedate));
2042
1726
  }
2043
1727
  setTimeSpanData();
2044
1728
  _this.scaleinfoY.type = 'trades';
@@ -2092,7 +1776,6 @@ function Milli_Chart(settings) {
2092
1776
 
2093
1777
  drawYAxis();
2094
1778
  drawXAxisMonth(_this.scaleinfoX.startTimeStamp, _this.scaleinfoX.endTimeStamp);
2095
- //console.log(_this.scaleinfoX.startTimeStamp, _this.scaleinfoX.endTimeStamp);
2096
1779
  if (false == checkChartData(_this.instruments[0].history)) return;
2097
1780
 
2098
1781
  for (i = 0; i < _this.instruments.length; i++) {
@@ -2136,39 +1819,42 @@ function Milli_Chart(settings) {
2136
1819
  for (i = 0; i < _this.instruments.length; i++)
2137
1820
  if (_this.instruments[i].insref != 0)
2138
1821
  plotData(_this.instruments[i].history, i);
2139
- //plotChart(_this.instruments[0].history, null);
2140
1822
 
2141
1823
  }
2142
- drawBoxShadow(m_canvas, m_ctx);
2143
- if (m_modules.quantity) {
2144
- m_modules.quantity.redraw();
1824
+ drawBoxShadow(m_chartspaces.chart);
1825
+ if (m_chartspaces.chart.percent != 100) {
1826
+ drawXAxisTickLowerChart();
1827
+ drawYAxisLower();
1828
+ drawBoxShadow(m_chartspaces.lowerChart);
2145
1829
  }
2146
1830
  onMouseOut();
2147
1831
  };
2148
1832
 
2149
- function drawBoxShadow(canvas, ctx) {
2150
- if (typeof m_chartCss['box-shadow'] === 'undefined') return;
2151
- var boxshadow = m_chartCss['box-shadow'].split(' ');
2152
- ctx.save();
1833
+ function drawBoxShadow(space) {
1834
+ if (typeof m_chartCss.boxShadow === 'undefined') return;
1835
+ var boxShadow = m_chartCss.boxShadow;
1836
+ if (boxShadow.indexOf(')') != -1) {
1837
+ boxShadow = boxShadow.substring(boxShadow.indexOf(')') + 2);
1838
+ }
1839
+ var boxshadow = boxShadow.split(' ');
1840
+ m_ctx.save();
1841
+ m_ctx.beginPath();
2153
1842
  if (parseInt(boxshadow[0]) > 0) {
2154
- ctx.beginPath();
2155
- ctx.strokeStyle = m_chartCss['border-top-color'];
2156
- ctx.lineWidth = parseInt(boxshadow[1]);
2157
- ctx.moveTo(parseInt(m_chartCss['margin-left']), parseInt(m_chartCss['margin-top']));
2158
- ctx.lineTo(canvas.width - parseInt(m_chartCss['margin-right']), parseInt(m_chartCss['margin-top']));
2159
- ctx.stroke();
2160
- ctx.closePath();
1843
+ m_ctx.strokeStyle = m_chartCss['border-top-color'];
1844
+ m_ctx.lineWidth = parseInt(boxshadow[1]);
1845
+ m_ctx.moveTo(space.left, space.top);
1846
+ m_ctx.lineTo(space.right, space.top);
1847
+ m_ctx.stroke();
2161
1848
  }
2162
1849
  if (parseInt(boxshadow[1]) > 0) {
2163
- ctx.beginPath();
2164
- ctx.strokeStyle = m_chartCss['border-right-color'];
2165
- ctx.lineWidth = parseInt(boxshadow[1]);
2166
- ctx.moveTo(canvas.width - parseInt(m_chartCss['margin-right']), parseInt(m_chartCss['margin-top']) + 0.5);
2167
- ctx.lineTo(canvas.width - parseInt(m_chartCss['margin-right']), canvas.height - parseInt(m_chartCss['margin-bottom']));
2168
- ctx.stroke();
2169
- ctx.closePath();
1850
+ m_ctx.strokeStyle = m_chartCss['border-right-color'];
1851
+ m_ctx.lineWidth = parseInt(boxshadow[1]);
1852
+ m_ctx.moveTo(space.right, space.top + 0.5);
1853
+ m_ctx.lineTo(space.right, space.bottom);
1854
+ m_ctx.stroke();
2170
1855
  }
2171
- ctx.restore();
1856
+ m_ctx.closePath();
1857
+ m_ctx.restore();
2172
1858
  }
2173
1859
 
2174
1860
  function parseData(data, factor) {
@@ -2262,39 +1948,50 @@ function Milli_Chart(settings) {
2262
1948
  }
2263
1949
 
2264
1950
  _this.addQuantity = function() {
2265
- if (m_modules.quantity == null)
2266
- m_modules.quantity = new MilliChart_Quantity(_this, m_canvas);
1951
+ if (m_chartspaces.chart.percent == 100) {
1952
+ m_chartspaces.chart.percent = 70;
1953
+ m_chartspaces.lowerChart.percent = 30;
1954
+ } else {
1955
+ m_chartspaces.chart.percent = 100;
1956
+ m_chartspaces.lowerChart.percent = 0;
1957
+ _this.drawChart();
1958
+ return;
1959
+ }
1960
+ /*if (m_modules.quantity == null)
1961
+ m_modules.quantity = new MilliChart_Quantity(_this, m_canvas);*/
1962
+ _this.drawChart();
2267
1963
  };
2268
1964
 
2269
1965
  _this.resizeStart = function(e, ui) {
2270
- m_resizing.resizing = true;
2271
1966
  m_resizing.width = m_canvas.width;
2272
1967
  m_resizing.height = m_canvas.height;
2273
1968
  };
2274
1969
 
2275
1970
  _this.resize = function(e, ui) {
2276
- var diff = (m_resizing.height - _this.settings.target.offsetHeight) / _this.settings.target.offsetHeight * 100;
2277
- if (Math.abs(diff) > 10) {
2278
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight + 'px');
2279
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth + 'px'); // läs från css?
1971
+ var diff = (m_resizing.height - m_canvas.height) / m_canvas.height * 100;
1972
+ if (Math.abs(diff) > 1) {
1973
+ setChartSize();
1974
+ m_resizing.width = m_canvas.width;
1975
+ m_resizing.height = m_canvas.height;
2280
1976
  _this.drawChart();
2281
1977
  } else {
2282
- diff = (m_resizing.width - _this.settings.target.offsetWidth) / _this.settings.target.offsetWidth * 100;
2283
- if (Math.abs(diff) > 10) {
2284
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight + 'px');
2285
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth + 'px'); // läs från css?
1978
+ diff = (m_resizing.width - m_canvas.width) / m_canvas.width * 100;
1979
+ if (Math.abs(diff) > 1) {
1980
+ setChartSize();
1981
+ m_canvas.height = _this.settings.target.offsetHeight;
1982
+ m_canvas.width = _this.settings.target.offsetWidth;
1983
+ m_resizing.width = m_canvas.width;
1984
+ m_resizing.height = m_canvas.height;
2286
1985
  _this.drawChart();
2287
1986
  }
2288
1987
  }
2289
- m_resizing.width = m_canvas.width;
2290
- m_resizing.height = m_canvas.height;
1988
+ var cache = m_ctx.getImageData(0, 0, m_canvas.width, m_canvas.height);
1989
+ setChartSize();
1990
+ m_ctx.putImageData(cache, 0, 0);
2291
1991
  };
2292
1992
 
2293
1993
  _this.resizeEnd = function(e, ui) {
2294
- m_resizeing = false;
2295
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight + 'px');
2296
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth + 'px'); // läs från css?
2297
- console.log('Width:', _this.settings.target.offsetWidth);
1994
+ setChartSize();
2298
1995
  _this.drawChart();
2299
1996
  };
2300
1997
 
@@ -2323,7 +2020,7 @@ function Milli_Chart(settings) {
2323
2020
  currentDate.setSeconds(lastdate.getSeconds());
2324
2021
 
2325
2022
  if (lastdate.toISOString().substring(0, 10) != currentDate.toISOString().substring(0, 10)) { // new date
2326
- tmp = new Date(lastdate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
2023
+ var tmp = new Date(lastdate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
2327
2024
  var nextDate = new Date(data[i].timestamp);
2328
2025
  tmp = tmp.getTime() + 86400000 - _this.scaleinfoX.milliPerDay; // increase to next days starttime
2329
2026
  currentDate = new Date(tmp);
@@ -2336,14 +2033,15 @@ function Milli_Chart(settings) {
2336
2033
  }
2337
2034
  offset += ((86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel); // * dateDiffInDays(lastdate, currentDate);
2338
2035
  lastdate = currentDate;
2339
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2036
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2340
2037
  // TODO: här blir det fel när det är från 00:00: 23:59 men göms av tmpx < startpoint.x
2341
2038
 
2342
2039
  }
2343
- startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2040
+ //startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2041
+ startpoint.y = Math.round(m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2344
2042
  var maxy = maxy > startpoint.y ? maxy : startpoint.y;
2345
2043
 
2346
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2044
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2347
2045
  startpoint.x -= offset;
2348
2046
  m_ctx.beginPath();
2349
2047
  m_ctx.arc(startpoint.x, startpoint.y - 20, 10, 0, 2 * Math.PI, false);
@@ -2409,11 +2107,13 @@ function Milli_Chart(settings) {
2409
2107
  currentDate = new Date(data[i].timestamp);
2410
2108
  offset = ((86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel) * dateDiffInDays(new Date(startDate), currentDate);
2411
2109
  tmp = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
2412
- endpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((tmp.getTime() - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2413
- if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') // plot the closeprice1d
2414
- endpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(_this.instruments[instrument].closeprice1d) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2415
- else {
2416
- endpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(lastItem.price) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2110
+ endpoint.x = Math.round(m_chartspaces.chart.left + ((tmp.getTime() - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2111
+ if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') { // plot the closeprice1d
2112
+ //endpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(_this.instruments[instrument].closeprice1d) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2113
+ endpoint.y = Math.round(m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(_this.instruments[instrument].closeprice1d) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2114
+ } else {
2115
+ //endpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(lastItem.price) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2116
+ endpoint.y = Math.round(m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(lastItem.price) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2417
2117
  // do not add closeprice to hover array
2418
2118
  /*
2419
2119
  var x = Math.round(endpoint.x);
@@ -2458,22 +2158,20 @@ function Milli_Chart(settings) {
2458
2158
  offset += ((86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel); // * dateDiffInDays(lastdate, currentDate);
2459
2159
  lastdate = currentDate;
2460
2160
  //console.log(startpoint.x);
2461
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2161
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2462
2162
  //console.log(startpoint.x);
2463
2163
 
2464
2164
  // TODO: här blir det fel när det är från 00:00: 23:59 men göms av tmpx < startpoint.x
2465
2165
  if (_this.scaleinfoY.type == 'trades' && tmpx < startpoint.x) {
2466
2166
  m_ctx.lineTo(startpoint.x, startpoint.y);
2467
2167
  }
2468
-
2469
2168
  //m_ctx.lineTo(startpoint.x, startpoint.y);
2470
-
2471
2169
  }
2472
- //p.x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset); // from drawXaxis
2473
- startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2170
+ //startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2171
+ startpoint.y = Math.round(m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2474
2172
  maxy = maxy > startpoint.y ? maxy : startpoint.y;
2475
2173
 
2476
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2174
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2477
2175
  startpoint.x -= offset;
2478
2176
  if (startpoint.x != endpoint.x || startpoint.y != endpoint.y) {
2479
2177
  var x = Math.round(startpoint.x);
@@ -2497,7 +2195,7 @@ function Milli_Chart(settings) {
2497
2195
  //m_ctx.bezierCurveTo(startpoint.x, startpoint.y, endpoint.x - 1, endpoint.y - 1, endpoint.x, endpoint.y); // läs på om detta för "runda linjer"
2498
2196
 
2499
2197
  if (instrument == 0) {
2500
- point = { price: data[i].price, open: data[i].openprice, x: startpoint.x - 0.5, y: startpoint.y - 0.5, timestamp: data[i].timestamp, date: new Date(data[i].timestamp) };
2198
+ point = { price: data[i].price, open: data[i].openprice, x: startpoint.x - 0.5, y: startpoint.y - 0.5, timestamp: data[i].timestamp, date: new Date(data[i].timestamp), quantity: data[i].quantity };
2501
2199
  if (typeof data[i].dividend !== 'undefined') point.dividend = data[i].dividend;
2502
2200
  if (typeof data[i].diff !== 'undefined') point.diff = data[i].diff;
2503
2201
  m_datapoints.push(point);
@@ -2511,19 +2209,19 @@ function Milli_Chart(settings) {
2511
2209
  if (_this.settings.hcurve && _this.scaleinfoY.type == 'history') {
2512
2210
  if (isToday(currentDate)) {
2513
2211
  // only 1 point in hcurve chart, draw line from start of chart to end date
2514
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), startpoint.y);
2212
+ m_ctx.moveTo(m_chartspaces.chart.left, startpoint.y);
2515
2213
  m_ctx.lineTo(startpoint.x, startpoint.y);
2516
- point = { price: data[i].price, open: data[i].openprice, x: parseInt(m_chartCss['margin-left']) - 0.5, y: startpoint.y - 0.5, timestamp: data[i].timestamp, date: new Date(data[i].timestamp), insref: _this.instruments[instrument].insref, diff: data[i].diff };
2214
+ point = { price: data[i].price, open: data[i].openprice, x: m_chartspaces.chart.left - 0.5, y: startpoint.y - 0.5, timestamp: data[i].timestamp, date: new Date(data[i].timestamp), insref: _this.instruments[instrument].insref, diff: data[i].diff };
2517
2215
  m_datapoints.push(point);
2518
2216
  point = { price: data[i].price, open: data[i].openprice, x: startpoint.x - 0.5, y: startpoint.y - 0.5, timestamp: data[i].timestamp, date: new Date(data[i].timestamp), insref: _this.instruments[instrument].insref, diff: data[i].diff };
2519
2217
  m_datapoints.push(point);
2520
- startx = parseInt(m_chartCss['margin-left']);
2218
+ startx = m_chartspaces.chart.left;
2521
2219
  starty = startpoint.y;
2522
2220
  // last point so break out and store startx and starty from fake point
2523
2221
  break;
2524
2222
  } else if (hCurveLastPoint) {
2525
2223
  /* var y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((hCurveLastPoint.price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2526
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), y);
2224
+ m_ctx.moveTo(m_chartspaces.chart.left, y);
2527
2225
  m_ctx.lineTo(startpoint.x, y);*/
2528
2226
  }
2529
2227
  }
@@ -2532,11 +2230,9 @@ function Milli_Chart(settings) {
2532
2230
  startx = startpoint.x;
2533
2231
  starty = startpoint.y;
2534
2232
  }
2535
- //console.log('in', startpoint.x);
2536
2233
  endpoint.x = startpoint.x;
2537
2234
  endpoint.y = startpoint.y;
2538
2235
  }
2539
- // console.log('div', data[i]);
2540
2236
  if (data[i].dividend) {
2541
2237
  //console.log('div', data[i]);
2542
2238
  }
@@ -2546,9 +2242,10 @@ function Milli_Chart(settings) {
2546
2242
  });
2547
2243
  m_ctx.stroke();
2548
2244
  if (_this.settings.fillchart == true && instrument == 0) {
2549
- //console.log(startpoint.x, endpoint.x);
2550
- m_ctx.lineTo(startpoint.x, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2551
- m_ctx.lineTo(startx, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2245
+ // m_ctx.lineTo(startpoint.x, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2246
+ // m_ctx.lineTo(startx, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2247
+ m_ctx.lineTo(startpoint.x, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2248
+ m_ctx.lineTo(startx, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2552
2249
  m_ctx.lineTo(startx, starty);
2553
2250
  m_ctx.closePath();
2554
2251
  if (true == m_instrumentCss[0]['background-image'].startsWith('linear-gradient')) {
@@ -2560,7 +2257,7 @@ function Milli_Chart(settings) {
2560
2257
  col0 = col0.substring(0, col0.lastIndexOf(','));
2561
2258
  col1 = colors.substring(colors.lastIndexOf('rgba'), colors.lastIndexOf(')'));
2562
2259
  }
2563
- var grd = m_ctx.createLinearGradient(0, 0, 0, m_canvas.height - parseInt(m_chartCss['margin-top']) - parseInt(m_chartCss['margin-bottom']));
2260
+ var grd = m_ctx.createLinearGradient(0, 0, 0, m_chartspaces.chart.height - m_chartspaces.chart.top - parseInt(m_chartCss['margin-bottom']));
2564
2261
  grd.addColorStop(0, col0);
2565
2262
  grd.addColorStop(1, col1);
2566
2263
  m_ctx.fillStyle = grd;
@@ -2657,6 +2354,16 @@ function Milli_Chart(settings) {
2657
2354
  return 1;
2658
2355
  };
2659
2356
 
2357
+ function setChartSize() {
2358
+ var offset = 0;
2359
+ if (_this.settings.target.parentNode.classList.contains('chartcontainer')) {
2360
+ offset = _this.settings.target.getBoundingClientRect().top - _this.settings.target.parentNode.getBoundingClientRect().top;
2361
+ }
2362
+ _this.settings.target.style.height = (_this.settings.target.parentNode.offsetHeight - offset) + 'px';
2363
+ _this.settings.target.style.width = _this.settings.target.parentNode.offsetWidth + 'px';
2364
+ m_canvas.height = _this.settings.target.offsetHeight;
2365
+ m_canvas.width = _this.settings.target.offsetWidth;
2366
+ }
2660
2367
  _this.buildwidget = function(resp) {
2661
2368
  //console.log('buildwidget', resp);
2662
2369
  parseData(resp[0], 1);
@@ -2674,13 +2381,8 @@ function Milli_Chart(settings) {
2674
2381
  if (m_canvas == null) {
2675
2382
  m_canvas = MillistreamWidgetApi_addElement(_this, 'canvas', 'millistream-chart-canvas', _this.settings.target);
2676
2383
 
2677
- //m_canvas.style.width = '100%';
2678
- //m_canvas.style.height = '100%';
2679
- //m_canvas.setAttribute('height', m_canvas.offsetHeight);
2680
- //m_canvas.setAttribute('width', m_canvas.offsetWidth); // läs från css?
2681
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight);
2682
- //m_canvas.setAttribute('height', 300);
2683
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth); // läs från css?
2384
+ setChartSize();
2385
+
2684
2386
  m_ctx = m_canvas.getContext("2d");
2685
2387
  m_canvas.addEventListener('mousemove', onMouseMove, false); // disable while loading and enable on drawReady
2686
2388
  m_canvas.addEventListener('mouseout', onMouseOut, false);
@@ -2706,9 +2408,13 @@ function Milli_Chart(settings) {
2706
2408
  if (m_datapoints.length == 0 || m_zoom.mousedown.timestamp == null) return;
2707
2409
  var rect = m_canvas.getBoundingClientRect();
2708
2410
  var x = evt.clientX - rect.left - 1;
2411
+ if (x < m_zoom.mousedown.pos) {
2412
+ clearZoom();
2413
+ return;
2414
+ }
2709
2415
  if (Math.abs(x - m_zoom.mousedown.pos) > 5) { // only handle zoom with 5 pixels width
2710
2416
  var i;
2711
- for (i = m_datapoints.length - 1; i > 0; i--) { // -1??
2417
+ for (i = m_datapoints.length - 1; i > 0; i--) {
2712
2418
  if (x >= m_datapoints[i].x) {
2713
2419
  m_zoom.end = x;
2714
2420
  break;
@@ -2784,7 +2490,8 @@ function Milli_Chart(settings) {
2784
2490
  m_chartCss['margin-bottom'] = properties.marginBottom;
2785
2491
  m_chartCss['margin-left'] = properties.marginLeft;
2786
2492
  m_chartCss['margin-right'] = properties.marginRight;
2787
- m_chartCss['box-shadow'] = properties.boxShadow;
2493
+ //m_chartCss['box-shadow'] = properties.boxShadow;
2494
+ m_chartCss.boxShadow = properties.boxShadow;
2788
2495
  m_chartCss['border-right-color'] = properties.borderRightColor;
2789
2496
  m_chartCss['border-left-color'] = properties.borderLeftColor;
2790
2497
 
@@ -2980,7 +2687,6 @@ function Milli_Chart(settings) {
2980
2687
  changeOrientation();
2981
2688
 
2982
2689
  });
2983
-
2984
2690
  }
2985
2691
  var milli_data_api_url = 'https://stage.millistream.com/widgets/3.0.3/data/milli_widget_dataapi.php?';
2986
2692
 
@@ -3628,7 +3334,7 @@ function print_field(widget, element, field, value, overridedecimals) {
3628
3334
  }
3629
3335
  }
3630
3336
  if (typeof widget.settings.translations !== 'undefined' && widget.translateTypes) {
3631
- element.innerHTML = widget.translateTypes(value);
3337
+ element.innerHTML = widget.translateTypes(value, field);
3632
3338
  return element.innerHTML;
3633
3339
  }
3634
3340
 
@@ -5555,4 +5261,5 @@ function MillistreamWidgetStreamingApi(settings) {
5555
5261
  }
5556
5262
  exports.Milli_Chart = Milli_Chart;
5557
5263
  exports.MillistreamWidgetSettings = MillistreamWidgetSettings;
5264
+ exports.milli_data_api_url = milli_data_api_url;
5558
5265
  exports.MillistreamWidgetStreamingApi = MillistreamWidgetStreamingApi;