我有一个Chart.js输出看起来像第一个图像,我希望它看起来像第二个图像,其中y轴线将"交叉"条形图并将其隔开一点以获得更好的可见性,我不认为我们可以称为这种填充,或者我如何使用Chart.js创建这种效果?

enter image description here

enter image description here

这是我的代码,它创建了像第一个图像一样的条形图,谢谢前面.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>$DOGI Listings Graph</title>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <style>
    html,
    body {
      height: 100%;
      margin: 0;
      background-color: black;
      color: white;
    }
    
    canvas {
      width: 100% !important;
      height: 100% !important;
    }
  </style>
</head>

<body style="background-color: black;color: white;">

  <canvas id="myChart"></canvas>

  <script>
    const chartData = [{
        x: 1,
        y: 34504
      }, {
        x: 2,
        y: 47831
      },
      {
        x: 3,
        y: 7050
      }, {
        x: 4,
        y: 8009
      },
      {
        x: 5,
        y: 8400
      }, {
        x: 6,
        y: 1250
      },
      {
        x: 7,
        y: 2505
      }, {
        x: 8,
        y: 2001
      },
      {
        x: 9,
        y: 88
      }, {
        x: 10,
        y: 650
      },
      {
        x: 11,
        y: 1350
      }, {
        x: 12,
        y: 200
      },
      {
        x: 13,
        y: 12782
      }, {
        x: 14,
        y: 1000
      },
      {
        x: 15,
        y: 50
      }, {
        x: 16,
        y: 500
      },
      {
        x: 17,
        y: 204
      }, {
        x: 18,
        y: 15
      },
      {
        x: 19,
        y: 250
      }, {
        x: 20,
        y: 1200
      },
      {
        x: 21,
        y: 200
      }, {
        x: 22,
        y: 200
      },
      {
        x: 23,
        y: 100
      }, {
        x: 24,
        y: 200
      },
      {
        x: 25,
        y: 450
      }
    ]


    async function createGraphData() {
      const ctx = document.getElementById('myChart').getContext('2d');
      const myChart = new Chart(ctx, {
        type: 'bar',
        data: {
          datasets: [{
            label: '# of Tokens',
            data: chartData,
            backgroundColor: 'rgb(255,165,3,0.8)',
          }]
        },
        options: {
          legend: {
            display: false
          },
          scales: {
            x: {
              type: 'linear',
              position: 'bottom',
              min: 0,
              max: 25,
              ticks: {
                stepSize: 1,
                color: 'white',
                autoSkip: false,
              },

              title: {
                display: true,
                text: 'Price $ (USD)',
                color: 'white'
              }
            },
            y: {
              beginAtZero: true,
              grid: {
                color: 'rgba(255,255,255,0.1)'
              },
              title: {
                display: true,
                text: 'Number of Tokens',
                color: 'white'
              },

              ticks: {
                color: 'white'
              }
            }
          },
          plugins: {
            legend: {
              display: false,
              labels: {
                color: 'white'
              }
            },
            title: {
              display: true,
              text: 'Chart Title',
              color: 'white',
              font: {
                size: 24
              }
            }
          },
          maintainAspectRatio: false,
          responsive: true,
          barPercentage: 0.7,
          categoryPercentage: 0.8,
        }
      });
    }

    createGraphData().catch(console.error);
  </script>

</body>

</html>

推荐答案

只需将z属性添加到选项中的zoom 网格中,并将其设置为1,而默认值为-1 和间距,您可以添加边界选项与破折号的适当性,并设置在数组中的间距 下面是一个例子,其中一些值可能适合您的 case 和 colored颜色 ,您可以将它们添加到scales对象内的"y"对象:

grid: {
    color: 'rgba(255,255,255,0.35)',
    z:1,
},
border:{
       dash:[3,3]
},

还可以查看文档中的ChartJs styling多个

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>$DOGI Listings Graph</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        html,
        body {
            height: 100%;
            margin: 0;
            background-color: black;
            color: white;
        }

        canvas {
            width: 100% !important;
            height: 100% !important;
        }
    </style>
</head>

<body style="background-color: black;color: white;">

<canvas id="myChart"></canvas>

<script>

    const chartData = [
        { x: 1, y: 34504 }, { x: 2, y: 47831 },
        { x: 3, y: 7050 }, { x: 4, y: 8009 },
        { x: 5, y: 8400 }, { x: 6, y: 1250 },
        { x: 7, y: 2505 }, { x: 8, y: 2001 },
        { x: 9, y: 88 }, { x: 10, y: 650 },
        { x: 11, y: 1350 }, { x: 12, y: 200 },
        { x: 13, y: 12782 }, { x: 14, y: 1000 },
        { x: 15, y: 50 }, { x: 16, y: 500 },
        { x: 17, y: 204 }, { x: 18, y: 15 },
        { x: 19, y: 250 }, { x: 20, y: 1200 },
        { x: 21, y: 200 }, { x: 22, y: 200 },
        { x: 23, y: 100 }, { x: 24, y: 200 },
        { x: 25, y: 450 }
    ]


    async function createGraphData() {
        const ctx = document.getElementById('myChart').getContext('2d');
        const myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                datasets: [{
                    label: '# of Tokens',
                    data: chartData,
                    backgroundColor: 'rgb(255,165,3,0.8)',
                }]
            },
            options: {
                legend: {
                    display: false
                },
                scales: {
                    x: {
                        type: 'linear',
                        position: 'bottom',
                        min: 0,
                        max: 25,
                        ticks: {
                            stepSize: 1,
                            color: 'white',
                            autoSkip: false,
                        },

                        title: {
                            display: true,
                            text: 'Price $ (USD)',
                            color: 'white'
                        }
                    },
                    y: {
                        beginAtZero: true,
                        grid: {
                            color: 'rgba(255,255,255,0.35)',
                            z:1,
                        },
                        border:{
                            dash:[3,3]
                        },
                        title: {
                            display: true,
                            text: 'Number of Tokens',
                            color: 'white'
                        },

                        ticks: {
                            color: 'white'
                        },
                    }
                },
                plugins: {
                    legend: {
                        display: false,
                        labels: {
                            color: 'white'
                        }
                    },
                    title: {
                        display: true,
                        text: 'Chart Title',
                        color: 'white',
                        font: {
                            size: 24
                        }
                    }
                },
                maintainAspectRatio: false,
                responsive: true,
                barPercentage: 0.7,
                categoryPercentage: 0.8,
            }
        });
    }

    createGraphData().catch(console.error);

</script>

</body>

</html>

Javascript相关问答推荐

在NextJS中使用计时器循环逐个打开手风琴项目?

为什么在集内和集外会产生不同的promise 状态(使用Promise.race)?

React Native平面列表自动滚动

在Vite React库中添加子模块路径

在grafana情节,避免冲突的情节和传说

如何在输入元素中附加一个属性为checkbox?

如何使用子字符串在数组中搜索重复项

rxjs插入延迟数据

对网格项目进行垂直排序不起作用

如何将数组用作复合函数参数?

在数组中查找重叠对象并仅返回那些重叠对象

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

如何在HTMX提示符中设置默认值?

在Odoo中如何以编程方式在POS中添加产品

使用自动识别发出信号(&Q)

如何用javascript更改元素的宽度和高度?

在Vercel中部署Next.js项目时获取`ReferenceError:未定义文档`

如何根据查询结果重新排列日期

为什么在运行于<;img>;事件处理程序中的JavaScript中x和y是不可变的?

我怎样才能点击一个元素,并获得一个与 puppeteer 师导航页面的URL?