Area Chart
Framework7 comes with simple Area Chart component. It produces nice looking fully responsive SVG charts.
Area Chart Layout
Because Area Chart SVG is generated by JavaScript its HTML layout is as simple as possible:
<!-- Area Chart element -->
<div class="area-chart"></div>
Area Chart App Methods
Now we need to create/initialize the Area Chart. Let's look at related App methods to work with it:
app.areaChart.create(parameters)- create Area Chart instance
- parameters - object. Object with Area Chart parameters
Method returns created Area Chart's instance
app.areaChart.destroy(el)- destroy Area Chart instance
- el - HTMLElement or string (with CSS Selector) or object. Area Chart element or Area Chart instance to destroy.
app.areaChart.get(el)- get Area Chart instance by HTML element
- el - HTMLElement or string (with CSS Selector). Area Chart element.
Method returns Area Chart's instance
app.areaChart.update(parameters)- update/rerender Area Chart SVG according to passed parameters
- parameters - object. Object with Area Chart parameters
Method returns Area Chart's instance
Area Chart Parameters
Now let's look at list of available parameters we need to create Area Chart:
Parameter | Type | Default | Description |
---|---|---|---|
el | HTMLElement string | Area Chart element. HTMLElement or string with CSS selector of Area Chart element. Generated SVG will be inserted into this element | |
width | number | 640 | Generated SVG image width (in px) |
height | number | 320 | Generated SVG image height (in px) |
datasets | array | [] | Chart data sets. Each object in datasets array has the following properties:
|
lineChart | boolean | false | Enables lines chart (instead of area chart) |
axis | boolean | false | Enables chart horizontal (X) axis |
axisLabels | array | [] | Chart horizontal (X) axis labels. Should have same amount of items as dataset values |
tooltip | boolean | false | Enables tooltip on hover |
legend | boolean | false | Enables chart legend |
toggleDatasets | boolean | false | When enabled it will toggle data sets on legend items click |
maxAxisLabels | number | 8 | Max numbers of axis labels (ticks) to be visible on axis |
formatAxisLabel | function(label) | Custom render function to format axis label text | |
formatLegendLabel | function(label) | Custom render function to format legend label text | |
formatTooltip | function(data) | Custom render function that must return tooltip's HTML content. Received data object has the following properties:
| |
formatTooltipAxisLabel | function(label) | Custom render function to format axis label text in Tooltip | |
formatTooltipTotal | function(total) | Custom render function to format total value text in Tooltip | |
formatTooltipDataset | function(label, value, color) | Custom render function to format dataset text in Tooltip | |
on | object | Object with events handlers. For example:
|
Area Chart Methods & Properties
So to create Area Chart we have to call:
var areaChart = app.areaChart.create({ /* parameters */ })
After that we have its initialized instance (like areaChart
variable in example above) with useful methods and properties:
Properties | |
---|---|
areaChart.app | Link to global app instance |
areaChart.el | Area Chart HTML element |
areaChart.$el | Dom7 instance with Area Chart HTML element |
areaChart.svgEl | Area Chart generated SVG HTML element |
areaChart.$svgEl | Dom7 instance with generated SVG HTML element |
areaChart.params | Area Chart parameters |
Methods | |
areaChart.update(parameters) | Update/rerender Area Chart SVG element according to passed parameters. It accepts object with same parameters required for Area Chart initialization. You can pass only parameters that needs to be updated, e.g.
|
areaChart.destroy() | Destroys Area Chart instance |
areaChart.on(event, handler) | Add event handler |
areaChart.once(event, handler) | Add event handler that will be removed after it was fired |
areaChart.off(event, handler) | Remove event handler |
areaChart.off(event) | Remove all handlers for specified event |
areaChart.emit(event, ...args) | Fire event on instance |
Area Chart Events
Area Chart will fire the following DOM events on Area Chart element and events on app and Area Chart instance:
DOM Events
Event | Target | Description |
---|---|---|
areachart:select | Area Chart Element<div class="area-chart"> | Event will be triggered (when tooltip enabled) on chart hover |
areachart:beforedestroy | Area Chart Element<div class="area-chart"> | Event will be triggered right before Area Chart instance will be destroyed |
App and Area Chart Instance Events
Area Chart instance emits events on both self instance and app instance. App instance events has same names prefixed with areaChart
.
Event | Arguments | Target | Description |
---|---|---|---|
select | (areaChart, index) | areaChart | Event will be triggered (when tooltip enabled) on chart hover |
areaChartSelect | (areaChart, index) | app | |
beforeDestroy | (areaChart) | areaChart | Event will be triggered right before Area Chart instance will be destroyed. As an argument event handler receives Area Chart instance |
areaChartBeforeDestroy | (areaChart) | app |
CSS Variables
Below is the list of related CSS variables (CSS custom properties).
:root {
--f7-area-chart-current-line-stroke-width: 2px;
--f7-area-chart-current-line-stroke: rgba(0, 0, 0, 0.15);
--f7-area-chart-axis-text-color: inherit;
--f7-area-chart-axis-height: 1px;
--f7-area-chart-axis-font-size: 10px;
--f7-area-chart-axis-font-weight: 500;
--f7-area-chart-tooltip-font-size: 12px;
--f7-area-chart-tooltip-total-font-size: 16px;
--f7-area-chart-tooltip-total-font-weight: bold;
--f7-area-chart-tooltip-color-size: 10px;
--f7-area-chart-legend-font-size: 14px;
--f7-area-chart-legend-font-weight: 500;
--f7-area-chart-legend-text-color: inherit;
--f7-area-chart-legend-padding: 4px 8px;
--f7-area-chart-legend-border-radius: 4px;
--f7-area-chart-legend-color-size: 14px;
--f7-area-chart-line-stroke-width: 2px;
--f7-area-chart-axis-bg-color: rgba(0, 0, 0, 0.15);
--f7-area-chart-legend-disabled-text-color: rgba(0, 0, 0, 0.22);
}
:root .dark,
:root.dark {
--f7-area-chart-axis-bg-color: rgba(255, 255, 255, 0.15);
--f7-area-chart-legend-disabled-text-color: rgba(255, 255, 255, 0.22);
}
.ios {
--f7-area-chart-tooltip-total-label-text-color: rgba(255, 255, 255, 0.75);
}
.md {
--f7-area-chart-tooltip-total-label-text-color: inherit;
}
Examples
<template>
<div class="page">
<div class="navbar">
<div class="navbar-bg"></div>
<div class="navbar-inner">
<div class="title">Area Chart</div>
</div>
</div>
<div class="page-content">
<div class="block block-strong-ios block-outline-ios">
<p>Framework7 comes with simple to use and fully responsive Area Chart component.</p>
<p>Area Chart generates SVG layout which makes it also compatible with SSR (server side rendering).</p>
</div>
<div class="block-title">Simple Area Chart</div>
<div class="block block-strong-ios block-outline-ios">
<div class="area-chart area-chart-simple"></div>
</div>
<div class="block-title">Area Chart With Tooltip</div>
<div class="block block-strong-ios block-outline-ios">
<div class="area-chart area-chart-with-tooltip"></div>
</div>
<div class="block-title">Area Chart With Axis</div>
<div class="block block-strong-ios block-outline-ios">
<div class="area-chart area-chart-with-axis"></div>
</div>
<div class="block-title">Area Chart With Legend</div>
<div class="block block-strong-ios block-outline-ios">
<div class="area-chart area-chart-with-legend"></div>
</div>
<div class="block-title">Lines Chart</div>
<div class="block block-strong-ios block-outline-ios">
<div class="area-chart area-chart-lines"></div>
</div>
</div>
</div>
</template>
<script>
export default (props, { $f7, $onMounted, $onBeforeUnmount }) => {
let areaSimple;
let areaWithTooltip;
let areaWithAxis;
let areaWithLegend;
let areaLines;
// helpers data for axis
const dates = [];
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
for (let i = 0; i < 4; i += 1) {
dates.push(new Date(year, month - (3 - i)))
}
const axisDateFormat = Intl.DateTimeFormat(undefined, { month: 'short', year: 'numeric' })
const tooltipDateFormat = Intl.DateTimeFormat(undefined, { month: 'long', year: 'numeric' })
$onMounted(() => {
areaSimple = $f7.areaChart.create({
el: '.area-chart-simple',
datasets: [
{
color: '#f00',
values: [0, 100, 250, 300, 175, 400]
},
{
color: '#00f',
values: [100, 75, 133, 237, 40, 200]
},
{
color: '#ff0',
values: [100, 300, 127, 40, 250, 80]
},
]
});
areaWithTooltip = $f7.areaChart.create({
el: '.area-chart-with-tooltip',
tooltip: true,
datasets: [
{
label: 'Red data',
color: '#f00',
values: [100, 75, 133, 237, 40, 200]
},
{
label: 'Blue data',
color: '#00f',
values: [100, 300, 127, 40, 250, 80]
},
{
label: 'Yellow data',
color: '#ff0',
values: [0, 100, 250, 300, 175, 400]
},
]
});
areaWithAxis = $f7.areaChart.create({
el: '.area-chart-with-axis',
tooltip: true,
axis: true,
axisLabels: dates,
formatAxisLabel(date) {
return axisDateFormat.format(date);
},
formatTooltipAxisLabel(date) {
return tooltipDateFormat.format(date);
},
datasets: [
{
label: 'Green data',
color: '#0f0',
values: [100, 75, 133, 237]
},
{
label: 'Red data',
color: '#f00',
values: [100, 300, 127, 47]
},
{
label: 'Yellow data',
color: '#ff0',
values: [0, 100, 250, 307]
},
]
});
areaWithLegend = $f7.areaChart.create({
el: '.area-chart-with-legend',
tooltip: true,
axis: true,
axisLabels: dates,
legend: true,
toggleDatasets: true,
formatAxisLabel(date) {
return axisDateFormat.format(date);
},
formatTooltipAxisLabel(date) {
return tooltipDateFormat.format(date);
},
datasets: [
{
label: 'Red data',
color: '#f00',
values: [100, 300, 127, 47]
},
{
label: 'Blue data',
color: '#00f',
values: [100, 75, 133, 237]
},
{
label: 'Yellow data',
color: '#ff0',
values: [0, 100, 250, 307]
},
]
});
areaLines = $f7.areaChart.create({
el: '.area-chart-lines',
tooltip: true,
axis: true,
axisLabels: dates,
legend: true,
toggleDatasets: true,
lineChart: true,
formatAxisLabel(date) {
return axisDateFormat.format(date);
},
formatTooltipAxisLabel(date) {
return tooltipDateFormat.format(date);
},
datasets: [
{
label: 'Red data',
color: '#f00',
values: [0, 300, 127, 47]
},
{
label: 'Blue data',
color: '#00f',
values: [0, 75, 133, 237]
},
{
label: 'Green data',
color: '#0f0',
values: [0, 100, 250, 307]
},
]
});
})
$onBeforeUnmount(() => {
areaSimple.destroy();
areaWithTooltip.destroy();
areaWithAxis.destroy();
areaWithLegend.destroy();
areaLines.destroy();
})
return $render;
}
</script>