-
-
Notifications
You must be signed in to change notification settings - Fork 838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Computed object won't populate chart #170
Comments
Please provide a minimal jsfiddle / codepen for reproduction. |
I don't have much experience setting up codepens, but I copied one and modified it to present the same issue (I think): https://codepen.io/ssuess/pen/zdPqGW |
In playing around with the codepen, I found the problem for that simple example anyway: I had to add array brackets around object brackets on the data object before passing it back. so But in my own code doing the same thing does not fix it. I am still investigating. |
Do you have any examples of a computed property being used in a chart that I could look at? |
Found this on stackexchange which seems to describe almost exactly the same problem, but trying their solution did not work for me. |
I have been examining this further, and it is definitely related to the prop data being passed down from the parent. It is for some reason inaccessible to the vue for the chart (although it is accessible and computing proper values as shown above in the debugger). When I copy my data to the data object and then compute THAT, it works fine. So somehow the prop with data I am passing down the chart refuses to see/use. |
I give up. At least chartist works, I will content myself with that for the time being. Thanks anyway. |
Hey. import { Bar } from 'vue-chartjs'
export default Bar.extend({
props: ['labels', 'datasets'],
computed: {
'chartdata': function () {
return {
labels: this.labels,
datasets: this.datasets
}
}
},
mounted () {
this.renderChart(this.chartdata,
{responsive: true, maintainAspectRatio: false})
}
}) |
Thanks, but as I said above, the problem seems not to be with computed properties per say, but with the prop (array) being passed down (even though it does pass down for my computed element in the vue dev tools). I notice in your example above your don't specify the prop as array btw, which I was under the impression you had to do for it to work in that case, no? |
yes, trying with your notation above fails for me, I am curious how you passed labels and datasets in that code since they are not defined as arrays? |
What do you mean, they are not defined as an array? The labels and datasets are arrays. <?php if(have_rows('datasets')): ?>
<?php
$datasets = [];
?>
<?php while ( have_rows('datasets') ) : the_row();
$datasets[] = array(
'label' => get_sub_field('dataset_label'),
'backgroundColor' => get_sub_field('dataset_color'),
'data' => explode(', ', get_sub_field('dataset_data'))
);
endwhile;
$labels = get_field('x_axis');
$legend = get_field('legend');
$chartDatasets = htmlspecialchars(json_encode($datasets));
$chartLabels = htmlspecialchars(json_encode(explode(', ', $labels)));
?>
<section class="chart-box text">
<div class="text__wrapper">
<bar-example
:labels="<?= $chartLabels; ?>"
:datasets="<?= $chartDatasets; ?>" >
</bar-example>
</div>
</section>
<?php endif; ?>
Well without seeing your code, parent + chart components, its hard to debug. |
I'm sorry, I am new to Vue and I was under the impression that one was required to define prop type as array ie |
Oh, I see. No, this is only for type checking. If you don't define a type, the type will be You can also make own prop validation methods. |
I have spent the last few hours looking at this, and have pinpointed the problem, but not a solution. If I populate the parent with a hand typed array to pass down as my prop, ie:
And then perform my computed function on that, it WILL populate my bar chart. But if my prop ("items" in this case) is an array returned from a call to my api, it will NOT populate the chart, even though the calculated data looks just fine in dev tools. So there is something about the formatting of the array that the bar chart refuses to render. I have copied the array directly from the dev tools and pasted it into the data array of the parent, and I do get eslint warnings, but only about using single quotes over doubles. Other than that, it looks identical and I can't figure out why that would make a difference to the bar render. |
Oh, you are working with an API ? So your chart component will get created and mounted before your data arrives. You could use a So in your return {
loaded: false
} and in your api call (if you're using axios) you could set it it to true. axios.get(`https://api.com/my-endpoint`)
.then(response => {
this.rawData = response.data
this.loaded = true
})
.catch(err => {
....
}) And then add an Another way would be to add a watcher and rerender the chart if your computed prop change. You can check out the resources in the docs. There are some tutorials on how to interact with APIS |
OMFG! That was it!!! Thank you!!! Sorry to have put you through all of this. |
You're welcome! ✌️ |
How can i get so, this does not work:
I have read dozens of questions. threads, tutorials etc but the computed properties loads first and that seem to be it. |
Expected Behavior
Passing a prop array and computing a data value from it to populate a chart should work.
Actual Behavior
Empty chart.
Looking in vue console tools, I see that my computed object is present and (seemingly) valid:
And my template code looks like this:
<bar-chart :data="actionbarlabels" :options="{responsive: true, maintainAspectRatio: false}"></bar-chart>
Environment
The text was updated successfully, but these errors were encountered: