Gauge - 图1

Gauge

Framework7 comes with Gauge component. It produces nice looking fully responsive SVG gauges.

Gauge Layout

Because Gauge SVG is generated by JavaScript its HTML layout is as simple as possible:

  1. <!-- Gauge element -->
  2. <div class="gauge"></div>

Gauge App Methods

Now we need to create/initialize the Gauge. Let’s look at related App methods to work with Gauge:

app.gauge.create(parameters)- create Gauge instance

  • parameters - object. Object with gauge parameters

Method returns created Gauge’s instance

app.gauge.destroy(el)- destroy Gauge instance

  • el - HTMLElement or string (with CSS Selector) or object. Gauge element or Gauge instance to destroy.

app.gauge.get(el)- get Gauge instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Gauge element.

Method returns Gauge’s instance

app.gauge.update(parameters)- update/rerender Gauge SVG according to passed parameters

  • parameters - object. Object with gauge parameters

Method returns gauge value

Gauge Parameters

Now let’s look at list of available parameters we need to create Gauge:

ParameterTypeDefaultDescription
elHTMLElement
string
Gauge element. HTMLElement or string with CSS selector of gauge element. Generated SVG will be inserted into this element
typestringcircleGauge type. Can be circle or semicircle
valuenumber0Gauge value/percentage. Must be a number between 0 and 1
sizenumber200Generated SVG image size (in px)
bgColorstringtransparentGauge background color. Can be any valid color string, e.g. #ff00ff, rgb(0,0,255), etc.
borderBgColorstring#eeeeeeMain border/stroke background color
borderColorstring#000000Main border/stroke color
borderWidthstring10Main border/stroke width
valueTextstringnullGauge value text (large text in the center of gauge)
valueTextColorstring#000000Value text color
valueFontSizestring31Value text font size
valueFontWeightstring500Value text font weight
labelTextstringnullGauge additional label text
labelTextColorstring#888888Label text color
labelFontSizestring14Label text font size
labelFontWeightstring400Label text font weight
onobject

Object with events handlers. For example:

  1. var gauge = app.gauge.create({
  2. el: ‘.gauge’,
  3. value: 33,
  4. valueText: 33%’,
  5. on: {
  6. beforeDestroy: function () {
  7. console.log(‘Gauge will be destroyed’)
  8. }
  9. }
  10. })

Gauge Methods & Properties

So to create Gauge we have to call:

  1. var gauge = app.gauge.create({ /* parameters */ })

After that we have its initialized instance (like gauge variable in example above) with useful methods and properties:

Properties
gauge.appLink to global app instance
gauge.elGauge HTML element
gauge.$elDom7 instance with gauge HTML element
gauge.gaugeSvgElGauge generated SVH HTML element
gauge.$gaugeSvgElDom7 instance with generated SVH HTML element
gauge.paramsGauge parameters
Methods
gauge.update(parameters)Update/rerender gauge SVG element according to passed parameters. It accepts object with same parameters required for gauge initialization. You can pass only parameters that needs to be updated, e.g.
  1. var gauge = app.gauge.create({
  2. value: 0.5,
  3. // …
  4. });
  5. // and when we need to update rendered SVG based on new value:
  6. gauge.update({
  7. value: 0.6,
  8. });
gauge.destroy()Destroys gauge instance
gauge.on(event, handler)Add event handler
gauge.once(event, handler)Add event handler that will be removed after it was fired
gauge.off(event, handler)Remove event handler
gauge.off(event)Remove all handlers for specified event
gauge.emit(event, …args)Fire event on instance

Gauge Events

Gauge will fire the following DOM events on gauge element and events on app and gauge instance:

DOM Events

EventTargetDescription
gauge:beforedestroyGauge Element<div class=”gauge”>Event will be triggered right before Gauge instance will be destroyed

App and Gauge Instance Events

Gauge instance emits events on both self instance and app instance. App instance events has same names prefixed with gauge.

EventArgumentsTargetDescription
beforeDestroy(gauge)gaugeEvent will be triggered right before Gauge instance will be destroyed. As an argument event handler receives Gauge instance
gaugeBeforeDestroy(gauge)app

Gauge Auto Initialization

If you don’t need to use Gauge API and your Gauge is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional gauge-init class, and specifying all parameters with data- attributes:

  1. <!-- Add gauge-init class, and specify parameters in data- attributes -->
  2. <div
  3. class="gauge gauge-init my-gauge"
  4. data-type="circle"
  5. data-value="0.44"
  6. data-value-text="44%"
  7. data-value-text-color="#ff9800"
  8. data-border-color="#ff9800"
  9. ></div>

In this case if you need to access automatically created Gauge instance you can use the app.gauge.get app method:

  1. var gauge = app.gauge.get('.my-gauge');
  2. gauge.update({
  3. value: 0.9,
  4. });

Examples

  1. <div class="block block-strong text-align-center">
  2. <!-- we will init this gauge in JavaScript -->
  3. <div class="gauge demo-gauge"></div>
  4. <!-- buttons to change gauge value -->
  5. <p class="segmented segmented-raised">
  6. <a href="#" class="button" data-value="0">0%</a>
  7. <a href="#" class="button" data-value="25">25%</a>
  8. <a href="#" class="button" data-value="50">50%</a>
  9. <a href="#" class="button" data-value="75">75%</a>
  10. <a href="#" class="button" data-value="100">100%</a>
  11. </p>
  12. </div>
  1. // Init top demo gauge
  2. var demoGauge = app.gauge.create({
  3. el: '.demo-gauge',
  4. type: 'circle',
  5. value: 0.5,
  6. size: 250,
  7. borderColor: '#2196f3',
  8. borderWidth: 10,
  9. valueText: '50%',
  10. valueFontSize: 41,
  11. valueTextColor: '#2196f3',
  12. labelText: 'amount of something',
  13. });
  14. // Change demo gauge on button click
  15. $$('.button').on('click', function () {
  16. var value = $$(this).attr('data-value');
  17. demoGauge.update({
  18. value: value / 100,
  19. valueText: value + '%'
  20. });
  21. });
  1. <div class="block-title">Circle Gauges</div>
  2. <div class="block block-strong">
  3. <div class="row">
  4. <div class="col text-align-center">
  5. <div
  6. class="gauge gauge-init"
  7. data-type="circle"
  8. data-value="0.44"
  9. data-value-text="44%"
  10. data-value-text-color="#ff9800"
  11. data-border-color="#ff9800"
  12. ></div>
  13. </div>
  14. <div class="col text-align-center">
  15. <div
  16. class="gauge gauge-init"
  17. data-type="circle"
  18. data-value="0.12"
  19. data-value-text="$120"
  20. data-value-text-color="#4caf50"
  21. data-border-color="#4caf50"
  22. data-label-text="of $1000 budget"
  23. data-label-text-color="#f44336"
  24. data-label-font-weight="700"
  25. ></div>
  26. </div>
  27. </div>
  28. </div>
  1. <div class="block-title">Semicircle Gauges</div>
  2. <div class="block block-strong">
  3. <div class="row">
  4. <div class="col text-align-center">
  5. <div
  6. class="gauge gauge-init"
  7. data-type="semicircle"
  8. data-value="0.3"
  9. data-value-text="30%"
  10. data-value-text-color="#f44336"
  11. data-border-color="#f44336"
  12. ></div>
  13. </div>
  14. <div class="col text-align-center">
  15. <div
  16. class="gauge gauge-init"
  17. data-type="semicircle"
  18. data-value="0.5"
  19. data-value-text="30kg"
  20. data-value-text-color="#e91e63"
  21. data-border-color="#e91e63"
  22. data-label-text="of 60kg total"
  23. data-label-text-color="#333"
  24. ></div>
  25. </div>
  26. </div>
  27. </div>
  1. <div class="block-title">Customization</div>
  2. <div class="block block-strong">
  3. <div class="row">
  4. <div class="col text-align-center">
  5. <div
  6. class="gauge gauge-init"
  7. data-type="circle"
  8. data-value="0.35"
  9. data-value-text="35%"
  10. data-value-text-color="#4caf50"
  11. data-value-font-size="51"
  12. data-value-font-weight="700"
  13. data-border-width="20"
  14. data-border-color="#4caf50"
  15. data-border-bg-color="#ffeb3b"
  16. data-bg-color="#ffeb3b"
  17. ></div>
  18. </div>
  19. <div class="col text-align-center">
  20. <div
  21. class="gauge gauge-init"
  22. data-type="circle"
  23. data-value="0.67"
  24. data-value-text="$670"
  25. data-value-text-color="#000"
  26. data-border-color="#ff9800"
  27. data-label-text="of $1000 spent"
  28. data-label-text-color="#4caf50"
  29. data-label-font-weight="800"
  30. data-label-font-size="12"
  31. data-border-width="30"
  32. ></div>
  33. </div>
  34. </div>
  35. <br>
  36. <div class="row">
  37. <div class="col text-align-center">
  38. <div
  39. class="gauge gauge-init"
  40. data-type="semicircle"
  41. data-value="0.5"
  42. data-value-text="50%"
  43. data-value-text-color="#ffeb3b"
  44. data-value-font-size="41"
  45. data-value-font-weight="700"
  46. data-border-width="10"
  47. data-border-color="#ffeb3b"
  48. data-border-bg-color="transparent"
  49. ></div>
  50. </div>
  51. <div class="col text-align-center">
  52. <div
  53. class="gauge gauge-init"
  54. data-type="semicircle"
  55. data-value="0.77"
  56. data-border-color="#ff9800"
  57. data-label-text="$770 spent so far"
  58. data-label-text-color="#ff9800"
  59. data-label-font-weight="800"
  60. data-label-font-size="12"
  61. data-border-width="10"
  62. ></div>
  63. </div>
  64. </div>
  65. </div>

← Form Data / Storage

Grid / Layout Grid →