New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

chartjs-gauge

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chartjs-gauge - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

samples/logo.svg

289

dist/chartjs-gauge.js
/*!
* chartjs-gauge.js v0.1.0
* chartjs-gauge.js v0.1.1
* https://github.com/haiiaaa/chartjs-gauge/
* (c) 2019 chartjs-gauge.js Contributors
* (c) 2020 chartjs-gauge.js Contributors
* Released under the MIT License

@@ -11,6 +11,55 @@ */

(global = global || self, global.Gauge = factory(global.Chart));
}(this, function (Chart) { 'use strict';
}(this, (function (Chart) { 'use strict';
Chart = Chart && Chart.hasOwnProperty('default') ? Chart['default'] : Chart;
Chart = Chart && Object.prototype.hasOwnProperty.call(Chart, 'default') ? Chart['default'] : Chart;
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
Chart.defaults._set('gauge', {

@@ -60,21 +109,30 @@ needle: {

const GaugeController = Chart.controllers.doughnut.extend({
getMaxValue(data) {
var GaugeController = Chart.controllers.doughnut.extend({
getMaxValue: function getMaxValue(data) {
// assuming array is sorted
return data[data.length - 1] || 1;
},
getWidth(chart) {
getWidth: function getWidth(chart) {
return chart.chartArea.right - chart.chartArea.left;
},
getAngle({
chart,
value,
maxValue
}) {
const {
rotation,
circumference
} = chart.options;
getTranslation: function getTranslation(chart) {
var chartArea = chart.chartArea,
offsetX = chart.offsetX,
offsetY = chart.offsetY;
var centerX = (chartArea.left + chartArea.right) / 2;
var centerY = (chartArea.top + chartArea.bottom) / 2;
var dx = centerX + offsetX;
var dy = centerY + offsetY;
return {
dx: dx,
dy: dy
};
},
getAngle: function getAngle(_ref) {
var chart = _ref.chart,
value = _ref.value,
maxValue = _ref.maxValue;
var _chart$options = chart.options,
rotation = _chart$options.rotation,
circumference = _chart$options.circumference;
return rotation + circumference * (value / maxValue);

@@ -94,3 +152,3 @@ },

*/
drawNeedle(ease) {
drawNeedle: function drawNeedle(ease) {
if (!this.chart.animating) {

@@ -101,31 +159,28 @@ // triggered when hovering

const {
ctx,
chartArea: {
bottom
},
config
} = this.chart;
const dataset = config.data.datasets[this.index];
const {
previous
} = this.getMeta();
const {
radiusPercentage,
widthPercentage,
lengthPercentage,
color
} = config.options.needle;
const {
innerRadius,
outerRadius
} = this.chart;
const width = this.getWidth(this.chart);
const needleRadius = radiusPercentage / 100 * width;
const needleWidth = widthPercentage / 100 * width;
const needleLength = lengthPercentage / 100 * (outerRadius - innerRadius) + innerRadius;
const dx = this.getWidth(this.chart) / 2;
const dy = bottom; // interpolate
var _this$chart = this.chart,
ctx = _this$chart.ctx,
config = _this$chart.config,
innerRadius = _this$chart.innerRadius,
outerRadius = _this$chart.outerRadius;
var dataset = config.data.datasets[this.index];
const origin = this.getAngle({
var _this$getMeta = this.getMeta(),
previous = _this$getMeta.previous;
var _config$options$needl = config.options.needle,
radiusPercentage = _config$options$needl.radiusPercentage,
widthPercentage = _config$options$needl.widthPercentage,
lengthPercentage = _config$options$needl.lengthPercentage,
color = _config$options$needl.color;
var width = this.getWidth(this.chart);
var needleRadius = radiusPercentage / 100 * width;
var needleWidth = widthPercentage / 100 * width;
var needleLength = lengthPercentage / 100 * (outerRadius - innerRadius) + innerRadius; // center
var _this$getTranslation = this.getTranslation(this.chart),
dx = _this$getTranslation.dx,
dy = _this$getTranslation.dy; // interpolate
var origin = this.getAngle({
chart: this.chart,

@@ -136,3 +191,3 @@ value: previous.value,

const target = this.getAngle({
var target = this.getAngle({
chart: this.chart,

@@ -142,3 +197,3 @@ value: dataset.value,

});
const angle = origin + (target - origin) * ease; // draw
var angle = origin + (target - origin) * ease; // draw

@@ -161,4 +216,3 @@ ctx.save();

},
drawValueLabel(ease) {
drawValueLabel: function drawValueLabel(ease) {
// eslint-disable-line no-unused-vars

@@ -169,24 +223,21 @@ if (!this.chart.config.options.valueLabel.display) {

const {
ctx,
chartArea: {
bottom
},
config
} = this.chart;
const dataset = config.data.datasets[this.index];
const {
formatter,
color,
backgroundColor,
borderRadius,
padding,
bottomMarginPercentage
} = config.options.valueLabel;
const width = this.getWidth(this.chart);
const bottomMargin = bottomMarginPercentage / 100 * width;
var _this$chart2 = this.chart,
ctx = _this$chart2.ctx,
config = _this$chart2.config;
var dataset = config.data.datasets[this.index];
var _config$options$value = config.options.valueLabel,
formatter = _config$options$value.formatter,
color = _config$options$value.color,
backgroundColor = _config$options$value.backgroundColor,
borderRadius = _config$options$value.borderRadius,
padding = _config$options$value.padding,
bottomMarginPercentage = _config$options$value.bottomMarginPercentage;
var width = this.getWidth(this.chart);
var bottomMargin = bottomMarginPercentage / 100 * width;
const fmt = formatter || (value => value);
var fmt = formatter || function (value) {
return value;
};
const valueText = fmt(dataset.value).toString();
var valueText = fmt(dataset.value).toString();
ctx.textBaseline = 'middle';

@@ -196,14 +247,21 @@ ctx.textAlign = 'center'; // const { width: textWidth, actualBoundingBoxAscent, actualBoundingBoxDescent } = ctx.measureText(valueText);

const {
width: textWidth
} = ctx.measureText(valueText); // approximate height until browsers support advanced TextMetrics
var _ctx$measureText = ctx.measureText(valueText),
textWidth = _ctx$measureText.width; // approximate height until browsers support advanced TextMetrics
const textHeight = ctx.measureText('M').width;
const x = -(padding.left + textWidth / 2);
const y = -(padding.top + textHeight / 2);
const w = padding.left + textWidth + padding.right;
const h = padding.top + textHeight + padding.bottom;
const dx = width / 2;
const dy = bottom - bottomMargin; // draw
var textHeight = ctx.measureText('M').width;
var x = -(padding.left + textWidth / 2);
var y = -(padding.top + textHeight / 2);
var w = padding.left + textWidth + padding.right;
var h = padding.top + textHeight + padding.bottom; // center
var _this$getTranslation2 = this.getTranslation(this.chart),
dx = _this$getTranslation2.dx,
dy = _this$getTranslation2.dy; // add rotation
var rotation = this.chart.options.rotation % (Math.PI * 2.0);
dx += bottomMargin * Math.cos(rotation + Math.PI / 2);
dy += bottomMargin * Math.sin(rotation + Math.PI / 2); // draw
ctx.save();

@@ -221,8 +279,7 @@ ctx.translate(dx, dy); // draw background

},
// overrides
update(reset) {
const dataset = this.chart.config.data.datasets[this.index];
const meta = this.getMeta();
const initialValue = {
update: function update(reset) {
var dataset = this.chart.config.data.datasets[this.index];
var meta = this.getMeta();
var initialValue = {
value: 0,

@@ -236,3 +293,5 @@ maxValue: 1

} else {
dataset.data.sort((a, b) => a - b);
dataset.data.sort(function (a, b) {
return a - b;
});
meta.previous = meta.current || initialValue;

@@ -247,28 +306,22 @@ meta.current = {

},
updateElement(arc, index, reset) {
updateElement: function updateElement(arc, index, reset) {
// TODO handle reset and options.animation
Chart.controllers.doughnut.prototype.updateElement.call(this, arc, index, reset);
const dataset = this.getDataset();
const {
data
} = dataset;
const {
options
} = this.chart.config; // scale data
var dataset = this.getDataset();
var data = dataset.data;
var options = this.chart.config.options; // scale data
const maxValue = this.getMaxValue(data);
const previousValue = data[index - 1] || 0;
const value = data[index];
const startAngle = options.rotation + options.circumference * (previousValue / maxValue);
const endAngle = startAngle + options.circumference * ((value - previousValue) / maxValue);
const circumference = endAngle - startAngle;
Object.assign(arc._model, {
startAngle,
endAngle,
circumference
var maxValue = this.getMaxValue(data);
var previousValue = data[index - 1] || 0;
var value = data[index];
var startAngle = options.rotation + options.circumference * (previousValue / maxValue);
var endAngle = startAngle + options.circumference * ((value - previousValue) / maxValue);
var circumference = endAngle - startAngle;
arc._model = _objectSpread2({}, arc._model, {
startAngle: startAngle,
endAngle: endAngle,
circumference: circumference
});
},
draw(ease) {
draw: function draw(ease) {
Chart.controllers.doughnut.prototype.draw.call(this, ease);

@@ -278,8 +331,22 @@ this.drawNeedle(ease);

}
});
/* eslint-disable max-len, func-names */
var polyfill = function polyfill() {
if (CanvasRenderingContext2D.prototype.ellipse === undefined) {
CanvasRenderingContext2D.prototype.ellipse = function (x, y, radiusX, radiusY, rotation, startAngle, endAngle, antiClockwise) {
this.save();
this.translate(x, y);
this.rotate(rotation);
this.scale(radiusX, radiusY);
this.arc(0, 0, 1, startAngle, endAngle, antiClockwise);
this.restore();
};
}
};
polyfill();
Chart.controllers.gauge = GaugeController;
Chart.Gauge = (context, config) => {
Chart.Gauge = function (context, config) {
config.type = 'gauge';

@@ -293,2 +360,2 @@ return new Chart(context, config);

}));
})));
/*!
* chartjs-gauge.js v0.1.0
* chartjs-gauge.js v0.1.1
* https://github.com/haiiaaa/chartjs-gauge/
* (c) 2019 chartjs-gauge.js Contributors
* (c) 2020 chartjs-gauge.js Contributors
* Released under the MIT License
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("chart.js")):"function"==typeof define&&define.amd?define(["chart.js"],e):(t=t||self).Gauge=e(t.Chart)}(this,(function(t){"use strict";(t=t&&t.hasOwnProperty("default")?t.default:t).defaults._set("gauge",{needle:{radiusPercentage:2,widthPercentage:3.2,lengthPercentage:80,color:"rgba(0, 0, 0, 1)"},valueLabel:{display:!0,formatter:null,color:"rgba(255, 255, 255, 1)",backgroundColor:"rgba(0, 0, 0, 1)",borderRadius:5,padding:{top:5,right:5,bottom:5,left:5},bottomMarginPercentage:5},animation:{duration:1e3,animateRotate:!0,animateScale:!1},cutoutPercentage:50,rotation:-Math.PI,circumference:Math.PI,legend:{display:!1},tooltips:{enabled:!1}});const e=t.controllers.doughnut.extend({getMaxValue:t=>t[t.length-1]||1,getWidth:t=>t.chartArea.right-t.chartArea.left,getAngle({chart:t,value:e,maxValue:a}){const{rotation:r,circumference:o}=t.options;return r+o*(e/a)},drawNeedle(t){this.chart.animating||(t=1);const{ctx:e,chartArea:{bottom:a},config:r}=this.chart,o=r.data.datasets[this.index],{previous:i}=this.getMeta(),{radiusPercentage:l,widthPercentage:n,lengthPercentage:s,color:c}=r.options.needle,{innerRadius:u,outerRadius:h}=this.chart,d=this.getWidth(this.chart),g=l/100*d,f=n/100*d,p=s/100*(h-u)+u,m=this.getWidth(this.chart)/2,b=a,x=this.getAngle({chart:this.chart,value:i.value,maxValue:i.maxValue}),v=x+(this.getAngle({chart:this.chart,value:o.value,maxValue:this.getMaxValue(o.data)})-x)*t;e.save(),e.translate(m,b),e.rotate(v),e.fillStyle=c,e.beginPath(),e.ellipse(0,0,g,g,0,0,2*Math.PI),e.fill(),e.beginPath(),e.moveTo(0,f/2),e.lineTo(p,0),e.lineTo(0,-f/2),e.fill(),e.restore()},drawValueLabel(e){if(!this.chart.config.options.valueLabel.display)return;const{ctx:a,chartArea:{bottom:r},config:o}=this.chart,i=o.data.datasets[this.index],{formatter:l,color:n,backgroundColor:s,borderRadius:c,padding:u,bottomMarginPercentage:h}=o.options.valueLabel,d=this.getWidth(this.chart),g=h/100*d,f=(l||(t=>t))(i.value).toString();a.textBaseline="middle",a.textAlign="center";const{width:p}=a.measureText(f),m=a.measureText("M").width,b=-(u.left+p/2),x=-(u.top+m/2),v=u.left+p+u.right,P=u.top+m+u.bottom,y=d/2,w=r-g;a.save(),a.translate(y,w),a.beginPath(),t.helpers.canvas.roundedRect(a,b,x,v,P,c),a.fillStyle=s,a.fill(),a.fillStyle=n||o.options.defaultFontColor,a.fillText(f,0,m/2-u.bottom),a.restore()},update(e){const a=this.chart.config.data.datasets[this.index],r=this.getMeta(),o={value:0,maxValue:1};e?(r.previous=null,r.current=o):(a.data.sort((t,e)=>t-e),r.previous=r.current||o,r.current={value:a.value,maxValue:this.getMaxValue(a.data)}),t.controllers.doughnut.prototype.update.call(this,e)},updateElement(e,a,r){t.controllers.doughnut.prototype.updateElement.call(this,e,a,r);const o=this.getDataset(),{data:i}=o,{options:l}=this.chart.config,n=this.getMaxValue(i),s=i[a-1]||0,c=i[a],u=l.rotation+l.circumference*(s/n),h=u+l.circumference*((c-s)/n),d=h-u;Object.assign(e._model,{startAngle:u,endAngle:h,circumference:d})},draw(e){t.controllers.doughnut.prototype.draw.call(this,e),this.drawNeedle(e),this.drawValueLabel(e)}});return t.controllers.gauge=e,t.Gauge=(e,a)=>(a.type="gauge",new t(e,a)),t.Gauge}));
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("chart.js")):"function"==typeof define&&define.amd?define(["chart.js"],e):(t=t||self).Gauge=e(t.Chart)}(this,(function(t){"use strict";function e(t,e,a){return e in t?Object.defineProperty(t,e,{value:a,enumerable:!0,configurable:!0,writable:!0}):t[e]=a,t}function a(t,e){var a=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),a.push.apply(a,r)}return a}(t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t).defaults._set("gauge",{needle:{radiusPercentage:2,widthPercentage:3.2,lengthPercentage:80,color:"rgba(0, 0, 0, 1)"},valueLabel:{display:!0,formatter:null,color:"rgba(255, 255, 255, 1)",backgroundColor:"rgba(0, 0, 0, 1)",borderRadius:5,padding:{top:5,right:5,bottom:5,left:5},bottomMarginPercentage:5},animation:{duration:1e3,animateRotate:!0,animateScale:!1},cutoutPercentage:50,rotation:-Math.PI,circumference:Math.PI,legend:{display:!1},tooltips:{enabled:!1}});var r=t.controllers.doughnut.extend({getMaxValue:function(t){return t[t.length-1]||1},getWidth:function(t){return t.chartArea.right-t.chartArea.left},getTranslation:function(t){var e=t.chartArea,a=t.offsetX,r=t.offsetY;return{dx:(e.left+e.right)/2+a,dy:(e.top+e.bottom)/2+r}},getAngle:function(t){var e=t.chart,a=t.value,r=t.maxValue,n=e.options;return n.rotation+n.circumference*(a/r)},drawNeedle:function(t){this.chart.animating||(t=1);var e=this.chart,a=e.ctx,r=e.config,n=e.innerRadius,o=e.outerRadius,i=r.data.datasets[this.index],l=this.getMeta().previous,c=r.options.needle,s=c.radiusPercentage,u=c.widthPercentage,h=c.lengthPercentage,d=c.color,g=this.getWidth(this.chart),f=s/100*g,p=u/100*g,b=h/100*(o-n)+n,m=this.getTranslation(this.chart),v=m.dx,y=m.dy,x=this.getAngle({chart:this.chart,value:l.value,maxValue:l.maxValue}),P=x+(this.getAngle({chart:this.chart,value:i.value,maxValue:this.getMaxValue(i.data)})-x)*t;a.save(),a.translate(v,y),a.rotate(P),a.fillStyle=d,a.beginPath(),a.ellipse(0,0,f,f,0,0,2*Math.PI),a.fill(),a.beginPath(),a.moveTo(0,p/2),a.lineTo(b,0),a.lineTo(0,-p/2),a.fill(),a.restore()},drawValueLabel:function(e){if(this.chart.config.options.valueLabel.display){var a=this.chart,r=a.ctx,n=a.config,o=n.data.datasets[this.index],i=n.options.valueLabel,l=i.formatter,c=i.color,s=i.backgroundColor,u=i.borderRadius,h=i.padding,d=i.bottomMarginPercentage/100*this.getWidth(this.chart),g=(l||function(t){return t})(o.value).toString();r.textBaseline="middle",r.textAlign="center";var f=r.measureText(g).width,p=r.measureText("M").width,b=-(h.left+f/2),m=-(h.top+p/2),v=h.left+f+h.right,y=h.top+p+h.bottom,x=this.getTranslation(this.chart),P=x.dx,O=x.dy,w=this.chart.options.rotation%(2*Math.PI);P+=d*Math.cos(w+Math.PI/2),O+=d*Math.sin(w+Math.PI/2),r.save(),r.translate(P,O),r.beginPath(),t.helpers.canvas.roundedRect(r,b,m,v,y,u),r.fillStyle=s,r.fill(),r.fillStyle=c||n.options.defaultFontColor,r.fillText(g,0,p/2-h.bottom),r.restore()}},update:function(e){var a=this.chart.config.data.datasets[this.index],r=this.getMeta(),n={value:0,maxValue:1};e?(r.previous=null,r.current=n):(a.data.sort((function(t,e){return t-e})),r.previous=r.current||n,r.current={value:a.value,maxValue:this.getMaxValue(a.data)}),t.controllers.doughnut.prototype.update.call(this,e)},updateElement:function(r,n,o){t.controllers.doughnut.prototype.updateElement.call(this,r,n,o);var i=this.getDataset().data,l=this.chart.config.options,c=this.getMaxValue(i),s=i[n-1]||0,u=i[n],h=l.rotation+l.circumference*(s/c),d=h+l.circumference*((u-s)/c),g=d-h;r._model=function(t){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{};r%2?a(Object(n),!0).forEach((function(a){e(t,a,n[a])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):a(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}({},r._model,{startAngle:h,endAngle:d,circumference:g})},draw:function(e){t.controllers.doughnut.prototype.draw.call(this,e),this.drawNeedle(e),this.drawValueLabel(e)}});return void 0===CanvasRenderingContext2D.prototype.ellipse&&(CanvasRenderingContext2D.prototype.ellipse=function(t,e,a,r,n,o,i,l){this.save(),this.translate(t,e),this.rotate(n),this.scale(a,r),this.arc(0,0,1,o,i,l),this.restore()}),t.controllers.gauge=r,t.Gauge=function(e,a){return a.type="gauge",new t(e,a)},t.Gauge}));

@@ -5,3 +5,3 @@ {

"homepage": "https://github.com/haiiaaa/chartjs-gauge/",
"version": "0.1.0",
"version": "0.1.1",
"licence": "MIT",

@@ -29,4 +29,4 @@ "jsdelivr": "dist/chartjs-gauge.min.js",

"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"eslint": "^6.5.1",

@@ -41,8 +41,8 @@ "eslint-config-airbnb-base": "^14.0.0",

"karma-rollup-preprocessor": "^7.0.2",
"rollup": "^1.25.1",
"rollup-plugin-babel": "^4.3.3",
"rollup": "^1.32.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-istanbul": "^2.0.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.2"
"rollup-plugin-terser": "^5.3.0"
},

@@ -49,0 +49,0 @@ "scripts": {

@@ -1,5 +0,11 @@

# chartjs-gauge
# ![chartjs-gauge logo](./samples/logo.svg) chartjs-gauge
Simple gauge chart for [Chart.js](https://www.chartjs.org/)
## Samples
- [Gauge Chart](https://codepen.io/haiiaaa/pen/rNVbmYy)
- [Gauge Chart with datalabels plugin](https://codepen.io/haiiaaa/pen/KKpYmRz)
- [Gauge Chart with datalabels plugin displaying labels](https://codepen.io/haiiaaa/pen/qBdwmyY)
## Install

@@ -6,0 +12,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc