面积图
Framework7 带有简单的区域图组件。它生成美观的完全响应式 SVG 图表。
区域图布局
由于区域图 SVG 是由 JavaScript 生成的,因此其 HTML 布局尽可能简单:
<!-- Area Chart element -->
<div class="area-chart"></div>
区域图应用方法
现在我们需要创建/初始化区域图。让我们看看与之相关的应用方法来处理它:
app.areaChart.create(参数)- 创建区域图实例
- 参数 - 对象. 带有区域图参数的对象
方法返回创建的区域图的实例
app.areaChart.destroy(el)- 销毁区域图实例
- el - HTMLElement或字符串(使用 CSS 选择器) 或对象. 要销毁的区域图元素或区域图实例。
app.areaChart.get(el)- 通过 HTML 元素获取区域图实例
- el - HTMLElement或字符串(带 CSS 选择器)。区域图元素。
方法返回区域图的实例
app.areaChart.update(参数)- 根据传入的参数更新/重新渲染区域图 SVG
- 参数 - 对象. 带有区域图参数的对象
方法返回区域图的实例
区域图参数
现在我们来看看创建区域图所需的可用参数列表:
参数 | 类型 | 默认 | 描述 |
---|---|---|---|
el | HTMLElement 字符串 | 区域图元素。HTMLElement 或区域图元素的 CSS 选择器字符串。生成的 SVG 将插入到此元素中 | |
宽度 | 数字 | 640 | 生成的 SVG 图像宽度(以像素为单位) |
高度 | 数字 | 320 | 生成的 SVG 图像高度(以像素为单位) |
datasets | 数组 | [] | 图表数据集。数组中的每个对象具有以下属性:datasets array
|
lineChart | 布尔值 | false | 启用线图(而不是区域图) |
axis | 布尔值 | false | 启用图表水平(X)轴 |
axisLabels | 数组 | [] | 图表水平(X)轴标签。应与数据集值具有相同数量的项目 |
提示 | 布尔值 | false | 启用悬停提示 |
legend | 布尔值 | false | 启用图表图例 |
toggleDatasets | 布尔值 | false | 当启用时,它将在图例项目点击时切换数据集 |
maxAxisLabels | 数字 | 8 | 轴标签(刻度)的最大数量,可在轴上可见 |
formatAxisLabel | function(label) | 自定义渲染函数以格式化轴标签文本 | |
formatLegendLabel | function(label) | 自定义渲染函数以格式化图例标签文本 | |
formatTooltip | function(data) | 自定义渲染函数,必须返回提示的 HTML 内容。接收data object
| |
formatTooltipAxisLabel | function(label) | 自定义渲染函数以格式化工具提示中的轴标签文本 | |
formatTooltipTotal | function(total) | 自定义渲染函数以格式化工具提示中的总值文本 | |
formatTooltipDataset | function(label, value, color) | 自定义渲染函数以格式化工具提示中的数据集文本 | |
on | 对象 | 带有事件处理程序的对象。例如:
|
区域图方法和属性
因此要创建区域图,我们必须调用:
var areaChart = app.areaChart.create({ /* parameters */ })
之后我们就有其初始化的实例(如areaChart
示例中上面的变量)以及有用的方法和属性:
属性 | |
---|---|
areaChart.app | 链接到全局应用实例 |
areaChart.el | 区域图 HTML 元素 |
areaChart.$el | 包含区域图 HTML 元素的 Dom7 实例 |
areaChart.svgEl | 区域图生成的 SVG HTML 元素 |
areaChart.$svgEl | 带有生成 SVG HTML 元素的 Dom7 实例 |
areaChart.params | 区域图参数 |
方法 | |
areaChart.update(参数) | 根据传入的参数更新/重新渲染区域图 SVG 元素。它接受与区域图初始化所需的相同参数的对象。您可以仅传递需要更新的参数,例如
|
areaChart.destroy() | 销毁区域图实例 |
areaChart.on(事件上触发, 处理程序) | 添加事件处理程序 |
areaChart.once(事件上触发, 处理程序) | 添加在触发后将被移除的事件处理程序 |
areaChart.off(事件上触发, 处理程序) | 移除事件处理程序 |
areaChart.off(事件上触发) | 移除指定事件的所有处理程序 |
areaChart.emit(事件上触发, ...args) | 在实例上触发事件 |
区域图事件
区域图将在区域图元素上触发以下 DOM 事件,并在应用和区域图实例上触发事件:
DOM 事件
事件 | Target | 描述 |
---|---|---|
areachart:select | 区域图元素<div class="area-chart"> | 当启用提示时,事件将在图表悬停时触发 |
areachart:beforedestroy | 区域图元素<div class="area-chart"> | 事件将在区域图实例销毁之前触发 |
应用和区域图实例事件
区域图实例在自身实例和应用实例上发出事件。应用实例事件名称前缀为areaChart
.
事件 | 参数 | Target | 描述 |
---|---|---|---|
select | (areaChart, index) | areaChart | 当启用提示时,事件将在图表悬停时触发 |
areaChartSelect | (areaChart, index) | app | |
beforeDestroy | (areaChart) | areaChart | 事件将在区域图实例销毁之前触发。作为参数,事件处理程序接收区域图实例 |
areaChartBeforeDestroy | (areaChart) | app |
CSS 变量
以下是相关CSS 变量(CSS 自定义属性) 的列表。
: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;
}
示例
area-chart.html
<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>