使用chartJS显示连接点的折线图

Ran*_*ika 3 javascript charts google-visualization chart.js

我想使用ChartJS绘制这样的图表.但我找不到连接第一个和最后一个点的解决方案,并在连接区域内显示单个唯一点.而且我还需要用不同的颜色设计每个点的样式.我尝试过探索ChartJS文档,但找不到解决方案.是否有任何图表绘制库具有这些功能或如何使用ChartJS执行此操作?

在此输入图像描述

ɢʀᴜ*_*ᴜɴᴛ 16

您可以创建散点图而不是线.

这是一个例子 :(
试图复制你给定的图像amap)

var chart = new Chart(ctx, {
   type: 'scatter',
   data: {
      datasets: [{
         data: [{
            x: 1,
            y: 1
         }, {
            x: 3,
            y: 7
         }, {
            x: 6,
            y: 5
         }, { // add same data as the first one, to draw the closing line
            x: 1,
            y: 1
         }],
         borderColor: 'black',
         borderWidth: 1,
         pointBackgroundColor: ['#000', '#00bcd6', '#d300d6'],
         pointBorderColor: ['#000', '#00bcd6', '#d300d6'],
         pointRadius: 5,
         pointHoverRadius: 5,
         fill: false,
         tension: 0,
         showLine: true
      }, {
         data: [{
            x: 3.5,
            y: 4.5
         }],
         pointBackgroundColor: 'orange',
         pointBorderColor: 'darkorange',
         pointRadius: 10,
         pointHoverRadius: 10
      }]
   },
   options: {
      legend: false,
      tooltips: false,
      scales: {
         xAxes: [{
            ticks: {
               min: 0,
               max: 10
            },
            gridLines: {
               color: '#888',
               drawOnChartArea: false
            }
         }],
         yAxes: [{
            ticks: {
               min: 0,
               max: 8,
               padding: 10
            },
            gridLines: {
               color: '#888',
               drawOnChartArea: false
            }
         }]
      }
   }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>
Run Code Online (Sandbox Code Playgroud)

注意:这只是一个示例,您可以根据官方文档进一步自定义以满足您的需求.

  • 这真的帮助我找出了我正在尝试创建的图表!谢谢咕噜 (3认同)
  • 这么好的答案 - 从官方文档中还不清楚这些东西可以在 Chartjs 中完成 (2认同)
  • +1 对于“showLine: true”选项,在旧版本的 Chart.js 中不需要它,但更新后它是必要的。 (2认同)