1

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

然后我尝试运行他们网站中给出的 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 而不是画布,但它总是显示相同的错误。

4

1 回答 1

0

我正在使用 laravel + vuejs。

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

使用 npm 提供的样本数据,

在 de data 中,只需像这样声明 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>

于 2019-05-26T23:18:58.110 回答