JavaScript/jQuery Sparkline API

The Sparkline widget is a compact chart that contains only one series. Owing to their size, sparklines occupy very little space and can be easily collected in a table or embedded straight in text.

import Sparkline from "devextreme/viz/sparkline"

DevExtreme widgets are integrated with many popular libraries and frameworks. See the Installation section (for JavaScript libraries) or the Prerequisites and Installation section (for ASP.NET MVC framework) to find details on setting up DevExtreme with a particular library or framework.

The following code shows how to create the Sparkline widget using every supported library and framework. For more details on working with widgets in these libraries and frameworks, see the Widget Basics topic for jQuery, Angular, AngularJS, Knockout or ASP.NET MVC.

jQuery
JavaScript
HTML
var temperature = [
    { month: "January", day: 6, night: 2 },
    { month: "February", day: 7, night: 2 },
    { month: "March", day: 10, night: 3 },
    { month: "April", day: 14, night: 5 },
    { month: "May", day: 18, night: 8 },
    { month: "June", day: 21, night: 11 },
    { month: "July", day: 22, night: 13 },
    { month: "August", day: 22, night: 13 },
    { month: "September", day: 19, night: 11 },
    { month: "October", day: 15, night: 8 },
    { month: "November", day: 10, night: 5 },
    { month: "December", day: 7, night: 3 }
];
$(function () {
    $("#daySparkline").dxSparkline({
        dataSource: temperature,
        argumentField: 'month',
        valueField: 'day',
        type: 'bar'
    });
    $("#nightSparkline").dxSparkline({
        dataSource: temperature,
        argumentField: 'month',
        valueField: 'night',
        type: 'bar'
    });
});
<div id="daySparkline"></div>
<div id="nightSparkline"></div>
Angular
HTML
TypeScript
<dx-sparkline
    [dataSource]="temperature"
    argumentField="month"
    valueField="day"
    type="bar">
</dx-sparkline>
<dx-sparkline
    [dataSource]="temperature"
    argumentField="month"
    valueField="night"
    type="bar">
</dx-sparkline>
import { DxSparklineModule } from 'devextreme-angular';
// ...
export class AppComponent {
    temperature = [
        { month: "January", day: 6, night: 2 },
        { month: "February", day: 7, night: 2 },
        { month: "March", day: 10, night: 3 }
    ];
}
@NgModule({
    imports: [
        // ...
        DxSparklineModule
    ],
    // ...
})
AngularJS
HTML
JavaScript
<div dx-sparkline="{
    dataSource: temperature,
    argumentField: 'month',
    valueField: 'day',
    type: 'bar'
}"></div>
<div dx-sparkline="{
    dataSource: temperature,
    argumentField: 'month',
    valueField: 'night',
    type: 'bar'
}"></div>
angular.module('DemoApp', ['dx'])
    .controller("DemoController", function ($scope) {
        $scope.temperature = [
            { month: "January", day: 6, night: 2 },
            { month: "February", day: 7, night: 2 },
            { month: "March", day: 10, night: 3 }
        ];
    });
Knockout
HTML
JavaScript
<div data-bind="dxSparkline: {
    dataSource: temperature,
    argumentField: 'month',
    valueField: 'day',
    type: 'bar'
}"></div>
<div data-bind="dxSparkline: {
    dataSource: temperature,
    argumentField: 'month',
    valueField: 'night',
    type: 'bar'
}"></div>
var viewModel = {
    temperature: [
        { month: "January", day: 6, night: 2 },
        { month: "February", day: 7, night: 2 },
        { month: "March", day: 10, night: 3 }
    ]
};
ko.applyBindings(viewModel);
ASP.NET MVC Controls
Razor C#
Razor VB
@(Html.DevExtreme().Sparkline()
    .ID("daySpakline")
    .DataSource(new JS ("temperature"))
    .ArgumentField("month")
    .ValueField("day")
    .Type(SparklineType.Bar)
)
@(Html.DevExtreme().Sparkline()
    .ID("nightSpakline")
    .DataSource(new JS("temperature"))
    .ArgumentField("month")
    .ValueField("night")
    .Type(SparklineType.Bar)
)
<script> 
    var temperature = [
        { month: "January", day: 6, night: 2 },
        { month: "February", day: 7, night: 2 },
        { month: "March", day: 10, night: 3 }
    ];
</script>
@(Html.DevExtreme().Sparkline() _
    .ID("daySpakline") _
    .DataSource(New JS("temperature")) _
    .ArgumentField("month") _
    .ValueField("day") _
    .Type(SparklineType.Bar)
)
@(Html.DevExtreme().Sparkline() _
    .ID("nightSpakline") _
    .DataSource(New JS("temperature")) _
    .ArgumentField("month") _
    .ValueField("night") _
    .Type(SparklineType.Bar)
)
<script>
    var temperature = [
        { month: "January", day: 6, night: 2 },
        { month: "February", day: 7, night: 2 },
        { month: "March", day: 10, night: 3 }
    ];
</script>

View Demo Watch Video

See Also

Configuration

An object that specifies configuration options for the Sparkline widget.

Name Description
argumentField

Specifies the data source field that provides arguments for a sparkline.

barNegativeColor

Sets a color for the bars indicating negative values. Available for a sparkline of the bar type only.

barPositiveColor

Sets a color for the bars indicating positive values. Available for a sparkline of the bar type only.

dataSource

Specifies a data source for the sparkline.

elementAttr

Specifies the attributes to be attached to the widget's root element.

firstLastColor

Sets a color for the boundary of both the first and last points on a sparkline.

ignoreEmptyPoints

Specifies whether a sparkline ignores null data points or not.

lineColor

Sets a color for a line on a sparkline. Available for the sparklines of the line- and area-like types.

lineWidth

Specifies a width for a line on a sparkline. Available for the sparklines of the line- and area-like types.

lossColor

Sets a color for the bars indicating the values that are less than the winloss threshold. Available for a sparkline of the winloss type only.

margin

Generates space around the widget.

maxColor

Sets a color for the boundary of the maximum point on a sparkline.

maxValue

Specifies the maximum value of the sparkline's value axis.

minColor

Sets a color for the boundary of the minimum point on a sparkline.

minValue

Specifies the minimum value of the sparkline value axis.

onDisposing

A handler for the disposing event. Executed when the widget is removed from the DOM using the remove(), empty(), or html() jQuery methods only.

onDrawn

A function that is executed when the widget's rendering has finished.

onExported

A handler for the exported event. Executed after data from the widget is exported.

onExporting

A handler for the exporting event. Executed before data from the widget is exported.

onFileSaving

A handler for the fileSaving event. Executed before a file with exported data is saved on the user's local storage.

onIncidentOccurred

A handler for the incidentOccurred event. Executed when an error or warning appears in the widget.

onInitialized

A handler for the initialized event. Executed only once, after the widget is initialized.

onOptionChanged

A handler for the optionChanged event. Executed after an option of the widget is changed.

onTooltipHidden

A handler for the tooltipHidden event.

onTooltipShown

A handler for the tooltipShown event.

pathModified

Notifies the widget that it is embedded into an HTML page that uses a tag modifying the path.

pointColor

Sets a color for points on a sparkline. Available for the sparklines of the line- and area-like types.

pointSize

Specifies the diameter of sparkline points in pixels. Available for the sparklines of line- and area-like types.

pointSymbol

Specifies a symbol to use as a point marker on a sparkline. Available for the sparklines of the line- and area-like types.

rtlEnabled

Switches the widget to a right-to-left representation.

showFirstLast

Specifies whether or not to indicate both the first and last values on a sparkline.

showMinMax

Specifies whether or not to indicate both the minimum and maximum values on a sparkline.

size

Specifies the widget's size in pixels.

theme

Sets the name of the theme the widget uses.

tooltip

Configures the tooltip.

type

Determines the type of a sparkline.

valueField

Specifies the data source field that provides values for a sparkline.

winColor

Sets a color for the bars indicating the values greater than a winloss threshold. Available for a sparkline of the winloss type only.

winlossThreshold

Specifies a value that serves as a threshold for the sparkline of the winloss type.

Methods

This section describes the methods that can be used in code to manipulate objects related to the Sparkline widget.

Name Description
beginUpdate()

Prevents the widget from refreshing until the endUpdate() method is called.

dispose()

Disposes of all the resources allocated to the Sparkline instance.

element()

Gets the root widget element.

endUpdate()

Refreshes the widget after a call of the beginUpdate() method.

exportTo(fileName, format)

Exports the widget.

getDataSource()

Gets the DataSource instance.

getInstance(element)

Gets the instance of a widget found using its DOM node.

getSize()

Gets the current widget size.

instance()

Gets the widget's instance. Use it to access other methods of the widget.

off(eventName)

Detaches all event handlers from a single event.

off(eventName, eventHandler)

Detaches a particular event handler from a single event.

on(eventName, eventHandler)

Subscribes to an event.

on(events)

Subscribes to events.

option()

Gets all widget options.

option(optionName)

Gets the value of a single option.

option(optionName, optionValue)

Updates the value of a single option.

option(options)

Updates the values of several options.

print()

Opens the browser's print window.

render()

Redraws the widget.

svg()

Gets the widget's SVG markup.

Events

This section describes events fired by this widget.

Name Description
disposing

Raised when the widget is removed from the DOM using the remove(), empty(), or html() jQuery methods only.

drawn

Raised when the widget's rendering has finished.

exported

Raised after data from the widget is exported.

exporting

Raised before data from the widget is exported.

fileSaving

Raised before a file with exported data is saved on the user's local storage.

incidentOccurred

Raised when an error or warning appears in the widget.

initialized

Raised only once, after the widget is initialized.

optionChanged

Raised after a widget option is changed.

tooltipHidden

Fires when a sparkline's tooltip becomes hidden.

tooltipShown

Fires when a sparkline tooltip appears.

See Also