Convert Figma logo to code with AI

antvis logoF2

📱📈An elegant, interactive and flexible charting library for mobile.

7,881
646
7,881
280

Top Related Projects

64,568

Simple HTML5 Charts using the <canvas> tag

108,427

Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:

101,622

JavaScript 3D Library.

16,856

Open-source JavaScript charting library behind Plotly and Dash

📊 Interactive JavaScript Charts built on SVG

Highcharts JS, the JavaScript charting framework

Quick Overview

F2 is a lightweight and high-performance charting library for mobile applications. It's designed to work efficiently on mobile devices, providing interactive and customizable charts with a small footprint. F2 is part of the AntV data visualization ecosystem developed by Ant Financial.

Pros

  • Optimized for mobile performance with a small file size
  • Supports a wide range of chart types and customizations
  • Provides smooth animations and interactions
  • Offers a flexible and extensible plugin system

Cons

  • Primarily focused on mobile, may not be the best choice for desktop applications
  • Learning curve might be steeper compared to simpler charting libraries
  • Documentation and community resources are not as extensive as some more popular libraries

Code Examples

Creating a basic line chart:

import F2 from '@antv/f2';

const chart = new F2.Chart({
  id: 'myChart',
  pixelRatio: window.devicePixelRatio
});

chart.source(data);
chart.line().position('year*value');
chart.render();

Adding interactivity with tooltips:

import F2 from '@antv/f2';

const chart = new F2.Chart({
  id: 'myChart',
  pixelRatio: window.devicePixelRatio
});

chart.source(data);
chart.tooltip({
  showCrosshairs: true,
  showItemMarker: false,
  onShow: function(ev) {
    const { items } = ev;
    items[0].name = null;
    items[0].value = '$ ' + items[0].value;
  }
});
chart.line().position('date*value');
chart.render();

Creating a custom shape:

import F2 from '@antv/f2';

F2.Shape.registerShape('point', 'custom', {
  draw(cfg, container) {
    const point = cfg.points[0];
    const group = container.addGroup();
    group.addShape('circle', {
      attrs: {
        x: point.x,
        y: point.y,
        r: 5,
        fill: cfg.color
      }
    });
    return group;
  }
});

const chart = new F2.Chart({ id: 'myChart' });
chart.source(data);
chart.point().position('x*y').shape('custom');
chart.render();

Getting Started

To use F2 in your project, first install it via npm:

npm install @antv/f2

Then, import and use it in your JavaScript file:

import F2 from '@antv/f2';

const data = [
  { year: '1951', sales: 38 },
  { year: '1952', sales: 52 },
  { year: '1953', sales: 61 },
  { year: '1954', sales: 145 },
  { year: '1955', sales: 48 },
  { year: '1956', sales: 38 },
  { year: '1957', sales: 38 },
  { year: '1958', sales: 38 },
];

const chart = new F2.Chart({
  id: 'myChart',
  pixelRatio: window.devicePixelRatio
});

chart.source(data);
chart.interval().position('year*sales');
chart.render();

Make sure to have a canvas element with the id 'myChart' in your HTML file where the chart will be rendered.

Competitor Comparisons

64,568

Simple HTML5 Charts using the <canvas> tag

Pros of Chart.js

  • Wider community support and more extensive documentation
  • Broader range of chart types and customization options
  • Better suited for complex, interactive visualizations

Cons of Chart.js

  • Larger file size, which may impact load times for mobile applications
  • Steeper learning curve for beginners due to more advanced features

Code Comparison

F2:

import F2 from '@antv/f2';

const chart = new F2.Chart({
  id: 'myChart',
  pixelRatio: window.devicePixelRatio
});

Chart.js:

import Chart from 'chart.js/auto';

const ctx = document.getElementById('myChart');
const chart = new Chart(ctx, {
  type: 'bar',
  data: chartData
});

Both libraries offer straightforward initialization, but F2 is more focused on mobile-specific features like pixel ratio adjustment. Chart.js provides a more familiar API for web developers and offers built-in chart types.

F2 excels in creating lightweight, performant charts for mobile devices, while Chart.js is more versatile for various web applications. F2's smaller file size and mobile-first approach make it ideal for projects prioritizing performance on mobile devices. Chart.js, with its extensive feature set and plugin ecosystem, is better suited for complex data visualizations across different platforms.

108,427

Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:

Pros of D3

  • More powerful and flexible for creating complex, interactive visualizations
  • Larger ecosystem with extensive documentation and community support
  • Supports a wider range of chart types and custom visualizations

Cons of D3

  • Steeper learning curve, especially for beginners
  • Requires more code to create basic charts
  • Performance can be an issue with large datasets

Code Comparison

F2 example:

chart.interval().position('genre*sold').color('genre');
chart.render();

D3 example:

svg.selectAll("rect")
   .data(data)
   .enter()
   .append("rect")
   .attr("x", (d, i) => i * 70)
   .attr("y", d => height - 10 * d)
   .attr("width", 65)
   .attr("height", d => d * 10)
   .attr("fill", "green");

F2 is more concise for simple charts, while D3 offers more granular control but requires more code. F2 is designed for mobile-first scenarios and performs well on mobile devices, whereas D3 is more versatile but may require additional optimization for mobile use. F2 has a smaller file size and faster rendering speed for basic charts, making it suitable for projects with performance constraints.

101,622

JavaScript 3D Library.

Pros of three.js

  • Powerful 3D rendering capabilities for complex scenes and animations
  • Extensive documentation and large community support
  • Wide range of features including physics, VR, and post-processing effects

Cons of three.js

  • Steeper learning curve due to its complexity and 3D focus
  • Larger file size and potentially higher performance overhead
  • May be overkill for simple 2D visualizations

Code Comparison

three.js (3D scene creation):

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

F2 (2D chart creation):

const chart = new F2.Chart({
  id: 'myChart',
  pixelRatio: window.devicePixelRatio
});
chart.source(data);
chart.interval().position('genre*sold').color('genre');
chart.render();

Summary

Three.js is a powerful 3D graphics library with extensive features, while F2 focuses on lightweight 2D visualizations for mobile devices. Three.js offers more complex rendering capabilities but comes with a steeper learning curve and larger file size. F2 is more suitable for simple, performance-optimized 2D charts on mobile platforms. The choice between them depends on the specific requirements of your project, whether you need 3D capabilities, and your target devices.

16,856

Open-source JavaScript charting library behind Plotly and Dash

Pros of plotly.js

  • More comprehensive and feature-rich, supporting a wider range of chart types and advanced visualizations
  • Extensive documentation and community support, making it easier for developers to find solutions and resources
  • Better suited for complex, interactive data visualizations and dashboards

Cons of plotly.js

  • Larger file size and potentially slower performance, especially for simpler charts or mobile applications
  • Steeper learning curve due to its extensive API and numerous configuration options
  • May be overkill for projects that only require basic charting functionality

Code Comparison

F2 example:

import F2 from '@antv/f2';

const chart = new F2.Chart({
  id: 'myChart',
  pixelRatio: window.devicePixelRatio
});

chart.source(data);
chart.interval().position('genre*sold').color('genre');
chart.render();

plotly.js example:

import Plotly from 'plotly.js-dist';

Plotly.newPlot('myDiv', [{
  x: ['genre1', 'genre2', 'genre3'],
  y: [20, 14, 23],
  type: 'bar'
}], {
  title: 'Sales by Genre'
});

Both libraries offer straightforward ways to create charts, but F2 focuses on mobile-first, lightweight charting, while plotly.js provides more extensive options for complex visualizations.

📊 Interactive JavaScript Charts built on SVG

Pros of ApexCharts.js

  • More extensive chart types and customization options
  • Better documentation and examples
  • Larger community and more frequent updates

Cons of ApexCharts.js

  • Larger file size, potentially impacting load times
  • Steeper learning curve due to more complex API
  • May be overkill for simple charting needs

Code Comparison

F2:

import { Chart } from '@antv/f2';

const chart = new Chart({
  id: 'myChart',
  pixelRatio: window.devicePixelRatio
});

chart.line().position('x*y').color('type');
chart.render();

ApexCharts.js:

import ApexCharts from 'apexcharts';

const options = {
  chart: { type: 'line' },
  series: [{ data: [30, 40, 35, 50, 49, 60, 70] }],
  xaxis: { categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997] }
};

const chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();

Both libraries offer straightforward ways to create charts, but ApexCharts.js requires more configuration upfront. F2 has a more concise syntax for basic charts, while ApexCharts.js provides more detailed control over chart appearance and behavior.

Highcharts JS, the JavaScript charting framework

Pros of Highcharts

  • More extensive feature set and chart types
  • Better support for complex, interactive visualizations
  • Larger community and ecosystem

Cons of Highcharts

  • Commercial license required for most use cases
  • Steeper learning curve due to more complex API
  • Larger file size, potentially impacting performance

Code Comparison

F2 example:

import { Chart } from '@antv/f2';

const chart = new Chart({
  container: 'mountNode',
  width: 400,
  height: 300
});

chart.line().position('x*y').color('type');
chart.render();

Highcharts example:

Highcharts.chart('container', {
  series: [{
    type: 'line',
    data: [1, 2, 3, 4, 5]
  }]
});

Key Differences

  • F2 is designed for mobile-first scenarios, while Highcharts is more versatile
  • F2 uses a declarative API, Highcharts uses a configuration object approach
  • F2 is open-source and free, Highcharts requires a license for commercial use
  • Highcharts offers more out-of-the-box features, while F2 focuses on performance and simplicity

Both libraries have their strengths, with F2 excelling in mobile and lightweight scenarios, and Highcharts offering more comprehensive charting capabilities for complex visualizations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

npm package NPM downloads Percentage of issues still open

F2,一个专注于移动,开箱即用的可视化解决方案,完美支持 H5 环境同时兼容多种环境(node, 小程序,weex)。完备的图形语法理论,满足你的各种可视化需求。专业的移动设计指引为你带来最佳的移动端图表体验。英文 README

在此衷心感谢《The Grammar of Graphics》的作者 Leland Wilkinson,为 F2 的图形语法提供了理论基础!

安装

$ npm install @antv/f2

特性

专注移动,体验优雅

  • 轻量化呈现,自然反馈:在设计上我们以人为本,追求自然简单易懂,有吸引力的表达效果,让用户在碎片化的时间里更快更高效得获取图表信息。同时在可视化的操作我们追求内容和操作有机融合,符合人的自然行为反应,让交互操作更自然。

  • 轻巧流畅:F2 一直致力于追求极致的性能,针对移动设备做了大量的优化,在支持丰富(50+)图表的基础上同时保持代码量的小巧(不带交互版本 gzip 压缩后 44k,带所有交互版本 56k),同时提供模块化的设计以支持动态加载,提供更优的大小。

  • 多端异构:在完美支持 H5 环境的同时,同时兼容 Node.js,支付宝小程序、微信小程序、React Native以及 Weex 端的渲染,一份代码,多设备多环境渲染。

图表丰富,组件完备

与传统的图表库不同,抛弃了特图特做的封装思路,基于强大的图形语法理论,以数据驱动,通过图形语法的组合灵活构建各类图表,目前可绘制 50+ 图表类型(当然,还可以更多),覆盖各类场景在提供基础的图表可视化能力外,我们还提供了丰富图表功能组件,满足各种功能需求。

扩展灵活,创意无限

我们在提供最佳实践的同时,还为开发者提供了灵活的扩展机制,包括 Shape、动画以及交互的自定义能力,当然还有图表样式的个性化定制,满足各种个性化的图表要求。

文档

快速开始

<canvas id="mountNode"></canvas>
// F2 对数据源格式的要求,仅仅是 JSON 数组,数组的每个元素是一个标准 JSON 对象。
const data = [
  { genre: 'Sports', sold: 275 },
  { genre: 'Strategy', sold: 115 },
  { genre: 'Action', sold: 120 },
  { genre: 'Shooter', sold: 350 },
  { genre: 'Other', sold: 150 },
];

// 获取 canvas context
const context = document.getElementById('mountNode').getContext('2d');
const { props } = (
  <Canvas context={context} pixelRatio={window.devicePixelRatio}>
    <Chart data={data}>
      <Axis field="genre" />
      <Axis field="sold" />
      <Interval x="genre" y="sold" color="genre" />
      <Tooltip />
    </Chart>
  </Canvas>
);

const canvas = new Canvas(props);
canvas.render();

更多示例:demos。

手机扫码观看 demos

本地开发

$ npm install

# 先初始化 monorepo
$ npm run bootstrap

# 再跑测试用例
$ npm run test

# 监听文件变化构建,并打开 demo 页面
$ npm run dev

# 打开某一个具体的测试用例
$ npm run test-watch -- 'TestFileName'

如何贡献

如果您在使用的过程中碰到问题,可以先通过 issues 看看有没有类似的 bug 或者建议。

如需提交代码,请遵从我们的贡献指南。

体验改进计划说明

F2 从 3.1.12(2018-06-20 发布)版本开始添加了F2.track(true)方法。 目前我们的体验改进计划已经完成,所以从 3.3.4 版本开始该方法将从 F2 中删除。 如果它给你带来麻烦,我们深表歉意。

License

MIT license.

NPM DownloadsLast 30 Days