chart.js 2.6.0
I need to render a chart that looks like this:
Always showing all tooltips is not an acceptable way, since they won't get rendered in a proper manner:
Unfortunately I couldn't find a solution yet. I've tried the piece-label plugin, but this has the same problems, since it's labels overlap and I can't hide certain labels.
Here is the code, that creates my chart using piece-label to position the labels above the slices:
private createStatusChart(): void {
const chartData = this.getStatusChartData();
if (!chartData) {
return;
}
const $container = $(Templates.Dashboard.ChartContainer({
ContainerID: 'chart-status',
HeaderText: 'Status'
}));
this._$content.append($container);
const legendOptions =
new Model.Charts.LegendOptions()
.SetDisplay(false);
const pieceLabelOptions =
new Model.Charts.PieceLabelOptions()
.SetRender('label')
.SetPosition('outside')
.SetArc(true)
.SetOverlap(true);
const options =
new Model.Charts.Options()
.SetLegend(legendOptions)
.SetPieceLabel(pieceLabelOptions);
const chartDefinition = new Model.Charts.Pie(chartData, options);
const ctx = this._$content.find('#chart-status canvas').get(0);
const chart = new Chart(ctx, chartDefinition);
}
private getStatusChartData(): Model.Charts.PieChartData {
if (!this._data) {
return;
}
const instance = this;
const data: Array<number> = [];
const labels: Array<string> = [];
const colors: Array<string> = [];
this._data.StatusGroupings.forEach(sg => {
if (!sg.StatusOID) {
data.push(sg.Count);
labels.push(i18next.t('Dashboard.NoStateSet'));
colors.push('#4572A7');
return;
}
const status = DAL.Properties.GetByOID(sg.StatusOID);
data.push(sg.Count);
labels.push(status ? status.Title : i18next.t('Misc.Unknown'));
colors.push(status ? status.Color : '#fff');
});
const dataset = new Model.Charts.Dataset(data).setBackgroundColor(colors);
return new Model.Charts.PieChartData(labels, [dataset]);
}
The result: