echarts の 1 つの座標軸に複数のカテゴリを描画する方法

レンダリングは次のとおりです。

ここに画像の説明を挿入

実装

1. まず、markLine の垂直 x 軸を実現します。

  • X 軸には 2 つのカテゴリがあり、1 つは離散点描画のカテゴリ、もう 1 つは連続点描画の値です。

ここに画像の説明を挿入

  • markLine が x 軸に対して垂直である場合、x 軸の主軸が使用され、データ ポイント タイプは連続ポイントになります。注: ここでのデータ ポイント タイプがカテゴリ離散タイプの場合、markLine はこのデータ ポイントに含まれるマーカー ラインのみを表示できます。また、連続点はデータ範囲内の任意の位置にマーカー線を引くことができます。
    ここに画像の説明を挿入

2. 基本バージョンの完全な実装コードは次のとおりです。


      const nums = 1.398
      const datas1 = [12.12, 18.3, 19.7, 21.1, 22.49, 23.89, 25.29, 26.69, 28.09, 29.48, 30.88, 35.55]
      const lsl = 23.12
      const usl = 28
      const nom = 25.34
      const sigma_plus3 = 34.26
      const sigma_minus3 = 14.34
      //均值
      const mean = 24.30
      //cpk标准差
      const std1 = 3.32
      //ppk标准差
      const std2 = 3.43
      //样本范围
      const range = [12.12, 35.55]
      //x轴分辨率
      const resolution = 10000
      //计算X轴上的每个点
      const xData = []
      //步长
      const step = (range[1] - range[0]) / resolution
      for (let i = range[0]; i <= range[1]; i += step) {
    
    
        xData.push(parseFloat(i.toFixed(2)))
      }
      //柱状图上的点
      const barData = [[18.3, 51],[19.7, 64],[21.1, 65],[22.49, 67],[23.89, 55],[25.29, 81],[18.3, 51],
      [26.69, 80],[28.09, 50],[29.48, 40],[30.88, 7]];

      //计算曲线上每个点的y值
      const yData1 = []
      for (let i = 0; i < xData.length; i++) {
    
    
        let x = xData[i]
        let y = Math.exp(-0.5 * Math.pow((x - mean) / std1, 2)) / (std1 * Math.sqrt(2 * Math.PI))
        yData1.push(y);
      }
      const yData2 = []
      for (let i = 0; i < xData.length; i++) {
    
    
        let x = xData[i]
        let y = Math.exp(-0.5 * Math.pow((x - mean) / std2, 2)) / (std2 * Math.sqrt(2 * Math.PI))
        yData2.push(y);
      }
      // Echarts 图的配置
      option = {
    
    
        title: {
    
    
          text: '正态分布图',
          subtext: ''
        },
        legend: {
    
    
          'data': ['过程能力(估算)', '过程能力(实测)']
        },
        //提示
        tooltip: {
    
    
          trigger: 'item',
          axisPointer: {
    
    
            type: 'shadow'
          },
          formatter: function (params) {
    
    
            if (params.componentType === 'series') {
    
    
              return `${
      
      params.seriesName}<br>${
      
      parseFloat(parseFloat(params.value[0]) - nums * 0.5).toFixed(2)}~${
      
      parseFloat(parseFloat(params.value[0]) + parseFloat(nums * 0.5)).toFixed(2)} : ${
      
      params.value[1]}</br>`
            } else {
    
    
              return `${
      
      params.name} : ${
      
      params.value}`
            }
          }
        },
        toolbox: {
    
    
          show: true,
          feature: {
    
    
            saveAsImage: {
    
     show: true }
          }
        },
        xAxis: [{
    
    
          type: 'value',
          axisLabel: {
    
    
            showMinLabel: false,  //不显示坐标轴最小值标签
            showMaxLabel: false   //不显示坐标轴最大值标签
          },
          boundaryGap: false, 
          min: Math.min.apply(null, xData),
          max: Math.max.apply(null, xData),
        },
        {
    
    
          type: 'category',
          axisTick: {
    
    
            show: false //不显示刻度
          },
          axisLabel: {
    
    
            show: false, // 将 show 参数设置为 false
            formatter: '{value}'
          },
          data: xData
        }
        ],
        yAxis: [
          {
    
    
            type: 'value',
            name: '频数',
            position: 'left',
            // 网格线
            splitLine: {
    
    
              show: false
            },
            axisLine: {
    
    
              show: true, //是否显示坐标轴轴线
              lineStyle: {
    
    
                color: 'orange'
              }
            },
          }, {
    
    
            type: 'value',
            name: ''
          },
          {
    
    
            type: 'value',
            name: '概率',
            position: 'right',
            // 网格线
            splitLine: {
    
    
              show: false
            },
            axisLine: {
    
    
              show: true,  //是否显示坐标轴轴线
              lineStyle: {
    
    
                color: 'black'
              }
            },
          }],
        series: [
          {
    
    
            name: '数据个数', // y 轴名称
            type: 'bar', // y 轴类型
            yAxisIndex: 0,  //使用主坐标轴
            xAxisIndex: 0,  //使用主坐标轴x轴
            // barWidth: '20%',              //---柱形宽度
            barCategoryGap: '40%',       //---柱形间距
            itemStyle: {
    
    
              normal: {
    
    
                show: true,
                color: 'rgba(255, 204, 0,.3)', //柱子颜色
                borderColor: '#FF7F50' //边框颜色
              }
            },
            data: barData, // y 轴数据 -- 源数据
          }, {
    
    
            name: '过程能力(估算)',
            type: 'line',
            xAxisIndex: 1,
            yAxisIndex: 1,
            symbol: 'none',
            smooth: true,
            data: yData1,
          },
          {
    
    
            name: '过程能力(实测)',
            type: 'line',
            yAxisIndex: 1,
            xAxisIndex: 1,
            symbol: 'none',
            smooth: true,
            data: yData2,
          },
          {
    
    
            name: '',
            type: 'line',
            xAxisIndex: 0, 
            yAxisIndex: 0,
            symbol: 'none',
            smooth: true,

            //实测数据警示线
            markLine: {
    
    
              symbol: ['none'], // 箭头方向
              itemStyle: {
    
     color: '#FF0000' },
              animation: false,
              label: {
    
    
                show: true,
                fontSize: 14,
                fontWeight: 'bold',
                formatter: function (params) {
    
    
                  if (params.name === '-3σ' || params.name === '+3σ' || params.name === 'X-bar') {
    
    
                    return `${
      
      params.name}\n`;
                  } else {
    
    
                    return `${
      
      params.name}`;
                  }
                },
              },
              data: [
                {
    
    
                  name: 'LSL',
                  xAxis: lsl,
                  lineStyle: {
    
     color: '#066d06', width: 2 },
                  label: {
    
     color: '#066d06', position: 'end' }
                },
                {
    
    
                  name: 'USL',
                  xAxis:usl,
                  lineStyle: {
    
     color: '#066d06', width: 2 },
                  label: {
    
     color: '#066d06', position: 'end' }
                },
                {
    
    
                  name: 'NOM',
                  xAxis:nom,
                  lineStyle: {
    
     color: '#ffbe11', width: 2 },
                  label: {
    
     color: '#ffbe11', position: 'end' }
                },
                {
    
    
                  name: '-3σ',
                  xAxis:sigma_minus3,
                  lineStyle: {
    
     color: 'red', width: 2 },
                  label: {
    
     color: 'red', position: 'end' }
                },
                {
    
    
                  name: '+3σ',
                  xAxis: sigma_plus3,
                  lineStyle: {
    
     color: 'red', width: 2 },
                  label: {
    
     color: 'red', position: 'end' }
                },
                {
    
    
                  name: 'X-bar',
                  xAxis:mean,
                  lineStyle: {
    
     color: 'red', width: 2 },
                  label: {
    
     color: 'red', position: 'end' }
                },
              ]
            }
          }
        ]
      }
    

3. 最終的な効果は次のとおりです。

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/qq_36660135/article/details/130700709