@millistream/millistream-widgets 0.0.13 → 0.0.16

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 +480 -725
  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',
@@ -419,11 +428,12 @@ function Milli_Chart(settings) {
419
428
  }
420
429
 
421
430
  function calcHighLow() {
422
- var datalen = 0;
423
431
  _this.scaleinfoY.lowValue = null;
424
432
  _this.scaleinfoY.highValue = null;
425
433
  _this.scaleinfoY2.lowValue = null;
426
434
  _this.scaleinfoY2.highValue = null;
435
+ _this.scaleinfoY.lowLowerChart = null;
436
+ _this.scaleinfoY.highLowerChart = null;
427
437
  var firstvalue = null;
428
438
 
429
439
  for (var s = 0; s < _this.instruments.length; s++) {
@@ -432,34 +442,26 @@ function Milli_Chart(settings) {
432
442
  if (_this.scaleinfoY.type != 'history') {
433
443
  if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d' && !m_zoom.mousedown.timestamp) {
434
444
  firstvalue = parseFloat(_this.instruments[s].closeprice1d);
435
- //console.log('closeprice1d', firstvalue);
436
445
  }
437
446
  }
438
- //console.log(_this.scaleinfoY.type, data, firstvalue);
439
447
  for (var i = 0; i < data.length; i++) {
440
448
  // only calc on visible data
441
449
  var price = data[i][_this.settings.graphvalue] * _this.instruments[s].factor;
450
+ var quantity = 0;
451
+ if (data[i].quantity !== 'undefined')
452
+ quantity = data[i].quantity;
442
453
  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
454
  continue;
446
455
  }
447
456
  if (data[i].timestamp > _this.scaleinfoX.endTimeStamp) {
448
- break; // continue?
457
+ break;
449
458
  }
450
459
 
451
460
  if (_this.scaleinfoY.type != 'history' && (data[i].timestamp % 86400000 < _this.instruments[0].opentimestamp || data[i].timestamp % 86400000 > _this.instruments[0].closetimestamp)) {
452
461
  // stämmer detta kan det bli överlapp vid sommartid/vintertid?
453
462
  continue;
454
463
  }
455
- if (data[i].timestamp > _this.scaleinfoX.endTimeStamp) break;
456
464
 
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
465
  if (firstvalue == null) {
464
466
  if (_this.scaleinfoY.type == 'history')
465
467
  firstvalue = price;
@@ -476,6 +478,8 @@ function Milli_Chart(settings) {
476
478
  data[i].diff = diff;
477
479
  if (_this.scaleinfoY2.lowValue == null || _this.scaleinfoY2.lowValue > diff) _this.scaleinfoY2.lowValue = diff;
478
480
  if (_this.scaleinfoY2.highValue == null || _this.scaleinfoY2.highValue < diff) _this.scaleinfoY2.highValue = diff;
481
+ if (_this.scaleinfoY.lowLowerChart == null || _this.scaleinfoY.lowLowerChart > quantity) _this.scaleinfoY.lowLowerChart = quantity;
482
+ if (_this.scaleinfoY.highLowerChart == null || _this.scaleinfoY.highLowerChart < quantity) _this.scaleinfoY.highLowerChart = quantity;
479
483
  }
480
484
  if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') { // if closeprice is used calch high/low on it
481
485
  var cp = parseFloat(_this.instruments[s].closeprice1d);
@@ -494,11 +498,96 @@ function Milli_Chart(settings) {
494
498
  _this.scaleinfoY.lowValue -= 1;
495
499
  _this.scaleinfoY.highValue += 1;
496
500
  }
497
- // console.log(_this.scaleinfoY);
501
+ console.log(_this.scaleinfoY);
498
502
  // console.log('CALCHHIGHLOW', _this.scaleinfoY);
499
503
  return 1;
500
504
  }
501
505
 
506
+ function plotLower(valuePerPixel) {
507
+ m_ctx.save();
508
+ m_ctx.strokeStyle = m_instrumentCss[0].color;
509
+ m_ctx.lineWidth = parseInt(m_instrumentCss[0].width);
510
+ for (var i = 0; i < m_datapoints.length; i++) {
511
+ m_ctx.moveTo(m_datapoints[i].x + 0.5, m_chartspaces.lowerChart.bottom - 0.5);
512
+ var y = Math.round(m_chartspaces.lowerChart.bottom - (m_datapoints[i].quantity * valuePerPixel));
513
+ m_ctx.lineTo(m_datapoints[i].x + 0.5, y - 0.5);
514
+ m_ctx.stroke();
515
+ }
516
+ m_ctx.restore();
517
+ }
518
+
519
+ function drawYLegendLower(x, numticks, lineLength, markers) {
520
+ m_ctx.save();
521
+ m_ctx.textAlign = 'right';
522
+ var tickSize = getTickValue(_this.scaleinfoY.lowLowerChart, _this.scaleinfoY.highLowerChart, numticks);
523
+ console.log(_this.scaleinfoY.highLowerChart, tickSize, lineLength);
524
+ var maxValue = _this.scaleinfoY.highLowerChart == 0 ? 100 : _this.scaleinfoY.highLowerChart + (tickSize * 0.2);
525
+ var valuePerPixel = lineLength / maxValue;
526
+ if (isNaN(valuePerPixel) || !isFinite(valuePerPixel)) {
527
+ console.log('cant draw valuePerPixel' + valuePerPixel);
528
+ return false;
529
+ }
530
+ var value = 0;
531
+ for (;;) {
532
+ var y = Math.round(m_chartspaces.lowerChart.bottom - (value * valuePerPixel));
533
+ console.log(value, y, tickSize, maxValue);
534
+
535
+ if (y <= m_chartspaces.lowerChart.top) break;
536
+ if (y <= m_chartspaces.lowerChart.bottom) {
537
+ if (_this.settings.gridHorizontalLines == true) {
538
+ m_ctx.save();
539
+ if (_this.settings.gridHorizontalLinesStyle == 'dash') {
540
+ m_ctx.setLineDash([3, 3]);
541
+ }
542
+ m_ctx.moveTo(m_chartspaces.lowerChart.left, y + 0.5);
543
+ m_ctx.lineTo(m_chartspaces.lowerChart.right, y + 0.5);
544
+ m_ctx.stroke();
545
+ m_ctx.restore();
546
+ } else
547
+ if (_this.settings.drawyaxis == true && markers == true) { // draw legenditem markers for price
548
+ m_ctx.moveTo(m_chartspaces.lowerChart.left, y + 0.5);
549
+ m_ctx.lineTo(m_chartspaces.lowerChart.left + 3, y + 0.5);
550
+ m_ctx.stroke();
551
+ }
552
+
553
+ if (markers == true && _this.settings.drawyaxis == true) {
554
+ var label = formatLargeNumber(value, 0, _this);
555
+ var textpos = x - 5;
556
+ if (m_yLegendCss['vertical-align'] == 'top') {
557
+ if (y - (getFontSize(m_yLegendCss)) > 0) // dont draw if cropped
558
+ m_ctx.fillText(label, textpos, y - ((getFontSize(m_yLegendCss) + 2)));
559
+ } else
560
+ m_ctx.fillText(label, textpos, y - (getFontSize(m_yLegendCss) / 2));
561
+ }
562
+ }
563
+ value += tickSize;
564
+ }
565
+ m_ctx.restore();
566
+ plotLower(valuePerPixel);
567
+ return true;
568
+ }
569
+
570
+ function drawYAxisLower() {
571
+
572
+ //m_ctx.save();
573
+ m_ctx.strokeStyle = m_gridHorizontalCss.color;
574
+ m_ctx.font = m_yLegendCss['font-weight'] + ' ' + m_yLegendCss['font-size'] + ' ' + m_yLegendCss['font-family'];
575
+ console.log(m_yLegendCss.color, m_gridHorizontalCss.color);
576
+ m_ctx.fillStyle = m_yLegendCss.color;
577
+ var lineLen = m_chartspaces.lowerChart.bottom - m_chartspaces.lowerChart.top;
578
+ var numticks = lineLen / (getFontSize(m_yLegendCss) * 2);
579
+ if (numticks > 8) numticks = 8; // limit to 8 items on Y legend ( this is not an absolut count, since we calculate nice legend numbers
580
+
581
+ m_ctx.moveTo(m_chartspaces.lowerChart.left + 0.5, m_chartspaces.lowerChart.top);
582
+ m_ctx.lineTo(m_chartspaces.lowerChart.left + 0.5, m_chartspaces.lowerChart.bottom);
583
+ m_ctx.stroke();
584
+ var x = m_chartspaces.lowerChart.left - 3;
585
+
586
+ drawYLegendLower(x, numticks, lineLen, true);
587
+ //m_ctx.restore();
588
+
589
+ }
590
+
502
591
  function drawYLegend(si, x, side, gridHorizontalLines, number) {
503
592
  var value;
504
593
  if (si.highValue == si.lowValue) console.log(si.tickSize);
@@ -523,25 +612,25 @@ function Milli_Chart(settings) {
523
612
  return false;
524
613
  }
525
614
  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'])) {
615
+ var y = Math.round(m_chartspaces.chart.bottom - ((value - si.minValue) * si.valuePerPixel));
616
+ if (y <= m_chartspaces.chart.top) break;
617
+ if (y <= m_chartspaces.chart.bottom) {
529
618
  if (gridHorizontalLines == true) {
530
619
  m_ctx.save();
531
620
  if (_this.settings.gridHorizontalLinesStyle == 'dash') {
532
621
  m_ctx.setLineDash([3, 3]);
533
622
  }
534
623
  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);
624
+ m_ctx.moveTo(m_chartspaces.chart.left, y + 0.5);
625
+ m_ctx.lineTo(m_chartspaces.chart.right, y + 0.5);
537
626
  m_ctx.stroke();
538
627
  m_ctx.closePath();
539
628
  m_ctx.restore();
540
629
  } else
541
630
  if (_this.settings.drawyaxis == true && number == 1) { // draw legenditem markers for price
542
631
  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);
632
+ m_ctx.moveTo(m_chartspaces.chart.left, y + 0.5);
633
+ m_ctx.lineTo(m_chartspaces.chart.left + 3, y + 0.5);
545
634
  m_ctx.stroke();
546
635
  m_ctx.closePath();
547
636
  } else
@@ -575,18 +664,16 @@ function Milli_Chart(settings) {
575
664
  }
576
665
 
577
666
  function drawYAxis() {
578
- // console.log(m_chartCss['margin-left']);
579
667
  m_ctx.save();
580
668
  m_ctx.strokeStyle = m_gridHorizontalCss.color;
581
669
  m_ctx.font = m_yLegendCss['font-weight'] + ' ' + m_yLegendCss['font-size'] + ' ' + m_yLegendCss['font-family'];
582
670
  m_ctx.fillStyle = m_yLegendCss.color;
583
- if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') firstvalue = _this.instruments[0].closeprice1d;
671
+ //if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') firstvalue = _this.instruments[0].closeprice1d;
584
672
 
585
673
  if (0 == calcHighLow()) {
586
- // console.log('no data to draw on');
587
674
  return;
588
675
  }
589
- _this.scaleinfoY.lineLength = m_canvas.height - parseInt(m_chartCss['margin-top']) - parseInt(m_chartCss['margin-bottom']);
676
+ _this.scaleinfoY.lineLength = m_chartspaces.chart.bottom - m_chartspaces.chart.top;
590
677
  _this.scaleinfoY2.lineLength = _this.scaleinfoY.lineLength;
591
678
  var numticks = _this.scaleinfoY.lineLength / (getFontSize(m_yLegendCss) * 4);
592
679
  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 +682,13 @@ function Milli_Chart(settings) {
595
682
  _this.scaleinfoY.decimals = _this.scaleinfoY.decimals > 4 ? 4 : _this.scaleinfoY.decimals;
596
683
  _this.scaleinfoY.decimals = _this.scaleinfoY.decimals < 2 ? 2 : _this.scaleinfoY.decimals;
597
684
  var label = formatNiceNumber(_this.scaleinfoY.highValue, _this.settings.thousandseparator, _this.settings.decimalseparator, _this.scaleinfoY.decimals);
598
- // console.log('SCALE', _this.scaleinfoY);
599
685
  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å?
686
+ 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å?
687
+ m_chartspaces.lowerChart.left = m_chartspaces.chart.left;
601
688
  } else {
602
689
  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å?
690
+ 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å?
691
+ m_chartspaces.lowerChart.right = m_chartspaces.chart.right;
604
692
  }
605
693
  // TODO: kolla setting om den skall vara "i diagrammet"
606
694
  }
@@ -612,55 +700,48 @@ function Milli_Chart(settings) {
612
700
  _this.scaleinfoY2.decimals = _this.scaleinfoY2.decimals < 0 ? 0 : _this.scaleinfoY2.decimals;
613
701
 
614
702
  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 {
703
+ if (m_y2LegendCss.float != 'right') {
704
+ 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å?
705
+ m_chartspaces.lowerChart.left = m_chartspaces.chart.left
706
+ } else {
618
707
  // 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å?
708
+ 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å?
709
+ m_chartspaces.lowerChart.right = m_chartspaces.chart.right;
620
710
  }
621
711
  }
622
- // console.log(m_chartCss['margin-left']);
623
712
  if (m_yLegendCss.float != 'right' && _this.settings.drawyaxis) {
624
713
  m_ctx.save();
625
714
  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;
715
+ 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
716
  m_ctx.restore();
628
717
  }
629
- // console.log('drawYXis 1');
630
-
631
- //if (_this.settings.drawyaxis == true) { // draw left y legend line
632
718
  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']));
719
+ m_ctx.moveTo(m_chartspaces.chart.left + 0.5, m_chartspaces.chart.top);
720
+ m_ctx.lineTo(m_chartspaces.chart.left + 0.5, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']));
635
721
  m_ctx.stroke();
636
722
  m_ctx.closePath();
637
- //}
638
- // console.log('drawYXis 2');
723
+
639
724
  var x;
640
725
  if (m_yLegendCss.float == 'right') {
641
726
  if (m_yLegendCss['text-align'] == 'right')
642
- x = m_canvas.width - parseInt(m_chartCss['margin-right']);
727
+ x = m_chartspaces.chart.right;
643
728
  else
644
- x = m_canvas.width - parseInt(m_chartCss['margin-right']) + 3;
729
+ x = m_chartspaces.chart.right + 3;
645
730
  } else {
646
- x = parseInt(m_chartCss['margin-left']) - 3;
731
+ x = m_chartspaces.chart.left - 3;
647
732
  }
648
- // console.log('drawYXis 3');
649
733
 
650
734
  m_ctx.textAlign = 'right';
651
- //if (_this.settings.drawyaxis) {
652
735
  if (m_yLegendCss['text-align'] == 'right') {
653
736
  m_ctx.textAlign = 'left';
654
737
  drawYLegend(_this.scaleinfoY, x, 'right', _this.settings.gridHorizontalLines, 1);
655
738
  } else
656
739
  drawYLegend(_this.scaleinfoY, x, 'left', _this.settings.gridHorizontalLines, 1);
657
- //}
658
- // console.log('drawYXis 4');
659
740
 
660
741
  if (_this.settings.drawy2axis) {
661
742
  x = 0;
662
743
  if (m_y2LegendCss.float == 'right') {
663
- x = m_canvas.width - parseInt(m_chartCss['margin-right']) - 3;
744
+ x = m_chartspaces.chart.right - 3;
664
745
  m_ctx.textAlign = 'left';
665
746
  }
666
747
  drawYLegend(_this.scaleinfoY2, x, 'right', _this.settings.gridHorizontalLines2, 2);
@@ -674,7 +755,7 @@ function Milli_Chart(settings) {
674
755
  m_ctx.save();
675
756
  m_ctx.strokeStyle = m_gridHorizontalCss.color;
676
757
  if (_this.settings.gridVerticalLines > 0) {
677
- if (_this.settings.drawy2axis == false || p.x != parseInt(m_chartCss['margin-left'])) {
758
+ if (_this.settings.drawy2axis == false || p.x != m_chartspaces.chart.left) {
678
759
  if (_this.settings.gridHorizontalLinesStyle == 'dash') {
679
760
  m_ctx.setLineDash([3, 3]);
680
761
  }
@@ -682,7 +763,7 @@ function Milli_Chart(settings) {
682
763
 
683
764
  m_ctx.beginPath();
684
765
  m_ctx.moveTo(p.x + 0.5, p.y + 3);
685
- m_ctx.lineTo(p.x + 0.5, parseInt(m_chartCss['margin-top']));
766
+ m_ctx.lineTo(p.x + 0.5, m_chartspaces.chart.top);
686
767
  m_ctx.stroke();
687
768
  m_ctx.closePath();
688
769
  } else if (_this.settings.drawxaxis == 0) {
@@ -708,7 +789,7 @@ function Milli_Chart(settings) {
708
789
  _this.scaleinfoX.startDate = new Date(starttime);
709
790
  _this.scaleinfoX.endDate = new Date(endtime);
710
791
  _this.scaleinfoX.days = getNumberOfDays(starttime, endtime);
711
- _this.scaleinfoX.lineLength = m_canvas.width - parseInt(m_chartCss['margin-right']) - parseInt(m_chartCss['margin-left']);
792
+ _this.scaleinfoX.lineLength = m_chartspaces.chart.right - m_chartspaces.chart.left;
712
793
  var datesize = new Date('2888-12-28'); // bredaste datum jag kan komma på
713
794
  //_this.scaleinfoX.itemwidth = getStringWidth(m_ctx, '88:88') * 2; // kolla rätt format en 8a för varje tecken
714
795
  _this.scaleinfoX.itemwidth = getStringWidth(m_ctx, formatDate(datesize, _this.settings.dateformat)) * 2; // kolla rätt format en 8a för varje tecken
@@ -747,15 +828,13 @@ function Milli_Chart(settings) {
747
828
  if (xx >= x) return timestamp;
748
829
  timestamp += 86400000;
749
830
  }
750
- //var timestamp = (x - Math.round(parseInt(m_chartCss['margin-left'] + offset)) * _this.scaleinfoX.timePerPixel) - _this.scaleinfoX.startDate.getTime();
751
-
752
831
  return 0;
753
832
  }
754
833
 
755
834
  function getXPosition(timestamp) {
756
835
  var offset = (timestamp.getTime() - _this.scaleinfoX.startDate.getTime()) / (86400000 * 7); // veckor
757
836
  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);
837
+ return Math.round(m_chartspaces.chart.left + ((timestamp.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
759
838
  }
760
839
 
761
840
  function drawXAxisYears(starttime, endtime) {
@@ -768,8 +847,8 @@ function Milli_Chart(settings) {
768
847
  calcXScale(starttime, endtime);
769
848
  if (_this.settings.drawxaxis != 0) {
770
849
  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);
850
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
851
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
773
852
  m_ctx.stroke();
774
853
  m_ctx.closePath();
775
854
  }
@@ -787,20 +866,23 @@ function Milli_Chart(settings) {
787
866
  }
788
867
  }
789
868
  if (draw) {
790
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
869
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
870
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
791
871
  text = year;
792
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - m_ctx.measureText(text).width) { // not to far right?
872
+ if (_this.scaleinfoX.lineLength + m_chartspaces.chart.left > x - m_ctx.measureText(text).width) { // not to far right?
793
873
  if (_this.settings.yearLabelsPos == 'top') {
794
874
  m_ctx.save(); // flip and write new years on top
795
875
  var fontMetrix = m_ctx.measureText(text);
796
876
  x = x + fontMetrix.actualBoundingBoxAscent + fontMetrix.actualBoundingBoxDescent + 2;
797
- var y = parseInt(m_chartCss['margin-top']); // + m_ctx.measureText(datum).width;
877
+ var y = m_chartspaces.chart.top; // + m_ctx.measureText(datum).width;
798
878
  m_ctx.translate(x, y);
799
879
  m_ctx.rotate(90 * Math.PI / 180);
800
880
  m_ctx.fillText(text, 0, 0);
801
881
  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);
882
+ } else {
883
+ //m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
884
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 10);
885
+ }
804
886
  }
805
887
  numItems++;
806
888
  legendItems.push({ text: year.toString(), timestamp: new Date(year + '-01-01T00:00:00Z'), 'x': x });
@@ -823,10 +905,12 @@ function Milli_Chart(settings) {
823
905
  }
824
906
  }
825
907
  if (draw) {
826
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
908
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
909
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
827
910
  text = formatDate(year.getFullYear() + '-07-01', _this.settings.dateformat);
828
911
  // 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);
912
+ //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_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 10);
830
914
  numItems++;
831
915
  legendItems.push({ 'text': text, timestamp: new Date(year + '-07-01T00:00:00Z'), 'x': x });
832
916
  }
@@ -851,9 +935,11 @@ function Milli_Chart(settings) {
851
935
  }
852
936
  if (dontPrint) break;
853
937
  if (draw) {
854
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
938
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
939
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
855
940
  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);
941
+ //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_chartspaces.height - parseInt(m_chartCss['margin-bottom']) + 10);
857
943
  }
858
944
  legendItems.push({ 'text': text, timestamp: new Date(year + '-04-01T00:00:00Z'), 'x': x });
859
945
  year = new Date((year.getFullYear() + 1) + '-04-01T00:00:00Z');
@@ -872,9 +958,11 @@ function Milli_Chart(settings) {
872
958
  }
873
959
  }
874
960
  if (draw) {
875
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
961
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
962
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
876
963
  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);
964
+ //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_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 10);
878
966
  }
879
967
  legendItems.push({ 'text': text, timestamp: new Date(year + '-10-01T00:00:00Z'), 'x': x });
880
968
  year = new Date((year.getFullYear() + 1) + '-10-01T00:00:00Z');
@@ -895,8 +983,8 @@ function Milli_Chart(settings) {
895
983
  calcXScale(starttime, endtime);
896
984
  if (_this.settings.drawxaxis != 0) { // draw line
897
985
  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);
986
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
987
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
900
988
  m_ctx.stroke();
901
989
  m_ctx.closePath();
902
990
  }
@@ -912,8 +1000,8 @@ function Milli_Chart(settings) {
912
1000
  currentDate = new Date(currentDate.getTime() + 86400000);
913
1001
  offset += 86400000 / _this.scaleinfoX.timePerPixel;
914
1002
  }
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
1003
+ x = Math.round(m_chartspaces.chart.left + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1004
+ if (lastx == 0 && m_chartspaces.chart.left > (x - (getMaxDateWidth() / 2))) { // do not print left of y legend
917
1005
  currentDate = new Date(currentDate.getTime() + 86400000);
918
1006
  continue;
919
1007
  }
@@ -924,106 +1012,33 @@ function Milli_Chart(settings) {
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_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1017
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 10);
1018
+ }
1019
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1020
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) });
930
1021
  }
931
1022
  currentDate = new Date(currentDate.getTime() + 86400000);
932
1023
  }
933
1024
  // 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);
937
-
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";
1025
+ if (typeof m_chartCss.boxShadow !== 'undefined') {
1026
+ var boxshadow = m_chartCss.boxShadow.split(' ');
1027
+ //if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1028
+ if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) }, false);
952
1029
 
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
- }
1030
+ } else {
1031
+ //drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1032
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) }, false);
1001
1033
  }
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
1034
  m_ctx.restore();
1017
- return;
1018
-
1019
-
1020
1035
  }
1021
1036
 
1022
1037
  function calcXScaleTick(starttime, endtime) {
1023
1038
  _this.scaleinfoX.startDate = new Date(starttime);
1024
1039
  _this.scaleinfoX.endDate = new Date(endtime);
1025
1040
  _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
1041
+ _this.scaleinfoX.lineLength = m_chartspaces.chart.right - m_chartspaces.chart.left; // daybreaks can have 88/88
1027
1042
  _this.scaleinfoX.itemwidth = getStringWidth(m_ctx, '88:88') * 2; // kolla rätt format en 8a för varje tecken
1028
1043
  var maxLegendItems = Math.floor(_this.scaleinfoX.lineLength / (_this.scaleinfoX.itemwidth * 2));
1029
1044
 
@@ -1053,7 +1068,7 @@ function Milli_Chart(settings) {
1053
1068
  pixelsperday[i] = arr[i] * timePerPixel;
1054
1069
  }
1055
1070
  var x = totalmilli / maxLegendItems / 60000;
1056
- numticks = maxLegendItems;
1071
+ var numticks = maxLegendItems;
1057
1072
 
1058
1073
  _this.scaleinfoX.milliPerDay = new Date('2019-01-01T' + _this.instruments[0].marketclose + 'Z') - new Date('2019-01-01T' + _this.instruments[0].marketopen + 'Z');
1059
1074
  _this.scaleinfoX.timePerPixel = timePerPixel;
@@ -1108,164 +1123,37 @@ function Milli_Chart(settings) {
1108
1123
  return datetime.toTimeString().substring(0, 8);
1109
1124
  }
1110
1125
 
1111
- function drawXAxisTick(starttime, endtime) {
1126
+ function calcTimeSpanInDays(startdate, enddate) {
1127
+ var s = Math.ceil(startdate / 86400000) * 86400000;
1128
+ var e = Math.ceil(enddate / 86400000) * 86400000;
1129
+ var days = 1;
1130
+ for (s; s < e; s += 86400000) {
1131
+ var d = new Date(s);
1132
+ if (d.getDay() != 0 && d.getDay() != 6)
1133
+ days++;
1134
+ }
1135
+ return Math.min(days, 5);
1136
+ // Dra av helgen om det är någon där
1137
+ return Math.min(((e - s) / 86400000 + 1), 5);
1138
+ }
1139
+
1140
+ function drawXAxisTickLowerChart() {
1112
1141
  m_ctx.save();
1113
1142
  m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1114
1143
 
1115
1144
  m_ctx.strokeStyle = m_gridHorizontalCss.color;
1116
1145
  m_ctx.fillStyle = m_xLegendCss.color;
1117
- m_ctx.textAlign = "left";
1118
- calcXScaleTick(starttime, endtime);
1119
1146
  if (_this.settings.drawxaxis != 0) { // draw line for legend
1120
1147
  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);
1148
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.lowerChart.bottom + 0.5);
1149
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.lowerChart.bottom + 0.5);
1123
1150
  m_ctx.stroke();
1124
1151
  m_ctx.closePath();
1125
1152
  }
1126
- // if(startime.date == endtime.date && starttime > open || endtime < close ) // no date
1127
-
1128
- var currentDate = new Date(starttime);
1129
- console.log('startdate', currentDate);
1130
- var x;
1131
- var lastx = 0;
1132
- var draw = true;
1133
- var offset = 0;
1134
- 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)
1137
- if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1138
- 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 });
1159
- }
1160
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }); // dash?
1161
- }
1162
- currentDate = new Date(currentDate.getTime() + 86400000);
1163
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1164
- //offset += (86400000 - _this.scaleinfoX.milliPerDay);
1165
- }
1166
- 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
- if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1170
- currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1171
- console.log(currentDate);
1172
-
1173
- var maxHourLegends;
1174
- console.log(legendItems);
1175
- if (legendItems.length == 0)
1176
- maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - parseInt(m_chartCss['margin-left']) - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1177
- 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 = 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'));
1183
- tickSize = tickSize.getTime() / 3600000;
1184
- var interval = Math.floor(tickSize / (maxHourLegends + 1)) * 3600000;
1185
- console.log('MAX', maxHourLegends, tickSize, interval);
1186
- // print other times
1187
- offset = 0;
1188
- lastx = 0;
1189
- var closeTime = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1190
- var workDate = new Date(currentDate);
1191
- console.log('workdate', workDate);
1192
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1193
- console.log('workdate', workDate);
1194
- draw = true;
1195
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1196
- console.log('Helg', currentDate);
1197
- closeTime = new Date(closeTime.getTime() + 86400000);
1198
- currentDate = new Date(currentDate.getTime() + 86400000);
1199
- //currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1200
- if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1201
- currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1202
- var workDate = new Date(workDate.getTime() + 86400000);
1203
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1204
- }
1205
- console.log(currentDate, closeTime);
1206
- if (currentDate.getTime() > closeTime.getTime()) {
1207
- // draw DayEnd(start) dash line
1208
- var dayStart = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z')
1209
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1210
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }); // dash?
1211
-
1212
-
1213
- closeTime = new Date(closeTime.getTime() + 86400000);
1214
- workDate = new Date(workDate.getTime() + 86400000);
1215
- currentDate = new Date(workDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1216
- //currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1217
- if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1218
- currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1219
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1220
- console.log('CLOSED', currentDate, closeTime);
1221
- continue;
1222
- }
1223
- console.log(currentDate, closeTime);
1224
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1225
- for (var i = 0; i < legendItems.length; i++) {
1226
- console.log('loop', x, legendItems[i].x);
1227
- if (x < legendItems[i].x && x + (getMaxTimeWidth() / 2) > (legendItems[i].x - getMaxTimeWidth())) {
1228
- draw = false;
1229
- console.log('False1');
1230
- break;
1231
- }
1232
- if (x > legendItems[i].x && legendItems[i].x + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1233
- draw = false;
1234
- console.log('False2');
1235
- break;
1236
- }
1237
- }
1238
- console.log(x, lastx, offset);
1239
- if (lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1240
- //draw = false;
1241
- console.log('false');
1242
- }
1243
- if (draw) {
1244
- lastx = x;
1245
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1246
- // if day change and setting print on top as well
1247
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxTimeWidth()) { // not to far right?
1248
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1249
- legendItems.push({ x: x, type: 0 });
1250
- }
1251
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1252
- }
1253
- currentDate = new Date(currentDate.getTime() + interval);
1254
- //currentDate = new Date(currentDate.getTime() + 3600000);
1255
- }
1256
-
1257
- // vad är detta?
1258
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1259
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1260
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1261
-
1262
- } else
1263
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1264
1153
  m_ctx.restore();
1265
-
1266
1154
  }
1267
1155
 
1268
- function drawXAxisTickBra(starttime, endtime) {
1156
+ function drawXAxisTick(starttime, endtime) {
1269
1157
  m_ctx.save();
1270
1158
  m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1271
1159
 
@@ -1275,372 +1163,203 @@ function Milli_Chart(settings) {
1275
1163
  calcXScaleTick(starttime, endtime);
1276
1164
  if (_this.settings.drawxaxis != 0) { // draw line for legend
1277
1165
  m_ctx.beginPath();
1278
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1279
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1166
+ m_ctx.moveTo(m_chartspaces.chart.left, m_chartspaces.chart.bottom + 0.5);
1167
+ m_ctx.lineTo(m_chartspaces.chart.right, m_chartspaces.chart.bottom + 0.5);
1280
1168
  m_ctx.stroke();
1281
1169
  m_ctx.closePath();
1282
1170
  }
1283
- // if(startime.date == endtime.date && starttime > open || endtime < close ) // no date
1284
-
1285
1171
  var currentDate = new Date(starttime);
1286
- console.log('startdate', currentDate);
1287
1172
  var x;
1288
1173
  var lastx = 0;
1289
1174
  var draw = true;
1290
1175
  var offset = 0;
1291
1176
  var legendItems = [];
1292
- // add days (daychanges)
1293
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1294
- console.log(currentDate);
1295
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1296
- draw = true;
1297
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1298
- // console.log('Helg', currentDate);
1299
- currentDate = new Date(currentDate.getTime() + 86400000);
1300
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1301
- }
1302
- //currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1303
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1304
- if (lastx != 0 && lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1305
- draw = false;
1306
- }
1307
- if (draw) {
1308
- lastx = x;
1309
- x += 0.5;
1310
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1311
- // if day change and setting print on top as well
1312
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxTimeWidth()) { // not to far right?
1313
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1314
- legendItems.push({ x: x, type: 0, text: text });
1177
+ var openhour = new Date(new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z')).getTime() % 86400000;
1178
+ // add daysstarts
1179
+ if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1180
+ currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1181
+
1182
+ var firstDate = new Date(currentDate);
1183
+ var days = calcTimeSpanInDays(starttime, endtime);
1184
+ if (m_zoom.mouseup.timestamp == null || (m_zoom.mouseup.timestamp != null && days > 1)) {
1185
+ while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1186
+ draw = true;
1187
+ while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1188
+ currentDate = new Date(currentDate.getTime() + 86400000);
1189
+ offset += 86400000 / _this.scaleinfoX.timePerPixel;
1315
1190
  }
1316
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }); // dash?
1191
+ x = Math.round(m_chartspaces.chart.left + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1192
+ if (lastx != 0 && lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1193
+ draw = false;
1194
+ } else
1195
+ if (m_zoom.mouseup.timestamp != null && firstDate.getTime() === currentDate.getTime())
1196
+ draw = false;
1197
+ if (draw) {
1198
+ lastx = x + 0.5;
1199
+ var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1200
+ // if day change and setting print on top as well
1201
+ if (_this.scaleinfoX.lineLength + m_chartspaces.chart.left > x - getMaxTimeWidth()) { // not to far right?
1202
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.bottom + 10);
1203
+ legendItems.push({ x: x, type: 0, text: text });
1204
+ }
1205
+ if (x != m_chartspaces.chart.left + 0.5) { // do not draw the first line, it is already drawn
1206
+ if (openhour == currentDate.getTime() % 86400000) {
1207
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, true); // dash?
1208
+ } else {
1209
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false); // dash?
1210
+ }
1211
+ }
1212
+ }
1213
+ // Move to next day and set starting hour correct if we have open at half hour
1214
+ currentDate = new Date(currentDate.getTime() + 86400000);
1215
+ currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1216
+ if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1217
+ currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1218
+ offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1317
1219
  }
1318
- currentDate = new Date(currentDate.getTime() + 86400000);
1319
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1320
1220
  }
1221
+ // add timestamps
1321
1222
  var currentDate = new Date(starttime);
1322
- console.log(currentDate);
1323
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1223
+ if (currentDate.getTime() % 3600000 != 0) // if not full hour, move up to next full hour
1224
+ currentDate = new Date(currentDate.getTime() - currentDate.getTime() % 3600000 + 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1324
1225
 
1325
1226
  var maxHourLegends;
1326
- console.log(legendItems);
1327
1227
  if (legendItems.length == 0)
1328
- maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - parseInt(m_chartCss['margin-left']) - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1329
- else
1330
- if (legendItems.length == 1)
1331
- maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1228
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - m_chartspaces.chart.left - getMaxTimeWidth()) / (getMaxTimeWidth() * 3)); // varför tar vi bort margin???
1332
1229
  else
1333
- maxHourLegends = Math.floor((legendItems[1].x - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1334
- 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'));
1335
- var tickSize = tickSize.getTime() / 3600000;
1336
- var interval = Math.floor(tickSize / (maxHourLegends + 1)) * 3600000;
1337
-
1230
+ if (legendItems.length == 1) {
1231
+ if (m_zoom.mouseup.timestamp && days == 2) {
1232
+ // ta den största delen av linjen och mät på
1233
+ if (_this.scaleinfoX.lineLength / 2 < legendItems[0].x + m_chartspaces.chart.left)
1234
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - (_this.scaleinfoX.lineLength - legendItems[0].x) - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1235
+ else
1236
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1237
+ } else
1238
+ maxHourLegends = Math.floor((_this.scaleinfoX.lineLength - legendItems[0].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3));
1239
+ } else {
1240
+ maxHourLegends = Math.floor((legendItems[legendItems.length - 1].x - legendItems[legendItems.length - 2].x - getMaxTimeWidth()) / (getMaxTimeWidth() * 3)); // calculate with the last days length
1241
+ }
1242
+
1243
+ var tickSize; // per day
1244
+ if (days == 1) {
1245
+ // use start and endtime as ticksize
1246
+ tickSize = new Date(endtime - starttime);
1247
+ } else if (m_zoom.mouseup.timestamp && days == 2) { // zoom with more than 2 days
1248
+ // use the day with most time as tickSize
1249
+ var tmpDate = new Date(starttime);
1250
+ tickSize = new Date(new Date(tmpDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z') - new Date(starttime)).getTime();
1251
+ var firstDay = tickSize;
1252
+ tmpDate = new Date(endtime);
1253
+ var secondDay = new Date(endtime - new Date(tmpDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z').getTime()).getTime();
1254
+ tickSize += new Date(endtime - new Date(tmpDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z').getTime()).getTime();
1255
+ tickSize = new Date(Math.max(firstDay, secondDay));
1256
+ } else {
1257
+ // use full day as tickSize
1258
+ 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'));
1259
+ }
1260
+ var interval;
1261
+ var modularvalue = 3600000;
1262
+
1263
+ // calc the ticksize to add to time(interval) when drawing and the modulu value
1264
+ if (tickSize.getTime() < 1800000) {
1265
+ tickSize = tickSize.getTime() / 60000;
1266
+ interval = Math.floor(tickSize / (maxHourLegends + 1)) * 60000;
1267
+ if (interval < 60000) interval = 60000;
1268
+ modularvalue = 60000;
1269
+ } else if (tickSize.getTime() < 3600000) {
1270
+ tickSize = tickSize.getTime() / 300000;
1271
+ interval = Math.floor(tickSize / (maxHourLegends + 1)) * 300000;
1272
+ modularvalue = 300000;
1273
+ } else if (tickSize.getTime() < 7200000 * 3) {
1274
+ tickSize = tickSize.getTime() / 600000;
1275
+ interval = Math.floor(tickSize / (maxHourLegends + 1)) * 600000;
1276
+ modularvalue = 600000;
1277
+ } else {
1278
+ tickSize = tickSize.getTime() / 3600000;
1279
+ interval = Math.floor(tickSize / (maxHourLegends + 1)) * 3600000;
1280
+ }
1281
+ if (interval == 0) interval = 300000;
1282
+ if (interval % 60000 != 0)
1283
+ interval = (interval - interval % 60000) + 60000; // remove sekunder
1338
1284
  // print other times
1339
1285
  offset = 0;
1340
1286
  lastx = 0;
1341
1287
  var closeTime = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1342
1288
  var workDate = new Date(currentDate);
1343
- console.log('workdate', workDate);
1344
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1345
- console.log('workdate', workDate);
1289
+ currentDate = new Date(starttime);
1290
+ if (currentDate.getTime() % modularvalue != 0) // if not full hour, move up to next full hour
1291
+ currentDate = new Date(currentDate.getTime() - currentDate.getTime() % modularvalue + modularvalue); // START om full hour (if modulo == 0 then + 3600000 kanske)
1292
+
1293
+ var count = 0;
1294
+ while (currentDate.getTime() <= _this.scaleinfoX.endTimeStamp) {
1295
+ if (count++ > 100) {
1296
+ break; // just make sure we dont do an infinity loop
1297
+
1298
+ }
1346
1299
  draw = true;
1347
1300
  while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1348
- console.log('Helg', currentDate);
1349
1301
  closeTime = new Date(closeTime.getTime() + 86400000);
1350
1302
  currentDate = new Date(currentDate.getTime() + 86400000);
1351
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1303
+ if (currentDate.getTime() % modularvalue != 0) // if not full hour, move up to next full hour
1304
+ currentDate = new Date(currentDate.getTime() - currentDate.getTime() % modularvalue + modularvalue); // START om full hour (if modulo == 0 then + 3600000 kanske)
1352
1305
  var workDate = new Date(workDate.getTime() + 86400000);
1353
1306
  offset += 86400000 / _this.scaleinfoX.timePerPixel;
1354
1307
  }
1355
- console.log(currentDate, closeTime);
1356
1308
  if (currentDate.getTime() > closeTime.getTime()) {
1309
+ // draw DayEnd(start) dash line
1310
+ var dayStart = new Date(workDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z')
1311
+ x = Math.round(m_chartspaces.chart.left + ((dayStart.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1312
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, true); // dash?
1313
+
1357
1314
  closeTime = new Date(closeTime.getTime() + 86400000);
1358
1315
  workDate = new Date(workDate.getTime() + 86400000);
1359
1316
  currentDate = new Date(workDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1360
- currentDate = new Date(currentDate.getTime() + currentDate.getTime() % 3600000); // START om full hour (if modulo == 0 then + 3600000 kanske)
1317
+ if (currentDate.getTime() % modularvalue != 0) // if not full hour, move up to next full hour
1318
+ currentDate = new Date(currentDate.getTime() - currentDate.getTime() % modularvalue + modularvalue); // START om full hour (if modulo == 0 then + 3600000 kanske)
1361
1319
  offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1362
- console.log('CLOSED', currentDate, closeTime);
1363
1320
  continue;
1364
1321
  }
1365
- console.log(currentDate, closeTime);
1366
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1322
+ x = Math.round(m_chartspaces.chart.left + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1367
1323
  for (var i = 0; i < legendItems.length; i++) {
1368
- console.log('loop', x, legendItems[i].x);
1369
1324
  if (x < legendItems[i].x && x + (getMaxTimeWidth() / 2) > (legendItems[i].x - getMaxTimeWidth())) {
1370
1325
  draw = false;
1371
- console.log('False1');
1372
1326
  break;
1373
1327
  }
1374
1328
  if (x > legendItems[i].x && legendItems[i].x + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1375
1329
  draw = false;
1376
- console.log('False2');
1330
+ break;
1331
+ }
1332
+ if (x == legendItems[i].x) {
1333
+ draw = false;
1377
1334
  break;
1378
1335
  }
1379
1336
  }
1380
- console.log(x, lastx, offset);
1381
1337
  if (lastx + (getMaxTimeWidth() / 2) > (x - getMaxTimeWidth())) {
1382
1338
  //draw = false;
1383
- console.log('false');
1339
+ //console.log('false');
1384
1340
  }
1341
+ var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1385
1342
  if (draw) {
1386
1343
  lastx = x;
1387
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1388
1344
  // if day change and setting print on top as well
1389
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxTimeWidth()) { // not to far right?
1390
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1345
+ if (_this.scaleinfoX.lineLength + m_chartspaces.chart.left > x - getMaxTimeWidth()) { // not to far right?
1346
+ m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_chartspaces.chart.bottom + 10);
1391
1347
  legendItems.push({ x: x, type: 0 });
1392
1348
  }
1393
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1349
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
1394
1350
  }
1395
1351
  currentDate = new Date(currentDate.getTime() + interval);
1396
- //currentDate = new Date(currentDate.getTime() + 3600000);
1397
- }
1398
-
1399
- // vad är detta?
1400
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1401
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1402
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1403
-
1404
- } else
1405
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1406
- m_ctx.restore();
1407
-
1408
- }
1409
-
1410
- function drawXAxisTickTest(starttime, endtime) {
1411
- m_ctx.save();
1412
- m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1413
-
1414
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
1415
- m_ctx.fillStyle = m_xLegendCss.color;
1416
- m_ctx.textAlign = "left";
1417
- calcXScaleTick(starttime, endtime);
1418
- if (_this.settings.drawxaxis != 0) { // draw line for legend
1419
- m_ctx.beginPath();
1420
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1421
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1422
- m_ctx.stroke();
1423
- m_ctx.closePath();
1424
1352
  }
1425
- // if(startime.date == endtime.date && starttime > open || endtime < close ) // no date
1426
1353
 
1427
- var currentDate = new Date(starttime);
1428
- console.log('startdate', currentDate);
1429
- var x;
1430
- var lastx = 0;
1431
- var draw = true;
1432
- var offset = 0;
1433
- var legendItems = [];
1434
- // add days (daychanges)
1435
- while (currentDate.getTime() < _this.scaleinfoX.endTimeStamp) {
1436
- draw = true;
1437
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) { // move past weekends , maybe skip this if date is available in data
1438
- console.log('Helg', currentDate);
1439
- currentDate = new Date(currentDate.getTime() + 86400000);
1440
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1441
- }
1442
- var closeTime = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1443
- if (currentDate.getTime() > closeTime.getTime()) {
1444
- currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1445
- currentDate = new Date(currentDate.getTime() + 86400000);
1446
- // offset += 86400000 / _this.scaleinfoX.timePerPixel;
1447
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1448
- console.log('STÄNGT', currentDate);
1449
- continue;
1450
- }
1451
- console.log(currentDate);
1452
- x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1453
- /*if (lastx == 0 && parseInt(m_chartCss['margin-left']) > (x - (getMaxTimeWidth() / 2))) { // do not print left of y legend
1454
- console.log('ftst', currentDate);
1455
- currentDate = new Date(currentDate.getTime() + 86400000);
1456
- continue;
1457
- }*/
1458
- console.log(x, lastx, offset);
1459
- if (lastx + (getMaxDateWidth() / 2) > (x - getMaxDateWidth())) {
1460
- draw = false;
1461
- console.log('false');
1462
- }
1463
- if (draw) {
1464
- lastx = x;
1465
- var text = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1466
- // if day change and setting print on top as well
1467
- if (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) > x - getMaxDateWidth()) // not to far right?
1468
- m_ctx.fillText(text, x - (m_ctx.measureText(text).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1469
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) });
1470
- }
1471
- //currentDate = new Date(currentDate.getTime() + 86400000);
1472
- currentDate = new Date(currentDate.getTime() + _this.scaleinfoX.ticksize);
1473
- }
1474
1354
  // vad är detta?
1475
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1476
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1477
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1478
-
1479
- } else
1480
- drawXAxisGridlines({ 'x': x, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) }, false);
1481
- m_ctx.restore();
1482
-
1483
- }
1484
-
1485
- function drawXAxisTick2(starttime, endtime) {
1486
- // day and month axis
1487
- m_ctx.save();
1488
- m_ctx.font = m_xLegendCss['font-weight'] + ' ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1355
+ if (typeof m_chartCss.boxShadow !== 'undefined') {
1356
+ var boxshadow = m_chartCss.boxShadow.split(' ');
1357
+ if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
1489
1358
 
1490
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
1491
- m_ctx.fillStyle = m_xLegendCss.color;
1492
- m_ctx.textAlign = "left";
1493
- calcXScaleTick(starttime, endtime);
1494
- if (_this.settings.drawxaxis != 0) { // draw line for legend
1495
- m_ctx.beginPath();
1496
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1497
- m_ctx.lineTo(m_canvas.width - parseInt(m_chartCss['margin-right']), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
1498
- m_ctx.stroke();
1499
- m_ctx.closePath();
1500
- }
1501
- var currentDate = new Date(starttime);
1502
- lastDate = new Date(new Date(starttime).toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1503
-
1504
- var p = { x: 0, y: m_canvas.height - parseInt(m_chartCss['margin-bottom']) };
1505
- var offset = 0; // timeoffset for closed hours,and weekends
1506
- var oldx = 1;
1507
- var newday = true;
1508
- var last = 0;
1509
- var nextYear = currentDate.getFullYear() + 1;
1510
- var drawDate = currentDate;
1511
- var lastDrawX = -100;
1512
- var datum;
1513
- var date;
1514
- var y, x;
1515
- while (p.x < (_this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) - (_this.scaleinfoX.itemwidth / 2))) { // && oldx != p.x) {
1516
- var dayChange = false;
1517
- if (currentDate.getTime() > endtime) {
1518
- break;
1519
- }
1520
- oldx = p.x;
1521
- p.x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1522
- var dash = false;
1523
- if (newday) {
1524
- //datum = currentDate.getDate() + '/' + (currentDate.getMonth() + 1); // format date
1525
- date = currentDate.getDate() + '/' + (currentDate.getMonth() + 1); // format date
1526
- dash = true;
1527
- dayChange = true;
1528
- }
1529
- //datum = currentDate.toTimeString().substring(0, 5); // Format time
1530
- _this.settings.timeformat = 'HH:mm'; // jonas får fixa
1531
- datum = formatChartTime(currentDate.toTimeString().substring(0, 8), _this.settings.timeformat);
1532
- drawDate = currentDate;
1533
- newday = false;
1534
- currentDate = new Date(currentDate.getTime() + _this.scaleinfoX.ticksize);
1535
- var printLegendItem = true;
1536
- var drawGridline = true;
1537
- 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
1538
- //console.log('hmm', currentDate, lastDate, _this.scaleinfoX.ticksize, newday);
1539
- currentDate = new Date((currentDate.getTime() + 86400000) - (currentDate.getTime() % 86400000));
1540
- currentDate = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
1541
-
1542
- // next day att starttime
1543
- while (currentDate.getDay() == 0 || currentDate.getDay() == 6) {
1544
- currentDate = new Date(currentDate.getTime() + 86400000);
1545
- offset += 86400000 / _this.scaleinfoX.timePerPixel;
1546
- }
1547
- //console.log(currentDate, _this.scaleinfoX.endDate);
1548
- if (currentDate.getTime() > _this.scaleinfoX.endDate.getTime()) {
1549
- break;
1550
- }
1551
- offset += (86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel;
1552
- lastDate = new Date(currentDate);
1553
- lastDate = new Date(lastDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
1554
- newday = true;
1555
- }
1556
- var fontMetrix;
1557
- var nextx = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset);
1558
- //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)
1559
- console.log(nextx, p.x, currentDate);
1560
- 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)
1561
- //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));
1562
- if (nextx < _this.scaleinfoX.lineLength + parseInt(m_chartCss['margin-left']) - (_this.scaleinfoX.itemwidth / 1)) { // is it too close to next?
1563
- //console.log('HIDE');
1564
- printLegendItem = false;
1565
- } else
1566
- if (p.x - lastDrawX < Math.floor(_this.scaleinfoX.itemwidth)) { // is it to close to previous?
1567
- // console.log('to close to previous',dash,datum);
1568
- printLegendItem = false;
1569
- }
1570
- if (dash) {
1571
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1572
- }
1573
- } else
1574
- if (p.x - lastDrawX < Math.floor(_this.scaleinfoX.itemwidth)) { // to close to previous?
1575
- //console.log('to close to previous',dash,datum);
1576
- printLegendItem = false;
1577
- if (dash) {
1578
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1579
- printLegendItem = true;
1580
- }
1581
- } else {
1582
- if (dash && _this.settings.intradaydates) { // lutande text
1583
- m_ctx.save();
1584
- fontMetrix = m_ctx.measureText(date);
1585
- x = p.x + fontMetrix.actualBoundingBoxAscent + fontMetrix.actualBoundingBoxDescent + 2;
1586
- y = parseInt(m_chartCss['margin-top']); // + m_ctx.measureText(datum).width;
1587
- m_ctx.translate(x, y);
1588
- m_ctx.rotate(90 * Math.PI / 180);
1589
- m_ctx.fillText(date, 0, 0);
1590
- m_ctx.restore();
1591
- }
1592
- if (drawGridline) {
1593
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1594
- drawGridline = false;
1595
- }
1596
- }
1597
- if (printLegendItem) {
1598
- last = p.x;
1599
- //console.log(newday,datum,dayChange);
1600
- if (dayChange) {
1601
- m_ctx.save();
1602
- fontMetrix = m_ctx.measureText(date);
1603
- x = p.x + fontMetrix.actualBoundingBoxAscent + fontMetrix.actualBoundingBoxDescent + 2;
1604
- y = parseInt(m_chartCss['margin-top']); // + m_ctx.measureText(datum).width;
1605
- m_ctx.translate(x, y);
1606
- m_ctx.rotate(90 * Math.PI / 180);
1607
- m_ctx.fillText(date, 0, 0);
1608
- m_ctx.restore();
1609
- //m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), parseInt(m_chartCss['margin-top']) + 5);
1610
- if (drawGridline) {
1611
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1612
- }
1613
- lastDrawX = p.x;
1614
- }
1615
- //else
1616
- if (p.x < m_canvas.width - m_ctx.measureText(datum).width) { // if text will be cropped do not print
1617
- m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1618
- if (drawGridline) {
1619
- drawXAxisGridlines(p, p.x == parseInt(m_chartCss['margin-left']) ? false : dash);
1620
- }
1621
- lastDrawX = p.x;
1622
- } else {
1623
- //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);
1624
- }
1625
- }
1359
+ } else {
1360
+ drawXAxisGridlines({ 'x': x, y: m_chartspaces.chart.bottom }, false);
1626
1361
  }
1627
- // print end x legend if not to close to last
1628
- /* p.x = parseInt(m_chartCss['margin-left']) + _this.scaleinfoX.lineLength; // - (getStringWidth(m_ctx, 'HH:MM'));
1629
- if (p.x - last > (Math.floor(_this.scaleinfoX.itemwidth))) {
1630
- var datum = _this.scaleinfoX.endDate.toTimeString().substring(0, 5);
1631
- m_ctx.fillText(datum, p.x - (m_ctx.measureText(datum).width / 2), m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 10);
1632
- }
1633
-
1634
- */
1635
- if (typeof m_chartCss['box-shadow'] !== 'undefined') {
1636
- var boxshadow = m_chartCss['box-shadow'].split(' ');
1637
- if (parseInt(boxshadow[1]) == 0) drawXAxisGridlines(p, false);
1638
-
1639
- } //else
1640
- //drawXAxisGridlines(p, false);
1641
-
1642
1362
  m_ctx.restore();
1643
- return;
1644
1363
  }
1645
1364
 
1646
1365
  function onMouseOut(evt) {
@@ -1684,15 +1403,15 @@ function Milli_Chart(settings) {
1684
1403
  m_canvas.parentNode.appendChild(m_zoom.div);
1685
1404
  m_zoom.div.setAttribute('class', 'millistream-chart-zoombox');
1686
1405
  }
1687
- if (x < parseInt(m_chartCss['margin-left'])) x = parseInt(m_chartCss['margin-left']);
1688
- m_zoom.div.style.top = parseInt(m_chartCss['margin-top']) + 'px';
1406
+ if (x < m_chartspaces.chart.left) x = m_chartspaces.chart.left;
1407
+ m_zoom.div.style.top = m_chartspaces.chart.top + 'px';
1689
1408
  m_zoom.div.style.left = (m_zoom.mousedown.pos > x ? x : m_zoom.mousedown.pos) + 'px';
1690
- if (x > m_canvas.width - parseInt(m_chartCss['margin-right'])) x = m_canvas.width - parseInt(m_chartCss['margin-right']);
1691
- m_zoom.div.style.height = m_canvas.height - parseInt(m_chartCss['margin-bottom']) - parseInt(m_chartCss['margin-top']) + 'px';
1409
+ if (x > m_chartspaces.chart.right) x = m_chartspaces.chart.right;
1410
+ m_zoom.div.style.height = m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) - m_chartspaces.chart.top + 'px';
1692
1411
  m_zoom.div.style.width = (m_zoom.mousedown.pos > x ? m_zoom.mousedown.pos - x : x - m_zoom.mousedown.pos) + 'px';
1693
1412
  }
1694
1413
  if (_this.settings.enablehover == false) return;
1695
- 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'])) {
1414
+ 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'])) {
1696
1415
  onMouseOut();
1697
1416
  return;
1698
1417
  }
@@ -1845,8 +1564,8 @@ function Milli_Chart(settings) {
1845
1564
 
1846
1565
  function setTimeSpanData() {
1847
1566
  if (typeof _this.settings.timespanelement === 'undefined' || _this.settings.timespanelement == null) return;
1848
- startdate = new Date(_this.scaleinfoX.startTimeStamp);
1849
- enddate = new Date(_this.scaleinfoX.endTimeStamp);
1567
+ var startdate = new Date(_this.scaleinfoX.startTimeStamp);
1568
+ var enddate = new Date(_this.scaleinfoX.endTimeStamp);
1850
1569
  if (startdate.getTime() - (startdate.getTime() % 86400000) == enddate.getTime() - (enddate.getTime() % 86400000)) {
1851
1570
  _this.settings.timespanelement.innerHTML = startdate.toTimeString().substring(0, 8) + ' - ' + enddate.toTimeString().substring(0, 8);
1852
1571
  } else {
@@ -1906,42 +1625,22 @@ function Milli_Chart(settings) {
1906
1625
  }
1907
1626
  }
1908
1627
 
1909
- function checkTickChartData() {
1910
- var count = 0;
1911
- for (var i = 0; i < _this.instruments[0].trades.length; i++) {
1912
- if (_this.instruments[0].trades[i].timestamp > _this.scaleinfoX.startTimeStamp && _this.instruments[0].trades[i].timestamp < _this.scaleinfoX.endTimeStamp) {
1913
- if (count++ > 0) {
1914
- return true;
1915
- }
1916
- }
1917
- }
1918
- var y = (m_canvas.height - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-top']);
1919
- var x = (m_canvas.width - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-left']);
1920
- m_ctx.save();
1921
- m_ctx.font = 'bold ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1922
- m_ctx.font = 'bold 12px ' + m_xLegendCss['font-family'];
1923
- m_ctx.strokeStyle = m_gridHorizontalCss.color;
1924
- m_ctx.fillStyle = m_xLegendCss.color;
1925
- m_ctx.textAlign = "left";
1926
-
1927
- m_ctx.fillText(_this.settings.nochartlabel, x - (m_ctx.measureText(_this.settings.nochartlabel).width / 2), y);
1928
- m_ctx.restore();
1929
- return false;
1930
- }
1931
-
1932
1628
  function checkChartData(data) {
1933
1629
  // _this.instruments[0].history / _this.instruments[0].trades
1934
1630
  var count = 0;
1935
1631
  for (var i = 0; i < data.length; i++) {
1936
- if (data[i].timestamp > _this.scaleinfoX.startTimeStamp && data[i].timestamp < _this.scaleinfoX.endTimeStamp) {
1632
+ if (data[i].timestamp >= _this.scaleinfoX.startTimeStamp && data[i].timestamp <= _this.scaleinfoX.endTimeStamp) {
1937
1633
  //console.log(new Date(data[i].timestamp), new Date(_this.scaleinfoX.startTimeStamp), new Date(_this.scaleinfoX.endTimeStamp), data[i]);
1938
1634
  if (count++ > 0) {
1939
1635
  return true;
1940
1636
  }
1941
1637
  }
1942
1638
  }
1943
- var y = (m_canvas.height - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-top']);
1944
- var x = (m_canvas.width - parseInt(m_chartCss['margin-bottom'])) / 2 + parseInt(m_chartCss['margin-left']);
1639
+ console.log('NO data to draw on');
1640
+ // var y = (m_canvas.height - parseInt(m_chartCss['margin-bottom'])) / 2 + m_chartspaces.chart.top;
1641
+ // var x = (m_canvas.width - parseInt(m_chartCss['margin-bottom'])) / 2 + m_chartspaces.chart.left;
1642
+ var y = m_chartspaces.chart.bottom - m_chartspaces.chart.top / 2 + m_chartspaces.chart.top;
1643
+ var x = m_chartspaces.chart.right - m_chartspaces.chart.left / 2 + m_chartspaces.chart.left;
1945
1644
  m_ctx.save();
1946
1645
  m_ctx.font = 'bold ' + m_xLegendCss['font-size'] + ' ' + m_xLegendCss['font-family'];
1947
1646
  m_ctx.font = 'bold 12px ' + m_xLegendCss['font-family'];
@@ -1954,8 +1653,36 @@ function Milli_Chart(settings) {
1954
1653
  return false;
1955
1654
  }
1956
1655
 
1656
+ function calcChartSpaces() {
1657
+ m_chartspaces.chart.height = m_canvas.height * (m_chartspaces.chart.percent / 100);
1658
+ m_chartspaces.chart.top = parseInt(m_chartCss['margin-top']);
1659
+ m_chartspaces.chart.left = parseInt(m_chartCss['margin-left']);
1660
+ m_chartspaces.chart.right = m_canvas.width - parseInt(m_chartCss['margin-right']);
1661
+ m_chartspaces.chart.bottom = m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']);
1662
+ m_chartspaces.chart.width = m_canvas.width;
1663
+ m_chartspaces.lowerChart.height = m_canvas.height * (m_chartspaces.lowerChart.percent / 100);
1664
+ m_chartspaces.lowerChart.top = m_chartspaces.chart.bottom + parseInt(m_chartCss['margin-bottom']) + m_chartspaces.chart.top;
1665
+ m_chartspaces.lowerChart.left = m_chartspaces.chart.left;
1666
+ m_chartspaces.lowerChart.right = m_chartspaces.chart.right;
1667
+ //m_chartspaces.lowerChart.bottom =m_chartspaces.lowerChart.top +m_chartspaces.lowerChart.height - parseInt(m_chartCss['margin-bottom']);
1668
+ m_chartspaces.lowerChart.bottom = m_canvas.height - parseInt(m_chartCss['margin-bottom']);
1669
+ m_chartspaces.lowerChart.width = m_canvas.width;
1670
+ console.log(m_chartspaces);
1671
+ }
1672
+
1957
1673
  _this.drawChart = function() {
1674
+ if (m_lastDrawnInstrument != _this.instruments[0].insref) {
1675
+ m_zoom.mousedown.pos = 0;
1676
+ m_zoom.mousedown.i = 0;
1677
+ m_zoom.mousedown.timestamp = null;
1678
+ m_zoom.mouseup.timestamp = null;
1679
+ if (m_zoom.div) m_canvas.parentNode.removeChild(m_zoom.div);
1680
+ m_zoom.div = null;
1681
+ m_zoom.isZooming = false;
1682
+ }
1683
+ m_lastDrawnInstrument = _this.instruments[0].insref;
1958
1684
  if (m_ctx == null) return;
1685
+ //console.clear();
1959
1686
  m_datapoints = [];
1960
1687
  if (m_dataPoints.map) m_dataPoints.map.clear();
1961
1688
  m_dataPoints.map = new Map();
@@ -1964,6 +1691,8 @@ function Milli_Chart(settings) {
1964
1691
  console.log('no chartdata');
1965
1692
  return;
1966
1693
  }
1694
+ calcChartSpaces();
1695
+
1967
1696
  var period = _this.settings.chartlen.substring(_this.settings.chartlen.length - 1);
1968
1697
  var len = parseInt(_this.settings.chartlen.substring(0, _this.settings.chartlen.length - 1));
1969
1698
  m_ctx.clearRect(0, 0, m_canvas.width, m_canvas.height);
@@ -1978,11 +1707,8 @@ function Milli_Chart(settings) {
1978
1707
  _this.scaleinfoX.endTimeStamp = m_zoom.mouseup.timestamp > m_zoom.mousedown.timestamp ? m_zoom.mouseup.timestamp : m_zoom.mousedown.timestamp;
1979
1708
  _this.scaleinfoX.startTimeStamp = m_zoom.mouseup.timestamp < m_zoom.mousedown.timestamp ? m_zoom.mouseup.timestamp : m_zoom.mousedown.timestamp;
1980
1709
  } else {
1981
- //console.log('len', len);
1982
1710
  _this.scaleinfoX.startTimeStamp = getTickStartDate(len); // calc not to start with weekends
1983
1711
  // back up x days from quotedate if needed
1984
- //console.log(new Date(_this.scaleinfoX.startTimeStamp), new Date(_this.instruments[0].quotedate));
1985
- //console.log(new Date(_this.scaleinfoX.startTimeStamp), new Date(_this.instruments[0].quotedate - (86400000 * (len - 1))));
1986
1712
  // TODO if startTimeStamp < quotedate -len borde det vara
1987
1713
  var quoteDate = businessDaysSubtraction(_this.instruments[0].quotedate, len);
1988
1714
  if (_this.scaleinfoX.startTimeStamp - _this.scaleinfoX.startTimeStamp % 86400000 > quoteDate) { // if startdate > quotedate back startdate
@@ -2116,36 +1842,44 @@ function Milli_Chart(settings) {
2116
1842
  //plotChart(_this.instruments[0].history, null);
2117
1843
 
2118
1844
  }
2119
- drawBoxShadow(m_canvas, m_ctx);
2120
- if (m_modules.quantity) {
2121
- m_modules.quantity.redraw();
1845
+ drawBoxShadow(m_chartspaces.chart);
1846
+ if (m_chartspaces.chart.percent != 100) {
1847
+ drawXAxisTickLowerChart();
1848
+ drawYAxisLower();
1849
+ drawBoxShadow(m_chartspaces.lowerChart);
2122
1850
  }
2123
1851
  onMouseOut();
2124
1852
  };
2125
1853
 
2126
- function drawBoxShadow(canvas, ctx) {
2127
- if (typeof m_chartCss['box-shadow'] === 'undefined') return;
2128
- var boxshadow = m_chartCss['box-shadow'].split(' ');
2129
- ctx.save();
1854
+ function drawBoxShadow(space) {
1855
+ console.log(space);
1856
+ if (typeof m_chartCss.boxShadow === 'undefined') return;
1857
+ var boxShadow = m_chartCss.boxShadow;
1858
+ if (boxShadow.indexOf(')') != -1) {
1859
+ boxShadow = boxShadow.substring(boxShadow.indexOf(')') + 2);
1860
+ }
1861
+ var boxshadow = boxShadow.split(' ');
1862
+ m_ctx.save();
2130
1863
  if (parseInt(boxshadow[0]) > 0) {
2131
- ctx.beginPath();
2132
- ctx.strokeStyle = m_chartCss['border-top-color'];
2133
- ctx.lineWidth = parseInt(boxshadow[1]);
2134
- ctx.moveTo(parseInt(m_chartCss['margin-left']), parseInt(m_chartCss['margin-top']));
2135
- ctx.lineTo(canvas.width - parseInt(m_chartCss['margin-right']), parseInt(m_chartCss['margin-top']));
2136
- ctx.stroke();
2137
- ctx.closePath();
1864
+ m_ctx.beginPath();
1865
+ m_ctx.strokeStyle = m_chartCss['border-top-color'];
1866
+ m_ctx.lineWidth = parseInt(boxshadow[1]);
1867
+ m_ctx.moveTo(space.left, space.top);
1868
+ m_ctx.lineTo(space.right, space.top);
1869
+ m_ctx.stroke();
1870
+ m_ctx.closePath();
2138
1871
  }
2139
1872
  if (parseInt(boxshadow[1]) > 0) {
2140
- ctx.beginPath();
2141
- ctx.strokeStyle = m_chartCss['border-right-color'];
2142
- ctx.lineWidth = parseInt(boxshadow[1]);
2143
- ctx.moveTo(canvas.width - parseInt(m_chartCss['margin-right']), parseInt(m_chartCss['margin-top']) + 0.5);
2144
- ctx.lineTo(canvas.width - parseInt(m_chartCss['margin-right']), canvas.height - parseInt(m_chartCss['margin-bottom']));
2145
- ctx.stroke();
2146
- ctx.closePath();
1873
+ m_ctx.beginPath();
1874
+ m_ctx.strokeStyle = m_chartCss['border-right-color'];
1875
+ m_ctx.lineWidth = parseInt(boxshadow[1]);
1876
+ m_ctx.moveTo(space.right, space.top + 0.5);
1877
+ //m_ctx.lineTo(spaces.chart.right, spaces.chart.height - parseInt(m_chartCss['margin-bottom']));
1878
+ m_ctx.lineTo(space.right, space.bottom);
1879
+ m_ctx.stroke();
1880
+ m_ctx.closePath();
2147
1881
  }
2148
- ctx.restore();
1882
+ m_ctx.restore();
2149
1883
  }
2150
1884
 
2151
1885
  function parseData(data, factor) {
@@ -2239,39 +1973,50 @@ function Milli_Chart(settings) {
2239
1973
  }
2240
1974
 
2241
1975
  _this.addQuantity = function() {
2242
- if (m_modules.quantity == null)
2243
- m_modules.quantity = new MilliChart_Quantity(_this, m_canvas);
1976
+ if (m_chartspaces.chart.percent == 100) {
1977
+ m_chartspaces.chart.percent = 70;
1978
+ m_chartspaces.lowerChart.percent = 30;
1979
+ } else {
1980
+ m_chartspaces.chart.percent = 100;
1981
+ m_chartspaces.lowerChart.percent = 0;
1982
+ _this.drawChart();
1983
+ return;
1984
+ }
1985
+ /*if (m_modules.quantity == null)
1986
+ m_modules.quantity = new MilliChart_Quantity(_this, m_canvas);*/
1987
+ _this.drawChart();
2244
1988
  };
2245
1989
 
2246
1990
  _this.resizeStart = function(e, ui) {
2247
- m_resizing.resizing = true;
2248
1991
  m_resizing.width = m_canvas.width;
2249
1992
  m_resizing.height = m_canvas.height;
2250
1993
  };
2251
1994
 
2252
1995
  _this.resize = function(e, ui) {
2253
- var diff = (m_resizing.height - _this.settings.target.offsetHeight) / _this.settings.target.offsetHeight * 100;
2254
- if (Math.abs(diff) > 10) {
2255
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight + 'px');
2256
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth + 'px'); // läs från css?
1996
+ var diff = (m_resizing.height - m_canvas.height) / m_canvas.height * 100;
1997
+ if (Math.abs(diff) > 1) {
1998
+ setChartSize();
1999
+ m_resizing.width = m_canvas.width;
2000
+ m_resizing.height = m_canvas.height;
2257
2001
  _this.drawChart();
2258
2002
  } else {
2259
- diff = (m_resizing.width - _this.settings.target.offsetWidth) / _this.settings.target.offsetWidth * 100;
2260
- if (Math.abs(diff) > 10) {
2261
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight + 'px');
2262
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth + 'px'); // läs från css?
2003
+ diff = (m_resizing.width - m_canvas.width) / m_canvas.width * 100;
2004
+ if (Math.abs(diff) > 1) {
2005
+ setChartSize();
2006
+ m_canvas.height = _this.settings.target.offsetHeight;
2007
+ m_canvas.width = _this.settings.target.offsetWidth;
2008
+ m_resizing.width = m_canvas.width;
2009
+ m_resizing.height = m_canvas.height;
2263
2010
  _this.drawChart();
2264
2011
  }
2265
2012
  }
2266
- m_resizing.width = m_canvas.width;
2267
- m_resizing.height = m_canvas.height;
2013
+ var cache = m_ctx.getImageData(0, 0, m_canvas.width, m_canvas.height);
2014
+ setChartSize();
2015
+ m_ctx.putImageData(cache, 0, 0);
2268
2016
  };
2269
2017
 
2270
2018
  _this.resizeEnd = function(e, ui) {
2271
- m_resizeing = false;
2272
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight + 'px');
2273
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth + 'px'); // läs från css?
2274
- console.log('Width:', _this.settings.target.offsetWidth);
2019
+ setChartSize();
2275
2020
  _this.drawChart();
2276
2021
  };
2277
2022
 
@@ -2300,7 +2045,7 @@ function Milli_Chart(settings) {
2300
2045
  currentDate.setSeconds(lastdate.getSeconds());
2301
2046
 
2302
2047
  if (lastdate.toISOString().substring(0, 10) != currentDate.toISOString().substring(0, 10)) { // new date
2303
- tmp = new Date(lastdate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
2048
+ var tmp = new Date(lastdate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketclose + 'Z');
2304
2049
  var nextDate = new Date(data[i].timestamp);
2305
2050
  tmp = tmp.getTime() + 86400000 - _this.scaleinfoX.milliPerDay; // increase to next days starttime
2306
2051
  currentDate = new Date(tmp);
@@ -2313,14 +2058,15 @@ function Milli_Chart(settings) {
2313
2058
  }
2314
2059
  offset += ((86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel); // * dateDiffInDays(lastdate, currentDate);
2315
2060
  lastdate = currentDate;
2316
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2061
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2317
2062
  // TODO: här blir det fel när det är från 00:00: 23:59 men göms av tmpx < startpoint.x
2318
2063
 
2319
2064
  }
2320
- startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2065
+ //startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2066
+ 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;
2321
2067
  var maxy = maxy > startpoint.y ? maxy : startpoint.y;
2322
2068
 
2323
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2069
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2324
2070
  startpoint.x -= offset;
2325
2071
  m_ctx.beginPath();
2326
2072
  m_ctx.arc(startpoint.x, startpoint.y - 20, 10, 0, 2 * Math.PI, false);
@@ -2386,11 +2132,13 @@ function Milli_Chart(settings) {
2386
2132
  currentDate = new Date(data[i].timestamp);
2387
2133
  offset = ((86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel) * dateDiffInDays(new Date(startDate), currentDate);
2388
2134
  tmp = new Date(currentDate.toISOString().substring(0, 10) + 'T' + _this.instruments[0].marketopen + 'Z');
2389
- endpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((tmp.getTime() - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2390
- if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') // plot the closeprice1d
2391
- 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;
2392
- else {
2393
- endpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(lastItem.price) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2135
+ endpoint.x = Math.round(m_chartspaces.chart.left + ((tmp.getTime() - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2136
+ if (_this.settings.chartlen == '1d' || _this.settings.chartlen == '0d') { // plot the closeprice1d
2137
+ //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;
2138
+ 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;
2139
+ } else {
2140
+ //endpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((parseFloat(lastItem.price) * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2141
+ 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;
2394
2142
  // do not add closeprice to hover array
2395
2143
  /*
2396
2144
  var x = Math.round(endpoint.x);
@@ -2435,22 +2183,20 @@ function Milli_Chart(settings) {
2435
2183
  offset += ((86400000 - _this.scaleinfoX.milliPerDay) / _this.scaleinfoX.timePerPixel); // * dateDiffInDays(lastdate, currentDate);
2436
2184
  lastdate = currentDate;
2437
2185
  //console.log(startpoint.x);
2438
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2186
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - startDate) / _this.scaleinfoX.timePerPixel)) + 0.5 - offset;
2439
2187
  //console.log(startpoint.x);
2440
2188
 
2441
2189
  // TODO: här blir det fel när det är från 00:00: 23:59 men göms av tmpx < startpoint.x
2442
2190
  if (_this.scaleinfoY.type == 'trades' && tmpx < startpoint.x) {
2443
2191
  m_ctx.lineTo(startpoint.x, startpoint.y);
2444
2192
  }
2445
-
2446
2193
  //m_ctx.lineTo(startpoint.x, startpoint.y);
2447
-
2448
2194
  }
2449
- //p.x = Math.round(parseInt(m_chartCss['margin-left']) + ((currentDate.getTime() - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel) - offset); // from drawXaxis
2450
- startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2195
+ //startpoint.y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((data[i].price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2196
+ 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;
2451
2197
  maxy = maxy > startpoint.y ? maxy : startpoint.y;
2452
2198
 
2453
- startpoint.x = Math.round(parseInt(m_chartCss['margin-left']) + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2199
+ startpoint.x = Math.round(m_chartspaces.chart.left + ((data[i].timestamp - _this.scaleinfoX.startDate.getTime()) / _this.scaleinfoX.timePerPixel)) + 0.5;
2454
2200
  startpoint.x -= offset;
2455
2201
  if (startpoint.x != endpoint.x || startpoint.y != endpoint.y) {
2456
2202
  var x = Math.round(startpoint.x);
@@ -2474,7 +2220,7 @@ function Milli_Chart(settings) {
2474
2220
  //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"
2475
2221
 
2476
2222
  if (instrument == 0) {
2477
- 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) };
2223
+ 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 };
2478
2224
  if (typeof data[i].dividend !== 'undefined') point.dividend = data[i].dividend;
2479
2225
  if (typeof data[i].diff !== 'undefined') point.diff = data[i].diff;
2480
2226
  m_datapoints.push(point);
@@ -2488,19 +2234,19 @@ function Milli_Chart(settings) {
2488
2234
  if (_this.settings.hcurve && _this.scaleinfoY.type == 'history') {
2489
2235
  if (isToday(currentDate)) {
2490
2236
  // only 1 point in hcurve chart, draw line from start of chart to end date
2491
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), startpoint.y);
2237
+ m_ctx.moveTo(m_chartspaces.chart.left, startpoint.y);
2492
2238
  m_ctx.lineTo(startpoint.x, startpoint.y);
2493
- 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 };
2239
+ 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 };
2494
2240
  m_datapoints.push(point);
2495
2241
  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 };
2496
2242
  m_datapoints.push(point);
2497
- startx = parseInt(m_chartCss['margin-left']);
2243
+ startx = m_chartspaces.chart.left;
2498
2244
  starty = startpoint.y;
2499
2245
  // last point so break out and store startx and starty from fake point
2500
2246
  break;
2501
2247
  } else if (hCurveLastPoint) {
2502
2248
  /* var y = Math.round(m_canvas.height - parseInt(m_chartCss['margin-bottom']) - (((hCurveLastPoint.price * factor) - _this.scaleinfoY.minValue) * _this.scaleinfoY.valuePerPixel)) + 0.5;
2503
- m_ctx.moveTo(parseInt(m_chartCss['margin-left']), y);
2249
+ m_ctx.moveTo(m_chartspaces.chart.left, y);
2504
2250
  m_ctx.lineTo(startpoint.x, y);*/
2505
2251
  }
2506
2252
  }
@@ -2509,11 +2255,9 @@ function Milli_Chart(settings) {
2509
2255
  startx = startpoint.x;
2510
2256
  starty = startpoint.y;
2511
2257
  }
2512
- //console.log('in', startpoint.x);
2513
2258
  endpoint.x = startpoint.x;
2514
2259
  endpoint.y = startpoint.y;
2515
2260
  }
2516
- // console.log('div', data[i]);
2517
2261
  if (data[i].dividend) {
2518
2262
  //console.log('div', data[i]);
2519
2263
  }
@@ -2523,9 +2267,10 @@ function Milli_Chart(settings) {
2523
2267
  });
2524
2268
  m_ctx.stroke();
2525
2269
  if (_this.settings.fillchart == true && instrument == 0) {
2526
- //console.log(startpoint.x, endpoint.x);
2527
- m_ctx.lineTo(startpoint.x, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2528
- m_ctx.lineTo(startx, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2270
+ // m_ctx.lineTo(startpoint.x, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2271
+ // m_ctx.lineTo(startx, m_canvas.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2272
+ m_ctx.lineTo(startpoint.x, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2273
+ m_ctx.lineTo(startx, m_chartspaces.chart.height - parseInt(m_chartCss['margin-bottom']) + 0.5);
2529
2274
  m_ctx.lineTo(startx, starty);
2530
2275
  m_ctx.closePath();
2531
2276
  if (true == m_instrumentCss[0]['background-image'].startsWith('linear-gradient')) {
@@ -2537,7 +2282,7 @@ function Milli_Chart(settings) {
2537
2282
  col0 = col0.substring(0, col0.lastIndexOf(','));
2538
2283
  col1 = colors.substring(colors.lastIndexOf('rgba'), colors.lastIndexOf(')'));
2539
2284
  }
2540
- var grd = m_ctx.createLinearGradient(0, 0, 0, m_canvas.height - parseInt(m_chartCss['margin-top']) - parseInt(m_chartCss['margin-bottom']));
2285
+ var grd = m_ctx.createLinearGradient(0, 0, 0, m_chartspaces.chart.height - m_chartspaces.chart.top - parseInt(m_chartCss['margin-bottom']));
2541
2286
  grd.addColorStop(0, col0);
2542
2287
  grd.addColorStop(1, col1);
2543
2288
  m_ctx.fillStyle = grd;
@@ -2634,6 +2379,16 @@ function Milli_Chart(settings) {
2634
2379
  return 1;
2635
2380
  };
2636
2381
 
2382
+ function setChartSize() {
2383
+ var offset = 0;
2384
+ if (_this.settings.target.parentNode.classList.contains('chartcontainer')) {
2385
+ offset = _this.settings.target.getBoundingClientRect().top - _this.settings.target.parentNode.getBoundingClientRect().top;
2386
+ }
2387
+ _this.settings.target.style.height = (_this.settings.target.parentNode.offsetHeight - offset) + 'px';
2388
+ _this.settings.target.style.width = _this.settings.target.parentNode.offsetWidth + 'px';
2389
+ m_canvas.height = _this.settings.target.offsetHeight;
2390
+ m_canvas.width = _this.settings.target.offsetWidth;
2391
+ }
2637
2392
  _this.buildwidget = function(resp) {
2638
2393
  //console.log('buildwidget', resp);
2639
2394
  parseData(resp[0], 1);
@@ -2651,13 +2406,8 @@ function Milli_Chart(settings) {
2651
2406
  if (m_canvas == null) {
2652
2407
  m_canvas = MillistreamWidgetApi_addElement(_this, 'canvas', 'millistream-chart-canvas', _this.settings.target);
2653
2408
 
2654
- //m_canvas.style.width = '100%';
2655
- //m_canvas.style.height = '100%';
2656
- //m_canvas.setAttribute('height', m_canvas.offsetHeight);
2657
- //m_canvas.setAttribute('width', m_canvas.offsetWidth); // läs från css?
2658
- m_canvas.setAttribute('height', _this.settings.target.offsetHeight);
2659
- //m_canvas.setAttribute('height', 300);
2660
- m_canvas.setAttribute('width', _this.settings.target.offsetWidth); // läs från css?
2409
+ setChartSize();
2410
+
2661
2411
  m_ctx = m_canvas.getContext("2d");
2662
2412
  m_canvas.addEventListener('mousemove', onMouseMove, false); // disable while loading and enable on drawReady
2663
2413
  m_canvas.addEventListener('mouseout', onMouseOut, false);
@@ -2683,9 +2433,13 @@ function Milli_Chart(settings) {
2683
2433
  if (m_datapoints.length == 0 || m_zoom.mousedown.timestamp == null) return;
2684
2434
  var rect = m_canvas.getBoundingClientRect();
2685
2435
  var x = evt.clientX - rect.left - 1;
2436
+ if (x < m_zoom.mousedown.pos) {
2437
+ clearZoom();
2438
+ return;
2439
+ }
2686
2440
  if (Math.abs(x - m_zoom.mousedown.pos) > 5) { // only handle zoom with 5 pixels width
2687
2441
  var i;
2688
- for (i = m_datapoints.length - 1; i > 0; i--) { // -1??
2442
+ for (i = m_datapoints.length - 1; i > 0; i--) {
2689
2443
  if (x >= m_datapoints[i].x) {
2690
2444
  m_zoom.end = x;
2691
2445
  break;
@@ -2761,7 +2515,8 @@ function Milli_Chart(settings) {
2761
2515
  m_chartCss['margin-bottom'] = properties.marginBottom;
2762
2516
  m_chartCss['margin-left'] = properties.marginLeft;
2763
2517
  m_chartCss['margin-right'] = properties.marginRight;
2764
- m_chartCss['box-shadow'] = properties.boxShadow;
2518
+ //m_chartCss['box-shadow'] = properties.boxShadow;
2519
+ m_chartCss.boxShadow = properties.boxShadow;
2765
2520
  m_chartCss['border-right-color'] = properties.borderRightColor;
2766
2521
  m_chartCss['border-left-color'] = properties.borderLeftColor;
2767
2522
 
@@ -2957,7 +2712,6 @@ function Milli_Chart(settings) {
2957
2712
  changeOrientation();
2958
2713
 
2959
2714
  });
2960
-
2961
2715
  }
2962
2716
  var milli_data_api_url = 'https://stage.millistream.com/widgets/3.0.3/data/milli_widget_dataapi.php?';
2963
2717
 
@@ -3605,7 +3359,7 @@ function print_field(widget, element, field, value, overridedecimals) {
3605
3359
  }
3606
3360
  }
3607
3361
  if (typeof widget.settings.translations !== 'undefined' && widget.translateTypes) {
3608
- element.innerHTML = widget.translateTypes(value);
3362
+ element.innerHTML = widget.translateTypes(value, field);
3609
3363
  return element.innerHTML;
3610
3364
  }
3611
3365
 
@@ -5532,4 +5286,5 @@ function MillistreamWidgetStreamingApi(settings) {
5532
5286
  }
5533
5287
  exports.Milli_Chart = Milli_Chart;
5534
5288
  exports.MillistreamWidgetSettings = MillistreamWidgetSettings;
5289
+ exports.milli_data_api_url = milli_data_api_url;
5535
5290
  exports.MillistreamWidgetStreamingApi = MillistreamWidgetStreamingApi;