Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pie-donut): formatter not recieving entire value #1662

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading