AntV 是蚂蚁金服全新一代数据可视化解决方案,致力于提供一套简单方便、专业可靠、无限可能的数据可视化最佳实践html
日常开发中你们都会接触一些数据可视化工具,经常使用的echarts,Highcharts,D3等。
我的以为AntV的UI好看一些,再加上F2是移动端可视化方案,因此就有了接下来的爬坑过程。vue
vue项目开发,我写的是折线面积图+滑动npm
下载canvas
npm install @antv/f2 --save 直接引入就ok const F2 = require('@antv/f2');
开始画图
建立canvas,指定id数组
<canvas id="myChart" width="400" height="260"></canvas>
开始绘制echarts
// 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 }, ]; // Step 1: 建立 Chart 对象 const chart = new F2.Chart({ id: 'myChart', pixelRatio: window.devicePixelRatio // 指定分辨率 }); // Step 2: 载入数据源 chart.source(data); // Step 3:建立图形语法,绘制柱状图,由 genre 和 sold 两个属性决定图形位置,genre 映射至 x 轴,sold 映射至 y 轴 chart.interval().position('genre*sold').color('genre'); // Step 4: 渲染图表 chart.render();
嘿嘿,都是copy官网的ide
最经常使用的,毕竟其余的我也不会
list: [ {"date": "2018-08-08","record": 166}, {"date": "2019-01-01","record": 174}, {"date": "2019-01-02","record": 166}, {"date": "2019-01-03","record": 166}, {"date": "2019-01-05","record": 187}, {"date": "2019-01-06","record": 189}, {"date": "2019-01-17","record": 156}, {"date": "2019-04-18","record": 231} ] //date为X轴,record Y轴 chart.axis('date', { label: function label(text, index, total) { var cfg = { textAlign: 'center',//定义样式 }; if (index === 0) { cfg.textAlign = 'start'; cfg.text = '+' + text;//能够更改X轴数据格式等 cfg.fill = '#F5222D';//文字颜色 } if (index > 0 && index === total - 1) { cfg.textAlign = 'end'; } return cfg; } }); //定义X轴Y轴 var defs = { date: { type: 'timeCat',//类型为日期 mask: 'YY/MM/DD',//日期格式 tickCount: 4,//区间,写几就有几个区间 }, record: { tickCount: 5, min: 0,//轴的最小值&最大值 alias: '身高'//定义这个轴是啥类型 } }; chart.source(this.list, defs);//载入数据
chart.tooltip(false)//直接关闭,没任何提示 chart.tooltip({ showItemMarker: false, onShow: function onShow(ev) { //自定义提示内容 var items = ev.items; items[0].name = null; items[0].name = items[0].title; items[0].value = items[0].value + "%"; } });
chart.guide().text({ position: ['min', 'max'], content: '提示', style: { textBaseline: 'middle', textAlign: 'start' }, offsetY: -23, offsetX: -25 });
就是这个东西工具
//先X轴后Y轴 chart.line().position("date*record").color("#FFB024");//线 chart.area().position("date*record").style({fillStyle: "l(90) 0:rgba(255,202,106,1) 1:rgba(255,201,104,0.3)",fillOpacity: 1});//面积渐变色 chart.point().position('date*record').color('red');//点
手机上看图,若是X轴数据过多,都堆一块儿了,因此就用滑动了
//引入 const ScrollBar = require("@antv/f2/lib/plugin/scroll-bar"); const pan = require("@antv/f2/lib/interaction/pan"); //注册 var chart = new F2.Chart({ id: 'myChart', pixelRatio: window.devicePixelRatio, plugins: [ScrollBar, pan] }); //官网用法 //X轴为1955-2015,5年一个区间,载入数据都时候定义一个最大最小都区间 chart.source(data, { release: { min: 1990, max: 2010 } }); chart.interaction('pan'); // 定义进度条 chart.scrollBar({ mode: 'x', xStyle: { offsetY: -5 } });
//由于咱们X轴日期格式为2019-01-01这种 换算了一下 当数据大于五条当时候开始滑动 const a = 1000000000 var dataMap = data.map((item, index) => { return { date: index + a, record: item.record } }) var min = a, max = a + 4, tickCount = 5; if (dataMap.length > 5) { tickCount = 5; chart.scrollBar({ mode: "x", xStyle: { backgroundColor: "#e8e8e8", fillerColor: "#808080", offsetY: -2 } }); chart.interaction("pan"); }else if(dataMap.length == 1){ max = a; tickCount = 2; chart.point().position("date*record").color("#FFB024"); }else{ max = dataMap.length +a -1 tickCount = dataMap.length; } chart.source(dataMap,{ date: { min:min, max:max, tickCount:tickCount } }) chart.axis('date', { label: function label(text, index, total) { const cfg = { textAlign: 'center' } cfg.text = data[text-a].date return cfg; } });
第一次发文章,可能不是太好,但愿你们喜欢,原谅我不会弄动图,因此没放效果图。ui