score:2
after a couple of days searching for a workaround for this and since i didn't get any response until now, i made some tests to identify the minimum supported value for it to work properly. right now, i have it working on the devices mentioned before.
on the following example there are various test values for the "data" array so feel free to test them and new ones to identify any bug.
so here's what i came up with:
<!doctype html>
<html>
<head>
<title>chart test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/1.0.1-beta.2/chart.js"></script>
</head>
<body>
<canvas width="300" height="300"></canvas>
<script>
var canvas = $( 'canvas' )
, lowestvalue = 0.001 // minimum supported value
, highestvalue = 0
, emptysection = {
value : lowestvalue
, color : '#e8e8e8'
, highlight : '#e8e8e8'
, label : ''
}
, data = []
/*, data = [
{
value : 0
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
]
, data = [
{
value : 1
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
]
, data = [
{
value : 0
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
, {
value : 0
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
]
, data = [
{
value : 0
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
, {
value : 1
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
]
, data = [
{
value : 1
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
, {
value : 0
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
]
, data = [
{
value : 1
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
, {
value : 2
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
]
, data = [
{
value : 0.000001
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
, {
value : 0.0058
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 1
, color : '#faec23'
, highlight : '#faec23'
, label : 'yellow'
}
]
, data = [
{
value : 0.00
, color : '#f7464a'
, highlight : '#ff5a5e'
, label : 'red'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 0.00
, color : '#15d42f'
, highlight : '#15d42f'
, label : 'green'
}
, {
value : 1
, color : '#faec23'
, highlight : '#faec23'
, label : 'yellow'
}
]*/
, options = {
'percentageinnercutout': 70
, 'showtooltips' : false
, 'animatescale' : true
}
, chart = {};
// if there are no valid segments, include two new ones an set the value of one of them to 1
// so that the chart appears.
if ( data.length === 0 ) {
data.push( $.extend( true, {}, emptysection ) );
data.push( $.extend( true, {}, emptysection ) );
data[ 0 ].value = 1;
// even if there are segments, we add a new one with one of the values:
// - 10: if the highest value of any segment is lower that the lowest allowed (0.001).
// - 0.001 of the highest value
} else {
$.each( data, function( index, el ) {
el.value = el.value < lowestvalue ? lowestvalue : el.value;
highestvalue = el.value > highestvalue ? el.value : highestvalue;
});
data.push( $.extend( true, {}, emptysection ) );
// set the value of the new segment.
// get 0.001% of the highest value if it's greater that the lowest allowed.
// if it's not greater that the lowest allowed, set it to a value big enought so the other segments don't appear.
data[ data.length - 1 ].value = highestvalue > lowestvalue ? determinepercentage( highestvalue ) : 10;
}
// create the chart.
chart = new chart( canvas.get( 0 ).getcontext( '2d' ) ).doughnut( data, options );
/**
* determines a specific percentage of a value. if no percentage is passed it assumes the lowest allowed (0.001).
*/
function determinepercentage( total, percentage ) {
percentage = percentage || lowestvalue;
return total ? ( parsefloat( total ) * percentage ) / 100 : 0;
}
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- Chartjs Doughnut disappears
- ChartJS Doughnut Charts Gradient Fill
- Chartjs extended doughnut with text tooltip issue
- Chartjs doughnut chart with gradient color
- ChartJs Doughnut parsing datasets
- How to add ChartJS code in Html2Pdf to view image
- Chartjs Nested Doughnut Layers With Different Thickness
- Add Text to Doughnut Chart - ChartJS
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Chartjs not working with d3 from csv source
- Force ChartJS to show Doughnut chart with null values
- Small value in doughnut chart is not visible - Chartjs
- ChartJs - Round borders on a doughnut chart with multiple datasets
- Highlight doughnut segment on mouseenter generateLegend Chartjs 2
- ChartJS doughnut data toggle on custom button click
- ChartJS doughnut legend click
- Chartjs - how to change the notation of doughnut chart
- hide label on doughnut chart of chartjs
- Chartjs doughnut chart rounded corners for half doghnut
- Chartjs Doughnut Chart smooth animation in countdown
- ChartJS : Hook before values are given to doughnut (absolute value in chart and real values in the tooltips)
- ChartJS have xAxes labels match data source
- Label issue in doughnut chart using chartjs
- Updating Chartjs to 2.5 with custom code
- ChartJs 2 How to remove numbers from the doughnut chart on loading
- Chartjs doughnut with multiple dataset issue in legend creation
- Angular 2 ng2-charts doughnut text in the middle disappears on mouse hover
- ChartJS showing old values when mouse over on Doughnut after updating values
- ChartJS custom doughnut chart not showing in version 2.6.0
- ChartJS custom legend doughnut separate legend from chart area
More Query from same tag
- How to show slice value inside of slice in pie chart using chart.js
- How to convert JSON data to object that contains other objects?
- Want to draw data using chartjs in django
- Can I interpret Django object values as JSON data within the JS script in my HTML?
- Chart.js tooltip not showing on line chart
- Possible to merge 2 bars in bar chart (chart.js)
- Converting ChartJSCore to Highcharts.net - issue with dynamic series data in a loop
- Vue.js Vue-chart.js linear gradient background fill
- Cannot read property 'labels' of undefined
- Updating Chart Data on Charts.js with Ng2-Charts
- Chart.js stacked bar ordering depending on value
- Chart.js2 Radar, how to configure the label padding/spacing?
- How to change colours for Angular-Chart.js
- Chart.js: How to calculate the labelOffset based on the distance between ticks?
- How to create vertical arbitrary line in financial chart?
- chart.js Legends issue (not showing)
- Dynamicly update scatter/line chart in Chart.JS with multiple x/y grids
- JQuery Line Chart's X Axis not display in ASP.NET, C#, SQL Server
- Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale
- When I try to include the chart.js and angular chartjs files, I get: ReferenceError: require is not defined
- Chart.js - if there is not value show 0
- How to use PrimeNg Chart of Width and Height?
- Adding Image inside Linechart points in ChartJs
- angular-chart.js - chart height not working on iOS but works fine on Android and Web
- Chartjs displays numbers not time
- Round pie chart in rectangular canvas
- chart js put meter square / superscript on y axis
- Display text in center of doughnut
- How to get the X position at a specified chart item index?
- ChartJS - Customize Ticks/Labels on Y Axis