AntV中折线图的显示和纵坐标排序错乱,折线图更改字体大小,横坐标斜体显示多条数据

在这里插入图片描述

import F2 from '@antv/f2';

const data = [{
  time: '2016-08-08 00:00:00',
  tem: 10
}, {
  time: '2016-08-08 00:10:00',
  tem: 22
}, {
  time: '2016-08-08 00:30:00',
  tem: 16
}, {
  time: '2016-08-09 00:35:00',
  tem: 26
}, {
  time: '2016-08-09 01:00:00',
  tem: 12
}, {
  time: '2016-08-09 01:20:00',
  tem: 26
}, {
  time: '2016-08-10 01:40:00',
  tem: 18
}, {
  time: '2016-08-10 02:00:00',
  tem: 26
}, {
  time: '2016-08-10 02:20:00',
  tem: 12
}];
const chart = new F2.Chart({
  id: 'container',
  pixelRatio: window.devicePixelRatio
});
chart.source(data, {
  time: {
    type: 'timeCat',
    tickCount: 3,//横坐标显示几个
    range: [ 0, 1 ]
  },
  tem: {
    tickCount: 5,
    min: 0,//纵坐标的最小值,一般不需要设置max,设置max容易导致纵坐标错乱
    alias: '总收益',//点击折线图后显示的tip值,如果不设置会直接显示tem:12,设置后显示总收益:12
  }
});

chart.axis('time', {
  label: function label(text, index, total) {
    const textCfg = {};
    if (index === 0) {
      textCfg.textAlign = 'left';
    } else if (index === total - 1) {
      textCfg.textAlign = 'right';
    }
    textCfg.fontSize='16'//设置time坐标轴的坐标轴字体大小
    return textCfg;
  }
});
chart.axis('tem', {
  label: function label(text, index, total) {
    const textCfg = {};
    textCfg.fontSize='18'//设置tem坐标轴的坐标轴字体大小
    return textCfg;
  }
});
chart.tooltip({
  showCrosshairs: true
});

chart.area()
  .position('time*tem')
  .color('l(90) 0:#1890FF 1:#f7f7f7')
  .shape('smooth');
chart.line()
  .position('time*tem')
  .color('l(90) 0:#1890FF 1:#f7f7f7')
  .shape('smooth');
chart.render();

其中遇到的坑
纵坐标排序混乱—>解决方法:data里的纵坐标需要是数字形式,不能是字符串的格式,可以通过parseFloat()或者parseInt()进行转化

在项目中可能会用到很多以下这种图:横坐标斜体显示多条数据
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

import F2 from '@antv/f2';

const data = [{
  time: '2016-08-08 00:00:00',
  tem: 10
}, {
  time: '2016-08-08 00:10:00',
  tem: 22
}, {
  time: '2016-08-08 00:30:00',
  tem: 16
}, {
  time: '2016-08-09 00:35:00',
  tem: 26
}, {
  time: '2016-08-09 01:00:00',
  tem: 12
}, {
  time: '2016-08-09 01:20:00',
  tem: 26
}, {
  time: '2016-08-10 01:40:00',
  tem: 18
}, {
  time: '2016-08-10 02:00:00',
  tem: 26
}, {
  time: '2016-08-10 02:20:00',
  tem: 12
}];
const chart = new F2.Chart({
  id: 'container',
  pixelRatio: window.devicePixelRatio
});
chart.source(data, {
  tem: {
    tickCount: 5,
    min: 0,//纵坐标的最小值,一般不需要设置max,设置max容易导致纵坐标错乱
    alias: '总收益',//点击折线图后显示的tip值,如果不设置会直接显示tem:12,设置后显示总收益:12
  }
});

chart.axis('time', {
  tickLine: {
     length: 4,
     stroke: '#e8e8e8',
     lineWidth: 1
   },
   label: {
     textAlign: 'end',//改变横坐标的显示位置,可以设置start或者end
     textBaseline: 'middle',
     fontSize:'13',//横坐标字号
     rotate: -Math.PI / 3 //倾斜角度
     
     //Math.PI / 2 纵坐标垂直显示
     //Math.PI / 3 和-Math.PI / 3都是倾斜显示,只是方向相反
     //如果是Math.PI / 3 正向的就设置textAlign: 'start'
     //如果是-Math.PI / 3 反向的就设置textAlign: 'end'
   }
});
chart.axis('tem', {
  label: function label(text, index, total) {
    const textCfg = {};
    textCfg.fontSize='18'//设置tem坐标轴的坐标轴字体大小
    return textCfg;
  }
});
chart.tooltip({
  showCrosshairs: true
});

chart.area()
  .position('time*tem')
  .color('l(90) 0:#1890FF 1:#f7f7f7')
  .shape('smooth');
chart.line()
  .position('time*tem')
  .color('l(90) 0:#1890FF 1:#f7f7f7')
  .shape('smooth');
chart.render();
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AntV G2 可以通过多种方式实现分组柱状图和折线图的混合画法,以下是其一种实现方法: 1. 首先,定义数据格式,包括 x 轴数据、柱状图数据折线图数据。 2. 然后,使用 G2 的图表配置方法,设置 x 轴和 y 轴的属性。 3. 接下来,使用 G2 的图表创建方法,创建柱状图和折线图对象。 4. 最后,将柱状图对象和折线图对象添加到同一个视图,实现分组柱状图和折线图的混合画法。 以下是一个示例代码,实现了分组柱状图和折线图的混合画法: ```javascript import { Chart } from '@antv/g2'; // 定义数据格式 const data = [ { x: '一月', y1: 10, y2: 20 }, { x: '二月', y1: 20, y2: 30 }, { x: '三月', y1: 30, y2: 40 }, { x: '四月', y1: 40, y2: 50 }, { x: '五月', y1: 50, y2: 60 }, { x: '六月', y1: 60, y2: 70 }, ]; // 创建图表对象 const chart = new Chart({ container: 'container', autoFit: true, height: 500, }); // 设置 x 轴和 y 轴的属性 chart.scale({ x: { range: [0, 1] }, y: { min: 0 }, }); // 创建柱状图对象 const bar = chart .interval() .position('x*y1') .color('x') .adjust([{ type: 'dodge', marginRatio: 0.1 }]); // 创建折线图对象 const line = chart .line() .position('x*y2') .color('x'); // 添加柱状图对象和折线图对象到同一个视图 chart.render(); bar.render(); line.render(); ``` 该示例代码创建了一个包含两组数据的分组柱状图和一组数据折线图,其柱状图和折线图共享 x 轴,分别对应 y1 和 y2,柱状图和折线图的颜色按照 x 轴分组。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值