I need to implement a scale diagram like the one shown in the following chart. enter image description here

我try 用Etcharts 5.5.0用boxplot创建这个比例哈特.

Here the code : https://codesandbox.io/p/sandbox/error-bar-on-catesian-v9vmmk?file=%2Findex.js

enter image description here

然而,我无法定义和完成我的图表.

有什么 idea 吗?

提前感谢您.

Ben

推荐答案

为了简化示例,我使用固定值而不是生成随机值.

在您的示例中,您将类别与X轴关联起来.如果您想创建水平图表,则应该将它们与Y轴关联.

您可以使用api.coord()来计算值从到的位置.这里,X轴和Y轴的值必须以相反的方式输入.

经过这两个修改,我们就得到了水平图.由于您没有提供更多信息,我只能假设这就是意图.

图表上的照片上有一条额外的中间线,称为Mid Value.为此,您只需取最大值和最小值的平均值即可.我增加了线的高度,并将其厚度增加了一倍.

/**
 ** Initialize
 */
const chartDom = document.getElementById('chart');
const chart = echarts.init(chartDom);

/**
 ** Data
 */
const categoryData = ['category0', 'category1', 'category2', 'category3', 'category4'];
const barData = [30, 50, 70, 60, 80];
const errorData = [
  ['category0', 10, 40],
  ['category1', 30, 60],
  ['category2', 50, 80],
  ['category3', 40, 70],
  ['category4', 60, 90]
];

/**
 ** Option
 */
const option = {
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  title: {
    text: 'Horizontal Boxplot'
  },
  legend: {
    data: ['bar', 'error']
  },
  yAxis: {
    type: 'category',
    data: categoryData
  },
  xAxis: {},
  series: [
    {
      type: 'bar',
      name: 'bar',
      data: barData,
      itemStyle: {
        color: '#cce0ff'
      }
    },
    {
      type: 'custom',
      name: 'error',
      itemStyle: {
        borderWidth: 1.5
      },
      renderItem: function (params, api) {
        const yValue = api.value(0);
        
        const lowPoint = api.coord([api.value(1), yValue]);
        const midPoint = api.coord([(api.value(1) + api.value(2)) / 2, yValue]);
        const highPoint = api.coord([api.value(2), yValue]);
        
        const halfHeight = api.size([1, 1])[1] * 0.1;
        
        const style = api.style({
          stroke: api.visual('color'),
          fill: undefined
        });
        return {
          type: 'group',
          children: [
            // Line
            {
              type: 'line',
              transition: ['shape'],
              shape: {
                x1: lowPoint[0],
                y1: lowPoint[1],
                x2: highPoint[0],
                y2: highPoint[1]
              },
              style: style
            },
            // Low Value
            {
              type: 'line',
              transition: ['shape'],
              shape: {
                x1: lowPoint[0],
                y1: lowPoint[1] - halfHeight,
                x2: lowPoint[0],
                y2: lowPoint[1] + halfHeight
              },
              style: style
            },
            // Mid Value
            {
              type: 'line',
              transition: ['shape'],
              shape: {
                x1: midPoint[0],
                y1: midPoint[1] - halfHeight * 2,
                x2: midPoint[0],
                y2: midPoint[1] + halfHeight * 2
              },
              style: {...style, lineWidth: style.lineWidth * 2}
            },
            // High Value
            {
              type: 'line',
              transition: ['shape'],
              shape: {
                x1: highPoint[0],
                y1: highPoint[1] - halfHeight,
                x2: highPoint[0],
                y2: highPoint[1] + halfHeight
              },
              style: style
            }
          ]
        };
      },
      encode: {
        x: [1, 2],
        y: 0
      },
      data: errorData,
      z: 100
    }
  ]
};

/**
 ** Render Chart
 */
chart.setOption(option);
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.5.0/echarts.min.js" integrity="sha512-k37wQcV4v2h6jgYf5IUz1MoSKPpDs630XGSmCaCCOXxy2awgAWKHGZWr9nMyGgk3IOxA1NxdkN8r1JHgkUtMoQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<div id="chart" style="width: 650px; height: 250px;"></div>

Javascript相关问答推荐

JavaScript:循环访问不断变化的数组中的元素

强制执行useStatego struct 化变量[foo,setFoo]的命名约定?

添加/删除时React图像上传重新渲染上传的图像

为什么从liveWire info js代码传递数组我出现错误?

Vega中的模运算符

单击更新页面的按钮后,页面刷新;测试/断言超时,有两个标题,但没有一个标题

硬币兑换运行超时

使用JavaScript重新排序行

空的结果抓取网站与Fetch和Cheerio

你怎么看啦啦队的回应?

以Angular 实现ng-Circle-Progress时出错:模块没有导出的成员

为什么我的自定义元素没有被垃圾回收?

为什么这个.add.group({})在教程中运行得很好,但在我的游戏中就不行了?

使用带有HostBinding的Angular 信号来更新样式?

在JS中动态创建对象,并将其追加到HTML表中

如果一个字符串前面有点、空格或无字符串,后面有空格、连字符或无字符串,则匹配正则表达式

第一项杀死下一项,直到数组长度在javascript中等于1

警告框不显示包含HTML输入字段的总和

如何在移动设备中使用JAVASSCRIPT移除点击时的焦点/悬停状态

JSX/React -如何在组件props 中循环遍历数组