echarts - Horizontal dataZoom slider does not plot data against the correct X axis values? - Stack Overflow

I would like to use an echarts horizontal dataZoom slider with timeseries data.The code below (complet

I would like to use an echarts horizontal dataZoom slider with timeseries data.

The code below (complete example) works fine except for the fact that the miniature chart of the data in the slider is evidently plotted using a sequential index (1, 2, 3, 4, etc.) for the X axis instead of the actual data values (1980, 2021, 2022, 2023, etc.). Hence it's confusing and misleading when actually using the slider to view a section of data, as in the example.

I found and tried this answer (complete example), but it did not change the slider appearance or behaviour.

<html>
<head>
<script type="text/javascript" src="/[email protected]/dist/echarts.min.js"></script>
</head>
<body>
<div id="chart" style="width: 500px; height: 400px"></div>
<script type="text/javascript">
var chartDom = document.getElementById('chart');
var chart = echarts.init(chartDom);
var option;
var data = [
  [new Date('1980-01-01'), 7],
  [new Date('2021-01-01'), 10],
  [new Date('2021-07-01'), 2],
  [new Date('2022-01-01'), 9],
  [new Date('2022-07-01'), 1.5],
  [new Date('2023-01-01'), 10],
  [new Date('2023-07-01'), 2],
  [new Date('2024-01-01'), 8.7],
  [new Date('2024-07-01'), 1.2],
  [new Date('2025-01-01'), 7],
];
option = {
    dataset: {
    source: data,
    dimensions: ['timestampCol', 'paramCol']
  },
  xAxis: {type: 'time'},
  yAxis: {type: 'value'},
  dataZoom: {
    type: 'slider',
    filterMode: 'none',
  },
  series: [
    {
      name: 'paramCol',
      type: 'line',
      symbol: 'triangle',
      encode: {x: 'timestampCol', y: 'paramCol'}
    }
  ]
};
option && chart.setOption(option);
</script>
</body>
</html>

I would like to use an echarts horizontal dataZoom slider with timeseries data.

The code below (complete example) works fine except for the fact that the miniature chart of the data in the slider is evidently plotted using a sequential index (1, 2, 3, 4, etc.) for the X axis instead of the actual data values (1980, 2021, 2022, 2023, etc.). Hence it's confusing and misleading when actually using the slider to view a section of data, as in the example.

I found and tried this answer (complete example), but it did not change the slider appearance or behaviour.

<html>
<head>
<script type="text/javascript" src="https://cdn.jsdelivr/npm/[email protected]/dist/echarts.min.js"></script>
</head>
<body>
<div id="chart" style="width: 500px; height: 400px"></div>
<script type="text/javascript">
var chartDom = document.getElementById('chart');
var chart = echarts.init(chartDom);
var option;
var data = [
  [new Date('1980-01-01'), 7],
  [new Date('2021-01-01'), 10],
  [new Date('2021-07-01'), 2],
  [new Date('2022-01-01'), 9],
  [new Date('2022-07-01'), 1.5],
  [new Date('2023-01-01'), 10],
  [new Date('2023-07-01'), 2],
  [new Date('2024-01-01'), 8.7],
  [new Date('2024-07-01'), 1.2],
  [new Date('2025-01-01'), 7],
];
option = {
    dataset: {
    source: data,
    dimensions: ['timestampCol', 'paramCol']
  },
  xAxis: {type: 'time'},
  yAxis: {type: 'value'},
  dataZoom: {
    type: 'slider',
    filterMode: 'none',
  },
  series: [
    {
      name: 'paramCol',
      type: 'line',
      symbol: 'triangle',
      encode: {x: 'timestampCol', y: 'paramCol'}
    }
  ]
};
option && chart.setOption(option);
</script>
</body>
</html>
Share Improve this question edited Mar 14 at 6:31 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Mar 14 at 6:29 kiikii 5093 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This is the standard behavior of echarts, no matter what kind of axes are used, the x axis positions in the slider are equidistant; see how SliderZoomView constructs the shadow polygon in the source code.

I wouldn't expect the polygon inside the slider to be an accurate representation of the chart data, but if you think this is important, you may add a feature request or bug report on github.

Just as an experiment, to prove the point, one gets an accurate representation of the x positions in the slider if one replaces at line 422:

areaPoints.push([thisCoord, otherCoord]);
linePoints.push([thisCoord, otherCoord]);

thisCoord +=  step;
lastIsEmpty = isEmpty;

with

const thisAxisIndex = info.thisAxis.index,
   thisMinValue = Math.min(...data.getDataExtent(thisAxisIndex)),
   thisMaxValue = Math.max(...data.getDataExtent(thisAxisIndex)),
   thisValue = data.getValues([thisAxisIndex], index)[0];
thisCoord = Math.round(thisShadowExtent[1] * (thisValue - thisMinValue) / (thisMaxValue - thisMinValue));
areaPoints.push([thisCoord, otherCoord]);
linePoints.push([thisCoord, otherCoord]);
lastIsEmpty = isEmpty;

where the first three lines (const declarations) may be moved outside the data.each call. The changed version (don't use in production!) is in this codesandbox.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744670706a4587021.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>