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

feat[pie/donut]: custom tooltips for aggregate #305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 16 additions & 7 deletions dist/frappe-charts.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,6 @@ class SvgTip {
}
}

/**
* Returns the value of a number upto 2 decimal places.
* @param {Number} d Any number
*/
function floatTwo(d) {
return parseFloat(d.toFixed(2));
}
Expand Down Expand Up @@ -2519,6 +2515,9 @@ class PieChart extends AggregationChart {
this.config.startAngle = args.startAngle || 0;

this.clockWise = args.clockWise || false;

args.tooltipOptions = args.tooltipOptions || {};
this.config.formatTooltip = args.tooltipOptions.formatTooltip;
}

calc() {
Expand Down Expand Up @@ -2612,7 +2611,11 @@ class PieChart extends AggregationChart {
let title = (this.formatted_labels && this.formatted_labels.length > 0
? this.formatted_labels[i] : this.state.labels[i]) + ': ';
let percent = (this.state.sliceTotals[i] * 100 / this.state.grandTotal).toFixed(1);
this.tip.setValues(x, y, {name: title, value: percent + "%"});
let valueTooltip = percent + "%";
if(this.config.formatTooltip){
valueTooltip = this.config.formatTooltip(this.state.sliceTotals[i],this.state.grandTotal);
}
this.tip.setValues(x, y, {name: title, value: valueTooltip });
this.tip.showTip();
} else {
transform(path,'translate3d(0,0,0)');
Expand Down Expand Up @@ -3887,6 +3890,9 @@ class DonutChart extends AggregationChart {

this.clockWise = args.clockWise || false;
this.strokeWidth = args.strokeWidth || 30;

args.tooltipOptions = args.tooltipOptions || {};
this.config.formatTooltip = args.tooltipOptions.formatTooltip;
}

calc() {
Expand Down Expand Up @@ -3985,7 +3991,11 @@ class DonutChart extends AggregationChart {
let title = (this.formatted_labels && this.formatted_labels.length > 0
? this.formatted_labels[i] : this.state.labels[i]) + ': ';
let percent = (this.state.sliceTotals[i] * 100 / this.state.grandTotal).toFixed(1);
this.tip.setValues(x, y, {name: title, value: percent + "%"});
let valueTooltip = percent + "%";
if(this.config.formatTooltip){
valueTooltip = this.config.formatTooltip(this.state.sliceTotals[i],this.state.grandTotal);
}
this.tip.setValues(x, y, {name: title, value: valueTooltip });
this.tip.showTip();
} else {
transform(path,'translate3d(0,0,0)');
Expand Down Expand Up @@ -4020,7 +4030,6 @@ class DonutChart extends AggregationChart {
}
}

// import MultiAxisChart from './charts/MultiAxisChart';
const chartTypes = {
bar: AxisChart,
line: AxisChart,
Expand Down
2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.iife.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js.map

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/js/charts/DonutChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export default class DonutChart extends AggregationChart {

this.clockWise = args.clockWise || false;
this.strokeWidth = args.strokeWidth || 30;

args.tooltipOptions = args.tooltipOptions || {};
this.config.formatTooltip = args.tooltipOptions.formatTooltip;
}

calc() {
Expand Down Expand Up @@ -125,7 +128,11 @@ export default class DonutChart extends AggregationChart {
let title = (this.formatted_labels && this.formatted_labels.length > 0
? this.formatted_labels[i] : this.state.labels[i]) + ': ';
let percent = (this.state.sliceTotals[i] * 100 / this.state.grandTotal).toFixed(1);
this.tip.setValues(x, y, {name: title, value: percent + "%"});
let valueTooltip = percent + "%";
if(this.config.formatTooltip){
valueTooltip = this.config.formatTooltip(this.state.sliceTotals[i],this.state.grandTotal);
}
this.tip.setValues(x, y, {name: title, value: valueTooltip });
this.tip.showTip();
} else {
transform(path,'translate3d(0,0,0)');
Expand Down
9 changes: 8 additions & 1 deletion src/js/charts/PieChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class PieChart extends AggregationChart {
this.config.startAngle = args.startAngle || 0;

this.clockWise = args.clockWise || false;

args.tooltipOptions = args.tooltipOptions || {};
this.config.formatTooltip = args.tooltipOptions.formatTooltip;
}

calc() {
Expand Down Expand Up @@ -119,7 +122,11 @@ export default class PieChart extends AggregationChart {
let title = (this.formatted_labels && this.formatted_labels.length > 0
? this.formatted_labels[i] : this.state.labels[i]) + ': ';
let percent = (this.state.sliceTotals[i] * 100 / this.state.grandTotal).toFixed(1);
this.tip.setValues(x, y, {name: title, value: percent + "%"});
let valueTooltip = percent + "%";
if(this.config.formatTooltip){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not reuse the formatTooltipX and formatTooltipY?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to confuse this single type of values of aggregate charts with the charts legend X and Y cause in aggregate the concept of X and Y is not present.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatTooltip itself is very generic, can we name it better? formatAggrTooltip is something that I can think of now, do you have any suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond this, the feature works like a breeze, we're good to merge it.

valueTooltip = this.config.formatTooltip(this.state.sliceTotals[i],this.state.grandTotal);
}
this.tip.setValues(x, y, {name: title, value: valueTooltip });
this.tip.showTip();
} else {
transform(path,'translate3d(0,0,0)');
Expand Down