-
-
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
Why separate .js files in the examples? #77
Comments
So your question is why I split the different chart types into own modules? It's more a historical reason, about how this wrapper started. I was working on a project where I was using Chart.js with many different charts. And to be honest, I was too lazy to create all this boilerplate over and over again for many different charts. Like the template with a canvas, props etc. Thats why I created this wrapper. Also keep in mind this is quite some time ago. It all was written in early vue 1 days and later ported to vue 2. I seperated the chart types into own modules for two reasons:
You could also create just one component and extend this and pass the type as a parameter: export default VueChartJs.extend({
....
mounted () {
this.renderChart(type, data, options)
}
}) However this way, I could not have seperate styles and options per chart type. Vue.extend()And I am using <vue-chartjs type="bar" options="options" chart-data="data" /> And I think there are also other chartjs wrappers for vue, that does it this way, or seperate the charts into own components so you can use them like this: <line-chart options="options" chart-data="data" /> The huge befenfit of extend is, that you have full access of the boilerplate component. You can call the renderChart method, when you want. In the Other chartjs wrapper have other approaches and like I said everything has a downside. In the end, for the user, it has no real difference to use the seperated modules or a single module. |
Many thanks for your detailed answer. Very clear and helpful. As you know, picking design patterns is tough when you're new to something. Setting default styles makes perfect sense. But for my use case - several charts of the same type and overall styles, only differing in data from the server -, I found myself creating multiple components identical to each other as in So my first thought was to do something like you mentioned:
In my mind, with this approach I'd have only 1 reusable and consistent line-chart component. And If I needed specific styles for one of the charts, I'd extend that component. Is it possible to do it like that with your wrapper? |
Well, it's fully up to you! If you want you can make reuseable charts or more specified charts. You can call your component line-chart and reuse it all over the place. |
Just a conceptual question. Perhaps because I'm new to vue, I'm not sure why in your examples (both in the docs and in the issues) you extend the chart components in separate files even though the chart data and options are prepared in their respective parent components.
For instance: #33
BasicChart.js, like MonthlyIncome.js from the docs, are using data directly from their parents. They basically do nothing but set the chart type, which could be done using props from the parent as well. So why split files and repeat the boilerplate code for multiple charts?
The text was updated successfully, but these errors were encountered: