Skip to content

Commit 171b2a4

Browse files
Fix custom format issue in chartjs #981
1 parent 65dd41d commit 171b2a4

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,31 @@
118118
return;
119119
}
120120

121+
// Format series label.
122+
settings.plugins.tooltip = {
123+
callbacks: {
124+
label: function(context) {
125+
var label = context.dataset.label || '';
126+
if ( label ) {
127+
label += ': ';
128+
}
129+
130+
var format = context.dataset.format || '';
131+
if ( format ) {
132+
label += format_datum( context.formattedValue, format );
133+
} else {
134+
format = 'undefined' !== typeof context.chart.config._config.options.format ? context.chart.config._config.options.format : '';
135+
if ( format ) {
136+
label += format_datum( context.formattedValue, format );
137+
} else {
138+
label += context.formattedValue;
139+
}
140+
}
141+
return label;
142+
}
143+
}
144+
};
145+
121146
var chartjs = new Chart(context, {
122147
type: type,
123148
data: {
@@ -131,7 +156,7 @@
131156
if ( $( '#chart-img' ).length ) {
132157
$( '#chart-img' ).val( canvas.toDataURL() );
133158
}
134-
},
159+
}
135160
}],
136161
});
137162

0 commit comments

Comments
 (0)