简体   繁体   English

Vuejs - Chartjs 不使用 api 请求呈现我自己的选项

[英]Vuejs - Chartjs doesn't render my own options with api request

I have a problem when I want to fill my chart with my api. My chart's extraOptions are set, but when I want to render my chart with it, it takes defaults options.当我想用我的 api 填充我的图表时遇到问题。我的图表的 extraOptions 已设置,但是当我想用它呈现我的图表时,它采用默认选项。

Here is my component:这是我的组件:

import { Bar, mixins } from 'vue-chartjs';

export default {
  name: 'bar-chart',
  extends: Bar,
  mixins: [mixins.reactiveProp],
  props: {
    extraOptions: Object,
    chartData: Object,

  },
  data() {
    return {
      ctx: null
    };
  },
  methods: {
    
  },
  mounted() {
    this.renderChart(
      this.chartData,
      this.extraOptions
    );
    this.$watch('chartData', (newVal, oldVal) => {
      if (!oldVal) {
        console.log(this.extraOptions)
        this.renderChart(
          this.chartData,
          this.extraOptions
        );
      }
    }, { immediate: true });
  }
};

And my overview with my Options and call api:我的选项概览并致电 api:

  <div class="row">
        <div class="col-lg-4">
          <card>
          <bar-chart :height="heightChart" :extraOptions="optionsBar" :chart-data="BarData"> </bar-chart >
          </card>
        </div>
  </div>
</template>
<script>
  import Loading from 'src/components/Dashboard/Layout/LoadingMainPanel.vue'
  import LineChart from 'src/components/UIComponents/Charts/LineChart.vue'
  import pieca from 'src/components/UIComponents/Charts/pieca.vue'
  import Clock from './Clock.vue'
  import BarChart from 'src/components/UIComponents/Charts/BarChart'
  import axios from 'axios'


  const WorldMap = () => ({
    component: import('./../Maps/WorldMap.vue'),
    loading: Loading,
    delay: 200
  })

  export default {
    components: {
      LineChart,
      pieca,
      Clock,
      BarChart
    },
    /**
     * Chart data used to render stats, charts. Should be replaced with server data
     */
    data () {
      return {
        optionsLine: {
          responsive: true,
          maintainAspectRatio: false,
          title: {
            display: true,
            text: 'CA Mensuel/12 mois',
            fontSize: 17,
            fontFamily: "'Montserrat', 'Helvetica Neue', Arial, sans-serif",
            fontStyle: 'normal',
            fontColor: 'black',
          }
        },
        optionsBar: {
          maintainAspectRatio: false,
          legend: {
            display: false,
            labels: {
              fontColor: '#ced4da'
            }
          },
          title: {
            display: true,
            text: 'Cmd/Transporteur',
            fontSize: 17,
            fontFamily: "'Montserrat', 'Helvetica Neue', Arial, sans-serif",
            fontStyle: 'normal',
            fontColor: 'black',
          },
          responsive: true,
          tooltips: {
            backgroundColor: '#f5f5f5',
            titleFontColor: '#333',
            bodyFontColor: '#666',
            bodySpacing: 4,
            xPadding: 12,
            mode: "nearest",
            intersect: 0,
            position: "nearest"
          },
          legend: {
            display:true,
            position: 'bottom'
          },
          scales: {
            yAxes: [{
        
              stacked: true,
              gridLines: {
                drawBorder: false,
                color: 'rgba(29,140,248,0.1)',
                zeroLineColor: "transparent",
              },
              ticks: {
                suggestedMin: 60,
                suggestedMax: 120,
                padding: 20,
                fontFamily: "'Poppins',sans-serif",
                fontColor: "#ced4da"
              }
            }],
            xAxes: [{
        
              stacked: true,
              gridLines: {
                drawBorder: false,
                color: 'rgba(29,140,248,0.1)',
                zeroLineColor: "transparent",
              },
              ticks: {
                padding: 20,
                fontFamily: "'Poppins',sans-serif",
                fontColor: "#ced4da"
              }
            }]
          }
        },

        heightChart: 255,

        BarData: {

        },

And my api request:还有我的 api 请求:

      axios
        .get('api_url')
        .then(response => (globalthis.BarData = response.data.transporteur_jour
        ))

I think my chart is rendering with my extraOptions and when it's fill with api data, it takes the defaults options.我认为我的图表是用我的 extraOptions 渲染的,当它填充 api 数据时,它采用默认选项。 When I don't call my api and fill the labels directly in my overview, it works.当我不拨打我的 api 并直接在我的概览中填写标签时,它会起作用。 How can I do to keep my extraOptions?我该怎么做才能保留我的 extraOptions?

Thanks谢谢

i had the same problem with chart data in nuxt.js我在 nuxt.js 的图表数据中遇到了同样的问题

add a key to your component and when you get the api response change the value of the key.向您的组件添加一个键,当您收到 api 响应时,更改键的值。 Vue will re-render the component after this change Vue 将在此更改后重新渲染组件

my component:我的组件:

  <LineChartComponent :chart-data="datacollection" :height="400" :key="datacollection.key"></LineChartComponent>

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

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