简体   繁体   English

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

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

I am trying to use axios in combination with a link containing this data: { "2015": 226, "2016": 152, "2017": 259, "2018": 265, "2019": 275}, written in JSON.我正在尝试将 axios 与包含此数据的链接结合使用:{ "2015": 226, "2016": 152, "2017": 259, "2018": 265, "2019": 275},用 JSON 编写.

I want to implement those data, the year for example: 2017 and the revenue: 259 in this chart:我想实现这些数据,例如年份: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
          }
        }
      ]
    }
  }

});

With the combination of vue and axios a get request should look like this:结合 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)
            })
    },
}
});

I don't want any button to press for the get request, it should always get that data when reloading the page.我不希望为获取请求按下任何按钮,它应该在重新加载页面时始终获取该数据。 I already tried the a few code snippets from stackoverflow, the official axios github don't help me at all.我已经尝试了 stackoverflow 中的一些代码片段,官方的 axios github 根本没有帮助我。

So to summarize i want that axios getrequest on my http datas, then saving and sorting this data and implementing it in my chart.js barchart.总而言之,我希望在我的 http 数据上使用 axios getrequest,然后保存和排序这些数据并在我的 chart.js 条形图中实现它。 I think its enough to just working withing my java.js.我认为仅使用我的 java.js 就足够了。

I apologize for the time and effort involved.对于所涉及的时间和精力,我深表歉意。 Thank you in advance.先感谢您。

mounted or created is the correct place to implement bootstrapping logic for this case.在这种情况下, mountedcreated是实现引导逻辑的正确位置。 However your code has some problems in definitions and typo:但是,您的代码在定义和拼写错误方面存在一些问题:

  • '(hiddenhttps:/api/GewinnNachJahr' : the initial parenthesis should be omitted '(hiddenhttps:/api/GewinnNachJahr' : 应该省略初始括号
  • console.log(response.data) : response should become res for matching callback parameter. console.log(response.data)response应该成为匹配回调参数的res

See a working example with a dummy api call and how it loads the data:查看一个带有虚拟 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>

You can implement chart.js into your vue app.你可以在你的 vue 应用中实现 chart.js。 I have created this code it should work.我已经创建了它应该可以工作的代码。

   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();
          });
        });
    }
  }
});

the for loop splits your key and your value seperate and then it pushes it into the data array. for循环将您的键和值分开,然后将其推送到数据数组中。 After everything is pushed inside the array the chart just needs to get updated with this.barChart.update()将所有内容推入数组后,图表只需要使用this.barChart.update()更新

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

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