I have a Webapp using Angular v4.0.1 and ngx-charts (uses d3) v5.1.2 creating a line-chart where the x-axis has date-values.
My Problem is that the x-axis does not show the german time-format. So I found out how I can set locale formatting for d3:
import * as d3 from "d3";
import * as formatDE from "d3-format/locale/de-DE.json";
import * as timeFormatDE from "d3-time-format/locale/de-DE.json";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
BrowserAnimationsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor() {
var formatBefore = d3.timeFormat("%A");
console.log('Before: '+formatBefore(new Date));
// output: Thursday -> OK
d3.formatDefaultLocale(formatDE);
d3.timeFormatDefaultLocale(timeFormatDE);
var formatAfter = d3.timeFormat("%A");
console.log('After: '+formatAfter(new Date));
// output: Donnerstag -> YES, nice
}
}
But this has now effect for the x-axis! The date and time-value are still in english format.
Although ngx-charts wraps d3 not all d3 tricks work with it. Most ngx-charts components have an
xAxisTickFormatting
input that you connect to your own formatting function, e.g.:[Updated 2018-11-03 with a more detailed example]
Pay attention to the Date.toLocaleString() reference:
Starting a new Angular project from scratch to demo this fully...
In app.module.ts:
In app.component.html:
Finally, in app.component.ts:
What we've done so far enables you to switch between the out-of-the-box languages for an ngx-rocket application, en-US and fr-FR:
.
.
You can add some basic plumbing and translations to enable switching to de-DE as well:
.
Hope this helps!