项目作者: naikus

项目描述 :
Minimalistic, animated SVG gauge. Zero dependencies
高级语言: JavaScript
项目地址: git://github.com/naikus/svg-gauge.git
创建时间: 2016-07-03T06:53:56Z
项目社区:https://github.com/naikus/svg-gauge

开源协议:MIT License

下载


SVG Gauge

Minmalistic, configurable, animated SVG gauge. Zero dependencies

Buy me a coffee ☕

If you like my work please consider making a small donation

ko-fi

Migration from 1.0.2

The new gauge uses a viewbox of 100x100 as opposed to previous 1000x1000. All the stroke and font values have to be adjusted accordingly in your CSS. Just divide those by 10

Demo

Check out the live demo for various options and styling tips for this gauge

Usage

HTML

  1. <div id="cpuSpeed" class="gauge-container"></div>

CSS

  1. .gauge-container {
  2. width: 150px;
  3. height: 150px;
  4. display: block;
  5. padding: 10px;
  6. }
  7. .gauge-container > .gauge .dial {
  8. stroke: #eee;
  9. stroke-width: 2;
  10. fill: rgba(0,0,0,0);
  11. }
  12. .gauge-container > .gauge .value {
  13. stroke: rgb(47, 227, 255);
  14. stroke-width: 2;
  15. fill: rgba(0,0,0,0);
  16. }
  17. .gauge-container > .gauge .value-text {
  18. fill: rgb(47, 227, 255);
  19. font-family: sans-serif;
  20. font-weight: bold;
  21. font-size: 1em;
  22. }

Javascript

  1. // npm install
  2. npm install svg-gauge
  3. // Require JS
  4. var Gauge = require("svg-gauge");
  5. // Standalone
  6. var Gauge = window.Gauge;
  7. // Create a new Gauge
  8. var cpuGauge = Gauge(document.getElementById("cpuSpeed"), {
  9. max: 100,
  10. // custom label renderer
  11. label: function(value) {
  12. return Math.round(value) + "/" + this.max;
  13. },
  14. value: 50,
  15. // Custom dial colors (Optional)
  16. color: function(value) {
  17. if(value < 20) {
  18. return "#5ee432"; // green
  19. }else if(value < 40) {
  20. return "#fffa50"; // yellow
  21. }else if(value < 60) {
  22. return "#f7aa38"; // orange
  23. }else {
  24. return "#ef4655"; // red
  25. }
  26. }
  27. });
  28. // Set gauge value
  29. cpuGauge.setValue(75);
  30. // Set value and animate (value, animation duration in seconds)
  31. cpuGauge.setValueAnimated(90, 1);

Options

Name Description
dialStartAngle The angle in degrees to start the dial (135)
dialEndAngle The angle in degrees to end the dial. This MUST be less than dialStartAngle (45)
dialRadius The radius of the gauge (40)
min The minimum value for the gauge. This can be a negative value (0)
max The maximum value for the gauge (100)
label Optional function that returns a string label that will be rendered in the center. This function will be passed the current value
showValue Whether to show the value at the center of the gauge (true)
gaugeClass The CSS class of the gauge (gauge)
dialClass The CSS class of the gauge’s dial (dial)
valueDialClass The CSS class of the gauge’s fill (value dial) (value)
valueClass The CSS class of the gauge’s text (value-text)
color (new) An optional function that can return a color for current value function(value) {}
viewBox (new) An optional string that specifies the crop region (0 0 100 100)

That’s all good, but what about React?

  1. import React, { useEffect, useRef } from "react";
  2. import SvgGauge from "svg-gauge";
  3. const defaultOptions = {
  4. animDuration: 1,
  5. showValue: true,
  6. initialValue: 0,
  7. max: 100
  8. // Put any other defaults you want. e.g. dialStartAngle, dialEndAngle, dialRadius, etc.
  9. };
  10. const Gauge = props => {
  11. const gaugeEl = useRef(null);
  12. const gaugeRef = useRef(null);
  13. useEffect(() => {
  14. if (!gaugeRef.current) {
  15. const options = { ...defaultOptions, ...props };
  16. gaugeRef.current = SvgGauge(gaugeEl.current, options);
  17. gaugeRef.current.setValue(options.initialValue);
  18. }
  19. gaugeRef.current.setValueAnimated(props.value, 1);
  20. }, [props]);
  21. return <div ref={gaugeEl} className="gauge-container" ></div>;
  22. };
  23. export default Gauge;
  24. // to render:
  25. const renderGauge = () => (
  26. <Gauge
  27. value={42}
  28. // any other options you want
  29. ></Gauge>
  30. );

React w/ TypeScript?

  1. import { useEffect, useRef } from 'react'
  2. import SvgGauge, { GaugeOptions, GaugeInstance } from 'svg-gauge'
  3. const Gauge = ({ value }: Props) => {
  4. const gaugeEl = useRef<HTMLDivElement>(null)
  5. const gaugeRef = useRef<GaugeInstance | null>(null)
  6. useEffect(() => {
  7. if (!gaugeRef.current) {
  8. if (!gaugeEl.current) return
  9. const options: GaugeOptions = { color: value => (value < 30 ? 'green' : 'red') }
  10. gaugeRef.current = SvgGauge(gaugeEl.current, options)
  11. gaugeRef.current?.setValue(1)
  12. }
  13. gaugeRef.current?.setValueAnimated(value, 1)
  14. }, [value])
  15. return (
  16. <div style={{ width: '500px', height: '500px' }}>
  17. <div ref={gaugeEl} ></div>
  18. </div>
  19. )
  20. }
  21. interface Props {
  22. value: number
  23. }
  24. export default Gauge

And Angular?

Ha! It’s already there