简体   繁体   中英

How should data be structured for lineplots with vue/chart.js

I am retrieving data from an api (working) and generating a line plot with vue-chartjs .

However only the first two points are plotted. I must have my data structured in the wrong way but I don't know what it is expecting. Here is my component:

import { Line } from "vue-chartjs";

export default {
  extends: Line,
  props: {
    chartdata: {
      type: Object,
      default: null
    }
  },
  mounted() {
    this.renderChart(this.chartdata);
  }
};

And in my App.vue file, I have this simple template:

<template>
  <div id="app">
    <div class="container">
      <div class="Chart">
        <h2>LineChart</h2>
        <line-chart v-if="loaded" :chartdata="chartdata"></line-chart>
      </div>
    </div>
  </div>
</template>

and this script:

<script>
import LineChart from './components/LineChart.js'
export default {
  name: 'app',
  components: { LineChart },
  data:() => ({
    loaded: false,
    chartdata: null
  }),
  async mounted () {
    this.loaded = false;
    try {
      let mydata = await fetch("http://myserver00/api/load/myserver02")
      .then(stream => stream.json())
      .then(mydata => {
        let usercpu = [];
        mydata.forEach(record => {
          usercpu.push( record.cpu.user );
        });
        this.usercpu = usercpu;
      });
    } catch (e) {
      console.error(e)
    }
    this.loaded = true;
    this.chartdata = {
      datasets: [
        {label: 'CPU', data: this.usercpu}
        ]
      };
  }
}
</script>

As you may notice, I'm trying to system data from psutils to monitor servers. The original record from the api has several fields. This example just shows my attempt at CPU usage.

The browser tools show the data I expect, but apparently not what the chart expects. Here's a view of the data from the chrome vue devtools extension

在此处输入图片说明

Edit: to make sure the data is actually loaded, I added {{chartdata}} into my template and I see it all. Here is how it starts, the array goes on with all the data array. And the actual plot again shows only the first two data points.

{ "datasets": [ { "label": "CPU", "data": [ 2.7, 2.9, 3

Finally got it; I needed to re-read the doc for Chartjs. So here's the mounted function now. See that I had to put in x and y values for each point.

async mounted () {
  this.loaded = false;
  try {
    let usercpu = [];
    await fetch("http://server/api/load/server02")
    .then(stream => stream.json())
    .then(mydata => { mydata.forEach(record => { 
      usercpu.push( { y: record.cpu.user, x: record.date });});});
    this.loaded = true;
    this.chartdata = { 
      datasets: [ {
        fill: false, 
        pointRadius: 0, 
        borderWidth: 2,
        label: 'CPU', 
        data: usercpu 
        } ] };
  } catch (e) {
    console.error(e)
  }
}

Also, in the data portion of the default function I had to add axes. I'm not exactly sure why, but even when the data was okay (above) I still couldn't see the plot. So when I added the axes, there everything was:

options: {
    scales: {
        xAxes: [{
            type: 'time',
            time: {
                  unit: "hour",
                  displayFormats: {
                    hour: "M/DD @ hA"
                  },
                  tooltipFormat: "MMM. DD @ hA"
                },
                scaleLabel: {
                  display: true,
                  labelString: "Date/Time"
                },
            ticks: { autoSkip: true, maxTicksLimit: 5},
            position: 'bottom'
        }],
        yAxes: [{
            ticks: {
                suggestedMin: 0,
                suggestedMax: 10
            }
        }]
    } 

And of course I had to include the options in the directive:

    <template>
    <div id="app">
        <div class="container">
        <div id="cpu">
        <h2>Server02</h2>
        <line-chart v-if="loaded"  :chartdata="chartdata" :options="options"></line-chart>
        </div>
        </div>
    </div>
    </template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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