0

rgraph我在 asp 中调用 canvas 时遇到问题mvc。这是我的代码,我添加了所有必需的rgraphs. 也jquery归档。并面临错误“ Uncaught TypeError: Cannot read property getContextof null ”

var key = RGraph.HTML.Key('containerPie', {
  colors: ['blue', 'green'],
  labels: response.labels,
  tableCss: {
    //position: 'absolute',
    top: '50%',
    right: '-80px',
    transform: 'translateY(-50%)'
  }
});

new RGraph.Pie({
  id: 'cvsPie',
  data: response.res,
  options: {
    strokestyle: '#e8e8e8',
    variant: 'pie3d',
    linewidth: 2,
    gutterLeft: 5,
    shadowOffsetx: 0,
    shadowOffsety: 7,
    shadowColor: '#ddd',
    shadowBlur: 15,
    radius: 40,
    tooltips: response.res,
    colors: ['blue', 'green']
  }
}).draw();

这是我的 div 代码:

<canvas id="cvsPie" width="450" height="300"></canvas>
4

1 回答 1

0

就是说canvas对象为null,所以没有getContext()方法。通常这归结为使用了错误的 ID - 从外观上看你没有 - 或者在画布存在之前运行代码。

第二个可能是您的问题 - 代码位于页面中的画布标记上方,并且您没有使用 DOMContentLoaded 事件或 window.onload 事件。

因此,请尝试将创建图表的代码移到 canvas 标记之后,或者将代码放在 onload 事件中,如下所示:

<script>
    window.onload = function ()
    {
        // Your code goes here
    }
</script>
于 2017-12-31T12:29:19.450 回答