Git Product home page Git Product logo

rx-g2's Introduction

RxG2

响应式G2(RxG2),是使用rxjs封装的响应式的antv/g2的可视化扩展。它使你可以使用函数响应式编程(Functional Reactive Programming, FRP)的形式,编写响应式的可视化图表。这也为G2拓展了交互式图形语法的可能(针对数据可视化,G2目前的交互语法其实更像是G层面的交互语法,其更关注图形的基础交互行为,而非可视化层面)。

基于rxjs的流对异步交互行为的抽象使你在实现复杂可视化交互逻辑时可以不需要花费过多的经历维护交互相关的状态。

相关资料

G2提供了一套基于图形语法的声明式的可视化接口,它使得你可以用简介的写法快速实现复杂的可视化。但当你使用G2处理一些可视化的交互行为时,这种简洁之美就会被破坏掉,大量的外部状态和回调函数破坏了图形语法本身的美感。那能否对G2进行扩展,在保留其原始的使用美感的同时,能用同样简洁优雅的形式来构建交互式的可视化。

受到Vega的启发,rx-g2使用了vega中的signals/predicates/transform的形式来抽象可视化中的异步行为。幸运的是,我们可以直接借助rxjs的能力实现这一机制,同时相比vega需要自定义一套复杂的dsl来支持这种机制,rxjs允许你直接使用JavaScript进行开发,我们也将一些原有的接口做了Observable的改造,这也使得你可以后续更自由的使用rxjs进行复杂交互行为的扩展。

使用

npm i --save rxg2

案例

实现下图的交互效果,即当鼠标覆盖在一个点上时,能够高亮出和他origin类别相同的所有点。

demo image

使用rx-g2的写法

import { GREY_CAT_VALUE, ObservableChart, Utils } from 'rxg2';
import * as op from "rxjs/operators";

const dataSource$: Observable<IRow[]> = from(fetch("/cars.json").then((res) => res.json())).pipe(
    op.startWith([])
);
// 声明变量
const xVar$ = createVariable(dataSource$, "Miles_per_Gallon");
const yVar$ = createVariable(dataSource$, "Horsepower");
const colorVar$ = createVariable(dataSource$, 'Origin')

// 初始化图表
const rxChart = new ObservableChart();

const selection$ = rxChart.useSelection({
    target: 'element',
    type: 'single',
    on: 'mouseover',
});
// 定义图表交互事件产生的predicates,vega中的概念,类似对selection的特征描述,由一堆筛选器构成。
const predicates$: Observable<IFilter[]> = selection$.pipe(
    op.map((rows) => [Utils.createFilter('Cylinders', 'in', rows.filter((row) => row !== null) as IRow[])])
);
// 定义颜色为一个随交互行为变化的变量,
// rx-g2允许你对任何映射到视觉通道上的字段可以升级为一个更为动态变量,而不是写死于一个数据集中已有的常量字段
const color$ = combineLatest([predicates$, colorVar$, dataSource$]).pipe(
    op.map(([predicates, origin, dataSource]) => {
        return dataSource.map((row, rIndex) => {
            if (Utils.rowFilteredOut(row, predicates)) return GREY_CAT_VALUE;
            return origin[rIndex];
        });
    })
);
// 配置图标
rxChart.geom('point').position([xVar$, yVar$])
    .color(color$);
Â
rxChart.data(dataSource$);

rxChart.render();

对比原生的写法,只是把静态的字段绑定变成了一个流的绑定。整体的书写习惯还沿用了G2的写法。

fetch('/cars.json').then(res => res.json()).then(res => {

    chart.point().position('Miles_per_Gallon*Horsepower')
    .color('Origin')

    chart.data(dataSource)

    chart.render();
})

引用

RxG2是参考reactive vega 和 vega-lite 中的一些概念

  • Satyanarayan, Arvind, et al. "Reactive vega: A streaming dataflow architecture for declarative interactive visualization." IEEE transactions on visualization and computer graphics 22.1 (2015): 659-668.

  • Satyanarayan, Arvind, et al. "Vega-lite: A grammar of interactive graphics." IEEE transactions on visualization and computer graphics 23.1 (2016): 341-350.

rx-g2's People

Contributors

observedobserver avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.