echarts的分时图实现

微信图片_20191024134934.jpg

如上图所示,前端时间做了个期权交易项目,涉及到该分时图的实现,没有在网上找到现成的配置,后面自己写了个,是用echarts(版本4.1.0)实现的,我会将相关配置项代码贴在下方:

let interval;//用作y轴的坐标轴分割间隔的值
  let min;//当前实时数据,价格最低
  let max;//当前实时数据,价格最高
  let judgePrice;//昨日行权价格,用来作为今日的涨跌判断基准值
  let responseData;//后台获取的实时行情买卖数据,属性i为数据时间(如:10:30:20),p为数据买卖价格,v为数据当次交易量
  let baseNumber = Math.max(Math.abs(max - junglePrice), Math.abs(min - junglePrice));
  interval = (baseNumber / 2).toFixed(4);

  let option = {
    backgroundColor: '#1f2025',
    animation: false,
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      },
      showContent: false
    },
    axisPointer: {
      link: {xAxisIndex: 'all'},
      label: {
        backgroundColor: '#777'
      }
    },
    visualMap: {
      show: false,
      seriesIndex: 4,
      dimension: 2,
      pieces: [{
        value: 1,
        color: 'red'
      }, {
        value: -1,
        color: 'green'
      },
        {
          value: 0,
          color: '#ffffff'
        }
      ]
    },
    grid: [
      {
        top: '5%',
        left: '15%',
        width:'35%',
        height: '50%'
      },
      {
        top: '5%',
        left: '50%',
        width:'35%',
        height: '50%'
      },
      {
        left: '15%',
        right: '8%',
        top: '72%',
        height: '20%'
      }
    ],
    xAxis: [
      {
        type: 'time',
        boundaryGap: true,
        axisLine: {
          onZero: false,
          lineStyle: {
            color: '#666'
          }
        },
        axisLabel: {
          formatter: function (val,index) {
            if(index === 2){
              return ''
            }
            return moment(val).format('HH:mm');
          }
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: '#666'
          }
        },
        splitArea: {
          areaStyle: {
            color: '#1f2025'
          }
        },
        // splitNumber: 5,
        min: 946690200000,
        max: 946697400000,
        interval: 3600000,
        axisTick: {show: false},
        axisPointer: {
          z: 100,
          label: {
            formatter: function (params) {
              return moment(params.value).format('HH:mm');
            }
          }
        }
      },
      {
        type: 'time',
        gridIndex: 1,
        boundaryGap: true,
        axisLine: {
          onZero: false,
          lineStyle: {
            color: '#666'
          }
        },
        axisLabel: {
          formatter: function (val,index) {
            if(index === 0){
              return '11:30/13:00'
            }
            return moment(val).format('HH:mm');
          }
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: '#666'
          }
        },
        splitArea: {
          areaStyle: {
            color: '#1f2025'
          }
        },
        // splitNumber: 5,
        min: 946702800000,
        max: 946710000000,
        interval: 3600000,
        axisTick: {show: false},
        axisPointer: {
          z: 100,
          label: {
            formatter: function (params) {
              return moment(params.value).format('HH:mm');
            }
          }
        }
      },
      {
        type: 'category',
        gridIndex: 2,
        data: responseData.map(item => item.i),
        // scale: true,
        boundaryGap: true,
        axisLine: {onZero: false},
        axisTick: {show: false},
        splitLine: {
          show: true,
          lineStyle: {
            color: '#666'
          }
        },
        splitArea: {
          areaStyle: {
            color: '#1f2025'
          }
        },
        axisLabel: {show: false},
        splitNumber: 20,
        min: 'dataMin',
        max: 'dataMax'
      }
    ],
    yAxis: [
      {
        min: function (val) {
          return (parseFloat(junglePrice) - 2 * parseFloat(interval)).toFixed(4);
        },
        max: function (val) {
          return (parseFloat(junglePrice) + 2 * parseFloat(interval)).toFixed(4);
        },
        interval: Math.abs(interval),
        axisLabel: {
          formatter: function (val) {
            return val.toFixed(4);
          },
          color:function (value) {
            return value-junglePrice === 0?'#cccccc':value-junglePrice > 0 ? 'red' : 'green';
          }
        },
        axisPointer: {
          show: true,
          label: {
            formatter: function (params) {
              return (params.value).toFixed(4);
            }
          }
        },
        axisLine: {
          lineStyle: {
            color: '#666'
          }
        },
        axisTick: {show: false},
        splitLine: {
          lineStyle: {
            color: '#666'
          }
        },
        splitArea: {
          areaStyle: {
            color: '#1f2025'
          }
        },
      },
      {
        gridIndex: 1,
        position:'right',
        min: function (val) {
          return (parseFloat(junglePrice) - 2 * parseFloat(interval)).toFixed(4);
        },
        max: function (val) {
          return (parseFloat(junglePrice) + 2 * parseFloat(interval)).toFixed(4);
        },
        interval: Math.abs(interval),
        axisLabel: {
          formatter: function (val) {
            let data = (val - junglePrice)/junglePrice*100;
            return data.toFixed(2)+'%';
          },
          color:function (value) {
            return value-junglePrice === 0?'#cccccc':value-junglePrice > 0 ? 'red' : 'green';
          }
        },
        axisPointer: {
          show: true,
          label: {
            formatter: function (params) {
              return (params.value).toFixed(4);
            }
          }
        },
        axisLine: {
          lineStyle: {
            color: '#666'
          }
        },
        axisTick: {show: false},
        splitLine: {
          lineStyle: {
            color: '#666'
          }
        },
        splitArea: {
          areaStyle: {
            color: '#1f2025'
          }
        },
      },
      {
        min: 'dataMin',
        max: 'dataMax',
        gridIndex: 2,
        splitNumber: 1,
        interval: 10000,
        showMinLabel: true,
        showMaxLabel: true,
        axisLabel: {
          formatter: function (value, index) {
            if (index === 0) {
              return '张'
            } else {
              return value;
            }
          },
        },
        axisPointer: {
          show: true,
          label: {
            formatter: function (params) {
              return (params.value).toFixed(0);
            }
          }
        },
        axisTick: {show: false},
        axisLine: {
          lineStyle: {
            color: '#666'
          }
        },
        splitLine: {
          lineStyle: {
            color: '#666'
          }
        },
        splitArea: {
          areaStyle: {
            color: '#1f2025'
          }
        },
      }
    ],
    series: [
      {
        name: '当前价',
        type: 'line',
        data: responseData.map(item => {
          let date = '2000-01-01' + ' ' + item.i;
          let dateNumber = Date.parse(new Date(date));
          return [dateNumber, item.p];
        }),
        smooth: true,
        symbol: 'none',
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgb(2,228,253)'
          }, {
            offset: 1,
            color: 'transparent'
          }])
        },
        lineStyle: {
          color: 'rgb(2,228,253)',
          width: 1
        }
      },
      {
        name: '当前价',
        type: 'line',
        xAxisIndex: 1,
        yAxisIndex: 1,
        data: responseData.map(item => {
          let date = '2000-01-01' + ' ' + item.i;
          let dateNumber = Date.parse(new Date(date));
          return [dateNumber, item.p];
        }),
        smooth: true,
        symbol: 'none',
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgb(2,228,253)'
          }, {
            offset: 1,
            color: 'transparent'
          }])
        },
        lineStyle: {
          color: 'rgb(2,228,253)',
          width: 1
        }
      },
      {
        name: '行权价',
        type: 'line',
        data: responseData.map(item => {
          let date = '2000-01-01' + ' ' + item.i;
          let dateNumber = Date.parse(new Date(date));
          return [dateNumber, judgePrice];
        }),
        smooth: true,
        symbol: 'none',
        lineStyle: {
          color: 'rgb(248,185,0)',
          width: 1
        }
      },
      {
        name: '行权价',
        type: 'line',
        xAxisIndex: 1,
        yAxisIndex: 1,
        data: responseData.map(item => {
          let date = '2000-01-01' + ' ' + item.i;
          let dateNumber = Date.parse(new Date(date));
          return [dateNumber, judgePrice];
        }),
        smooth: true,
        symbol: 'none',
        lineStyle: {
          color: 'rgb(248,185,0)',
          width: 1
        }
      },
      {
        name: '买卖量',
        type: 'bar',
        xAxisIndex: 2,
        yAxisIndex: 2,
        itemStyle: {
          normal: {
            color: 'green',
            color0: 'red',
            borderColor: null,
            borderColor0: null
          }
        },
        data: responseData.map((item, index, arr) => {
          let judge = item.p === junglePrice ? 0 : item.p > junglePrice ? 1 : -1;
          if (index === 0) {
            judge = item.p === junglePrice ? 0 : item.p > junglePrice ? 1 : -1;
          } else {
            judge = item.p === arr[index - 1].p ? 0 : item.p > arr[index - 1].p ? 1 : -1;
          }
          return [index, item.v, judge];
        })
      }
    ]
  }; 

由于分时图的特殊性,只关注9:30到下午15:00间的实时交易数据,并且中午11:30到下午13:00之间是暂停交易时间,所以该分时图实现的难点一是x轴的实现,二是y轴的价格区间必须得是实时的当日最高价格和当日最高价格以行权价为基准计算,以最高价和最低价与行权价相减值大的为基准值,然后除2得到y轴的分割间隔值。

上面的配置中,涨我用的颜色是red,跌用的green,具体色值根据实际项目需要调整。上图实际将图表渲染层分为三块,用grid调整相应布局。由于横坐标需要用时间戳来计算interval,所以我都是用2000-01-01加相应时间获取到的时间戳,好用于设置x轴的min和max,并且将interval设置为3600000,那么x轴的间隔刚好是一小时,并且09:30到11:30和13:00到15:00用两个单独的grid配置,组合为实际想要的一整个分时图。

responseData为后台获取数据,由于从新浪期权抓取的数据,属性名依照接口返回的有相应的v(交易量),i(时间),p(价格)几个属性,大家在实际使用该配置时,还是得根据自己得后台接口做相应调整。min和max根据自己接口获取得值设置,若接口未提供该值,就需要前端从实时数据中遍历获取实时得min和max值。

综上,我为大家提供了现成得配置,具体实现还得实际在项目中进行调试,有什么问题可以在评论中提问或者私信我,我会及时回答。走过路过,记得帮忙点个赞哟

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

推荐阅读更多精彩内容