score:-1
The problem with the style is type: 'doughnut'
. This will display a doughnut style chart, if you want the pie style you should use type: 'pie'
For more info: http://www.chartjs.org/docs/latest/charts/doughnut.html
Also, about the data labels, I don't think that style is currently supported by chartJs, you can try to customize it: http://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip-configuration
Or try some options.. https://github.com/chartjs/chartjs-plugin-datalabels
score:2
For having this outlet you must add the following plugin to your project:
chartjs-plugin-piechart-outlabels
score:3
You have to write a plugin to do it.
In afterDraw
hook, you have to iterate over Chart Elements and calculate 3 points:
- Point1: center point of current Arc
- Point2: created by connecting the chart's center and
Point1
, have length = radius + X (x > 0 will give a better visual) - Point3: created by connecting
Point2
with chart's edge (left/right) based onPoint2.x
Draw 2 lines to connect Point1 with Point2, and Point2 with Point3 will give you this chart:
Sample code for chartjs 2.9.4:
const DoughnutLabelPlugin = {
afterDraw: (chart) => {
const ctx = chart.chart.ctx;
ctx.save();
ctx.font = "10px 'Averta Std CY'";
const chartCenterPoint = {
x:
(chart.chartArea.right - chart.chartArea.left) / 2 +
chart.chartArea.left,
y:
(chart.chartArea.bottom - chart.chartArea.top) / 2 +
chart.chartArea.top
};
chart.config.data.labels.forEach((label, i) => {
const meta = chart.getDatasetMeta(0);
const arc = meta.data[i];
const dataset = chart.config.data.datasets[0];
// Prepare data to draw
// important point 1
const centerPoint = arc.getCenterPoint();
const model = arc._model;
let color = model.borderColor;
let labelColor = model.borderColor;
if (dataset.polyline && dataset.polyline.color) {
color = dataset.polyline.color;
}
if (dataset.polyline && dataset.polyline.labelColor) {
labelColor = dataset.polyline.labelColor;
}
const angle = Math.atan2(
centerPoint.y - chartCenterPoint.y,
centerPoint.x - chartCenterPoint.x
);
// important point 2
const point2X =
chartCenterPoint.x + Math.cos(angle) * (model.outerRadius + 20);
let point2Y =
chartCenterPoint.y + Math.sin(angle) * (model.outerRadius + 20);
let value = dataset.data[i];
if (dataset.polyline && dataset.polyline.formatter) {
value = dataset.polyline.formatter(value);
}
let edgePointX = point2X < chartCenterPoint.x ? 10 : chart.width - 10;
//DRAW CODE
// first line: connect between arc's center point and outside point
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(centerPoint.x, centerPoint.y);
ctx.lineTo(point2X, point2Y);
ctx.stroke();
// second line: connect between outside point and chart's edge
ctx.beginPath();
ctx.moveTo(point2X, point2Y);
ctx.lineTo(edgePointX, point2Y);
ctx.stroke();
//fill custom label
const labelAlignStyle =
edgePointX < chartCenterPoint.x ? "left" : "right";
const labelX = edgePointX;
const labelY = point2Y;
ctx.textAlign = labelAlignStyle;
ctx.textBaseline = "bottom";
ctx.fillStyle = labelColor;
ctx.fillText(value, labelX, labelY);
});
ctx.restore();
}
}
Demo: https://codesandbox.io/s/wispy-bash-nt7ty?file=/src/Chart.js
For draw a line with hand-drawn style, I recommend to use https://roughjs.com
Source: stackoverflow.com
Related Query
- How to display data labels outside in pie chart with lines in ionic
- How to display labels outside the pie chart border?
- How to display Line Chart dataset point labels with Chart.js?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Display data labels on a pie chart in angular-chart.js
- How to display chart using Chartjs with JSON data in Laravel
- How to display Tooltip without hovering pie chart with Chart.JS
- how to write labels along with data on top and bottom of each stack in bar chart
- How write the labels of the data inside a Doughnut Chart made with Chart.js?
- How to display data with percentage from database in pie chartjs?
- How to create a chart-js pie chart with PHP data variables?
- Chart.js - Display data label leader lines on a pie chart
- How to set data values as labels in Chart.js with a Radar Chart
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- chart.js: Show labels outside pie chart
- Display values outside of pie chart in chartjs
- How to display inline values in a stacked bar chart with Chart.js?
- How to display the labels in doughnut chart using ng2 charts?
- ChartJs line chart - display permanent icon above some data points with text on hover
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- How can I trigger the hover mode from outside the chart with charts.js 2?
- Show "No Data" message for Pie chart with no data
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- How to add images to chart labels with vue-chartjs?
- Angular chart how to show the legend data value by default along with legend name
- How to create chartjs chart with data from database C#
- How to display timestamped data with PrimeFaces and ChartJs?
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
More Query from same tag
- Stacked line chart not displaying correctly when Xaxis is time
- Chart.js 3.4/Vue3 How to hide border ticks and set specific amount of grid lines
- How to add border in chartjs?
- Chart.js is only displaying after f12 is pressed
- Multi-checkboxes And Chart Data
- chart.js - problems with axes options - what am I doing wrong?
- Chart.js - Unable to refresh the page
- cartjs tooltips displays in the wrong place on a chart
- Start bar-chart at 0 using ChartJS
- React Chartjs - Update Chart on interval
- ChartJS - change chart type by changing y-axis Label
- How to show/hide animation when legend clicked chart js
- Charts.js is automatically adding comma as thousands separator when it shouldn't
- How to unit test statement 'chart.chart.update()' in jasmine?
- Chart.js isn't able to read elements from php as label
- How to increase space between label and chart area in chart.js
- Charts.js - Bubble chart with two word axis
- Chart.js labels of x-axes makes useless space in chartarea
- React-chartjs-2: How to display timeline of released dates over 5 past years in Bar Chart?
- Display Date Label in axes - chart js/ng2 charts
- Ng2-Charts in Angular: Is 'Auto' supported for display property of Axes
- PHP restructure array of data
- chart js Labels and Grouping labels
- Chart.js library V.2 change Cursor to not allowed
- Why is my chart not working? What am I missing
- Horizontal bar chart in chart.js
- Creating dynamic object to reorganize API response using Typescript
- How to add an extra tick on top of the highest bar in Chart.js v2.9.4 (grace)?
- Chart.js how to increase segments size to be more evenly distributed
- Line graph - ChartJS