Chart.js canvas resize

前端 未结 13 1097
北海茫月
北海茫月 2020-11-30 23:39

In (Android WebView HTML5 canvas error) i posted a question regarding plotting graphs using Graph.js library. The problem i have now is that if i call the function to plot t

相关标签:
13条回答
  • 2020-11-30 23:46

    I tried to Resize Canvas using jQuery but it din't work well. I think CSS3 is the best option you can try on, if you want on hover zooming at certain level.

    Following hover option from other codepan link:

    .style_prevu_kit:hover{
        z-index: 2;
        -webkit-transition: all 200ms ease-in;
        -webkit-transform: scale(1.5);
        -ms-transition: all 200ms ease-in;
        -ms-transform: scale(1.5);   
        -moz-transition: all 200ms ease-in;
        -moz-transform: scale(1.5);
        transition: all 200ms ease-in;
        transform: scale(1.5);
    }
    

    Follow my codepan link:

    https://codepen.io/hitman0775/pen/XZZzqN

    0 讨论(0)
  • 2020-11-30 23:52

    I had a lot of problems with that, because after all of that my line graphic looked terrible when mouse hovering and I found a simpler way to do it, hope it will help :)

    Use these Chart.js options:

    // Boolean - whether or not the chart should be responsive and resize when the browser does.
    
    responsive: true,
    
    // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
    
    maintainAspectRatio: false,
    
    0 讨论(0)
  • 2020-11-30 23:55

    I had a similar problem and found your answer.. I eventually came to a solution.

    It looks like the source of Chart.js has the following(presumably because it is not supposed to re-render and entirely different graph in the same canvas):

        //High pixel density displays - multiply the size of the canvas height/width by the device pixel ratio, then scale.
    if (window.devicePixelRatio) {
        context.canvas.style.width = width + "px";
        context.canvas.style.height = height + "px";
        context.canvas.height = height * window.devicePixelRatio;
        context.canvas.width = width * window.devicePixelRatio;
        context.scale(window.devicePixelRatio, window.devicePixelRatio);
    }
    

    This is fine if it is called once, but when you redraw multiple times you end up changing the size of the canvas DOM element multiple times causing re-size.

    Hope that helps!

    0 讨论(0)
  • 2020-11-30 23:58
     let canvasBox = ReactDOM.findDOMNode(this.refs.canvasBox);
     let width = canvasBox.clientWidth;
     let height = canvasBox.clientHeight;
     let charts = ReactDOM.findDOMNode(this.refs.charts);
     let ctx = charts.getContext('2d');
     ctx.canvas.width = width;
     ctx.canvas.height = height;
     this.myChart = new Chart(ctx);
    
    0 讨论(0)
  • 2020-12-01 00:02

    I had to use a combination of multiple answers here with some minor tweaks.

    First, it is necessary that you wrap the canvas element within a block-level container. I say to you, do not let the canvas element have any siblings; let it be a lonely child, for it is stubborn and spoiled. (The wrapper may not need any sizing restrictions placed on it, but for safety it may be good to have a max-height applied to it.)

    After assuring that the previous conditions are met, when initiating the chart, make sure the following options are used:

    var options = { 
        "responsive": true,
        "maintainAspectRatio": false
    }
    

    If you want to adjust the height of the chart, do so at the canvas element level.

    <canvas height="500"></canvas>
    

    Do not try to deal with the child in any other manner. This should result in a satisfyingly, properly laid-out chart, one that stays in its crib peacefully.

    0 讨论(0)
  • 2020-12-01 00:02

    I was having the same problem. I was able to solve it by setting option:

    responsive: false,
    maintainAspectRatio: true,
    showScale: false,

    And in css, set the width of the container div the same as the canvas:

        #canvasContainer { 
          width: 300px;
        }
        
        canvas {
          width: 300px;
        }

    0 讨论(0)
提交回复
热议问题