【问题标题】:How to get data from JSON for chart.js, using vue.js如何使用 vue.js 从 JSON 中获取 chart.js 的数据
【发布时间】:2018-05-21 11:38:51
【问题描述】:

全部。

我想从我的 JSON 数据构建图表,但不明白怎么做。 数据获取('receive'),并建立表。没关系

var ordersTable = Vue.component('orders-table', {
    template: ` 
    <div>
          <table class="table table-bordered">
          <thead>
            <tr>
              <th>orderdate</th>
              <th>cancelled</th>
              <th>reserved</th>
              <th>delivered</th>
              <th>ordersumm</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="column in orders">
              <td>{{column.orderdate}}</td>
              <td>{{column.cancelled}}</td>
              <td>{{column.reserved}}</td>
              <td>{{column.delivered}}</td>
              <td>{{column.ordersumm}}</td>
            </tr>
          </tbody>
        </table>
    </div>
        `, 

  data: function ()  {
    return {
      orders: []
    }
  },
  created:function() {
    fetch('/receive/')    
    .then(res => res.json())
    .then(res => {
      this.orders = res;
      });
  },
});

我尝试使用 orderdate 标签和 3 个数据集构建图表:已取消、已保留、已交付。 像这样: enter image description here 我该怎么做?标签和数据必须从 fetch 中填充。我不工作的代码:

var orderChart = Vue.component('order-chart', {
  extends: VueChartJs.Bar,
  mounted () {
    this.renderChart({
      labels: [],
      datasets: [
        {
          label: 'cancelled',
          backgroundColor: 'red',
          data: []
        },
        {
          label: 'reserved',
          backgroundColor: 'blue',
          data: []
        },
        {
          label: 'delivered',
          backgroundColor: 'green',
          data: []
        }
      ]
    }, {responsive: true, maintainAspectRatio: false})}
  ,
  data: function ()  {
    return {
      orders: []
    }
  },
  created:function() {
    fetch('/receive/')    
    .then(res => res.json())
    .then(res => {
      this.orders = res;
      });
  },
})

【问题讨论】:

    标签: javascript web vue.js chart.js frontend


    【解决方案1】:

    你需要使用reactiveData mixin 来实现响应式

    var orderChart = Vue.component('order-chart', {
      extends: VueChartJs.Bar,
      mixins: [VueChartJs.mixins.reactiveData]
      mounted () {
        this.renderChart(this.chartData, {responsive: true, maintainAspectRatio: false})
      },
      data: function ()  {
        return {
          chartData: {
            labels: [],
            datasets: [
              {
                label: 'cancelled',
                backgroundColor: 'red',
                data: []
              },
              {
                label: 'reserved',
                backgroundColor: 'blue',
                data: []
              },
              {
                label: 'delivered',
                backgroundColor: 'green',
                data: []
              }
            ]
          }
        }
      },
      created: function() {
        fetch('/receive/')    
        .then(res => res.json())
        .then(res => {
          const datasets = [
            {
              label: 'cancelled',
              backgroundColor: 'red',
              data: res.map(item => item.cancelled)
            },
            {
              label: 'reserved',
              backgroundColor: 'blue',
              data: res.map(item => item.reserved)
            },
            {
              label: 'delivered',
              backgroundColor: 'green',
              data: data: res.map(item => item.delivered)
            }
          ]
          this.chartData = {
            datasets: datasets,
            labels: res.map(item => item.orderdate)
          }
        });
      }
    })
    

    演示:https://codepen.io/ittus/pen/MGxQqO

    【讨论】:

    • 谢谢!但我还有一个问题。 [Vue 警告]:避免直接改变 prop,因为每当父组件重新渲染时,该值都会被覆盖。相反,使用基于道具值的数据或计算属性。正在变异的道具:在 ---> 中找到“chartData”
    • 还有什么问题?
    • [Vue 警告]:避免直接改变 prop,因为只要父组件重新渲染,该值就会被覆盖。相反,使用基于道具值的数据或计算属性。正在变异的道具:在 ---> 中找到“chartData”
    • 我已经更新了。您介意尝试使用计算的第二个解决方案吗?
    • 不起作用。只有标签(已取消、保留、已交付)出现,但未构建图表,未显示来自 orderdate 的标签
    猜你喜欢
    • 2019-06-17
    • 1970-01-01
    • 2014-09-01
    • 2018-09-08
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多