0

Problem

Codepen: https://codesandbox.io/s/vyy0z2my33

  1. First, you can keep track this problem on: https://github.com/apertureless/vue-chartjs/issues/473

  2. Also I have a project using datepicker which rerenders the chart based on given date thus if you look on the codepen, at the BarChart.vue, you can see I add watcher to rerender the chart

My Project

But turned out got error when every time I select a date

Console Log Erro

Background

In a nutshell, I can't find the example how to put the gradient to the chart.

Code

BarChart.vue

<script>
import { Bar, mixins } from "vue-chartjs";
import { mapGetters } from "vuex";

export default {
  extends: Bar,
  mixins: [mixins.reactiveProp],
  props: ["chartData", "chartLabels", "options"],

  mounted() {
    // chartData is created in the mixin
    // let gradientFill = this.$refs.canvas.getContext('2d').createLinearGradient(2000, 0, 0, 0);

    // gradientFill.addColorStop(0, "#18FFFF");
    // gradientFill.addColorStop(1, "#FF1744");
    this.renderChart(
      {
        labels: this.chartLabels,
        datasets: [
          {
            data: this.chartData,
            backgroundColor: "red",
            hoverBackgroundColor: "red"
          }
        ]
      },
      this.options
    );
  },
  computed: {
    ...mapGetters(["getBarHourTooltip"])
  },
  watch: {
    chartData() {
      this.$data._chart.update();
    },
    chartLabel() {
      this.$data._chart.update();
    }
  }
};
</script>

Actual Result

  1. Can't implement the gradient color
  2. Data's well shown on initial, but got error when a date selected (as shown on image above)
mending3
  • 586
  • 7
  • 21

2 Answers2

1

I was not able to reproduce with your codesandbox project. How are you changing the date?

The error message is referring to a map() method used on an undefined array, presumably originating in src/store/stores.js. You could try to wrap the offending map() method call in a control statement, like this:

if (payloadInitAllChart.barDataHour) { // Example, could be other map() method
  payloadInitAllChart.barDataHour.map(response =>
   state.barChartPerHour.datasetBarHour.push(response)
  );
}
Rob Axelsen
  • 844
  • 6
  • 8
  • 1
    I think it won't solve my problem. You can look it out on: https://stackoverflow.com/questions/47278617/vue-chartjs-reactive-data-error the datepicker-chart project is on my local project by the way. Would be difficult to produce a huge reproduction. – mending3 Feb 18 '19 at 09:01
0

If you are using the mixin your chart data has to be the Chart.js data object. With labels, datasets etc. in it. But then you won't be able to set the gradient colour.

But you could also simply pass in your labels and data (if you have only one dataset), as you did right now.

Add a watcher on both and then trigger this.$data._chart.update()

But then you have to remove the reactiveMixin.

https://codesandbox.io/s/0yl728w0n

Jakub Juszczak
  • 7,126
  • 3
  • 21
  • 38
  • I've added a button to repopulate the chart data to see if it's reactive. However it's not reactive. I have to re-render (`this.renderChart()`) the chart in both watchers but it's ugly in the eyes. You can open my updated sandbox: https://codesandbox.io/s/zljw20zp0m. Click the button. You can see nothing happened – mending3 Feb 21 '19 at 10:59