Skip to content

Commit

Permalink
fix(pie-donut): formatter not recieving entire value (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiyaJethwa authored Sep 18, 2023
1 parent 7fd2334 commit 9facba4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
15 changes: 12 additions & 3 deletions packages/core/src/components/graphs/pie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export class Pie extends Component {

// Draw the slice labels
const renderLabels = options.pie.labels.enabled
const labelData = renderLabels ? pieLayoutData.filter((x) => (x.data as any)[valueMapsTo] > 0) : []
const labelData = renderLabels
? pieLayoutData.filter(x => (x.data as any)[valueMapsTo] > 0)
: []
const labelsGroup = DOMUtils.appendOrSelect(svg, 'g.labels')
.attr('role', Roles.GROUP)
.attr('data-name', 'labels')
Expand All @@ -153,9 +155,16 @@ export class Pie extends Component {
.style('text-anchor', 'middle')
.text((d: any) => {
if (options.pie.labels.formatter) {
return options.pie.labels.formatter(d)
return options.pie.labels.formatter({
...d,
percentageValue: convertValueToPercentage(
d.data[valueMapsTo],
displayData,
valueMapsTo,
true
)
})
}

return convertValueToPercentage(d.data[valueMapsTo], displayData, valueMapsTo) + '%'
})
// Calculate dimensions in order to transform
Expand Down
26 changes: 16 additions & 10 deletions packages/core/src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { pointer, type Numeric } from 'd3'
import {
merge,
cloneDeep,
unionBy,
} from 'lodash-es'
import { merge, cloneDeep, unionBy } from 'lodash-es'
import { CartesianOrientations, ScaleTypes, TruncationTypes } from '@/interfaces/enums'
import { defaultLegendAdditionalItems } from './configuration-non-customizable'

export function debounceWithD3MousePosition(fn: any, delay: number, holder: any) {
let timer: any = null
return function(...args: any) {
return function (...args: any) {
const context = this

// Get D3 event here
Expand Down Expand Up @@ -199,11 +195,21 @@ export function capitalizeFirstLetter(word: string) {
* @param {string} key
* @returns The percentage in the form of a number (1 significant digit if necessary)
*/
export function convertValueToPercentage(item: any, fullData: any, key = 'value') {
export function convertValueToPercentage(
item: any,
fullData: any,
key = 'value',
entireValue = false
) {
const percentage =
(item / fullData.reduce((accum: number, val: any) => accum + val[key], 0)) * 100
// if the value has any significant figures, keep 1
return percentage % 1 !== 0 ? parseFloat(percentage.toFixed(1)) : percentage
//in need for entire float percentage value
if (entireValue) {
return percentage
} else {
// if the value has any significant figures, keep 1
return percentage % 1 !== 0 ? parseFloat(percentage.toFixed(1)) : percentage
}
}

/**
Expand Down Expand Up @@ -244,7 +250,7 @@ export function updateLegendAdditionalItems(defaultOptions: any, providedOptions

// Get default items in default options but not in provided options
const updatedDefaultItems = defaultLegendAdditionalItems.filter(
(item) => defaultTypes.includes(item.type) && !providedTypes.includes(item.type)
item => defaultTypes.includes(item.type) && !providedTypes.includes(item.type)
)

defaultOptions.legend.additionalItems = updatedDefaultItems
Expand Down

0 comments on commit 9facba4

Please sign in to comment.