最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Draw horizontal target line using EChart.JS - Stack Overflow

programmeradmin0浏览0评论

I would like to draw a horizontal target line showing threshold limits on a line, bar and pie chart using EChart.JS (.html).

There are other threads - "Chart.js - draw horizontal line" which detail how to do it with Chart.JS. Has anyone out there got particular experience on this with EChart?

Thanks in advance.

I would like to draw a horizontal target line showing threshold limits on a line, bar and pie chart using EChart.JS (https://ecomfe.github.io/echarts-doc/public/en/index.html).

There are other threads - "Chart.js - draw horizontal line" which detail how to do it with Chart.JS. Has anyone out there got particular experience on this with EChart?

Thanks in advance.

Share Improve this question asked Aug 20, 2017 at 4:48 Tina_SwiftTina_Swift 731 gold badge1 silver badge5 bronze badges 2
  • I found this demo: ecomfe.github.io/echarts-examples/public/…, which seems to indicate that the option you're searching is 'markLine': ecomfe.github.io/echarts-doc/public/en/…. Haven't tried it yet though. – Bob Commented Oct 25, 2017 at 4:45
  • Thanks, looks that that will do it – Tina_Swift Commented Oct 26, 2017 at 5:14
Add a comment  | 

1 Answer 1

Reset to default 16

[Edit] Since Echarts v3 came up and was passed to the Apache Foundation, the documentation has been sclattered through different URLs, some options have gone away, some are not shown in all documentation resources, and so on. Links provided below have been updated (as of 24/02/2020) but might break again. I haven't fully tried v3 but provided code below should still work.[/Edit]

The option markLine is designed for that, see documentation here: https://echarts.apache.org/en/option.html#series-line.markLine

Note that there are different uses for it, and different options to provide, depending on what you want to draw:

  • arbitrary line on the canvas (any size, any direction, any style)
  • lines matching data caracteristics (min, max, average)
  • horizontal/vertical lines

You have to use the attribute markLine.data in all cases, and description of specifics is described here: https://echarts.apache.org/en/option.html#series-line.markLine.data

Here's how I go, with a line curve on a time serie. Note that I couldn't get, within markLine.data[0], yAxis to be enough to draw a horizontal line: xAxis must be specified too (start and end points).

   xAxis:  {
       type: 'time',
   },
   yAxis: {
       type: 'value'
   },
   series: [{
    type: 'line',
    xAxisIndex: 0,
    yAxisIndex: 0,
    data: [
      [1509762600, 7.11376],
      [1509832800, 7.54459],
      [1509849000, 7.64559]
    ],
    markLine: {
      data: [
        // 1st line we want to draw
        [
          // start point of the line
          // we have to defined line attributes only here (not in the end point)
          {
            xAxis: 1509762600,
            yAxis: 3,
            symbol: 'none',
            lineStyle: {
              normal: {
                color: "#00F"
              }
            },
            label: {
              normal: {
                show: true,
                position: 'end',
                formatter: 'my label'
              }
            }
          },
          // end point of the line
          {
            xAxis: 1509849000,
            yAxis: 3,
            symbol: 'none'
          }
        ]
      ]
    }
  }]

Here's a fiddle I found: https://jsfiddle.net/381510688/hff93ska/

Note that ECharts really like to display markLines with arrow symbols in the end of it, hence my use of symbol: 'none' in above code, to have just the line drawn.

发布评论

评论列表(0)

  1. 暂无评论