[js]roughViz.js 手绘风格的网页图表js库


title: roughViz.js 手绘风格的网页图表js库
date: 2020-01-05 14:58:15
tags:

  • "js"
  • "工具/插件"
    categories:
  • "js"

cover

是一个非常个性化的图标插件,且可以根据自己喜好变换出很多不同的显示样式。
推荐:★★★★☆

支持图表类型

  • Bar (roughViz.Bar) 条形图
  • Horizontal Bar (roughViz.BarH) 水平条形图
  • Donut (roughViz.Donut) 圆环图
  • Line (roughViz.Line) 折线图
  • Pie (roughViz.Pie) 饼图
  • Scatter (roughViz.Scatter) 散点图
  • Stacked Bar (roughViz.StackedBar) 堆积条形图

cdn

<script src="https://unpkg.com/rough-viz@1.0.5"></script>

npm

npm install rough-viz
npm install react-roughviz
npm install vue-roughviz

use

data 处理有三种方式:

  • csv 文件
  • tsv 文件
  • data object
// add this to abstract base
  resolveData(data) {
    if (typeof data === 'string') {
      if (data.includes('.csv')) {
        return () => {
          csv(data).then(d => {
            // console.log(d);
            this.data = d;
            this.drawFromFile();
          });
        };
      } else if (data.includes('.tsv')) {
        return () => {
          tsv(data).then(d => {
            this.data = d;
            this.drawFromFile();
          });
        };
      }
    } else {
      return () => {
        this.data = data;
        this.drawFromObject();
      };
    }
  }

How to use

<!-- container -->
<div id="vis0"></div>
// bar chart
new roughViz.Bar({
  element: "#vis0", // container selection
  data: {
    flavor: ["North", "South", "East", "West"],
    price: [10, 5, 8, 3]
  },
  labels: "flavor",
  values: "price"
});

// Horizontal Bar Chart
new roughViz.BarH({
  element: "#vis2",
  title: "Vehicles I've Had",
  data: {
    labels: [
      "1992 Ford Aerostar Van",
      "2013 Kia Rio",
      "1980 Honda CB 125s",
      "1992 Toyota Tercel"
    ],
    values: [8, 4, 6, 2]
  },
  xLabel: "Time Owned (Years)"
});

// Donut chart
new roughViz.Donut({
  element: "#vis1",
  data: {
    labels: ["North", "South", "East", "West"],
    values: [10, 5, 8, 3]
  }
});

// line chart
new roughViz.Line({
  element: "#vis3",
  data:
    "https://raw.githubusercontent.com/jwilber/random_data/master/tweets.csv",
  title: "Line Chart",
  y: "favorites",
  y2: "retweets",
  y3: "tweets",
  yLabel: "hey"
});

// pie chart
new roughViz.Pie({
  element: "#vis4",
  titleFontSize: "1.5rem",
  data: {
    labels: ["yes", "no", "lol idk man"],
    values: [2, 8, 4]
  },
  title: "'Yarn Plot': Useful?"
});

// Scatter chart
new roughViz.Scatter({
  element: "#vis5",
  data:
    "https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv",
  title: "Iris Scatter Plot",
  x: "sepal_width",
  y: "petal_length",
  colorVar: "species",
  highlightLabel: "species",
  fillWeight: 4,
  radius: 12,
  colors: ["pink", "coral", "skyblue"],
  stroke: "black",
  strokeWidth: 0.4,
  roughness: 1,
  width: 400,
  height: 450,
  font: 0,
  xLabel: "sepal width",
  yLabel: "petal length",
  curbZero: false
});
// Stacked Bar chart
new roughViz.StackedBar({
  element: "#vis6",
  data: [
    {
      month: "Jan",
      A: 20,
      B: 5,
      C: 10
    },
    {
      month: "Feb",
      A: 25,
      B: 10,
      C: 20
    },
    {
      month: "March",
      A: 30,
      B: 50,
      C: 10
    }
  ],
  labels: "month",
  title: "Monthly Revenue",
  roughness: 2,
  colors: ["blue", "#f996ae", "skyblue", "#9ff4df"],
  fillWeight: 0.35,
  strokeWidth: 0.5,
  fillStyle: "cross-hatch",
  stroke: "black"
});

在原本基础的图形表现上,还可以自定义图表显示样式,从而使图表更具独特风格。

  • axisFontSize [string]: Font-size for axes' labels. Default: '1rem'. 坐标轴字体大小
  • axisRoughness [number]: Roughness for x & y axes. Default: 0.5. 坐标轴粗糙程度
  • axisStrokeWidth [number]: Stroke-width for x & y axes. Default: 0.5. 坐标轴线宽
  • bowing [number]: Chart bowing. Default: 0. 弯曲程度
  • color [string]: Color for each bar. Default: 'skyblue'. 填色
  • fillStyle [string]: Bar fill-style. Should be one of fillStyles shown above. 填充风格
  • fillWeight [number]: Weight of inner paths' color. Default: 0.5. 填充饱和度
  • font: Font-family to use. You can use 0 or gaegu to use Gaegu, or 1 or indie flower to use Indie Flower. Or feed it something else. Default: Gaegu. 字体类型
  • highlight [string]: Color for each bar on hover. Default: 'coral'. 高亮
  • innerStrokeWidth [number]: Stroke-width for paths inside bars. Default: 1. 内部绘线宽度
  • interactive [boolean]: Whether or not chart is interactive. Default: true. 可交互性,鼠标 hover 操作等
  • labelFontSize [string]: Font-size for axes' labels. Default: '1rem'. 标签字体大小
  • margin [object]: Margin object. Default: {top: 50, right: 20, bottom: 70, left: 100} 外边距
  • padding [number]: Padding between bars. Default: 0.1. 内边距
  • roughness [number]: Roughness level of chart. Default: 1. 粗糙程度
  • simplification [number]: Chart simplification. Default 0.2. 简化
  • stroke [string]: Color of bars' stroke. Default: black. 绘线颜色
  • strokeWidth [number]: Size of bars' stroke. Default: 1. 绘线宽度
  • title [string]: Chart title. Optional. 图表标题
  • titleFontSize [string]: Font-size for chart title. Default: '1rem'. 标题字体大小
  • tooltipFontSize [string]: Font-size for tooltip. Default: '0.95rem'. 提示字体大小
  • xLabel [string]: Label for x-axis. x 轴标签
  • yLabel [string]: Label for y-axis. y 轴标签
  • legend [boolean]: Whether or not to add legend. Default: 'true'. 图例
  • legendPosition [string]: Position of legend. Should be either 'left' or 'right'. Default: 'right'. 图例位置
  • circle [boolean]: Whether or not to add circles to chart. Default: true. 圆形
  • circleRadius [number]: Radius of circles. Default: 10. 圆型半径
  • circleRoughness [number]: Roughness of circles. Default: 2. 圆形粗糙程度

fillStyle


rough_fillStyles

roughness


roughViz_roughnessbars

fillWeight


roughViz_fillweight

传送门 roughViz github

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 看着是欢喜 打开是惊喜 闻着是香气 吃着是福气 得到是运气 点开是意外 (商场敲彩蛋、喜蛋、网络金蛋)
    清风伏笔阅读 146评论 0 2
  • 到现在为止《品牌洗脑》这本书也可以画下一个完美的句号了。
    孙明磊阅读 160评论 0 0