# Self-Powered Gauge

An analog meter that shows how much of the home's energy came from solar and the battery. The needle springs to the value, overshoots a little and settles, then trembles slightly like a live meter. Below it, a stacked bar splits today's energy by source; hover a source to pick it out. The last row shows the past days as segmented columns with today highlighted. Hover or focus a day to read its value.

<demo-self-powered-gauge>



</demo-self-powered-gauge>

## Installation

```bash
npx shadcn-vue@latest add "https://nxui.geoql.in/r/self-powered-gauge.json"
```

## Usage

```vue
<script setup lang="ts">
  import SelfPoweredGauge from '~/components/ui/self-powered-gauge/SelfPoweredGauge.vue';
  import type {
    SelfPoweredDay,
    SelfPoweredSource,
  } from '~/components/ui/self-powered-gauge/types';

  const sources: SelfPoweredSource[] = [
    { label: 'Solar, direct', value: 7.2, color: '#ff9a2e' },
    { label: 'Battery', value: 2.9, color: '#4ade80' },
    { label: 'Grid', value: 1.7, color: '#7ea6ff' },
  ];

  const history: SelfPoweredDay[] = [
    { label: 'M', name: 'Monday', value: 68 },
    { label: 'T', name: 'Tuesday', value: 79 },
    { label: 'W', name: 'Wednesday', value: 80 },
    { label: 'T', name: 'Thursday', value: 86 },
  ];
</script>

<template>
  <SelfPoweredGauge
    :value="86"
    badge="+9 pts"
    :zone-from="80"
    :sources="sources"
    :history="history"
  />
</template>
```

The last `history` entry is today. Its column always follows `value`, so the meter and the chart agree.

## Props

<table>
<thead>
  <tr>
    <th>
      Prop
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        value
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        86
      </code>
    </td>
    
    <td>
      Self-powered share, 0–100. The needle springs to it.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        title
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Self-powered'
      </code>
    </td>
    
    <td>
      Card title.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        subtitle
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Today, so far'
      </code>
    </td>
    
    <td>
      Line under the title.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        badge
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        '+9 pts'
      </code>
    </td>
    
    <td>
      Pill on the right of the header. Hidden when empty.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Self-powered'
      </code>
    </td>
    
    <td>
      Small caps label inside the meter.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        zone-from
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        80
      </code>
    </td>
    
    <td>
      Where the accent zone on the scale starts.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        sources
      </code>
    </td>
    
    <td>
      <code>
        SelfPoweredSource[]
      </code>
    </td>
    
    <td>
      solar, battery, grid
    </td>
    
    <td>
      Energy per source, drawn as a stacked bar with a legend.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        unit
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'kWh'
      </code>
    </td>
    
    <td>
      Unit for source values.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        sources-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Where it came from'
      </code>
    </td>
    
    <td>
      Heading of the source breakdown.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        history
      </code>
    </td>
    
    <td>
      <code>
        SelfPoweredDay[]
      </code>
    </td>
    
    <td>
      seven sample days
    </td>
    
    <td>
      One column per day. The last one is today.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        history-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Last 7 days'
      </code>
    </td>
    
    <td>
      Heading of the day chart.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        segments
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        10
      </code>
    </td>
    
    <td>
      Segments per day column.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        jitter
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        0.5
      </code>
    </td>
    
    <td>
      Needle tremble in points. <code>
        0
      </code>
      
       turns it off.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        color
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        '#ff9a2e'
      </code>
    </td>
    
    <td>
      Accent for the zone, the readout and today's column.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        class
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      Extra classes for the root element.
    </td>
  </tr>
</tbody>
</table>

## Events

<table>
<thead>
  <tr>
    <th>
      Event
    </th>
    
    <th>
      Payload
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        day-hover
      </code>
    </td>
    
    <td>
      <code>
        SelfPoweredDay | null
      </code>
    </td>
    
    <td>
      Fires when a day column is hovered or focused, <code>
        null
      </code>
      
       on leave.
    </td>
  </tr>
</tbody>
</table>

## Accessibility

- The meter has `role="meter"` with `aria-valuenow`, `aria-valuemin`, `aria-valuemax` and a text value.
- Each day column is a button labelled with the day and its value. Focus shows the value in the heading, which is announced through `aria-live`.
- With reduced motion, the needle jumps to its value without the spring or tremble, and bar transitions are turned off.
- The animation pauses while the meter is offscreen.
