Element Framework

Sparkline

  • HTML
  • JS
  • CSS
<ef-sparkline reference-value="0" data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 6, 4, 5, 0, 3, 2, 3, -1, -4, 2, 3, 4, 3, 6]"></ef-sparkline>

                  
ef-sparkline {
  width: 250px;
  height: 100px;
}

ef-sparkline is a small chart component for quickly representing historical data without axes.

Basic usage

To create a sparkline, pass data as an array of numbers using the data attribute. The style of the sparkline is managed by the theme, but chart size can be customized using standard CSS.

  • HTML
  • JS
  • CSS
<ef-sparkline data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 6, 4, 5, 0, 3, 2, 3, -1, -4, 2, 3, 4, 3, 6]"></ef-sparkline>

                  
ef-sparkline {
  width: 200px;
  height: 100px;
}
ef-sparkline {
  width: 200px;
  height: 100px;
}
<ef-sparkline data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 6, 4, 5, 0, 3, 2, 3, -1, -4, 2, 3, 4, 3, 6]"></ef-sparkline>

Reference line

Sparkline supports adding a reference line. The area above or below the reference line will be filled with different colors. The reference value can be set using the reference-value attribute or the referenceValue property.

  • HTML
  • JS
  • CSS
<ef-sparkline reference-value="0" data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 6, 4, 5, 0, 3, 2, 3, -1, -4, 2, 3, 4, 3, 6]"></ef-sparkline>

                  
ef-sparkline {
  width: 200px;
  height: 100px;
}
<ef-sparkline reference-value="0" data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 6, 4, 5, 0, 3, 2, 3, -1, -4, 2, 3, 4, 3, 6]"></ef-sparkline>

Display previous data

Sparkline can be set to display previous data with an inactive line color. It's common to use previous data for comparison with the current dataset.

When previous-data is provided, sparkline will use the value of last point in the dataset as a reference line. It is recommended to not set reference-value when previous data is used.

  • HTML
  • JS
  • CSS
<ef-sparkline previous-data="[-3, -2, 1, 0, 4, 2, -2, 4, 4, 6, -1]" data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 3, 4, 3, 6]"></ef-sparkline>

                  
ef-sparkline {
  width: 200px;
  height: 100px;
}
<ef-sparkline previous-data="[-3, -2, 1, 0, 4, 2, -2, 4, 4, 6, -1]" data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 3, 4, 3, 6]"></ef-sparkline>

Customize colors and line width

Sparkline colors and line width are managed by the theme but can be overridden using CSS variables.

  • HTML
  • JS
  • CSS
<ef-sparkline custom-line-color data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 6, 4, 5, 0, 3, 2, 3, -1, -4, 2, 3, 4, 3, 6]"></ef-sparkline>

                  
ef-sparkline[custom-line-color] {
  width: 200px;
  height: 100px;
  --line-color: #D94255;
  --line-width: 5px;
}
<style>
  ef-sparkline[custom-line-color] {
    --line-color: #D94255;
    --line-width: 5px;
  }
</style>
<ef-sparkline custom-line-color data="[-2, -3, 1, 1, 4, 6, -3, 1, 4, 6, 10, 9, 10, 9, 6, 4, 5, 0, 3, 2, 3, -1, -4, 2, 3, 4, 3, 6]"></ef-sparkline>
CSS Variables Names Description
--line-color Line color
--line-width Line width
--reference-line-color Reference line color
--previous-line-color Previous data line color
--upper-line-color Color of line that is higher than the reference value
--lower-line-color Color of line that is lower than the reference value
--fill-color-style Color style of higher and lower area. (e.g. gradient, solid or none)

API Reference

Attributes

object
data
Chart data as an array of number.
object
previous-data
Chart previous data as an array of number.
number | undefined
reference-value
Baseline value to show horizontal line (optional)

Properties

object
data
Chart data as an array of number.
[]
object
previousData
Chart previous data as an array of number.
[]
number | undefined
referenceValue
Baseline value to show horizontal line (optional)

Events

data-error
Fired when data has error and chart cannot be updated
data-changed
Fired when data is changed