Skip to content

Commit 6fe8cf5

Browse files
Merge pull request #982 from Codeinwp/bugfix/981
Fixed custom format issue in chartjs
2 parents 5aaf4d0 + 879efd5 commit 6fe8cf5

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

classes/Visualizer/Render/Sidebar/Type/ChartJS/Pie.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ protected function _renderChartTypeSettings() {
109109
esc_html__( 'If checked, the chart will be rendered as a donut chart.', 'visualizer' )
110110
);
111111

112+
self::_renderTextItem(
113+
esc_html__( 'Number Format', 'visualizer' ),
114+
'format',
115+
isset( $this->format ) ? $this->format : '',
116+
sprintf(
117+
'%s<br><br>%s<br><br>%s',
118+
esc_html__( 'Enter custom format pattern to apply to horizontal axis labels.', 'visualizer' ),
119+
sprintf(
120+
esc_html__( 'For number axis labels, this is a subset of the decimal formatting %1$sICU pattern set%2$s. For instance, $#,###.## will display values $1,234.56 for value 1234.56. Pay attention that if you use #&#37;&#37; percentage format then your values will be multiplied by 100.', 'visualizer' ),
121+
'<a href="http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details" target="_blank">',
122+
'</a>'
123+
),
124+
sprintf(
125+
esc_html__( 'For date axis labels, this is a subset of the date formatting %1$sICU date and time format%2$s.', 'visualizer' ),
126+
'<a href="https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax" target="_blank">',
127+
'</a>'
128+
)
129+
)
130+
);
131+
112132
self::_renderSectionEnd();
113133

114134
self::_renderGroupEnd();

js/render-chartjs.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,35 @@
118118
return;
119119
}
120120

121+
// Format series label.
122+
settings.plugins.tooltip = {
123+
callbacks: {
124+
label: function(context) {
125+
var label = '';
126+
if ( 'object' === typeof context.dataset.label ) {
127+
label = context.label || '';
128+
} else {
129+
label = context.dataset.label || '';
130+
}
131+
if ( label ) {
132+
label += ': ';
133+
}
134+
var format = context.dataset.format || '';
135+
if ( format ) {
136+
label += format_datum( context.formattedValue, format );
137+
} else {
138+
format = 'undefined' !== typeof context.chart.config._config.options.format ? context.chart.config._config.options.format : '';
139+
if ( format ) {
140+
label += format_datum( context.formattedValue, format );
141+
} else {
142+
label += context.formattedValue;
143+
}
144+
}
145+
return label;
146+
}
147+
}
148+
};
149+
121150
var chartjs = new Chart(context, {
122151
type: type,
123152
data: {
@@ -131,7 +160,7 @@
131160
if ( $( '#chart-img' ).length ) {
132161
$( '#chart-img' ).val( canvas.toDataURL() );
133162
}
134-
},
163+
}
135164
}],
136165
});
137166

0 commit comments

Comments
 (0)