-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffc03df
commit 86beba0
Showing
18 changed files
with
368 additions
and
14 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
src/visualizations/config/adapters/dhis_highcharts/customSVGOptions/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...gleValue/getSingleValueBackgroundColor.js → ...e/config/getSingleValueBackgroundColor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ngleValue/getSingleValueFormattedValue.js → ...ue/config/getSingleValueFormattedValue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../singleValue/getSingleValueLegendColor.js → ...Value/config/getSingleValueLegendColor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ns/singleValue/getSingleValueTextColor.js → ...leValue/config/getSingleValueTextColor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
src/visualizations/config/adapters/dhis_highcharts/customSVGOptions/singleValue/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { getSingleValueCustomSVGOptions } from './getSingleValueCustomSVGOptions.js' | ||
export { getSingleValueBackgroundColor } from './getSingleValueBackgroundColor.js' | ||
export { getSingleValueTextColor } from './getSingleValueTextColor.js' | ||
export { getSingleValueCustomSVGOptions } from './config/getSingleValueCustomSVGOptions.js' | ||
export { getSingleValueBackgroundColor } from './config/getSingleValueBackgroundColor.js' | ||
export { getSingleValueTextColor } from './config/getSingleValueTextColor.js' |
25 changes: 25 additions & 0 deletions
25
...s/config/adapters/dhis_highcharts/customSVGOptions/singleValue/renderer/addIconElement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const parser = new DOMParser() | ||
|
||
export function addIconElement(svgString, color) { | ||
const svgIconDocument = parser.parseFromString(svgString, 'image/svg+xml') | ||
const iconElHeight = svgIconDocument.documentElement.getAttribute('height') | ||
const iconElWidth = svgIconDocument.documentElement.getAttribute('width') | ||
const iconGroup = this.renderer | ||
.g('icon') | ||
.attr('data-test', 'visualization-icon') | ||
.css({ | ||
color, | ||
}) | ||
/* Force the group element to have the same dimensions as the original | ||
* SVG image by adding this rect. This ensures the icon has the intended | ||
* whitespace around it and makes scaling and translating easier. */ | ||
this.renderer.rect(0, 0, iconElWidth, iconElHeight).add(iconGroup) | ||
|
||
Array.from(svgIconDocument.documentElement.children).forEach((node) => | ||
iconGroup.element.appendChild(node) | ||
) | ||
|
||
iconGroup.add() | ||
|
||
return iconGroup | ||
} |
23 changes: 23 additions & 0 deletions
23
...pters/dhis_highcharts/customSVGOptions/singleValue/renderer/checkIfFitsWithinContainer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export function checkIfFitsWithinContainer( | ||
availableSpace, | ||
valueElement, | ||
subTextElement, | ||
icon, | ||
subText, | ||
spacing | ||
) { | ||
const valueRect = valueElement.getBBox() | ||
const subTextRect = subTextElement.getBBox() | ||
const requiredValueWidth = icon | ||
? valueRect.width + spacing.iconGap + spacing.iconSize | ||
: valueRect.width | ||
const requiredHeight = subText | ||
? valueRect.height + spacing.subTextTop + subTextRect.height | ||
: valueRect.height | ||
const fitsHorizontally = | ||
availableSpace.width > requiredValueWidth && | ||
availableSpace.width > subTextRect.width | ||
const fitsVertically = availableSpace.height > requiredHeight | ||
|
||
return fitsHorizontally && fitsVertically | ||
} |
15 changes: 15 additions & 0 deletions
15
...onfig/adapters/dhis_highcharts/customSVGOptions/singleValue/renderer/computeSpacingTop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function computeSpacingTop(valueSpacingTop) { | ||
if (this.subtitle.textStr) { | ||
/* If a subtitle is present this will be below the title so base | ||
* the value X position on this */ | ||
const subTitleRect = this.subtitle.element.getBBox() | ||
return subTitleRect.y + subTitleRect.height + valueSpacingTop | ||
} else if (this.title.textStr) { | ||
// Otherwise base on title | ||
const titleRect = this.title.element.getBBox() | ||
return titleRect.y + titleRect.height + valueSpacingTop | ||
} else { | ||
// If neither are present only adjust for valueSpacingTop | ||
return this.chartHeight - valueSpacingTop | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...onfig/adapters/dhis_highcharts/customSVGOptions/singleValue/renderer/getAvailableSpace.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { computeSpacingTop } from './computeSpacingTop.js' | ||
import { MIN_SIDE_WHITESPACE } from './styles.js' | ||
|
||
export function getAvailableSpace(valueSpacingTop) { | ||
return { | ||
height: | ||
this.chartHeight - computeSpacingTop.call(this, valueSpacingTop), | ||
width: this.chartWidth - MIN_SIDE_WHITESPACE * 2, | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
...config/adapters/dhis_highcharts/customSVGOptions/singleValue/renderer/positionElements.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { computeSpacingTop } from './computeSpacingTop.js' | ||
|
||
export function positionElements( | ||
valueElement, | ||
subTextElement, | ||
iconElement, | ||
spacing | ||
) { | ||
console.log( | ||
'++++positionElements++++', | ||
'\nvalueElement: ', | ||
valueElement, | ||
'\nsubTextElement: ', | ||
subTextElement, | ||
'\niconElement: ', | ||
iconElement, | ||
'\nspacing: ', | ||
spacing, | ||
'\n===============' | ||
) | ||
/* Layout here refers to a virtual rect that wraps | ||
* all indiviual parts of the single value visualization | ||
* (value, subtext and icon) */ | ||
const layoutRect = computeLayoutRect.call( | ||
this, | ||
valueElement, | ||
subTextElement, | ||
iconElement, | ||
spacing | ||
) | ||
|
||
// DEBUGGING THE RECT | ||
const debugRect = this.renderer | ||
.rect(layoutRect.x, layoutRect.y, layoutRect.width, layoutRect.height) | ||
.attr({ fill: 'orange', opacity: 0.3 }) | ||
.add() | ||
|
||
const myBBox = debugRect.getBBox() | ||
|
||
// const valueBox = valueElement.getBBox() | ||
// const valueTranslateX = iconElement | ||
// ? layoutRect.x + spacing.iconSize + spacing.iconGap | ||
// : layoutRect.x | ||
// valueElement.css({ | ||
// transform: `translate(${valueTranslateX}px, ${layoutRect.y}px)`, | ||
// }) | ||
// valueElement.attr({ | ||
// // TODO: cover the case where subtext is wider than value | ||
// x: iconElement | ||
// ? layoutRect.x + spacing.iconSize + spacing.iconGap | ||
// : layoutRect.x, | ||
// y: layoutRect.y, | ||
// dy: valueBox.height, | ||
// }) | ||
const valueElementBox = valueElement.getBBox() | ||
valueElement.align( | ||
{ | ||
align: 'right', | ||
verticalAlign: 'top', | ||
alignByTranslate: false, | ||
x: valueElementBox.width * -1, | ||
y: valueElementBox.height * (2 / 3), | ||
}, | ||
false, | ||
layoutRect | ||
) | ||
|
||
if (iconElement) { | ||
const { height } = iconElement.getBBox() | ||
const scaleFactor = spacing.iconSize / height | ||
|
||
// This all needs to be done using CSS translate because of the path cooordinates in the SVG icon | ||
iconElement.css({ | ||
transform: `translate(${layoutRect.x}px, ${layoutRect.y}px) scale(${scaleFactor})`, | ||
}) | ||
} | ||
|
||
if (subTextElement) { | ||
const { height: subTextHeight } = subTextElement.getBBox() | ||
subTextElement.attr({ | ||
x: iconElement | ||
? layoutRect.x + spacing.iconSize + spacing.iconGap | ||
: layoutRect.x, | ||
y: layoutRect.y + layoutRect.height - subTextHeight, | ||
}) | ||
} | ||
|
||
console.log( | ||
'++++positionElements++++', | ||
'\nvalueElement: ', | ||
valueElement, | ||
'\nsubTextElement: ', | ||
subTextElement, | ||
'\niconElement: ', | ||
iconElement, | ||
'\nspacing: ', | ||
spacing, | ||
'\nlayoutRect: ', | ||
layoutRect, | ||
'\n===============' | ||
) | ||
} | ||
|
||
function computeLayoutRect(valueElement, subTextElement, iconElement, spacing) { | ||
const valueRect = valueElement.getBBox() | ||
const containerCenterY = this.chartHeight / 2 | ||
const containerCenterX = this.chartWidth / 2 | ||
const minY = computeSpacingTop.call(this, spacing.valueTop) | ||
|
||
let width = valueRect.width | ||
let height = valueRect.height | ||
|
||
if (iconElement) { | ||
width += spacing.iconGap + spacing.iconSize | ||
} | ||
|
||
if (subTextElement) { | ||
const subTextRect = subTextElement.getBBox() | ||
console.log( | ||
`What is bigger? valueWidth: ${width} subTexttWidth ${subTextRect.width}` | ||
) | ||
width = Math.max(width, subTextRect.width) | ||
height += spacing.subTextTop + subTextRect.height | ||
} | ||
|
||
return { | ||
x: containerCenterX - width / 2, | ||
y: Math.max(containerCenterY - height / 2, minY), | ||
width, | ||
height, | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ig/adapters/dhis_highcharts/customSVGOptions/singleValue/renderer/renderSingleValueSVG.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { addIconElement } from './addIconElement.js' | ||
import { checkIfFitsWithinContainer } from './checkIfFitsWithinContainer.js' | ||
import { getAvailableSpace } from './getAvailableSpace.js' | ||
import { positionElements } from './positionElements.js' | ||
import { DynamicStyles } from './styles.js' | ||
|
||
export function renderSingleValueSVG() { | ||
const color = this.title.styles.color | ||
const { dashboard, formattedValue, icon, subText } = | ||
this.userOptions.customSVGOptions | ||
const dynamicStyles = new DynamicStyles() | ||
const valueElement = this.renderer | ||
.text(formattedValue) | ||
.css({ color, visibility: 'visible' }) | ||
.add() | ||
const subTextElement = subText | ||
? this.renderer | ||
.text(subText) | ||
.css({ color, visibility: 'visible' }) | ||
.add() | ||
: null | ||
const iconElement = icon ? addIconElement.call(this, icon) : null | ||
|
||
let fitsWithinContainer = false | ||
let styles = {} | ||
|
||
while (!fitsWithinContainer && dynamicStyles.hasNext()) { | ||
styles = dynamicStyles.next() | ||
|
||
valueElement.css(styles.value) | ||
subTextElement?.css(styles.subText) | ||
|
||
fitsWithinContainer = checkIfFitsWithinContainer( | ||
getAvailableSpace.call(this, styles.spacing.valueTop), | ||
valueElement, | ||
subTextElement, | ||
icon, | ||
subText, | ||
styles.spacing | ||
) | ||
} | ||
|
||
positionElements.call( | ||
this, | ||
valueElement, | ||
subTextElement, | ||
iconElement, | ||
styles.spacing | ||
) | ||
|
||
console.log( | ||
'+++++Render the SVG++++++', | ||
'\ncolor: ', | ||
color, | ||
'\ndashboard: ', | ||
dashboard, | ||
'\nformattedValue: ', | ||
formattedValue, | ||
'\nicon: ', | ||
icon, | ||
'\nsubText: ', | ||
subText, | ||
'\n=============' | ||
) | ||
console.log('CHART', this) | ||
} |
Oops, something went wrong.