score:2
Accepted answer
you can use the plugin core api that offers a range of hooks that may be used for performing custom code. the afterdraw
hook may be used to draw the needle directly on the canvas
through canvasrenderingcontext2d
.
first we need to define the property needlevalue
inside the dataset.
datasets: [{
data: [35, 35, 35],
needlevalue: 580,
...
}],
the solution is based on the code sample provided by konstantin s.. please have a look at your amended code below.
new chart('oilchart', {
type: 'doughnut',
plugins: [{
afterdraw: chart => {
var needlevalue = chart.chart.config.data.datasets[0].needlevalue;
var datatotal = chart.chart.config.data.datasets[0].data.reduce((a, b) => a + b, 0);
var angle = math.pi + (1 / datatotal * needlevalue * math.pi);
var ctx = chart.chart.ctx;
var cw = chart.chart.canvas.offsetwidth;
var ch = chart.chart.canvas.offsetheight;
var cx = cw / 2;
var cy = ch - 6;
ctx.translate(cx, cy);
ctx.rotate(angle);
ctx.beginpath();
ctx.moveto(0, -3);
ctx.lineto(ch - 20, 0);
ctx.lineto(0, 3);
ctx.fillstyle = 'rgb(0, 0, 0)';
ctx.fill();
ctx.rotate(-angle);
ctx.translate(-cx, -cy);
ctx.beginpath();
ctx.arc(cx, cy, 5, 0, math.pi * 2);
ctx.fill();
}
}],
data: {
labels: [],
datasets: [{
data: [35, 35, 35],
needlevalue: 27,
backgroundcolor: [
'rgba(255, 99, 132, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(63, 191, 63, 0.2)'
]
}]
},
options: {
layout: {
padding: {
bottom: 3
}
},
rotation: -math.pi,
cutoutpercentage: 30,
circumference: math.pi,
legend: {
position: 'left'
},
animation: {
animaterotate: false,
animatescale: true
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.min.js"></script>
<canvas id="oilchart" height="80"></canvas>
Source: stackoverflow.com
Related Query
- How to draw a needle on a custom donut chart and add datapoints to the needle?
- How to add custom text inside the bar and how to reduce the step size in y axis in chart js( Bar chart )
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- How to increase the size of the donut or pie chart and keep the legend next to it in chart JS?
- How do you create rounded corners on the top and bottom of a bar chart and the between 2 part still draw like image?
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- How to add text inside the doughnut chart using Chart.js?
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
- How to change the color of legend in chartjs and be able to add one more legend?
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Chart js: how can I align the legend and the title
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- How to get the actual chart width and height in chart.js
- Add a custom label to the top or bottom of a stacked bar chart
- How can I add some text in the middle of a half doughnut chart in Chart.JS?
- How to add an image to a slice of a donut chart in chart.js?
- Using chart js version 3, how to add custom data to external tooltip?
- How to create a chart that uses strings for both the X and Y axes?
- how to add FAHRENHEIT symbol in chart js donut chart
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- How to sort XY line chart tooltip items based on value and add thousands separators?
- How to expand the slice of donut chart in chartjs?
- How can I have different values for the chart and the tooltip in chart.js?
- How to add text inside the doughnut chart using Chart.js version 3.2.1
- How to set the xAxes min and max values of time cartesian chart in Chart.js
- How to style a pie chart in chart js? I want to change the border color, border width and give them shadow
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- How to only draw the graph and the Xaxis gridlines using chart.js
More Query from same tag
- How to render Chart.js 2.x tooltips on top of everything
- Why tooltip position absolute does not apply using js?
- ChartJS - Change legend to toggle buttons
- Fade other lines when hovering legend in Chart.js
- HTML how block elements to not go over footer etc
- Chart.js don't work when I open my app as a stand alone app in Chrome
- Rest api / json error while using it with react chartjs
- Chart.js dynamically upating data using variable
- Chart.js -> line chart -> multiple points with the same X
- Chart.js Ionic 2 Angular2: Background color of bar graph not changing
- how to make realtime update chart like in package express-status-monitor?
- How can I change the position of legends in chart.js?
- Chart.JS Not Formatting Y-Axis-1 Properly
- Cant pass variable for some reason? PHP
- How i can refresh chart without refresh page?
- Visual Studio 2013 IIS web site - problems with JS files
- Pass Ajax response array to Chart.js
- Chart.js - Adding Legend in a JSP Page
- Using CHARTJS with a JavaScript Array
- Adding a label to a doughnut chart in Chart.js shows all values in each chart
- e.labels.map is not a function when creating a chart with chart.js
- ChartJS: datalabels: show percentage value in Pie piece
- why is the background black when with Chartjs export to a image
- Why is my dependency injection not working in Angular?
- Chart.js draw mathematical function
- Polar Area Chart
- Add Y-Axis dynamically Chartjs
- How can I apply hover properties only to the current point not the entire dataset
- ChartJS v2: how to remove strange line in Line Chart
- How do i make a new chart by pressing a button (chart.js)?