如何隐藏顶部的数据标签并(在背景中)绘制一条不同于数据集的 colored颜色 的线性线y = 0(以红色水平线显示)

我还想再画一条dotted水平线来表示平均水平(例如,y = 2.4)

new Chart(canvas_id, {
    type: 'line',
    data: {
        datasets: [{
            data: dates,
            borderWidth: 2
        }],
        //labels: ['a', 'b']
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        elements: {
            point: {
                radius: 0
            }
        },
        plugins: {
            tooltip: {
                enabled: false
            }
        }
    }
});

enter image description here

推荐答案

对于第一个问题,您必须禁用图例:

plugins: {
   // ..... other plugins options
   legend: {
     display: false
   }
}

对于第二个问题,你可以用chartjs-plugin-annotation

在加载插件后,例如从CDN加载插件(根据包含它的方式,您还将提供注册插件-CDN脚本不需要),您可以定义如下行:

   plugins: {
      // .... other plugin options
      annotation: {
        annotations: {
          line1: {
            type: 'line',
            yMin: 2.4,
            yMax: 2.4,
            borderColor: 'rgb(255, 99, 132)',
            borderWidth: 2,
          }
        }
      }
    }

代码片段:

const dates = Array.from({
    length: 16
  },
  (_, i) => ({
    x: '' + (Math.floor(new Date().valueOf() / 1000) + 24 * 3600 * i),
    y: 50 * Math.exp(-(i - 8) * (i - 8)) - Math.random() * 20
  }))

new Chart('chart1', {
  type: 'line',
  data: {
    datasets: [{
      data: dates,
      borderWidth: 2
    }],
    //labels: ['a', 'b']
  },
  options: {
    responsive: true,
    maintainAspectRatio: false,
    elements: {
      point: {
        radius: 0
      }
    },
    plugins: {
      tooltip: {
        enabled: false
      },
      legend: {
        display: false
      },
      annotation: {
        annotations: {
          line1: {
            type: 'line',
            yMin: 2.4,
            yMax: 2.4,
            borderColor: 'rgb(255, 99, 132)',
            borderWidth: 2,
          }
        }
      }
    }
  }
});
<div style="min-height: 300px">
    <canvas id="chart1">
    </canvas>
  </div>
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.0/chart.umd.js" integrity="sha512-6HrPqAvK+lZElIZ4mZ64fyxIBTsaX5zAFZg2V/2WT+iKPrFzTzvx6QAsLW2OaLwobhMYBog/+bvmIEEGXi0p1w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/3.0.1/chartjs-plugin-annotation.min.js" integrity="sha512-Hn1w6YiiFw6p6S2lXv6yKeqTk0PLVzeCwWY9n32beuPjQ5HLcvz5l2QsP+KilEr1ws37rCTw3bZpvfvVIeTh0Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

or fiddle

当然,如果没有插件,您可以很好地定义第二个数据集,在第一个数据集的x间隔的末尾有两个点,所需的值为y--jsFiddle

Javascript相关问答推荐

我的YouTube视频没有以html形式显示,以获取免费加密信号

用户单击仅在JavaScript中传递一次以及其他行为

使用typeof运算符获取对象值类型-接收字符串而不是数组

具有相同参数的JS类

对象和数字减法会抵消浏览器js中的数字

如何为GrapesJS模板编辑器创建自定义撤销/重复按钮?

是什么原因导致此Angular 16应用程序中类型错误时属性结果不存在?

仅针对RTK查询中的未经授权的错误取消maxRetries

如何使用JavaScript将文本插入空div

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

如何将多维数组插入到另一个多维数组中?

如何将innerHTML字符串修剪为其中的特定元素?

如何使用基于promise (非事件emits 器)的方法来传输数据?

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

Next.js中的服务器端组件列表筛选

在验证和提交表单后使用useNavigate()进行react 重定向,使用带有加载器和操作的路由

如何找到带有特定文本和测试ID的div?

React Refs不与高阶组件(HOC)中的动态生成组件一起工作

如何使用Cypress在IFRAME中打字

如果查询为空,则MongoDB将所有文档与$in匹配