Chart.js:最后一点在折线图之外

时间:2018-10-12 14:47:37

标签: javascript vue.js charts chart.js

请原谅我的英语不完美。

当我有一个包含大量数据的折线图时,例如6个月中每天1个点,则最后一个点没有标签,并且该点不在图表中。此后,要恢复正常图表,我必须设置一个较小的日期范围,例如2个月,然后我可以将其范围扩大到该错误出现之前的5或6个月。

正常点:https://i.gyazo.com/b4e88afc7c94adfbeb86bf6543900d4b.jpg

出门且没有标签:https://i.gyazo.com/f782e5b752ca87340ebaabe32c1e88d4.jpg

您是否曾经遇到过此错误,您知道我该如何解决?

图表vue:

<template>
  <div class="cdr-chart" style="height: 500px">
    <cdr-loader v-if="loading" :size="50"></codeur-loader>
    <div class="d-flex justify-content-between">
      <div class="col-md-3">
        <p class="m-0 small"><strong>Début le</strong></p>
        <input type="text" class="form-control form-control-sm admin-datepicker" :id="`${this.name}__starts_at`">
      </div>
      <div class="col-md-3">
        <p class="m-0 small"><strong>Fin le</strong></p>
        <input type="text" class="form-control form-control-sm admin-datepicker" :id="`${this.name}__ends_at`">
      </div>
      <div class="col-md-3 mt-3 mb-2">
        <div class="input-group input-group-sm">
          <span class="input-group-addon">
            <input type="checkbox" v-model="checked" :id="`${this.name}_checkbox`">
          </span>
        <label :for="`${this.name}_checkbox`" class="form-control">Par semaine</label>
        </div>
      </div>
    </div>
    <canvas height="300"></canvas>
  </div>
</template>

<script>
/* eslint-disable func-names */
import Chart from 'chart.js';
import CdrLoader from '../../components/CdrLoader.vue';
import moment from 'moment';

const FETCH_URL = '/admin/dashboards/fetch_data';

const colors = {
  backgroundColor: [
    'rgba(54, 162, 235, 0.2)',
    'rgba(255, 99, 132, 0.2)',
    'rgba(255, 206, 86, 0.2)',
    'rgba(75, 192, 192, 0.2)',
    'rgba(153, 102, 255, 0.2)',
    'rgba(255, 159, 64, 0.2)'
  ],
  borderColor: [
    'rgba(54, 162, 235, 1)',
    'rgba(255,99,132,1)',
    'rgba(255, 206, 86, 1)',
    'rgba(75, 192, 192, 1)',
    'rgba(153, 102, 255, 1)',
    'rgba(255, 159, 64, 1)',
  ],
  borderWidth: 1,
};

export default {
  components: { CdrLoader },
  props: {
    name: {
      type: String,
      default: '',
    },
    title: {
      type: String,
      default: '',
    },
    type: {
      type: String,
      default: 'line',
    },
  },
  data() {
    return {
      loading: true,
      datasets: null,
      chart: null,
      starts_at: null,
      ends_at: null,
      checked: null,
    };
  },
  mounted() {
    this.$nextTick(function () {
      const self = this;
      const startsAtDp = this.$el.querySelector(`#${this.name}__starts_at`);
      const endsAtDp = this.$el.querySelector(`#${this.name}__ends_at`);
      const checked = this.$el.querySelector(`#${this.name}_checkbox`);

      $(startsAtDp).pickadate({
        format: 'yyyy-mm-dd',
        onSet(context) {
          if (!context.select) return false;
          self.starts_at = moment(context.select).format('YYYY-MM-DD');
        },
      });

      $(endsAtDp).pickadate({
        format: 'yyyy-mm-dd',
        onSet(context) {
          if (!context.select) return false;
          self.ends_at = moment(context.select).format('YYYY-MM-DD');
        },
      });
      this.loadChart();
    });
  },
  methods: {
    loadChart() {
      this.loading = true;
      this.$http.get(`${FETCH_URL}?data=${this.name}&starts_at=${this.starts_at}&ends_at=${this.ends_at}&checked=${this.checked}`).then((response) => {
        this.datasets = response.body.datasets;
        if (this.chart) {
          this.updateChart();
        } else {
          this.initChart();
        }
        this.loading = false;
      });
    },
    initChart() {
      const ctx = this.$el.querySelector('canvas').getContext('2d');

      const datasets = [];

      this.datasets.charts.forEach((chart, index) => {
        datasets.push({
          backgroundColor: chart.background_color ? chart.background_color : colors.backgroundColor[index],
          borderColor: chart.border_color ? chart.border_color : colors.borderColor[index],
          borderWidth: 1,
          data: chart.data,
          type: chart.type,
          label: chart.label,
        });
      });

      this.chart = new Chart(ctx, {
        type: this.type,
        data: {
          labels: this.datasets.labels,
          datasets,
        },
        options: {
          responsive: true,
          maintainAspectRatio: false,
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true,
              },
            }]
          },
        },
      });
    },
    updateChart() {

      const datasets = [];

      this.datasets.charts.forEach((chart, index) => {
        datasets.push({
          backgroundColor: chart.background_color ? chart.background_color : colors.backgroundColor[index],
          borderColor: chart.border_color ? chart.border_color : colors.borderColor[index],
          borderWidth: 1,
          data: chart.data,
          type: chart.type,
          label: chart.label,
        });
      });

      this.chart.data = {
        labels: this.datasets.labels,
        datasets,
      };

      this.chart.update();
    },
  },
  watch: {
    starts_at(newStartsAt) {
      this.loadChart();
    },
    ends_at() {
      this.loadChart();
    },
    checked() {
      this.loadChart();
    },
  }
};
</script>

<style lang="scss" scoped>
  .cdr-chart {
    width: 100%;
    canvas {
      height: 100%;
    }
  }
</style>

0 个答案:

没有答案