Skip to content
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

How to scale the radius property of each bubble in a vue-chartjs bubble chart #205

Closed
johnboc opened this issue Sep 22, 2017 · 5 comments
Closed

Comments

@johnboc
Copy link

johnboc commented Sep 22, 2017

I am trying to scale the radius property of each bubble in a vue-chartjs bubble chart.

I have been trying to replicate the example at https://github.com/chartjs/Chart.js/blob/master/samples/scriptable/bubble.html but I cannot see how to obtain the data value to pass to the function. In my code below d should be the data value of the bubble.

data: function() {
    return {
        options: {
            elements: {                    
                point: {                                
                    radius: this.calcRadius(d)
				}
			}
		}
	}
},

methods: {
    calcRadius(d) {
        var base = Math.abs(d) / 1000;
        return (this.width / 24) * base;
    }
} 

I have tried accessing the chart.js instance using this._chart but this doesn't seem to be available when setting the chart options.

Any help much appreciated.

@apertureless
Copy link
Owner

Hey @johnboc

Well this._chart is only available after the first initialization. See here https://github.com/apertureless/vue-chartjs/blob/develop/src/BaseCharts/Bar.js#L78

So you can't really access it in other methods before the first renderChart() is called.

But generally I don't see a problem achieving a custom bubble radius. However you are passing (d) to your method. But this is imo the whole context you have. I guess you need to get the proper datapoint

Like in your example

radius: function(context) {
	var value = context.dataset.data[context.dataIndex];
	var size = context.chart.width;
	var base = Math.abs(value.v) / 1000;
	return (size / 24) * base;
}

@johnboc
Copy link
Author

johnboc commented Oct 10, 2017

Thanks, but what I don't understand is how to access context to be able to access the data points when the calcRadius function is called.

data: function() {
    return {
        options: {
            elements: {                    
                point: {                                
                    radius: this.calcRadius(context)
				}
			}
		}
	}
},

Your code example works in chart.js but I can't see how to replicate it in vue-chartjs.

@apertureless
Copy link
Owner

Well you just pass the context. Works well.

https://codepen.io/apertureless/pen/NwrLWX

@johnboc
Copy link
Author

johnboc commented Nov 6, 2017

Great - many thanks

@dagdashboard
Copy link

I am using the latest version of chart.js which is 2.9.3. While using this function I am getting error the following error
Type '(context: any) => number' is not assignable to type 'number'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants