javascript - 如何删除无法创建图表: can't acquire context from the given item error while using it with Ruby on Rails?

标签 javascript html ruby ruby-on-rails-3 frappe

我通过 gem 在 Rails 上安装了 ruby​​ 的 frappe 图表。

然后我尝试运行 website 中给出的 frappe-chart 代码之一:

 let data = {
    labels: ["12am-3am", "3am-6am", "6am-9am", "9am-12pm",
      "12pm-3pm", "3pm-6pm", "6pm-9pm", "9pm-12am"],

    datasets: [
      {
        title: "Some Data",
        values: [25, 40, 30, 35, 8, 52, 17, -4]
      },
      {
        title: "Another Set",
        values: [25, 50, -10, 15, 18, 32, 27, 14]
      },
      {
        title: "Yet Another",
        values: [15, 20, -3, -15, 58, 12, -17, 37]
      }
    ]
  };

  let chart = new Chart({
    parent: "#note-graph", // or a DOM element
    title: "My Awesome Chart",
    data: data,
    type: 'bar', // or 'line', 'scatter', 'pie', 'percentage'
    height: 250,

    colors: ['#7cd6fd', 'violet', 'blue'],
    // hex-codes or these preset colors;
    // defaults (in order):
    // ['light-blue', 'blue', 'violet', 'red',
    // 'orange', 'yellow', 'green', 'light-green',
    // 'purple', 'magenta', 'grey', 'dark-grey']

    format_tooltip_x: d => (d + '').toUpperCase(),
    format_tooltip_y: d => d + ' pts'
  });
<canvas id="note-graph"></canvas>

我也尝试过使用 div 而不是 canvas,但它总是显示相同的错误。

最佳答案

我正在使用 laravel + vuejs。

这解决了我的问题“错误:没有提供要渲染的 parent 元素。”与碎裂

使用 npm 提供的示例数据,

在 de data 中只需声明数据变量,如下所示:

<template>
    <div>
        <h1>CHARTS</h1>
            <div id="chart"></div>
    </div>
</template>


<script>
import { Chart } from 'frappe-charts/dist/frappe-charts.esm.js'
// import css
import 'frappe-charts/dist/frappe-charts.min.css'

export default{
data(){
    return{
         data : {
    labels: ["12am-3am", "3am-6pm", "6am-9am", "9am-12am",
        "12pm-3pm", "3pm-6pm", "6pm-9pm", "9am-12am"
    ],
    datasets: [
        {
            name: "Some Data", type: "bar",
            values: [25, 40, 30, 35, 8, 52, 17, -4]
        },
        {
            name: "Another Set", type: "line",
            values: [25, 50, -10, 15, 18, 32, 27, 14]
        }
    ]
},

chart : null // THIS IS WHAT YOU NEED TO SET NULL
    }
},
methods:{
    setChart(){ // IN METHOD YOU SET A NEW CHART
     this.chart=new Chart("#chart", { 
    title: "My Awesome Chart",
    data: this.data,
    type: 'axis-mixed',
    height: 250,
    colors: ['#7cd6fd', '#743ee2']
})
    }
},
mounted(){
    this.setChart(); // CALL THE METHOD IN A lifecycle hook
}    
}


</script>

关于javascript - 如何删除无法创建图表: can't acquire context from the given item error while using it with Ruby on Rails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48834766/

相关文章:

javascript - 如何防止对象/数组突变?

javascript - jQuery mouseenter 函数不会在使用 Javascript 的 ext HTML 中包含的元素上触发

html - 无法访问自定义 Front Matter 变量

html - 在一行中向左浮动两个 flex 元素,向右浮动一个元素

html - 将元素分组为两列并将它们对齐到顶部(使用 Flexbox)

ruby - 如何在ruby中多次使用动态变量

javascript - 了解事件冒泡

javascript - 使用 jQuery 根据条件选中/取消选中单选按钮的问题

javascript - Ruby on Rails View 通过渲染插入 js.erb 文件

ruby - 在windows上的ruby中查找文件的真实文件名