简体   繁体   中英

vue-chartjs line-chart with API-call not rendering

I made some bar charts with vue2 and vue-chart.js and they work great. The data comes from an API call. I have now problems with line charts , it is not displayed, without any errors. The data are loaded, the line-chart is not showing. I can't find the error in my code. Any ideas? THX.

条形图

Versions:

"vue": "^2.6.11"
"vue-chartjs": "^4.1.1"

my base-vue:

<template>
        
          <div id="app" class="content">
        
            <div class="dist_30"></div>
            <h3>Charts</h3>
        
            <div class="dist_10"></div>
        
            <div class="row ">
        
              <div class="col-12">
        
                <div style="margin: 10px 10px 10px 10px;">
                  <BarChart />
                </div>
        
              </div>
        
            </div>
        
            <div class="row ">
        
              <div class="col-12">
        
                <div style="margin: 10px 10px 10px 10px;">
                  <LineChart />
                </div>
        
              </div>
        
            </div>
        
          </div>
        
        </template>
        
        <script>
        import BarChart from '@/components/BarChart'
        import LineChart from '@/components/LineChart'
        
        export default {
          name: 'App',
          components: { BarChart , LineChart}
        }
        </script>

my LineChart.vue:

<template>
          <Line v-if="loaded"
            :chart-options="chartOptions"
            :chart-data="chartData"
            :chart-id="chartId"
            :dataset-id-key="datasetIdKey"
            :plugins="plugins"
            :css-classes="cssClasses"
            :styles="myStyles"
            :width="300"
            :height="height"
          />
        </template>
        
        <script>
        import { Line } from 'vue-chartjs/legacy'
        import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, LineElement, CategoryScale, LinearScale } from 'chart.js';
        
        ChartJS.register(Title, Tooltip, Legend, BarElement,  LineElement, CategoryScale, LinearScale);
        
        const chartAPI = 'https://m.myapp2go.de/services/getstatistik_tables.php';
        
        export default {
          
          name: 'LineChart',
          components: { Line },
        
            async mounted () {
            this.loaded = false
        
            try {
        
              const response = await fetch(chartAPI);
              const chartArray = await response.json();
              this.chartdata = chartArray.items;
        
                // Get all the numbers/labels from the response
                  this.chartData.labels = this.chartdata.map(i => i.table).slice(1); 
                  this.chartData.datasets[0].data = this.chartdata.map(i => i.count).slice(1); 
        
                  console.log("chart Data ", JSON.stringify(this.chartdata));
                  console.log("chart Data numbers: ", this.newChartArray);
        
                  this.loaded = true
        
            } catch (e) {
              console.error(e)
            }
          },
            computed: {
            myStyles () {
              return {
                position: 'relative'
              }
            }
          },
          props: {
            chartId: {
              type: String,
              default: 'line-chart'
            },
            datasetIdKey: {
              type: String,
              default: 'label'
            },
            width: {
              type: Number,
              default: 200
            },
            height: {
              type: Number,
              default: 0
            },
            cssClasses: {
              default: '',
              type: String
            },
            styles: {
              type: Object,
              default: () => {}
            },
            plugins: {
              type: Array,
              default: () => []
            }
          },
          data() {
            return {
              chartData: {
                labels: [ 'num', 'call', 'blog', 'key', 'mod,' ,'pic', 'acc', 'ifc', 'req', 'inf' ],
                datasets: [ { 
                  label: 'Database statistics',
                  borderWidth: 1,
                  
                  data: [ ]} ]
              },
              chartOptions: {
                responsive: true,
                maintainAspectRatio: false
              },
              newChartArray: [],
              loaded: false
            }
          }
        }
        </script>

output from chrome-dev-tools:

<Line chart-options="[object Object]" chart-data="[object Object]" chart-id="line-chart" dataset-id-key="label" plugins="" css-classes="" styles="[object Object]" width="300" height="0"></Line>

Because Bar chart and Line chart use different components you must import these following components like this:

import { Line as LineChartGenerator } from 'vue-chartjs/legacy'

import {
    Chart as ChartJS,
    Title,
    Tooltip,
    Legend,
    LineElement,
    LinearScale,
    CategoryScale,
    PointElement
} from 'chart.js'

ChartJS.register(
    Title,
    Tooltip,
    Legend,
    LineElement,
    LinearScale,
    CategoryScale,
    PointElement
)

and for the template:

<LineChartGenerator v-if="loaded"
            :chart-options="chartOptions"
            :chart-data="chartData"
            :chart-id="chartId"
            :dataset-id-key="datasetIdKey"
            :plugins="plugins"
            :css-classes="cssClasses"
            :styles="myStyles"
            :width="300"
            :height="height"
          />

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