0

我想使用 vue-chartjs 库创建一个折线图。

到目前为止,我创建的内容没有产生任何错误,但它也只呈现空白画布。当我切换到开发人员视图时,我注意到我的所有数据都打印出来了。我只是不确定为什么它不渲染。

这是我的 HTML 和 Vue 代码片段:

<div class="app">
    <h1>Line Chart</h1>
    <line-chart></line-chart>
</div>


<script>

Vue.component('line-chart', {
  extends: VueChartJs.Line,
  mounted () {
    this.renderChart({          
      labels: this.chartDate,
      datasets: [
        {
          label: 'Data One',
          backgroundColor: '#f87979',
          data: this.expectedFund
        }
      ]
    }, {responsive: true, maintainAspectRatio: false})
  }

})

new Vue({
  el: '.app',
  data: {
    message: 'Hello World',
        dataSetData: [],
        expectedFund: '',
        chartDate: '',
        crossOver: '',
        billing: ''     
  },
    methods: {
    getDataSet: function(dataField) {
      console.log("get data sets");
      console.log(this.dataSetData);

      this.expectedFund = this.dataSetData.map(function(chartData) {        
                //alert("expected");
        console.log(chartData);
        return chartData.ExpectedFund;
      });
        this.billing = this.dataSetData.map(function(chartData) {
          return chartData.Billing;
        });
              this.billing = this.dataSetData.map(function(chartData) {
          return chartData.Billing;
        });
              this.chartDate = this.dataSetData.map(function(chartData) {
          return chartData.date;
        });
              this.crossOver = this.dataSetData.map(function(chartData) {
          return chartData.crossOver;
        });
    },
    getListData: async function() {
      const { data } = await axios.get(
        "https://my-json-server.typicode.com/isogunro/jsondb/chartData"
      );

      return data;
    }
  },
  mounted: async function() {
    this.dataSetData = await this.getListData();
    console.log("ok", this.dataSetData);
    this.getDataSet();
  }
})

</script>

如果粘贴的代码还不够,这里是Pen

4

1 回答 1

0

经过一番挣扎和围绕一堆 Vue 不和谐弹跳,我能够弄清楚如何使用 Vue-Chartjs 创建一个多线和条形图。这是一场值得的奋斗,因为我终于了解了道具的使用以及它们是如何工作的,而这正是我在 vuejs 图表中所缺少的。这是一支显示解决方案的笔。

我在下面发布 json 是因为我的图表使用了在“我的假 json服务器/ typicode ”中找到的数据。将来可能会发生变化,所以我将其粘贴在这里。

{"chartData":
    [
        {
            "date":"4/4/2019",
            "totalCount":381,
            "ExpectedFund":191,
            "Funded":290,
            "Billing":125,
            "crossOver":241,
            "AcceptedTotal":515
        },
        {
            "date":"4/11/2019",
            "totalCount":233,
            "ExpectedFund":12,
            "Funded":220,
            "Billing":125,
            "crossOver":211,
            "AcceptedTotal":315
        },
        {
            "date":"4/18/2019",
            "totalCount":542,
            "ExpectedFund":34,
            "Funded":240,
            "Billing":125,
            "crossOver":125,
            "AcceptedTotal":415
        },
        {
            "date":"4/25/2019",
            "totalCount":154,
            "ExpectedFund":49,
            "Funded":210,
            "Billing":243,
            "crossOver":35,
            "AcceptedTotal":115
        },
        {
            "date":"5/2/2019",
            "totalCount":300,
            "ExpectedFund":55,
            "Funded":200,
            "Billing":125,
            "crossOver":145,
            "AcceptedTotal":105
        },
        {
            "date":"5/9/2019",
            "totalCount":231,
            "ExpectedFund":55,
            "Funded":250,
            "Billing":125,
            "crossOver":355,
            "AcceptedTotal":215
        },
        {
            "date":"5/16/2019",
            "totalCount":331,
            "ExpectedFund":77,
            "Funded":270,
            "Billing":312,
            "crossOver":15,
            "AcceptedTotal":615
        },
        {
            "date":"5/23/2019",
            "totalCount":498,
            "ExpectedFund":232,
            "Funded":270,
            "Billing":312,
            "crossOver":15,
            "AcceptedTotal":615
        },
        {
            "date":"5/30/2019",
            "totalCount":102,
            "ExpectedFund":33,
            "Funded":150,
            "Billing":25,
            "crossOver":155,
            "AcceptedTotal":315
        },
        {
            "date":"6/6/2019",
            "totalCount":293,
            "ExpectedFund":235,
            "Funded":170,
            "Billing":112,
            "crossOver":125,
            "AcceptedTotal":315
        },
        {
            "date":"6/13/2019",
            "totalCount":198,
            "ExpectedFund":432,
            "Funded":470,
            "Billing":112,
            "crossOver":315,
            "AcceptedTotal":215
        }
    ]
}
于 2019-08-15T11:56:44.553 回答