繁体   English   中英

尝试为我的 chartjs 和 googlecharts 使用 axios、vuejs 和一个虚拟 api(仅获取请求)

[英]Trying to use axios, vuejs and a dummy api for my chartjs and googlecharts (get requests only)

我正在尝试将 axios 与包含此数据的链接结合使用:{ "2015": 226, "2016": 152, "2017": 259, "2018": 265, "2019": 275},用 JSON 编写.

我想实现这些数据,例如年份:2017 和收入:这张图表中的 259:

//BARCHART
var ctx = document.getElementById("myChart");
var barChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["2016", "2017", "2018", "2019", "2020"],
    datasets: [
      {
        label: 'Vergangenheit', //Vergangenheit = Past
        data: [226, 152, 259, 265, 0],
        backgroundColor: 'rgba(110, 205, 126, 1)',
        borderColor: 'rgba(110, 205, 126, 1)',
        borderWidth: 1,
      }
    ]
  },

  options: {
    responsive: true,
    responsiveAnimationDuration: 0,
    maintainAspectRatio: true,
    aspectRatio: 2,
    oneResie: null,
    scales: {
      yAxes: [
        {
          ticks: {
            beginAtZero: true
          }
        }
      ]
    }
  }

});

结合 vue 和 axios,一个 get 请求应该是这样的:

var vm = new Vue({
  el: '#get',
  data: {
    messages: [],
    message: ""
},
mounted: function () {
    this.getMessages(); // get all messages automatically when the page is loaded
},
methods: {
    getMessages: function() {
        axios.get('(hiddenhttps:/api/GewinnNachJahr')
            .then( (res) => {
                this.messages = res.data;
                console.log(response.data)
            })
    },
}
});

我不希望为获取请求按下任何按钮,它应该在重新加载页面时始终获取该数据。 我已经尝试了 stackoverflow 中的一些代码片段,官方的 axios github 根本没有帮助我。

总而言之,我希望在我的 http 数据上使用 axios getrequest,然后保存和排序这些数据并在我的 chart.js 条形图中实现它。 我认为仅使用我的 java.js 就足够了。

对于所涉及的时间和精力,我深表歉意。 先感谢您。

在这种情况下, mountedcreated是实现引导逻辑的正确位置。 但是,您的代码在定义和拼写错误方面存在一些问题:

  • '(hiddenhttps:/api/GewinnNachJahr' : 应该省略初始括号
  • console.log(response.data)response应该成为匹配回调参数的res

查看一个带有虚拟 api 调用的工作示例以及它如何加载数据:

 var vm = new Vue({ el: '#app', data: { messages: [], message: "" }, mounted() { this.getMessages(); // get all messages automatically when the page is loaded }, methods: { getMessages() { axios.get('http://dummy.restapiexample.com/api/v1/employees') .then((res) => { this.messages = res.data; console.log(res.data) }) }, } });
 <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <ul> <li v-for="m in messages">{{JSON.stringify(m)}}</li> </ul> </div>

你可以在你的 vue 应用中实现 chart.js。 我已经创建了它应该可以工作的代码。

   var vm = new Vue({
  el: "#get",
  data: {
    messages: [],
    message: "",
    labels: [],
    data_set: [],
    barChart: ""
  },
  mounted: function() {
    var ctx = document.getElementById("myChart");
    this.barChart = new Chart(ctx, {
      type: "bar",
      data: {
        labels: this.labels,
        datasets: [
          {
            label: "Vergangenheit", //Vergangenheit = Past
            data: this.data_set,
            backgroundColor: "rgba(110, 205, 126, 1)",
            borderColor: "rgba(110, 205, 126, 1)",
            borderWidth: 1
          }
        ]
      },

      options: {
        responsive: true,
        responsiveAnimationDuration: 0,
        maintainAspectRatio: true,
        aspectRatio: 2,
        oneResie: null,
        scales: {
          yAxes: [
            {
              ticks: {
                beginAtZero: true
              }
            }
          ]
        }
      }
    });
  },
  created: function() {
    this.getMessages(); // get all messages automatically when the page is loaded
  },
  methods: {
    getMessages: function() {

      axios
        .get(
          "https://webenggroup06ln3.free.beeceptor.com/zalando/api/GewinnNachJahr"
        )
        .then(res => {
          console.log(res.data);
          for (let [key, value] of Object.entries(res.data)) {
            this.labels.push(key);
            this.data_set.push(value);
          }
          this.$nextTick(function() {
            this.barChart.update();
          });
        });
    }
  }
});

for循环将您的键和值分开,然后将其推送到数据数组中。 将所有内容推入数组后,图表只需要使用this.barChart.update()更新

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM