如何在不更改 charts.js 中第一个 y 轴上的刻度的情况下删除第二个 y 轴上的刻度?

时间:2021-03-02 18:19:45

标签: javascript chart.js

随附您可以找到一张图片来说明我的情况。正如你所看到的,我已经编码了一个额外的 y 轴。但是,我想删除顶部和底部的刻度线。希望你能帮我解决这个问题。我只是在寻找一条简单的垂直线,而无需穿过我的点。这是使用第二个 y 轴的意图。但是,顶部和底部的刻度看起来不太好

enter image description here

<div class="wrapper" style="height:400px;position: relative;">
<canvas id="mychart2" width="400" height="400"></canvas>
</div>




<script>
  var canvas = document.getElementById("mychart2");
  var ctx = canvas.getContext('2d');
  const decimals = 0;
  var config = { //configure the chart
    type: 'line',
    data: {
      labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9+'],
      datasets: [{
          label: "25th Percentile",
          backgroundColor: '#c4c1ff',
          pointBackgroundColor: "#645bff",
          borderColor: '#645bff',
          fill: 4,
          pointRadius: 0,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3, //no fill here
          data: [28, 35, 40, 45, 50, 55, 62, 66, 70, 78]
        }, {
          label: "10th - 90th Percentile",
          backgroundColor: '#c4c1ff',
          pointBackgroundColor: "#c4c1ff",
          borderColor: '#c4c1ff',
          pointHoverBackgroundColor: "#c4c1ff",
          pointHoverBorderColor: "#c4c1ff",
          borderWidth: 1,
          pointRadius: 0,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          fill: 3,
          //no fill here
          data: [40, 65, 63, 64, 72, 79, 83, 87, 100, 108]
        }, {
          label: "Median",
          backgroundColor: '#0d0e25',
          pointBackgroundColor: "#0d0e25",
          borderColor: '#0d0e25',
          borderWidth: 1,
          pointRadius: 2,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          fill: false, //no fill here
          data: [30, 40, 45, 50, 56, 60, 66, 73, 78, 85]
        },
        {
          label: "25th - 75th Percentile",
          showInLegend: false,
          backgroundColor: '#645bff',
          pointBackgroundColor: "#645bff",
          borderColor: '#645bff',
          pointRadius: 0,
          lineTension: 0.1,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          borderCapStyle: 'butt',
          borderDash: [],
          borderDashOffset: 0.0,
          borderJoinStyle: 'miter',
          fill: 0,
          //fill until previous dataset
          data: [35, 50, 51, 55, 63, 69, 73, 80, 85, 94]
        }, {
          label: "10th Percentile",
          backgroundColor: "#c4c1ff",
          pointBackgroundColor: "#c4c1ff",
          pointHoverBackgroundColor: "#c4c1ff",
          pointHoverBorderColor: "#c4c1ff",
          borderColor: "#c4c1ff",
          pointStyle: "circle",
          borderWidth: 1,
          lineWidth: 1,
          hoverRadius: 9,
          pointRadius: 0,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          lineTension: 0.3,

          fill: '0', //fill until previous dataset
          data: [25, 30, 36, 39, 45, 49, 53, 56, 60, 68]
        }
      ]
    },
    options: {responsive:true,maintainAspectRatio:false,
      tooltips: {
        callbacks: {
          label: function(tooltipItem, data) {
            var label = data.datasets[tooltipItem.datasetIndex].label || '';
            if (label) {
              label += ': ';
            }
            if (label === "25th - 75th Percentile: ") {
              label = "75th Percentile: "
            }
            if (label === "10th - 90th Percentile: ") {
              label = "90th Percentile: "
              }
            label += tooltipItem.yLabel
            return label;
            
          }
        }
      },
      title: {
        display: true,
        text: ' Engineer salaries (753 datapoints)',
        fontSize: 20
      },
      maintainAspectRatio: false,
      legend: {
        onClick: (e) => e.stopPropagation(),
        display: true,
        labels: {
          filter: function(item,
            mychart2) {
          return item.datasetIndex !== 0 && item.datasetIndex !== 4;
          }
        }
      },
      spanGaps: false,
      elements: {
        line: {
          tension: 0.000001
        }
      },
      plugins: {
        filler: {
          propagate: false
        }
      },
      scales: { yAxes: [{
                id: 'a',
                type: 'linear',
                position: 'left',
                gridLines: {
                    drawOnChartArea:false
                },
                scaleLabel: {
                    display: true,
                    labelString: 'Salary',
                    fontSize: 20
                },
                ticks: {
                    beginAtZero: true,
                    stepSize: 20,
                    callback: function(value, index, values) {
                        return '$' + value.toFixed(decimals)
                    }
                }
            }, {
                id: 'b',
                type: 'linear',
                position: 'right',
                ticks: {
                display: false},
                gridLines: { 
                    lineWidth:0.5
                },
                scaleLabel: {
                    display: false
                },
                ticks: {
                    display: false,
                    beginAtZero: true,
                    stepSize: 20
                }
            }] ,xAxes: [{
          gridLines: {
            drawOnChartArea:false
          },
          ticks: {
            beginAtZero: true,
            stepSize: 20,

          },
          scaleLabel: {
            display: true,
            labelString: 'Years of relevant experience',
            fontSize: 20
          }
        }]
      }
    }
  };
  var chart = new Chart(ctx, config);
</script>

0 个答案:

没有答案
相关问题