maidr 2.0.0 → 2.0.1

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.
package/dist/maidr.js CHANGED
@@ -2982,8 +2982,8 @@ class Display {
2982
2982
 
2983
2983
  constants.brailleInput.setSelectionRange(adjustedPos, adjustedPos);
2984
2984
  } else if (
2985
- singleMaidr.type == 'point' ||
2986
- singleMaidr.type.includes('point')
2985
+ singleMaidr.type == 'smooth' ||
2986
+ singleMaidr.type.includes('smooth')
2987
2987
  ) {
2988
2988
  constants.brailleInput.setSelectionRange(positionL1.x, positionL1.x);
2989
2989
  }
@@ -3180,8 +3180,8 @@ class Display {
3180
3180
  else if (constants.textMode == 'terse')
3181
3181
  output = '<p>' + textTerse + '</p>\n';
3182
3182
  } else if (
3183
- singleMaidr.type == 'point' ||
3184
- singleMaidr.type.includes('point')
3183
+ [].concat(singleMaidr.type).includes('point') ||
3184
+ [].concat(singleMaidr.type).includes('smooth')
3185
3185
  ) {
3186
3186
  if (constants.chartType == 'point') {
3187
3187
  // point layer
@@ -5493,7 +5493,7 @@ class ScatterPlot {
5493
5493
  }
5494
5494
  } else if (singleMaidr.type == 'smooth') {
5495
5495
  if ('selector' in singleMaidr) {
5496
- this.plotLine = document.querySelectorAll(singleMaidr.selector);
5496
+ this.plotLine = document.querySelectorAll(singleMaidr.selector)[0];
5497
5497
  } else if ('elements' in singleMaidr) {
5498
5498
  this.plotLine = singleMaidr.elements;
5499
5499
  }
@@ -5507,8 +5507,14 @@ class ScatterPlot {
5507
5507
  this.curveX = smoothCurvePoints[0]; // actual values of x
5508
5508
  this.curvePoints = smoothCurvePoints[1]; // actual values of y
5509
5509
 
5510
- this.curveMinY = Math.min(...this.curvePoints);
5511
- this.curveMaxY = Math.max(...this.curvePoints);
5510
+ // if there is only point layer, then curvePoints will be empty
5511
+ if (this.curvePoints && this.curvePoints.length > 0) {
5512
+ this.curveMinY = Math.min(...this.curvePoints);
5513
+ this.curveMaxY = Math.max(...this.curvePoints);
5514
+ } else {
5515
+ this.curveMinY = Number.MAX_VALUE;
5516
+ this.curveMaxY = Number.MIN_VALUE;
5517
+ }
5512
5518
  this.gradient = this.GetGradient();
5513
5519
  }
5514
5520
 
@@ -5521,30 +5527,62 @@ class ScatterPlot {
5521
5527
 
5522
5528
  if (this.plotPoints) {
5523
5529
  for (let i = 0; i < this.plotPoints.length; i++) {
5524
- let x = parseFloat(this.plotPoints[i].getAttribute(this.prefix + 'x')); // .toFixed(1);
5525
- let y = parseFloat(this.plotPoints[i].getAttribute(this.prefix + 'y'));
5530
+ let x;
5531
+ let y;
5532
+
5533
+ // extract x, y coordinates based on the SVG element type
5534
+ if (this.plotPoints[i] instanceof SVGPathElement) {
5535
+ let pathD = this.plotPoints[i].getAttribute('d');
5536
+ let regex = /M\s*(-?\d+(\.\d+)?)\s+(-?\d+(\.\d+)?)/g;
5537
+
5538
+ let match = regex.exec(pathD);
5539
+ x = parseFloat(match[1]);
5540
+ y = parseFloat(match[3]);
5541
+ } else {
5542
+ x = parseFloat(this.plotPoints[i].getAttribute(this.prefix + 'x')); // .toFixed(1);
5543
+ y = parseFloat(this.plotPoints[i].getAttribute(this.prefix + 'y'));
5544
+ }
5545
+
5526
5546
  if (!points.has(x)) {
5527
5547
  points.set(x, new Set([y]));
5528
5548
  } else {
5529
5549
  points.get(x).add(y);
5530
5550
  }
5531
5551
  }
5532
- } else {
5552
+ } else if ([].concat(singleMaidr.type).includes('point')) {
5533
5553
  // pull from data instead
5534
5554
  let elIndex = this.GetElementIndex('point');
5535
- for (let i = 0; i < singleMaidr.data[elIndex].length; i++) {
5536
- let x;
5537
- let y;
5538
- if ('x' in singleMaidr.data[elIndex][i]) {
5539
- x = singleMaidr.data[elIndex][i]['x'];
5555
+ let xyFormat = this.GetDataXYFormat(elIndex);
5556
+ let data;
5557
+ if (elIndex > -1) {
5558
+ data = singleMaidr.data[elIndex];
5559
+ } else {
5560
+ data = singleMaidr.data;
5561
+ }
5562
+ let x = [];
5563
+ let y = [];
5564
+ if (xyFormat == 'array') {
5565
+ if ('x' in data) {
5566
+ x = data['x'];
5540
5567
  }
5541
- if ('y' in singleMaidr.data[elIndex][i]) {
5542
- y = singleMaidr.data[elIndex][i]['y'];
5568
+ if ('y' in data) {
5569
+ y = data['y'];
5543
5570
  }
5544
- if (!points.has(x)) {
5545
- points.set(x, new Set([y]));
5571
+ } else if (xyFormat == 'object') {
5572
+ for (let i = 0; i < data.length; i++) {
5573
+ let xValue = data[i]['x'];
5574
+ let yValue = data[i]['y'];
5575
+ x.push(xValue);
5576
+ y.push(yValue);
5577
+ }
5578
+ }
5579
+ for (let i = 0; i < x.length; i++) {
5580
+ let xValue = x[i];
5581
+ let yValue = y[i];
5582
+ if (!points.has(xValue)) {
5583
+ points.set(xValue, new Set([yValue]));
5546
5584
  } else {
5547
- points.get(x).add(y);
5585
+ points.get(xValue).add(yValue);
5548
5586
  }
5549
5587
  }
5550
5588
  }
@@ -5578,7 +5616,7 @@ class ScatterPlot {
5578
5616
  */
5579
5617
  GetElementIndex(elementName = 'point') {
5580
5618
  let elIndex = -1;
5581
- if ('type' in singleMaidr) {
5619
+ if ('type' in singleMaidr && Array.isArray(singleMaidr.type)) {
5582
5620
  elIndex = singleMaidr.type.indexOf(elementName);
5583
5621
  }
5584
5622
  return elIndex;
@@ -5591,12 +5629,20 @@ class ScatterPlot {
5591
5629
  */
5592
5630
  GetDataXYFormat(dataIndex) {
5593
5631
  // detect if data is in form [{x: 1, y: 2}, {x: 2, y: 3}] (object) or {x: [1, 2], y: [2, 3]]} (array)
5594
- let xyFormat = 'array';
5595
- if (singleMaidr.data[dataIndex]) {
5596
- if (Array.isArray(singleMaidr.data[dataIndex])) {
5597
- xyFormat = 'object';
5598
- }
5632
+ let data;
5633
+ if (dataIndex > -1) {
5634
+ data = singleMaidr.data[dataIndex];
5635
+ } else {
5636
+ data = singleMaidr.data;
5637
+ }
5638
+
5639
+ let xyFormat;
5640
+ if (Array.isArray(data)) {
5641
+ xyFormat = 'object';
5642
+ } else {
5643
+ xyFormat = 'array';
5599
5644
  }
5645
+
5600
5646
  return xyFormat;
5601
5647
  }
5602
5648
 
@@ -5689,8 +5735,10 @@ class ScatterPlot {
5689
5735
  GetPointValues() {
5690
5736
  let points = new Map(); // keep track of x and y values
5691
5737
 
5692
- let xValues = [];
5693
- let yValues = [];
5738
+ let X = [];
5739
+ let Y = [];
5740
+ let points_count = [];
5741
+ let max_points;
5694
5742
 
5695
5743
  // prepare to fetch data from the correct index in the correct format
5696
5744
  let elIndex = this.GetElementIndex('point');
@@ -5706,18 +5754,19 @@ class ScatterPlot {
5706
5754
  }
5707
5755
  if (typeof data !== 'undefined') {
5708
5756
  // assuming we got something, loop through the data and extract the x and y values
5709
-
5757
+ let xValues = [];
5758
+ let yValues = [];
5710
5759
  if (xyFormat == 'array') {
5711
- if ('x' in singleMaidr.data[elIndex]) {
5712
- xValues = singleMaidr.data[elIndex]['x'];
5760
+ if ('x' in data) {
5761
+ xValues = data['x'];
5713
5762
  }
5714
- if ('y' in singleMaidr.data[elIndex]) {
5715
- yValues = singleMaidr.data[elIndex]['y'];
5763
+ if ('y' in data) {
5764
+ yValues = data['y'];
5716
5765
  }
5717
5766
  } else if (xyFormat == 'object') {
5718
- for (let i = 0; i < singleMaidr.data[elIndex].length; i++) {
5719
- let x = singleMaidr.data[elIndex][i]['x'];
5720
- let y = singleMaidr.data[elIndex][i]['y'];
5767
+ for (let i = 0; i < data.length; i++) {
5768
+ let x = data[i]['x'];
5769
+ let y = data[i]['y'];
5721
5770
  xValues.push(x);
5722
5771
  yValues.push(y);
5723
5772
  }
@@ -5765,9 +5814,6 @@ class ScatterPlot {
5765
5814
  });
5766
5815
  });
5767
5816
 
5768
- let X = [];
5769
- let Y = [];
5770
- let points_count = [];
5771
5817
  for (const [x_val, y_val] of points) {
5772
5818
  X.push(x_val);
5773
5819
  let y_arr = [];
@@ -5779,12 +5825,10 @@ class ScatterPlot {
5779
5825
  Y.push(y_arr.sort());
5780
5826
  points_count.push(y_count);
5781
5827
  }
5782
- let max_points = Math.max(...points_count.map((a) => Math.max(...a)));
5783
-
5784
- return [X, Y, points_count, max_points];
5785
- } else {
5786
- return;
5828
+ max_points = Math.max(...points_count.map((a) => Math.max(...a)));
5787
5829
  }
5830
+
5831
+ return [X, Y, points_count, max_points];
5788
5832
  }
5789
5833
 
5790
5834
  /**
@@ -5833,28 +5877,54 @@ class ScatterPlot {
5833
5877
  let y_points = [];
5834
5878
 
5835
5879
  if (this.plotLine) {
5836
- // extract all the y coordinates from the point attribute of polyline
5837
- let str = this.plotLine.getAttribute('points');
5838
- let coords = str.split(' ');
5839
- for (let i = 0; i < coords.length; i++) {
5840
- let coord = coords[i].split(',');
5841
- x_points.push(parseFloat(coord[0]));
5842
- y_points.push(parseFloat(coord[1]));
5880
+ // scatterplot SVG containing path element instead of polyline
5881
+ if (this.plotLine instanceof SVGPathElement) {
5882
+ // Assuming the path data is in the format "M x y L x y L x y L x y"
5883
+ const pathD = this.plotLine.getAttribute('d');
5884
+ const regex = /[ML]\s*(-?\d+(\.\d+)?)\s+(-?\d+(\.\d+)?)/g;
5885
+
5886
+ let match;
5887
+ while ((match = regex.exec(pathD)) !== null) {
5888
+ x_points.push(match[1]); // x coordinate
5889
+ y_points.push(match[3]); // y coordinate
5890
+ }
5891
+ } else if (this.plotLine instanceof SVGPolylineElement) {
5892
+ // extract all the y coordinates from the point attribute of polyline
5893
+ let str = this.plotLine.getAttribute('points');
5894
+ let coords = str.split(' ');
5895
+
5896
+ for (let i = 0; i < coords.length; i++) {
5897
+ let coord = coords[i].split(',');
5898
+ x_points.push(parseFloat(coord[0]));
5899
+ y_points.push(parseFloat(coord[1]));
5900
+ }
5843
5901
  }
5844
- } else {
5902
+ } else if ([].concat(singleMaidr.type).includes('smooth')) {
5845
5903
  // fetch from data instead
5846
- let elIndex = this.GetElementIndex('point');
5847
- for (let i = 0; i < singleMaidr.data[elIndex].length; i++) {
5848
- if ('x' in singleMaidr.data[elIndex][i]) {
5849
- x_points.push(singleMaidr.data[elIndex][i]['x']);
5904
+ let elIndex = this.GetElementIndex('smooth');
5905
+ let xyFormat = this.GetDataXYFormat(elIndex);
5906
+ let data;
5907
+ if (elIndex > -1) {
5908
+ data = singleMaidr.data[elIndex];
5909
+ } else {
5910
+ data = singleMaidr.data
5911
+ }
5912
+ if (xyFormat == 'object') {
5913
+ for (let i = 0; i < data.length; i++) {
5914
+ x_points.push(data[i]['x']);
5915
+ y_points.push(data[i]['y']);
5916
+ }
5917
+ } else if (xyFormat == 'array') {
5918
+ if ('x' in data) {
5919
+ x_points = data['x'];
5850
5920
  }
5851
- if ('y' in singleMaidr.data[elIndex][i]) {
5852
- y_points.push(singleMaidr.data[elIndex][i]['y']);
5921
+ if ('y' in data) {
5922
+ y_points = data['y'];
5853
5923
  }
5854
5924
  }
5855
5925
  }
5856
5926
 
5857
- return [x, y];
5927
+ return [x_points, y_points];
5858
5928
  }
5859
5929
 
5860
5930
  /**
@@ -5878,23 +5948,21 @@ class ScatterPlot {
5878
5948
  }
5879
5949
  if (typeof data !== 'undefined') {
5880
5950
  if (xyFormat == 'object') {
5881
- for (let i = 0; i < singleMaidr.data[elIndex].length; i++) {
5882
- x_points.push(singleMaidr.data[elIndex][i]['x']);
5883
- y_points.push(singleMaidr.data[elIndex][i]['y']);
5951
+ for (let i = 0; i < data.length; i++) {
5952
+ x_points.push(data[i]['x']);
5953
+ y_points.push(data[i]['y']);
5884
5954
  }
5885
5955
  } else if (xyFormat == 'array') {
5886
- if ('x' in singleMaidr.data[elIndex]) {
5887
- x_points = singleMaidr.data[elIndex]['x'];
5956
+ if ('x' in data) {
5957
+ x_points = data['x'];
5888
5958
  }
5889
- if ('y' in singleMaidr.data[elIndex]) {
5890
- y_points = singleMaidr.data[elIndex]['y'];
5959
+ if ('y' in data) {
5960
+ y_points = data['y'];
5891
5961
  }
5892
5962
  }
5893
-
5894
- return [x_points, y_points];
5895
- } else {
5896
- return;
5897
5963
  }
5964
+
5965
+ return [x_points, y_points];
5898
5966
  }
5899
5967
 
5900
5968
  /**
@@ -5926,12 +5994,18 @@ class ScatterPlot {
5926
5994
  */
5927
5995
  GetRectStatus(type) {
5928
5996
  let elIndex = this.GetElementIndex(type);
5929
- if ('selector' in singleMaidr) {
5930
- return singleMaidr.selector[elIndex] ? true : false;
5931
- } else if ('elements' in singleMaidr) {
5932
- return singleMaidr.elements[elIndex] ? true : false;
5997
+ if (elIndex > -1) {
5998
+ if ('selector' in singleMaidr) {
5999
+ return !!singleMaidr.selector[elIndex];
6000
+ } else if ('elements' in singleMaidr) {
6001
+ return !!singleMaidr.elements[elIndex];
6002
+ }
5933
6003
  } else {
5934
- return false;
6004
+ if ('selector' in singleMaidr) {
6005
+ return !!singleMaidr.selector;
6006
+ } else if ('elements' in singleMaidr) {
6007
+ return !!singleMaidr.elements;
6008
+ }
5935
6009
  }
5936
6010
  }
5937
6011
  }
@@ -5948,11 +6022,13 @@ class Layer0Point {
5948
6022
  * @constructor
5949
6023
  */
5950
6024
  constructor() {
5951
- this.x = plot.chartPointsX[0];
5952
- this.y = plot.chartPointsY[0];
5953
- this.strokeWidth = 1.35;
5954
- this.circleIndex = [];
5955
- this.hasRect = plot.GetRectStatus('point');
6025
+ if ([].concat(singleMaidr.type).includes('point')) {
6026
+ this.x = plot.chartPointsX[0];
6027
+ this.y = plot.chartPointsY[0];
6028
+ this.strokeWidth = 1.35;
6029
+ this.hasRect = plot.GetRectStatus('point');
6030
+ this.circleIndex = [];
6031
+ }
5956
6032
  }
5957
6033
 
5958
6034
  /**
@@ -5967,10 +6043,25 @@ class Layer0Point {
5967
6043
  this.circleIndex = [];
5968
6044
  for (let j = 0; j < this.y.length; j++) {
5969
6045
  for (let i = 0; i < plot.plotPoints.length; i++) {
5970
- if (
5971
- plot.plotPoints[i].getAttribute(plot.prefix + 'x') == this.x &&
5972
- plot.plotPoints[i].getAttribute(plot.prefix + 'y') == this.y[j]
6046
+ let x;
6047
+ let y;
6048
+
6049
+ if (plot.plotPoints[i] instanceof SVGPathElement) {
6050
+ const pathD = plot.plotPoints[i].getAttribute('d');
6051
+ const regex = /M\s*(-?\d+(\.\d+)?)\s+(-?\d+(\.\d+)?)/g;
6052
+
6053
+ let match = regex.exec(pathD);
6054
+ x = parseFloat(match[1]);
6055
+ y = parseFloat(match[3]);
6056
+ } else if (
6057
+ plot.plotPoints[i] instanceof SVGUseElement ||
6058
+ plot.plotPoints[i] instanceof SVGCircleElement
5973
6059
  ) {
6060
+ x = plot.plotPoints[i].getAttribute(plot.prefix + 'x');
6061
+ y = plot.plotPoints[i].getAttribute(plot.prefix + 'y');
6062
+ }
6063
+
6064
+ if (x == this.x && y == this.y[j]) {
5974
6065
  this.circleIndex.push(i);
5975
6066
  break;
5976
6067
  }
@@ -5998,20 +6089,28 @@ class Layer0Point {
5998
6089
  constants.chart.getBoundingClientRect().height - this.y[i]
5999
6090
  );
6000
6091
  } else {
6001
- point.setAttribute(
6002
- 'cy',
6003
- plot.plotPoints[this.circleIndex[i]].getAttribute('cy')
6004
- );
6092
+ let y;
6093
+
6094
+ if (plot.plotPoints[this.circleIndex[i]] instanceof SVGPathElement) {
6095
+ const pathD = plot.plotPoints[this.circleIndex[i]].getAttribute('d');
6096
+ const regex = /M\s*(-?\d+(\.\d+)?)\s+(-?\d+(\.\d+)?)/g;
6097
+
6098
+ let match = regex.exec(pathD);
6099
+ y = parseFloat(match[3]);
6100
+ } else if (
6101
+ plot.plotPoints[this.circleIndex[i]] instanceof SVGUseElement ||
6102
+ plot.plotPoints[this.circleIndex[i]] instanceof SVGCircleElement
6103
+ ) {
6104
+ y = plot.plotPoints[this.circleIndex[i]].getAttribute(plot.prefix + 'y');
6105
+ }
6106
+
6107
+ point.setAttribute('cy', y);
6005
6108
  }
6006
6109
  point.setAttribute('r', 3.95);
6007
6110
  point.setAttribute('stroke', constants.colorSelected);
6008
6111
  point.setAttribute('stroke-width', this.strokeWidth);
6009
6112
  point.setAttribute('fill', constants.colorSelected);
6010
- if (plot.svgScaler[1] == -1) {
6011
- constants.chart.appendChild(point);
6012
- } else {
6013
- plot.plotPoints[this.circleIndex[i]].parentNode.appendChild(point);
6014
- }
6113
+ constants.chart.appendChild(point);
6015
6114
  }
6016
6115
  }
6017
6116
 
@@ -6049,10 +6148,12 @@ class Layer1Point {
6049
6148
  * @constructor
6050
6149
  */
6051
6150
  constructor() {
6052
- this.x = plot.chartLineX[0];
6053
- this.y = plot.chartLineY[0];
6054
- this.strokeWidth = 1.35;
6055
- this.hasRect = plot.GetRectStatus('point');
6151
+ if ([].concat(singleMaidr.type).includes('smooth')) {
6152
+ this.x = plot.chartLineX[0];
6153
+ this.y = plot.chartLineY[0];
6154
+ this.strokeWidth = 1.35;
6155
+ this.hasRect = plot.GetRectStatus('point');
6156
+ }
6056
6157
  }
6057
6158
 
6058
6159
  /**
@@ -6991,7 +7092,11 @@ class Control {
6991
7092
  }
6992
7093
 
6993
7094
  // switch layer controls
6994
- if (Array.isArray(singleMaidr.type)) {
7095
+ if (
7096
+ Array.isArray(singleMaidr.type) &&
7097
+ [].concat(singleMaidr.type).includes('point') &&
7098
+ [].concat(singleMaidr.type).includes('smooth')
7099
+ ) {
6995
7100
  // page down /(fn+down arrow): change chart type (layer)
6996
7101
  if (e.key == 'PageDown' && constants.brailleMode == 'off') {
6997
7102
  display.changeChartLayer('down');
@@ -8512,7 +8617,7 @@ class Control {
8512
8617
  }
8513
8618
  } else if (
8514
8619
  [].concat(singleMaidr.type).includes('point') ||
8515
- singleMaidr.type == 'point'
8620
+ [].concat(singleMaidr.type).includes('smooth')
8516
8621
  ) {
8517
8622
  // variable initialization
8518
8623
  constants.plotId = 'geom_point.points.12.1';