I have this code to show bar-chart with VueJS:
JavaScript
x
60
60
1
Vue.component('bar-chart', {
2
extends: VueChartJs.Bar,
3
data: function () {
4
return {
5
datacollection: {
6
labels: ['MICROFINANZAS -SECTOR COMERCIO','MICROFINANZAS -SECTOR SERVICIOS'],
7
datasets: [
8
{
9
label: 'Data One',
10
backgroundColor: '#f87979',
11
pointBackgroundColor: 'white',
12
borderWidth: 1,
13
pointBorderColor: '#249EBF',
14
data: [15000, 71700]
15
}
16
]
17
},
18
options: {
19
scales: {
20
yAxes: [{
21
ticks: {
22
beginAtZero: true
23
},
24
gridLines: {
25
display: true
26
}
27
}],
28
xAxes: [{
29
ticks: {
30
beginAtZero: true
31
},
32
gridLines: {
33
display: false
34
}
35
}]
36
},
37
legend: {
38
display: false
39
},
40
tooltips: {
41
enabled: true,
42
mode: 'single',
43
callbacks: {
44
label: function (tooltipItems, data) {
45
return '$' + tooltipItems.yLabel;
46
}
47
}
48
},
49
responsive: true,
50
maintainAspectRatio: false,
51
height: 200
52
}
53
}
54
},
55
mounted() {
56
// this.chartData is created in the mixin
57
this.renderChart(this.datacollection, this.options)
58
}
59
})
60
Method in VueJS
JavaScript
1
27
27
1
var app = new Vue({
2
el: '#grid',
3
data: {
4
columns: ['id', 'nombre'],
5
objeto: "",
6
searchQuery: "",
7
dataChart: "",
8
dataChart1: "",
9
},
10
created: function () {
11
this.getDeudas();
12
},
13
methods: {
14
getDeudas: function () {
15
this.$http.get(baseURL + "/Home/ConsultarDeudasHome").then(function (response) {
16
this.lista = response.data.data;
17
console.log(this.lista);
18
this.objeto = JSON.parse(this.lista);
19
console.log(this.objeto[1].original);
20
21
this.dataChart = [this.objeto[0].original, this.objeto[0].actual];
22
console.log(this.dataChart);
23
this.dataChart1 = [this.objeto[1].original, this.objeto[1].actual];
24
});
25
},
26
},
27
This code show this bar chart:
But I need replace in my code two variables dynamic:
labels: [‘MICROFINANZAS -SECTOR COMERCIO’,’MICROFINANZAS -SECTOR SERVICIOS’],
data: [15000, 71700]
With the information of method getDeudas()
How can to made this action?
Advertisement
Answer
This is the solution, I use props
and watch
:
JavaScript
1
60
60
1
Vue.use(VueTables.ClientTable);
2
Vue.component("bar-chart", {
3
extends: VueChartJs.Bar,
4
props: ["data", "options"],
5
mounted() {
6
this.renderLineChart();
7
},
8
computed: {
9
chartData: function () {
10
return this.data;
11
}
12
},
13
methods: {
14
renderLineChart: function () {
15
this.renderChart(
16
{
17
labels: ["Sector Comercio", "Sector Servicios"],
18
datasets: [
19
{
20
label: "Consolidado",
21
backgroundColor: "#f87979",
22
data: this.chartData
23
},
24
],
25
},
26
{ responsive: true, maintainAspectRatio: false }
27
);
28
}
29
},
30
watch: {
31
data: function () {
32
this.renderLineChart();
33
}
34
}
35
});
36
37
38
39
const baseURL = window.location.protocol + "//" + window.location.host;
40
var app = new Vue({
41
el: '#grid',
42
data: {
43
columns: ['id', 'nombre'],
44
objeto: "",
45
dataChart: "",
46
},
47
created: function () {
48
this.getDeudas();
49
},
50
methods: {
51
getDeudas: function () {
52
this.$http.get(baseURL + "/Home/ConsultarDeudasHome").then(function (response) {
53
this.lista = response.data.data;
54
this.objeto = JSON.parse(this.lista);
55
this.dataChart = [this.objeto[0].original, this.objeto[1].original];
56
});
57
},
58
},
59
})
60