javascript - 在 Chart.js 中对一个数据集中的各个点进行样式设置

标签 javascript html chart.js

我正在尝试使用 Chart.js 创建时间线。 我的目标是用一行显示游戏中发生的各种图标/事件。 通过使用折线图并删除线条,仅保留点,您可以在时间轴上放置标记。

您可以通过将图像变量分配给数据集的 pointStyle 属性来将这些点显示为自定义图像。

使用单个图像,效果非常好,如下面的代码片段所示。

//Create the image    
const img = new Image(20,20); 
img.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Skull_Icon_%28Noun_Project%29.svg/1200px-Skull_Icon_%28Noun_Project%29.svg.png";

var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        datasets: [
            {
            label: "Deaths",
            data: [{x:"00:01:23", y: 0},{x:"00:03:41", y: 0},{x:"00:04:29", y: 0},{x:"00:05:35", y: 0},{x:"00:06:27", y: 0},{x:"00:07:07", y: 0},{x:"00:08:48", y: 0},{x:"00:09:31", y: 0}],
            //Assign the image in the point Style attribute
            pointStyle:img,
            showLine: false,
            fill: false,
            tooltip: "Player Died",
            borderColor: "#000000",
            backgroundColor: "#000000"
        },
                ]},
     //The Rest is just styling
    options: {
        interaction:{
            mode:'nearest'
        },
        tooltips:{
            custom: function(tooltip){
                if(!tooltip)return;
                tooltip.displayColors = false;
            },
            callbacks:{
                title: function(tooltipItem, data) {
                    return data.datasets[tooltipItem[0].datasetIndex]['tooltip'];
                },
                label: function(tooltipItem, data) {
                    return tooltipItem.xLabel;
                }
            }
        },
        legend: {
            display: true
        },
        scales: {
            xAxes: [{
                type: 'time',
                time: {
                    parser: 'hh:mm:ss',
                    tooltipFormat: 'HH:mm:ss',
                    displayFormats: {
                        second: "HH:mm:ss"
                    },
                unitStepSize: 30
                },
                ticks:{
                    fontColor: "white"
                }
            }],
            yAxes: [{
                ticks: {
                    display: false,
                },        
                gridLines: {
                    display: false
                }
            }]
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
<canvas id="chart" height= "50px" style="background-color: rgb(200, 200, 200);"></canvas>

如您所见,我正在创建一个 Image(),将其指向源并将其指定为数据集的 pointStyle 属性。 但是使用这种方法,每当我想显示另一个图像时,我都需要创建一个新的数据集。

出于我的目的,我需要能够为每个点动态分配不同的图像。

这就是我研究 Chart.js 中的脚本的原因。文档中指出,您可以使用自定义函数返回 pointStyles 的图像/样式。

https://www.chartjs.org/docs/latest/general/options.html

在下面的代码片段中您可以看到我当前的代码。 我将图像的创建移至数据集的点样式属性中的自定义函数。这样我以后就可以从更多图像中进行选择。

但目前我无法使其与一张图像一起使用。那么我怎样才能让函数返回图像,以便样式起作用呢?这样我以后就可以从多个图像中进行选择。或者还有其他我没有想到的解决方案吗?

非常感谢您的帮助。

var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        datasets: [
            {
            label: "Deaths",
            data: [{x:"00:01:23", y: 0},{x:"00:03:41", y: 0},{x:"00:04:29", y: 0},{x:"00:05:35", y: 0},{x:"00:06:27", y: 0},{x:"00:07:07", y: 0},{x:"00:08:48", y: 0},{x:"00:09:31", y: 0}],
            //Try to create the image in a custom function
            pointStyle: function(context){ 
                var img = new Image(20,20); 
                img.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Skull_Icon_%28Noun_Project%29.svg/1200px-Skull_Icon_%28Noun_Project%29.svg.png"; 
                return img; 
                },
            showLine: false,
            fill: false,
            tooltip: "Player Died",
            borderColor: "#000000",
            backgroundColor: "#000000"
        },
                ]},
    options: {
        interaction:{
            mode:'nearest'
        },
        tooltips:{
            custom: function(tooltip){
                if(!tooltip)return;
                tooltip.displayColors = false;
            },
            callbacks:{
                title: function(tooltipItem, data) {
                    return data.datasets[tooltipItem[0].datasetIndex]['tooltip'];
                },
                label: function(tooltipItem, data) {
                    return tooltipItem.xLabel;
                }
            }
        },
        legend: {
            display: true
        },
        scales: {
            xAxes: [{
                type: 'time',
                time: {
                    parser: 'hh:mm:ss',
                    tooltipFormat: 'HH:mm:ss',
                    displayFormats: {
                        second: "HH:mm:ss"
                    },
                unitStepSize: 30
                },
                ticks:{
                    fontColor: "white"
                }
            }],
            yAxes: [{
                ticks: {
                    display: false,
                },        
                gridLines: {
                    display: false
                }
            }]
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
<canvas id="chart" height= "50px" style="background-color: rgb(200, 200, 200);"></canvas>

最佳答案

您可以通过数组添加不同的图像。

//Create the image    
const img = new Image(20, 20);
img.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Skull_Icon_%28Noun_Project%29.svg/1200px-Skull_Icon_%28Noun_Project%29.svg.png";
const img2 = new Image(20, 20);
img2.src = "https://pngimg.com/uploads/pacman/pacman_PNG21.png";
const img3 = new Image(20, 20);
img3.src = "https://pngimg.com/uploads/pacman/pacman_PNG70.png"
var ctx = document.getElementById('chart').getContext('2d');


var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      label: "Deaths",
      data: [{
        x: "00:01:23",
        y: 0
      }, {
        x: "00:03:41",
        y: 0
      }, {
        x: "00:04:29",
        y: 0
      }, {
        x: "00:05:35",
        y: 0
      }, {
        x: "00:06:27",
        y: 0
      }, {
        x: "00:07:07",
        y: 0
      }, {
        x: "00:08:48",
        y: 0
      }, {
        x: "00:09:31",
        y: 0
      }],
      //Assign the image in the point Style attribute
      pointStyle: [img2, img, img3],

      showLine: false,
      fill: false,
      tooltip: "Player Died",
      borderColor: "#000000",
      backgroundColor: "#000000"
    }, ]
  },
  //The Rest is just styling
  options: {
    interaction: {
      mode: 'nearest'
    },
    tooltips: {
      custom: function(tooltip) {
        if (!tooltip) return;
        tooltip.displayColors = false;
      },
      callbacks: {
        title: function(tooltipItem, data) {
          return data.datasets[tooltipItem[0].datasetIndex]['tooltip'];
        },
        label: function(tooltipItem, data) {
          return tooltipItem.xLabel;
        }
      }
    },
    legend: {
      display: true
    },
    scales: {
      xAxes: [{
        type: 'time',
        time: {
          parser: 'hh:mm:ss',
          tooltipFormat: 'HH:mm:ss',
          displayFormats: {
            second: "HH:mm:ss"
          },
          unitStepSize: 30
        },
        ticks: {
          fontColor: "white"
        }
      }],
      yAxes: [{
        ticks: {
          display: false,
        },
        gridLines: {
          display: false
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"></script>
<canvas id="chart" height="50px" style="background-color: rgb(200, 200, 200);"></canvas>

如何将图像获取到数组取决于您。

你可以用这样的东西更新它

function addimage(img) {
  myChart.data.datasets[0].pointStyle.push(img);
  myChart.update();
};
addimage(img2)

当然,我确信您会希望以其他动态方式添加图像,但这应该会有所帮助。

关于javascript - 在 Chart.js 中对一个数据集中的各个点进行样式设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67595666/

相关文章:

javascript - 当给定 xmlns 时,DOM XPath 查询不起作用

javascript - 如何显示两条有延迟的消息?

javascript - 重置(清除)输入字段 JS/HTML 按钮 "clean"

javascript - 动态更新 Angular 5 中 Chart.js 注释插件中水平线的值

php - How to use 'time'(来自数据库的数据,数据类型: timestamp ) for plotting graph in Chart JS

javascript - 如何使用 JS 检查元素是否包含带有 'while' 函数的文本?

javascript - php 表单 html 按钮

javascript - 第二次尝试刷新时如何将页面重定向到另一个页面

javascript - 将文件转换为数组并使用数据列表和选项标签添加数组元素以用于自动完成文本框

javascript - Chart.js:对饼图 y 轴上事件/显示数据集中的项目数进行求和