@websy/websy-designs 1.7.18 → 1.7.20

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.
@@ -15,6 +15,7 @@
15
15
  WebsyIcons
16
16
  WebsyChart
17
17
  WebsyChartTooltip
18
+ WebsyPie
18
19
  WebsyLegend
19
20
  WebsyMap
20
21
  WebsyKPI
@@ -2174,8 +2175,8 @@ class WebsyDragDrop {
2174
2175
  el.innerHTML = html
2175
2176
  this.options.items.forEach((item, i) => {
2176
2177
  if (item.component) {
2177
- if (item.isQlikPlugin && WebsyDesigns.QlikPlugin[item.component]) {
2178
- item.instance = new WebsyDesigns.QlikPlugin[item.component](`${item.id}_component`, item.options)
2178
+ if (item.isQlikPlugin && WebsyDesigns.QlikPlugins[item.component]) {
2179
+ item.instance = new WebsyDesigns.QlikPlugins[item.component](`${item.id}_component`, item.options)
2179
2180
  }
2180
2181
  else if (WebsyDesigns[item.component]) {
2181
2182
  item.instance = new WebsyDesigns[item.component](`${item.id}_component`, item.options)
@@ -5065,11 +5066,21 @@ class Switch {
5065
5066
  }
5066
5067
  disable () {
5067
5068
  this.options.enabled = false
5068
- this.render()
5069
+ let method = this.options.enabled === true ? 'add' : 'remove'
5070
+ const el = document.getElementById(`${this.elementId}_switch`)
5071
+ el.classList[method]('enabled')
5072
+ if (this.options.onToggle) {
5073
+ this.options.onToggle(this.options.enabled)
5074
+ }
5069
5075
  }
5070
5076
  enable () {
5071
5077
  this.options.enabled = true
5072
- this.render()
5078
+ let method = this.options.enabled === true ? 'add' : 'remove'
5079
+ const el = document.getElementById(`${this.elementId}_switch`)
5080
+ el.classList[method]('enabled')
5081
+ if (this.options.onToggle) {
5082
+ this.options.onToggle(this.options.enabled)
5083
+ }
5073
5084
  }
5074
5085
  handleClick (event) {
5075
5086
  this.options.enabled = !this.options.enabled
@@ -6456,7 +6467,7 @@ class WebsyTable3 {
6456
6467
  const el = document.getElementById(this.elementId)
6457
6468
  if (el) {
6458
6469
  let html = `
6459
- <div id='${this.elementId}_tableContainer' class='websy-vis-table-3 ${this.options.paging === 'pages' ? 'with-paging' : ''} ${this.options.virtualScroll === true ? 'with-virtual-scroll' : ''}'>
6470
+ <div id='${this.elementId}_tableContainer' class='websy-vis-table-3 ${this.options.paging === 'pages' ? 'with-paging' : ''} ${this.options.virtualScroll === true ? 'with-virtual-scroll' : ''} ${this.isTouchDevice === true && this.options.virtualScroll === true ? 'touch-device' : ''}'>
6460
6471
  <!--<div class="download-button">
6461
6472
  <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M16 11h5l-9 10-9-10h5v-11h8v11zm1 11h-10v2h10v-2z"/></svg>
6462
6473
  </div>-->
@@ -6976,11 +6987,11 @@ class WebsyTable3 {
6976
6987
  }
6977
6988
  }
6978
6989
  }
6979
- handleMouseDown (event) {
6980
- if (this.isTouchDevice === true) {
6990
+ handleMouseDown (event) {
6991
+ if (this.isTouchDevice) {
6981
6992
  return
6982
6993
  }
6983
- if (event.target.classList.contains('websy-scroll-handle-y')) {
6994
+ if (event.target.classList.contains('websy-scroll-handle-y')) {
6984
6995
  // set up the scroll start values
6985
6996
  this.scrollDragging = true
6986
6997
  this.scrollDirection = 'y'
@@ -6991,6 +7002,9 @@ class WebsyTable3 {
6991
7002
  // console.log(scrollHandleEl.offsetTop)
6992
7003
  }
6993
7004
  else if (event.target.classList.contains('websy-scroll-handle-x')) {
7005
+ if (this.isTouchDevice) {
7006
+ event.preventDefault()
7007
+ }
6994
7008
  this.scrollDragging = true
6995
7009
  this.scrollDirection = 'x'
6996
7010
  const scrollHandleEl = document.getElementById(`${this.elementId}_hScrollHandle`)
@@ -6998,9 +7012,12 @@ class WebsyTable3 {
6998
7012
  this.mouseXStart = event.pageX
6999
7013
  }
7000
7014
  }
7001
- handleMouseMove (event) {
7015
+ handleMouseMove (event) {
7002
7016
  // event.preventDefault()
7003
7017
  if (this.scrollDragging === true) {
7018
+ if (this.isTouchDevice) {
7019
+ event.preventDefault()
7020
+ }
7004
7021
  if (this.scrollDirection === 'y') {
7005
7022
  const diff = event.pageY - this.mouseYStart
7006
7023
  this.scrollY(diff)
@@ -7024,11 +7041,24 @@ class WebsyTable3 {
7024
7041
  event.preventDefault()
7025
7042
  // console.log('scrollwheel', event)
7026
7043
  if (Math.abs(event.deltaX) > Math.abs(event.deltaY)) {
7027
- this.scrollX(Math.max(-2, Math.min(2, event.deltaX)))
7044
+ // this.scrollX(Math.max(-2, Math.min(2, event.deltaX)))
7045
+ if (this.hScrollRequired === false) {
7046
+ return
7047
+ }
7048
+ const scrollHandleEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7049
+ const scrollContainerEl = document.getElementById(`${this.elementId}_hScrollContainer`)
7050
+ let resolvedDelta = (scrollContainerEl.getBoundingClientRect().width - scrollHandleEl.getBoundingClientRect().width) / this.totalColumnCount
7051
+ if (event.deltaX < 0) {
7052
+ resolvedDelta = resolvedDelta * -1
7053
+ }
7054
+ this.scrollX(resolvedDelta)
7028
7055
  }
7029
7056
  else {
7030
7057
  // console.log('delta', event.deltaY)
7031
7058
  // force the scroll to be a single row at a time
7059
+ if (this.vScrollRequired === false) {
7060
+ return
7061
+ }
7032
7062
  const scrollHandleEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7033
7063
  const scrollContainerEl = document.getElementById(`${this.elementId}_vScrollContainer`)
7034
7064
  let resolvedDelta = (scrollContainerEl.getBoundingClientRect().height - scrollHandleEl.getBoundingClientRect().height) / this.totalRowCount
@@ -7047,12 +7077,18 @@ class WebsyTable3 {
7047
7077
  }
7048
7078
  handleTouchEnd (event) {
7049
7079
  // console.log('touch end fired')
7080
+ this.scrollDragging = false
7081
+ this.cellDragging = false
7082
+ this.handleYStart = null
7083
+ this.mouseYStart = null
7084
+ this.handleXStart = null
7085
+ this.mouseXStart = null
7050
7086
  if (typeof event.targetTouches !== 'undefined') {
7051
7087
  this.isTouchScrolling = false
7052
- this.touchEndTime = (new Date()).getTime()
7053
- this.isPerpetual = true
7088
+ // this.touchEndTime = (new Date()).getTime()
7089
+ // this.isPerpetual = true
7054
7090
  // this.perpetualScroll()
7055
- this.touchStartTime = null
7091
+ // this.touchStartTime = null
7056
7092
  // const touchScrollEl = document.getElementById(`${this.elementId}_touchScroller`)
7057
7093
  // touchScrollEl.classList.add('hidden')
7058
7094
  }
@@ -7063,47 +7099,68 @@ class WebsyTable3 {
7063
7099
  event.preventDefault()
7064
7100
  event.stopPropagation()
7065
7101
  if (typeof event.targetTouches !== 'undefined' && event.targetTouches.length > 0) {
7066
- let deltaX = (this.mouseXStart - event.targetTouches[0].pageX)
7067
- let deltaY = (this.mouseYStart - event.targetTouches[0].pageY)
7068
- const hScrollContainerEl = document.getElementById(`${this.elementId}_hScrollContainer`)
7069
- const vScrollContainerEl = document.getElementById(`${this.elementId}_vScrollContainer`)
7070
- const hScrollHandleEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7071
- const vScrollHandleEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7072
- let translatedDeltaX = deltaX * (hScrollHandleEl.getBoundingClientRect().width / vScrollContainerEl.getBoundingClientRect().width)
7073
- let translatedDeltaY = deltaY * (vScrollHandleEl.getBoundingClientRect().height / vScrollContainerEl.getBoundingClientRect().height)
7074
- // need to adjust the delta so that it scrolls at a reasonable speed/distance
7075
- const scrollHandleXEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7076
- const scrollHandleYEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7077
- // if (Math.abs(deltaY) > this.sizes.cellHeight) {
7078
- // this.isTouchScrolling = true
7079
- // }
7080
- // else {
7081
- // this.isTouchScrolling = false
7082
- // }
7083
- // console.log('delta', this.mouseYStart, event.targetTouches[0].pageY, deltaY)
7084
- // deltaX = deltaX * (scrollHandleXEl.offsetWidth / this.sizes.scrollableWidth)
7085
- // deltaY = deltaY * (scrollHandleYEl.offsetHeight / this.sizes.bodyHeight)
7086
- // console.log('delta', deltaY)
7087
- // NW
7088
- // else if (Math.abs(deltaX) > 50) {
7089
- // this.isTouchScrolling = false
7090
- // }
7091
- this.currentClientY = event.targetTouches[0].pageY
7092
- this.currentTouchtime = (new Date()).getTime()
7093
- // end
7094
- // delta = Math.min(10, delta)
7095
- // delta = Math.max(-10, delta)
7096
- if (this.isTouchScrolling === true) {
7097
- // this.$scope.scrollTop += (delta / (this.$scope.layout.qHyperCube.qSize.qcy / this.$scope.rowsToLoad / (this.$scope.totalSpaceAvailable / 250)))
7098
- if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX > 20) {
7099
- // this.scrollX(Math.max(-5, Math.min(5, translatedDeltaX)))
7100
- this.scrollX(translatedDeltaX)
7101
- }
7102
- else if (deltaY > 20) {
7103
- // this.scrollY(Math.max(-5, Math.min(5, translatedDeltaY)))
7104
- this.scrollY(translatedDeltaY)
7105
- }
7106
- }
7102
+ event.deltaX = this.touchXStart - event.targetTouches[0].pageX
7103
+ event.deltaY = this.touchYStart - event.targetTouches[0].pageY
7104
+ if (Math.abs(event.deltaY) > 50) {
7105
+ event.deltaX = 0
7106
+ this.handleScrollWheel(event)
7107
+ }
7108
+ else if (Math.abs(event.deltaX) > 50) {
7109
+ event.deltaY = 0
7110
+ this.handleScrollWheel(event)
7111
+ }
7112
+ // let deltaX = (this.mouseXStart - event.targetTouches[0].pageX)
7113
+ // let deltaY = (this.mouseYStart - event.targetTouches[0].pageY)
7114
+ // const hScrollContainerEl = document.getElementById(`${this.elementId}_hScrollContainer`)
7115
+ // const vScrollContainerEl = document.getElementById(`${this.elementId}_vScrollContainer`)
7116
+ // const hScrollHandleEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7117
+ // const vScrollHandleEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7118
+ // let translatedDeltaX = deltaX * (hScrollHandleEl.getBoundingClientRect().width / vScrollContainerEl.getBoundingClientRect().width)
7119
+ // let translatedDeltaY = deltaY * (vScrollHandleEl.getBoundingClientRect().height / vScrollContainerEl.getBoundingClientRect().height)
7120
+ // // need to adjust the delta so that it scrolls at a reasonable speed/distance
7121
+ // const scrollHandleXEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7122
+ // const scrollHandleYEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7123
+ // // if (Math.abs(deltaY) > this.sizes.cellHeight) {
7124
+ // // this.isTouchScrolling = true
7125
+ // // }
7126
+ // // else {
7127
+ // // this.isTouchScrolling = false
7128
+ // // }
7129
+ // // console.log('delta', this.mouseYStart, event.targetTouches[0].pageY, deltaY)
7130
+ // // deltaX = deltaX * (scrollHandleXEl.offsetWidth / this.sizes.scrollableWidth)
7131
+ // // deltaY = deltaY * (scrollHandleYEl.offsetHeight / this.sizes.bodyHeight)
7132
+ // // console.log('delta', deltaY)
7133
+ // // NW
7134
+ // // else if (Math.abs(deltaX) > 50) {
7135
+ // // this.isTouchScrolling = false
7136
+ // // }
7137
+ // this.currentClientY = event.targetTouches[0].pageY
7138
+ // this.currentTouchtime = (new Date()).getTime()
7139
+ // // end
7140
+ // // delta = Math.min(10, delta)
7141
+ // // delta = Math.max(-10, delta)
7142
+ // if (this.isTouchScrolling === true) {
7143
+ // // this.$scope.scrollTop += (delta / (this.$scope.layout.qHyperCube.qSize.qcy / this.$scope.rowsToLoad / (this.$scope.totalSpaceAvailable / 250)))
7144
+ // if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX > 20) {
7145
+ // // this.scrollX(Math.max(-5, Math.min(5, translatedDeltaX)))
7146
+ // this.scrollX(translatedDeltaX)
7147
+ // }
7148
+ // else if (deltaY > 20) {
7149
+ // // this.scrollY(Math.max(-5, Math.min(5, translatedDeltaY)))
7150
+ // this.scrollY(translatedDeltaY)
7151
+ // }
7152
+ // }
7153
+ }
7154
+ }
7155
+ if (this.scrollDragging === true) {
7156
+ event.preventDefault()
7157
+ if (this.scrollDirection === 'y') {
7158
+ const diff = event.targetTouches[0].pageY - this.mouseYStart
7159
+ this.scrollY(diff)
7160
+ }
7161
+ else if (this.scrollDirection === 'x') {
7162
+ const diff = event.targetTouches[0].pageX - this.mouseXStart
7163
+ this.scrollX(diff)
7107
7164
  }
7108
7165
  }
7109
7166
  }
@@ -7115,19 +7172,42 @@ class WebsyTable3 {
7115
7172
  return
7116
7173
  }
7117
7174
  // console.log(event.target.classList)
7118
- if (this.options.virtualScroll === true) {
7119
- this.touchStartTime = (new Date()).getTime()
7120
- this.isTouchScrolling = true
7121
- this.isPerpetual = false
7175
+ if (event.target.classList.contains('websy-table-touch-scroller')) {
7176
+ if (this.options.virtualScroll === true) {
7177
+ // this.touchStartTime = (new Date()).getTime()
7178
+ this.isTouchScrolling = true
7179
+ // this.isPerpetual = false
7180
+ this.touchYStart = event.targetTouches[0].pageY
7181
+ this.touchXStart = event.targetTouches[0].pageX
7182
+ }
7183
+ }
7184
+ if (event.target.classList.contains('websy-scroll-handle-y')) {
7185
+ // set up the scroll start values
7186
+ this.scrollDragging = true
7187
+ this.scrollDirection = 'y'
7188
+ const scrollHandleEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7189
+ this.handleYStart = scrollHandleEl.offsetTop
7122
7190
  this.mouseYStart = event.targetTouches[0].pageY
7123
- this.mouseXStart = event.targetTouches[0].pageX
7124
- // const touchScrollEl = document.getElementById(`${this.elementId}_touchScroller`)
7125
- // touchScrollEl.classList.remove('hidden')
7126
- const handleYEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7127
- this.handleYStart = handleYEl.offsetTop
7128
- const handleXEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7129
- this.handleXStart = handleXEl.offsetLeft
7130
- }
7191
+ // console.log('mouse down', this.handleYStart, this.mouseYStart)
7192
+ // console.log(scrollHandleEl.offsetTop)
7193
+ }
7194
+ else if (event.target.classList.contains('websy-scroll-handle-x')) {
7195
+ if (this.isTouchDevice) {
7196
+ event.preventDefault()
7197
+ }
7198
+ this.scrollDragging = true
7199
+ this.scrollDirection = 'x'
7200
+ const scrollHandleEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7201
+ this.handleXStart = scrollHandleEl.offsetLeft
7202
+ this.mouseXStart = event.targetTouches[0].pageX
7203
+ }
7204
+ // // const touchScrollEl = document.getElementById(`${this.elementId}_touchScroller`)
7205
+ // // touchScrollEl.classList.remove('hidden')
7206
+ // const handleYEl = document.getElementById(`${this.elementId}_vScrollHandle`)
7207
+ // this.handleYStart = handleYEl.offsetTop
7208
+ // const handleXEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7209
+ // this.handleXStart = handleXEl.offsetLeft
7210
+ // }
7131
7211
  }
7132
7212
  hideError () {
7133
7213
  const el = document.getElementById(`${this.elementId}_tableContainer`)
@@ -7236,7 +7316,7 @@ class WebsyTable3 {
7236
7316
  let hHandleEl = document.getElementById(`${this.elementId}_hScrollHandle`)
7237
7317
  if (this.hScrollRequired === true) {
7238
7318
  hScrollEl.style.left = `${this.sizes.table.width - this.sizes.scrollableWidth}px`
7239
- hScrollEl.style.width = `${this.sizes.scrollableWidth - 20}px`
7319
+ hScrollEl.style.width = `${this.sizes.scrollableWidth - (this.isTouchDevice ? 30 : 20)}px`
7240
7320
  hHandleEl.style.width = Math.max(this.options.minHandleSize, this.sizes.scrollableWidth * (this.sizes.scrollableWidth / this.sizes.totalNonPinnedWidth)) + 'px'
7241
7321
  }
7242
7322
  else {
@@ -9074,7 +9154,12 @@ if (!this.brushBarsInitialized[series.key]) {
9074
9154
  }
9075
9155
  removebar (key) {
9076
9156
  /* global key d3 */
9077
- let bars = this.barLayer.selectAll(`.bar_${key}`)
9157
+ this.barLayer.selectAll(`.bar_${key}`)
9158
+ .transition(this.transition)
9159
+ .style('fill-opacity', 1e-6)
9160
+ .remove()
9161
+ // remove from the brush as well
9162
+ this.brushArea.selectAll(`.bar_${key}`)
9078
9163
  .transition(this.transition)
9079
9164
  .style('fill-opacity', 1e-6)
9080
9165
  .remove()
@@ -9656,6 +9741,434 @@ if (el) {
9656
9741
  }
9657
9742
  }
9658
9743
 
9744
+ /* global include */
9745
+ class WebsyPie {
9746
+ constructor (elementId, options) {
9747
+ this.elementId = elementId
9748
+ const DEFAULTS = {
9749
+ labelMode: 'auto',
9750
+ labelLocs: [],
9751
+ showLabels: false,
9752
+ segments: [
9753
+ '#9dbca6',
9754
+ '#e3c080',
9755
+ '#fd7336',
9756
+ '#b12737',
9757
+ '#50424b'
9758
+ ],
9759
+ label: {
9760
+ value: {
9761
+ size: '12px',
9762
+ colour: '#BBB',
9763
+ family: 'arial',
9764
+ weight: 'normal'
9765
+ },
9766
+ label: {
9767
+ size: '10px',
9768
+ colour: '#888',
9769
+ family: 'arial',
9770
+ weight: 'normal'
9771
+ }
9772
+ },
9773
+ kpi: {
9774
+ background: '#555',
9775
+ value: {
9776
+ size: '60px',
9777
+ colour: '#CCC',
9778
+ family: 'arial',
9779
+ weight: 'bold'
9780
+ },
9781
+ label: {
9782
+ size: '20px',
9783
+ colour: '#FFF',
9784
+ family: 'arial',
9785
+ weight: 'normal'
9786
+ }
9787
+ }
9788
+ }
9789
+ this.options = Object.assign({}, DEFAULTS, options)
9790
+ this.listening = true
9791
+ if (!elementId) {
9792
+ console.log('No element Id provided for Websy Chart')
9793
+ return
9794
+ }
9795
+ const el = document.getElementById(this.elementId)
9796
+ if (el) {
9797
+ el.classList.add('websy-pie')
9798
+ }
9799
+ else {
9800
+ console.error(`No element found with ID ${this.elementId}`)
9801
+ }
9802
+ }
9803
+ calculateArcAngle (arcLength, radius) {
9804
+ return this.convertRadiansToDegrees(arcLength / (Math.PI * (2 * radius)))
9805
+ }
9806
+ calculateCircumference (perc) {
9807
+ return (Math.PI * 2) * (perc)
9808
+ }
9809
+ close () {
9810
+
9811
+ }
9812
+ convertDegreesToRadians (degrees) {
9813
+ return degrees * (Math.PI / 180)
9814
+ }
9815
+ convertRadiansToDegrees (radians) {
9816
+ return radians * (180 / Math.PI)
9817
+ }
9818
+ createCanvases (element) {
9819
+ /* global element */
9820
+ if (element && !element.target) {
9821
+ if (!this.listening) {
9822
+ // add event listeners to help with responsiveness
9823
+ element.addEventListener('resize', this.render.bind(this), false)
9824
+ window.addEventListener('resize', this.render.bind(this), false)
9825
+ this.listening = true
9826
+ }
9827
+ let height = element.clientHeight
9828
+ let width = element.clientWidth
9829
+ this.width = width
9830
+ this.height = height
9831
+ this.center = {
9832
+ x: (width / 2),
9833
+ y: (height / 2)
9834
+ }
9835
+ this.outerRadius = (Math.min(this.height, this.width) / 2) - 20
9836
+ console.log('radius is ' + this.outerRadius)
9837
+ // segment canvas
9838
+ const segmentCanvas = document.createElement('canvas')
9839
+ this.segmentPaper = {
9840
+ canvas: segmentCanvas,
9841
+ pen: segmentCanvas.getContext('2d')
9842
+ }
9843
+ this.segmentPaper.canvas.style.position = 'absolute'
9844
+ this.segmentPaper.canvas.style.top = '0px'
9845
+ this.segmentPaper.canvas.style.left = '0px'
9846
+ this.segmentPaper.canvas.style.zIndex = '5'
9847
+ this.segmentPaper.canvas.style.width = width + 'px'
9848
+ this.segmentPaper.canvas.style.height = height + 'px'
9849
+ // make the canvase size double to accommodate for retina displays
9850
+ this.segmentPaper.canvas.width = width * 2
9851
+ this.segmentPaper.canvas.height = height * 2
9852
+ this.segmentPaper.pen.scale(2, 2)
9853
+ element.appendChild(this.segmentPaper.canvas)
9854
+
9855
+ // label canvas
9856
+ const labelCanvas = document.createElement('canvas')
9857
+ this.labelPaper = {
9858
+ canvas: labelCanvas,
9859
+ pen: labelCanvas.getContext('2d')
9860
+ }
9861
+ this.labelPaper.canvas.style.position = 'absolute'
9862
+ this.labelPaper.canvas.style.top = '0px'
9863
+ this.labelPaper.canvas.style.left = '0px'
9864
+ this.labelPaper.canvas.style.zIndex = '5'
9865
+ this.labelPaper.canvas.style.width = width + 'px'
9866
+ this.labelPaper.canvas.style.height = height + 'px'
9867
+ // make the canvase size double to accommodate for retina displays
9868
+ this.labelPaper.canvas.width = width * 2
9869
+ this.labelPaper.canvas.height = height * 2
9870
+ this.labelPaper.pen.scale(2, 2)
9871
+ element.appendChild(this.labelPaper.canvas)
9872
+
9873
+ // kpi canvas
9874
+ const kpiCanvas = document.createElement('canvas')
9875
+ this.kpiPaper = {
9876
+ canvas: kpiCanvas,
9877
+ pen: kpiCanvas.getContext('2d')
9878
+ }
9879
+ this.kpiPaper.canvas.style.position = 'absolute'
9880
+ this.kpiPaper.canvas.style.top = '0px'
9881
+ this.kpiPaper.canvas.style.left = '0px'
9882
+ this.kpiPaper.canvas.style.zIndex = '5'
9883
+ this.kpiPaper.canvas.style.width = width + 'px'
9884
+ this.kpiPaper.canvas.style.height = height + 'px'
9885
+ // make the canvase size double to accommodate for retina displays
9886
+ this.kpiPaper.canvas.width = width * 2
9887
+ this.kpiPaper.canvas.height = height * 2
9888
+ this.kpiPaper.pen.scale(2, 2)
9889
+ element.appendChild(this.kpiPaper.canvas)
9890
+ }
9891
+ else {
9892
+ // clear the canvases
9893
+ this.segmentPaper.canvas.width = this.width * 2
9894
+ this.labelPaper.canvas.width = this.width * 2
9895
+ this.kpiPaper.canvas.width = this.width * 2
9896
+ }
9897
+
9898
+ }
9899
+ drawKPI () {
9900
+ let kpiStyleDefaults = this.options.kpi
9901
+ if (this.kpi) {
9902
+ // draw the circle for the background colour if one has been specified
9903
+ if (!this.kpi.background || this.kpi.background !== 'transparent') {
9904
+ this.kpiPaper.pen.beginPath()
9905
+ this.kpiPaper.pen.fillStyle = this.kpi.background || kpiStyleDefaults.background
9906
+ this.kpiPaper.pen.moveTo(this.center.x, this.center.y)
9907
+ this.kpiPaper.pen.arc(this.center.x, this.center.y, this.innerRadiusPixels + (5 * !this.addShading) + 1, 0, Math.PI * 2)
9908
+ this.kpiPaper.pen.fill()
9909
+ this.kpiPaper.pen.closePath()
9910
+ }
9911
+ // draw the kpi value
9912
+ if (this.kpi.value) {
9913
+ this.kpiPaper.pen.save()
9914
+ this.kpiPaper.pen.beginPath()
9915
+ this.kpiPaper.pen.moveTo(this.center.x, this.center.y)
9916
+ this.kpiPaper.pen.font = (this.kpi.value.weight || kpiStyleDefaults.value.weight) + ' ' + (this.kpi.value.size || kpiStyleDefaults.value.size) + ' ' + (this.kpi.value.family || kpiStyleDefaults.value.family)
9917
+ this.kpiPaper.pen.textAlign = 'center'
9918
+ if (this.kpi.label) {
9919
+ this.kpiPaper.pen.textBaseline = 'text-bottom'
9920
+ }
9921
+ else {
9922
+ this.kpiPaper.pen.textBaseline = 'middle'
9923
+ }
9924
+ this.kpiPaper.pen.fillStyle = this.kpi.value.colour || kpiStyleDefaults.value.colour
9925
+ this.kpiPaper.pen.fillText(this.kpi.value.text, this.center.x, this.center.y)
9926
+ this.kpiPaper.pen.closePath()
9927
+ this.kpiPaper.pen.restore()
9928
+ }
9929
+ // draw the kpi label
9930
+ if (this.kpi.label) {
9931
+ this.kpiPaper.pen.save()
9932
+ this.kpiPaper.pen.beginPath()
9933
+ this.kpiPaper.pen.moveTo(this.center.x, this.center.y)
9934
+ this.kpiPaper.pen.font = (this.kpi.label.weight || kpiStyleDefaults.label.weight) + ' ' + (this.kpi.label.size || kpiStyleDefaults.label.size) + ' ' + (this.kpi.label.family || kpiStyleDefaults.label.family)
9935
+ this.kpiPaper.pen.textAlign = 'center'
9936
+ if (this.kpi.value) {
9937
+ this.kpiPaper.pen.textBaseline = 'top'
9938
+ }
9939
+ else {
9940
+ this.kpiPaper.pen.textBaseline = 'middle'
9941
+ }
9942
+ this.kpiPaper.pen.fillStyle = this.kpi.label.colour || kpiStyleDefaults.label.colour
9943
+ this.kpiPaper.pen.fillText(this.kpi.label.text, this.center.x, this.center.y + 10)
9944
+ this.kpiPaper.pen.closePath()
9945
+ this.kpiPaper.pen.restore()
9946
+ }
9947
+ }
9948
+
9949
+ }
9950
+ drawLabels () {
9951
+ // this.labelPaper.pen.translate(this.center.x, this.center.y)
9952
+ if (this.options.showLabels) {
9953
+ for (let s in this.data.segments) {
9954
+ this.labelPaper.pen.beginPath()
9955
+ this.labelPaper.pen.font = '20px arial'
9956
+ this.labelPaper.pen.textAlign = 'center'
9957
+ this.labelPaper.pen.textBaseline = 'text-bottom'
9958
+ this.labelPaper.pen.fillStyle = 'black'
9959
+ // calculate the nearest 90 degrees to the mid point and reduce to within 360 degrees
9960
+ let midAngleDeg = this.data.segments[s].midAngleDeg % 360
9961
+ let nearest90Deg = (Math.round(midAngleDeg / 90) * 90) % 360
9962
+ console.log(Math.abs(nearest90Deg - midAngleDeg))
9963
+ let missingAngleDeg = 270 - (Math.abs(nearest90Deg - midAngleDeg))
9964
+ console.log(nearest90Deg)
9965
+ let oppLength = (this.outerRadius * Math.sin(this.convertDegreesToRadians(Math.abs(nearest90Deg - midAngleDeg)))) / Math.sin(this.convertDegreesToRadians(missingAngleDeg))
9966
+ oppLength = Math.abs(oppLength)
9967
+ let moveLength = this.outerRadius + 5
9968
+ let slopeMultiplier = 1.2
9969
+ let flatMultiplier = 1.3
9970
+ if (midAngleDeg > 0 && midAngleDeg <= 45) {
9971
+ this.labelPaper.pen.moveTo(this.center.x + moveLength, this.center.y + oppLength)
9972
+ this.labelPaper.pen.lineTo(this.center.x + (moveLength * slopeMultiplier), this.center.y + (oppLength * slopeMultiplier))
9973
+ this.labelPaper.pen.lineTo(this.center.x + (moveLength * flatMultiplier), this.center.y + (oppLength * slopeMultiplier))
9974
+ }
9975
+ else if (midAngleDeg > 45 && midAngleDeg <= 90) {
9976
+ this.labelPaper.pen.moveTo(this.center.x + oppLength, this.center.y + moveLength)
9977
+ this.labelPaper.pen.lineTo(this.center.x + (oppLength * slopeMultiplier), this.center.y + (moveLength * slopeMultiplier))
9978
+ this.labelPaper.pen.lineTo(this.center.x + (oppLength * flatMultiplier), this.center.y + (moveLength * slopeMultiplier))
9979
+ }
9980
+ else if (midAngleDeg > 90 && midAngleDeg <= 135) {
9981
+ this.labelPaper.pen.moveTo(this.center.x - oppLength, this.center.y + moveLength)
9982
+ this.labelPaper.pen.lineTo(this.center.x - (oppLength * slopeMultiplier), this.center.y + (moveLength * slopeMultiplier))
9983
+ this.labelPaper.pen.lineTo(this.center.x - (oppLength * flatMultiplier), this.center.y + (moveLength * slopeMultiplier))
9984
+ }
9985
+ else if (midAngleDeg > 135 && midAngleDeg <= 180) {
9986
+ this.labelPaper.pen.moveTo(this.center.x - moveLength, this.center.y + oppLength)
9987
+ this.labelPaper.pen.lineTo(this.center.x - (moveLength * slopeMultiplier), this.center.y + (oppLength * slopeMultiplier))
9988
+ this.labelPaper.pen.lineTo(this.center.x - (moveLength * flatMultiplier), this.center.y + (oppLength * slopeMultiplier))
9989
+ }
9990
+ else if (midAngleDeg > 180 && midAngleDeg <= 225) {
9991
+ this.labelPaper.pen.moveTo(this.center.x - moveLength, this.center.y - oppLength)
9992
+ this.labelPaper.pen.lineTo(this.center.x - (moveLength * slopeMultiplier), this.center.y - (oppLength * slopeMultiplier))
9993
+ this.labelPaper.pen.lineTo(this.center.x - (moveLength * flatMultiplier), this.center.y - (oppLength * slopeMultiplier))
9994
+ }
9995
+ else if (midAngleDeg > 225 && midAngleDeg <= 270) {
9996
+ this.labelPaper.pen.moveTo(this.center.x - oppLength, this.center.y - moveLength)
9997
+ this.labelPaper.pen.lineTo(this.center.x - (oppLength * slopeMultiplier), this.center.y - (moveLength * slopeMultiplier))
9998
+ this.labelPaper.pen.lineTo(this.center.x - (oppLength * flatMultiplier), this.center.y - (moveLength * slopeMultiplier))
9999
+ }
10000
+ else if (midAngleDeg > 270 && midAngleDeg <= 315) {
10001
+ this.labelPaper.pen.moveTo(this.center.x + oppLength, this.center.y - moveLength)
10002
+ this.labelPaper.pen.lineTo(this.center.x + (oppLength * slopeMultiplier), this.center.y - (moveLength * slopeMultiplier))
10003
+ this.labelPaper.pen.lineTo(this.center.x + (oppLength * flatMultiplier), this.center.y - (moveLength * slopeMultiplier))
10004
+ }
10005
+ else {
10006
+ this.labelPaper.pen.moveTo(this.center.x + moveLength, this.center.y - oppLength)
10007
+ this.labelPaper.pen.lineTo(this.center.x + (moveLength * slopeMultiplier), this.center.y - (oppLength * slopeMultiplier))
10008
+ this.labelPaper.pen.lineTo(this.center.x + (moveLength * flatMultiplier), this.center.y - (oppLength * slopeMultiplier))
10009
+ }
10010
+ this.labelPaper.pen.strokeStyle = '#cccccc'
10011
+ this.labelPaper.pen.stroke()
10012
+
10013
+ this.labelPaper.pen.fillText(this.center.x + this.data.segments[s].label.x, this.center.y + this.data.segments[s].label.y, s)
10014
+ this.labelPaper.pen.closePath()
10015
+ }
10016
+ }
10017
+
10018
+ }
10019
+ drawSegments () {
10020
+ let originalStartPoint = this.convertDegreesToRadians(270)
10021
+ let startPoint = originalStartPoint
10022
+ let colourIndex = 0
10023
+ for (let s in this.data.segments) {
10024
+ if (colourIndex >= this.options.colors.length) {
10025
+ colourIndex = 0
10026
+ }
10027
+ let segmentColour = this.options.colors[colourIndex]
10028
+ let endPoint = startPoint + this.data.segments[s].circumference
10029
+ let midAngle = (startPoint + endPoint) / 2
10030
+
10031
+ this.data.segments[s].labelCanvasRotation = startPoint
10032
+ this.data.segments[s].startAngle = startPoint
10033
+ this.data.segments[s].midAngle = midAngle
10034
+ this.data.segments[s].endAngle = endPoint
10035
+ this.data.segments[s].startAngleDeg = this.convertRadiansToDegrees(startPoint)
10036
+ this.data.segments[s].midAngleDeg = this.convertRadiansToDegrees(midAngle)
10037
+ this.data.segments[s].endAngleDeg = this.convertRadiansToDegrees(endPoint)
10038
+ this.segmentPaper.pen.beginPath()
10039
+ this.segmentPaper.pen.moveTo(this.center.x, this.center.y)
10040
+ this.segmentPaper.pen.fillStyle = segmentColour
10041
+ this.segmentPaper.pen.arc(this.center.x, this.center.y, this.outerRadius, startPoint, endPoint)
10042
+ console.log(this.segmentPaper.pen)
10043
+ this.segmentPaper.pen.fill()
10044
+ this.segmentPaper.pen.closePath()
10045
+ startPoint = endPoint
10046
+ colourIndex++
10047
+ }
10048
+ // cut out the center of the pie if an inner radius has been set
10049
+ if (this.innerRadius && this.innerRadius !== 0) {
10050
+ // inner radius is a percentage so we compare it against outerRadius (px) to calculate the pixel value
10051
+ this.innerRadiusPixels = Math.round(this.outerRadius * (this.innerRadius / 100))
10052
+ this.segmentPaper.pen.save()
10053
+ this.segmentPaper.pen.beginPath()
10054
+ this.segmentPaper.pen.moveTo(this.center.x, this.center.y)
10055
+ this.segmentPaper.pen.arc(this.center.x, this.center.y, (this.innerRadiusPixels + 5), 0, Math.PI * 2)
10056
+ this.segmentPaper.pen.clip()
10057
+ this.segmentPaper.pen.clearRect(0, 0, this.width * 2, this.height * 2)
10058
+ this.segmentPaper.pen.closePath()
10059
+ this.segmentPaper.pen.restore()
10060
+
10061
+ // add shading if required
10062
+ if (this.addShading === true) {
10063
+ startPoint = originalStartPoint
10064
+ colourIndex = 0
10065
+ for (let s in this.data.segments) {
10066
+ if (colourIndex >= this.options.colors.length) {
10067
+ colourIndex = 0
10068
+ }
10069
+ let segmentColour = this.hexToRGBA(this.options.colors[colourIndex], 0.8)
10070
+ let endPoint = startPoint + this.data.segments[s].circumference
10071
+ this.segmentPaper.pen.beginPath()
10072
+ this.segmentPaper.pen.moveTo(this.center.x, this.center.y)
10073
+ this.segmentPaper.pen.fillStyle = segmentColour
10074
+ this.segmentPaper.pen.arc(this.center.x, this.center.y, (this.innerRadiusPixels + 6), startPoint, endPoint)
10075
+ this.segmentPaper.pen.fill()
10076
+ this.segmentPaper.pen.closePath()
10077
+ startPoint = endPoint
10078
+ colourIndex++
10079
+ }
10080
+ // cut out the center again
10081
+ this.segmentPaper.pen.save()
10082
+ this.segmentPaper.pen.beginPath()
10083
+ this.segmentPaper.pen.moveTo(this.center.x, this.center.y)
10084
+ this.segmentPaper.pen.arc(this.center.x, this.center.y, this.innerRadiusPixels, 0, Math.PI * 2)
10085
+ this.segmentPaper.pen.clip()
10086
+ this.segmentPaper.pen.clearRect(0, 0, this.width * 2, this.height * 2)
10087
+ this.segmentPaper.pen.closePath()
10088
+ this.segmentPaper.pen.restore()
10089
+ }
10090
+ }
10091
+ console.log(this.data)
10092
+
10093
+ }
10094
+ hexToRGBA (hex, opacity) {
10095
+ if (!opacity) {
10096
+ opacity = 1
10097
+ }
10098
+ let c
10099
+ if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
10100
+ c = hex.substring(1).split('')
10101
+ if (c.length === 3) {
10102
+ c = [c[0], c[0], c[1], c[1], c[2], c[2]]
10103
+ }
10104
+ c = '0x' + c.join('')
10105
+ return 'rgba(' + [(c >> 16) & 255, (c >> 8) & 255, c & 255, opacity].join(',') + ')'
10106
+ }
10107
+ console.error(`Unable to convert hex (${hex}) to RGBA`)
10108
+ return 'rgba(0, 0, 0, 1)'
10109
+ }
10110
+ render (data) {
10111
+ /* global data */
10112
+ const el = document.getElementById(this.elementId)
10113
+ if (el && !el.target) {
10114
+ this.createCanvases(el)
10115
+ }
10116
+ if (data && !data.target) {
10117
+ this.originalData = [...data]
10118
+ }
10119
+ this.processData(data)
10120
+ this.drawSegments()
10121
+ this.drawLabels()
10122
+ this.drawKPI()
10123
+
10124
+ }
10125
+ resize () {
10126
+
10127
+ }
10128
+ processData (data) {
10129
+ /* global data */
10130
+ let tempData = {}
10131
+ let tempTotal = 0
10132
+ for (let i = 0; i < data.length; i++) {
10133
+ // if the dimension does not exists as a property on tempData, add it
10134
+ if (!tempData[data[i][0].label]) {
10135
+ tempData[data[i][0].label] = {
10136
+ segmentValue: 0,
10137
+ drill: []
10138
+ }
10139
+ }
10140
+ // add the value to the relevent segmentValue
10141
+ tempData[data[i][0].label].segmentValue += data[i][1].value
10142
+ // add the value to the total
10143
+ tempTotal += data[i][1].value
10144
+ // if a drill has been specified add the data to the drill property
10145
+ if (this.drill && this.drill !== '') {
10146
+ tempData[data[i][0].label].drill.push({
10147
+ label: data[i][0].label,
10148
+ value: data[i][1].value
10149
+ })
10150
+ }
10151
+ }
10152
+ // loop through the full data set to calculate the percentages
10153
+ for (let s in tempData) {
10154
+ tempData[s].percValue = tempData[s].segmentValue / tempTotal
10155
+ tempData[s].circumference = this.calculateCircumference(tempData[s].percValue)
10156
+ tempData[s].labelAngle = 360 * (tempData[s].percValue / 2)
10157
+ // calculate where the label should be for that segment
10158
+ tempData[s].label = {
10159
+ x: ((this.outerRadius + 5) / Math.sin(this.convertDegreesToRadians(90)) * Math.sin(this.convertDegreesToRadians(180 - 90 - tempData[s].labelAngle)))
10160
+ }
10161
+ tempData[s].label.y = ((this.outerRadius + 5) / Math.sin(this.convertDegreesToRadians(90)) * Math.sin(this.convertDegreesToRadians(tempData[s].labelAngle)))
10162
+ }
10163
+ this.data = {
10164
+ segments: tempData,
10165
+ total: tempTotal
10166
+ }
10167
+ console.log(this.data)
10168
+
10169
+ }
10170
+ }
10171
+
9659
10172
  class WebsyLegend {
9660
10173
  constructor (elementId, options) {
9661
10174
  const DEFAULTS = {
@@ -10128,6 +10641,8 @@ const WebsyDesigns = {
10128
10641
  Chart: WebsyChart,
10129
10642
  WebsyChartTooltip,
10130
10643
  ChartTooltip: WebsyChartTooltip,
10644
+ Pie: WebsyPie,
10645
+ WebsyPie,
10131
10646
  Legend: WebsyLegend,
10132
10647
  WebsyMap,
10133
10648
  Map: WebsyMap,