echarts上根据时间戳打点

文章详细描述了一个JavaScript对象,展示了如何配置一个图表,用于显示股票的当前价格、成交量等信息,包括颜色、动画效果、轴标签和数据系列的样式。
摘要由CSDN通过智能技术生成
  const options = {
    backgroundColor: 'transparent',
    animationDuration: 5000,
    title: {
      text: [`{a|[${fundInfo.stockName}] ${fundInfo.stockCode}}`, `{b|${(fundInfo.exunbusibuy / 10000).toFixed(2)}万元}`, `{c|${(fundInfo.exunbusisale / 10000).toFixed(2)}万元}`, `{${data.priceLimit == 0 ? 'd' : (data.priceLimit > 0 ? 'b' : 'c')}|${data.priceLimit}%}`].join(''),
      // text:fundInfo.stockName,
      left: 10,
      top: 0,
      textStyle: {
        rich: {
          a: {
            color: '#fff',
            fontSize: 13,
            // fontWeight: '550',
            marginRight: '10px',
          },
          b: {
            color: '#F50057',
            fontSize: 13,
            // fontWeight: '550',
            padding: [2, 4],
          },
          c: {
            color: '#1de9b6',
            fontSize: 13,
            // fontWeight: '550',
            padding: [2, 4],
          },
          d: {
            color: 'skyblue',
            fontSize: 13,
            // fontWeight: '550',
            padding: [2, 4],
          },
        },
      },
    },
    tooltip: {
      hideDelay: 500,
      trigger: 'axis',
      axisPointer: {
        crossStyle: {
          color: '#999',
        },
      },
    },

    visualMap: {
      show: false,
      seriesIndex: 1,
      dimension: 2,
      pieces: [
        {
          value: 1,
          color: '#F50057',
        },
        {
          value: -1,
          color: '#1DE9B6',
        },
      ],
    },
    axisPointer: {
      link: [
        {
          xAxisIndex: 'all',
        },
      ],
    },
    grid: [
      {
        left: 50, //图形距离左边的距离
        right: 10,
        top: 35,
        width: '75%',
        height: 120, // 图表的高度
      },
      {
        left: 50,
        right: 10,
        top: 165,
        bottom: 0,
        width: '75%',
        height: 45,
      },
    ],
    xAxis: [
      {
        type: 'category',
        axisTick: {
          show: false,
        },
        axisPointer: {
          type: 'shadow',
        },

        axisLine: {
          show: false,
          lineStyle: {
            color: '#474747',
          },
        },
        data: timeLineGroup,
        axisLabel: {
          show: false,
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: '#E5E5E5',
            width: 0.3,
            type: 'solid',
          },
        },
        boundaryGap: true,
      },
      {
        gridIndex: 1,
        type: 'category',
        data: timeLineGroup,
        boundaryGap: false,
        axisLabel: {
          interval: 0,
          color: "#97DDFA",
          formatter: function (val: any, index: any) {
            if (index === 0) return '09:30';
            if (index === 60) return '10:30';
            if (index === 120) return '11:30/13:00';
            if (index === 180) return '14:00';
            if (index === 240) return '15:00';
            return '';
          },
        },
        axisTick: {
          interval: function (index: any, value: any) {
            const indexs = [0, 60, 120, 180, 240];
            if (indexs.includes(index)) return true;
          },
        },
        splitLine: {
          show: false,
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
        color: '#fff',
        splitLine: {
          show: true,
          lineStyle: {
            color: '#E5E5E5',
            width: 0.1,
            type: 'solid',
          },
        },
        scale: true,
        interval: data.lineYvalueGap,
        min: data.lineYvalueMin,
        max: data.lineYvalueMax,
        z: 5,
        axisLabel: {
          show: true,
          inside: true,
          margin: -5,
          fontSize: 12,
          align: 'right',
          formatter: function (value: any) {
            if (value > data.preClosPrice) {
              return '{red|' + value + '}';
            } else {
              return '{green|' + value + '}';
            }
          },
          rich: {
            green: { color: '#1DE9B6' },
            red: { color: '#F50057' },
          },
        },
      },
      {
        scale: true,
        gridIndex: 1,
        type: 'value',
        min: data.barYvalueMin,
        max: data.barYvalueMax,
        splitNumber: 2,
        splitLine: {
          lineStyle: {
            color: 'rgba(255,255,255,0.2)',
            type: 'solid',
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          inside: true,
          show: true,
          showMinlabel: true,
          showMaxlabel: true,
          color: '#fff',
          margin: -5,
          fontSize: 12,
          align: 'right',
          formatter: function (value: any, index: any) {
            if (index === 0) {
              return '0手';
            }
            return `${value}`;
          },
        },
      },
      {
        gridIndex: 0,
        type: 'value',
        max: data.fractionYvaluemMax,
        min: data.fractionYvaluemMin,
        interval: data.fractionYvaluemGap,
        splitLine: {
          show: false,
          lineStyle: {
            color: 'rgba(255,255,255,0.2)',
            type: 'solid',
          },
        },
        axisLine: {
          show: false,
        },
        splitArea: {
          show: false,
        },
        scale: true,
        // splitNumber: 1,
        z: 3,
        boundaryGap: false,
        axisLabel: {
          show: true,
          inside: true,
          margin: -5,
          fontSize: 12,
          align: 'left',
          formatter: function (val: any) {
            return `${val}%`;
          },
          textStyle: {
            color: function (val: any, index: any) {
              return val > 0 ? '#F50057' : val < 0 ? '#1DE9B6' : 'skyblue';
            },
          },
        },
      },
    ],
    series: [
      {
        name: '当前价格',
        type: 'line',
        animation: false,
        smooth: true,
        symbol: 'none',
        data: data.lineData,
        lineStyle: {
          color: 'skyblue',
          width: 1,
        },
        z: 3,
        markPoint: {
          data: markPointData
        },
      },
      {
        name: '成交量',
        type: 'bar',
        animation: false,
        xAxisIndex: 1,
        yAxisIndex: 1,
        data: data.barData,
        barWidth: 1,
        itemStyle: {
          normal: {
            color: function (params: any) {
              if (params.data[2]) {
                // console.log(params.data[2],'params.data[2]')
                return '#F50057';
              } else {
                return '#1DE9B6';
              }
            },
          },
        },
      },
    ],
  };

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts 的数据视图中,如果需要将时间戳转换为特定的格式进行显示,可以使用 `optionToContent` 属性和 `contentToOption` 属性来自定义数据视图的转换函数。 下面是一个示例,展示如何将时间戳转换为年月日的格式进行显示: ```javascript option = { xAxis: { type: 'time', data: [1643836800000, 1643923200000, 1644009600000, 1644096000000, 1644182400000] }, yAxis: { type: 'value' }, series: [{ data: [120, 200, 150, 80, 70], type: 'line' }], dataView: { show: true, readOnly: false, title: 'Data View', lang: ['Data View', 'Close', 'Refresh'], optionToContent: function (opt) { var table = '<table class="data-table"><tbody>'; for (var i = 0; i < opt.xAxis[0].data.length; i++) { table += '<tr>' table += `<td>${formatDate(opt.xAxis[0].data[i])}</td>` table += `<td>${opt.series[0].data[i]}</td>` table += '</tr>' } table += '</tbody></table>'; return table; }, contentToOption: function (content) { var newData = []; $(content).find('tr').each(function (i) { newData[i] = $(this).find('td').map(function () { return $(this).text(); }).get(); }); option.xAxis[0].data = newData.map(function (value) { return new Date(value[0]).getTime(); }); option.series[0].data = newData.map(function (value) { return +value[1]; }); return option; } } }; function formatDate(timestamp) { var date = new Date(timestamp); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); return year + '-' + month + '-' + day; } myChart.setOption(option); ``` 在这个示例中,通过 `formatDate` 函数将时间戳格式化为年月日的格式,然后在 `optionToContent` 属性中使用这个函数将时间戳转换为特定的格式进行显示。在 `contentToOption` 属性中,将修改后的数据转换为图表的配置项。最后使用 `setOption` 方法将修改后的配置项应用到图表中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值