简体   繁体   English

Chartjs - 调整位于列中的图像大小

[英]Chartjs - resize an image positioned in a column

I successfully inserted an image within a column, and I want to modify the width and the height of the image positioned on the column我在列中成功插入了图像,我想修改位于列上的图像的宽度和高度

The question is:问题是:

How can I access a canvaspattern height and width?.如何访问画布图案的高度和宽度?

If there is no way, what other approaches are there to position an image within a column?如果没有办法,还有什么其他方法可以 position 列内的图像? -Other than amcharts-. -除了amcharts-。

Here is the code :这是代码

var img = new Image();
img.src = document.getElementById('image').src;
img.onload = function() {
var ctx = document.getElementById("myChart").getContext("2d");
var fillPattern = ctx.createPattern(img, 'no-repeat');

var chartData = {
  datasets : 
  [{
    label: 'user1',
    backgroundColor:'rgba(229,204,255,1)' ,
    data : [150]
  },
  {
    label: 'user2',
    backgroundColor: fillPattern,  
    data: [120]
    
  }
   ]
};

    var myChart = new Chart(ctx, {
        type: 'bar',
        data: chartData,
        options:options
 });
};

The output: output:

part of the left of the image is just shown on the bar:图像左侧的一部分仅显示在栏上:
图像左侧的一部分仅显示在栏上

the picture:图片:

apple.png苹果.png

One possible way is to use chartjs-plugin-labels to add images of desired size inside individual bars.一种可能的方法是使用chartjs-plugin-labels在各个条形图中添加所需大小的图像。

Please have a look at the code below.请看下面的代码。

 new Chart(document.getElementById('myChart'), { type: 'bar', data: { labels: ['A', 'B'], datasets: [{ label: 'My First Dataset', data: [500, 450], backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(255, 159, 64, 0.2)'], borderColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)'], borderWidth: 1 }] }, options: { plugins: { labels: { render: 'image', textMargin: -160, images: [ null, { src: 'https://i.stack.imgur.com/556XZ.png', width: 150, height: 150 } ] } }, scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/emn178/chartjs-plugin-labels/src/chartjs-plugin-labels.js"></script> <canvas id="myChart" height="200"></canvas>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM