I can't work out how to tree shake chartjs with vue3 - chart.js

I'm trying to tree shale chart.js with vue3 but dont know what i'm doing, I currently import the whole chart.js using: import Chart from 'chart.js/auto' but how can i import only the needed bits , so as to make ny build smaller?

as stated in the docs here you need to import and register all the elements you are using:
import {Chart, DoughnutController, ArcElement} from 'chart.js';
Chart.register(DoughnutController, ArcElement)
Instead of importing the DoughnutController and ArcElement you need to import and register the elements you are using.

Related

"export 'default' (imported as 'Chart') was not found in 'chart.js'

Hi im getting this issue and can't quite figure why its saying it. im using vue3 with tailwind.
""export 'default' (imported as 'Chart') was not found in 'chart.js'"
Guess you are trying to import chart.js like this import Chart from 'chart.js', since chart.js v3 chart.js is treeshakable so you will have to import and register all the components you want to use or import and register everything with the auto import like this: import Chart from 'chart.js/auto'
Docs: https://www.chartjs.org/docs/master/getting-started/integration.html#bundlers-webpack-rollup-etc
For me, I just had to downgrade from chart.js#3.x to chart.js#2.9.4

Do I need to import core-js/stable and regenerator-runtime/runtime when using #babel/preset-env with useBuiltIns:'usage'?

I use babel 7.8.3 together with #babel/preset-env, useBuiltIns: 'usage' and corejs: 3. The documentation for #babel/preset-env is not clear to me.
Do I need to add the following lines at the top of my entry file or is it done automatically by babel?
import 'core-js/stable';
import 'regenerator-runtime/runtime';
According to babel-preset-env docs you should import those modules by yourself
Don't need to add import code by yourself when use usage.
And no more need to manually include the regenerator-runtime helper when compiling generators after babel/core >= 7.18

Pymatgen XRD Plot

I am following the XRD plot tutorial and as it told in this tutorial I imported
from pymatgen import Lattice, Structure
from pymatgen.analysis.diffraction.xrd import XRDCalculator
from IPython.display import Image, display
%matplotlib inline
And after defining the structure I try to plot it with these commands
c = XRDCalculator()
c.show_xrd_plot(structure)
But I bump into this error: 'no attribute 'show_xrd_plot'
AttributeError: 'XRDCalculator' object has no attribute 'show_xrd_plot'
What should I do to make it work, many thanks in advance,
Happy Thanksgiving
The XRD tutorial of pymatgen is updated and xrd_show_plot it changed to
c.show_plot(structure)

How to integrate in Python inside For loop

I would like to use scipy's integrate.simps to get a integrated function from a data sample.
Data sample is getting calculated inside For loop (variable fx).
In the same step I store integrated values in the variable intfx.
import numpy as np
import pylab as pl
from scipy import integrate
t=np.arange(0,10.01,0.01)
fx=[]
intfx=[]
counter=0
for i in t:
counter+=1
fx.append(np.sin(i))
intfx.append(fx[-1]+integrate.trapz(fx[-2:], dx=0.1))
pl.plot(t,fx)
pl.plot(t,intfx)
pl.show()
On plots it can be seen that the two functions are very similar, which is obviously wrong.
Can anyone help me with this?
Made a stupid mistake.
Line:
intfx.append(fx[-1]+integrate.trapz(fx[-2:], dx=0.1))
should look like this:
intfx.append(intfx[-1]+integrate.trapz(fx[-2:], dx=0.1))

Relative Imports

I'm reading Two Scoops Django Best Practices to make my coding style improve. I'm in relative imports and here is the sample code to make it reusable.
Old Way:
from cones.foo import bar
New way:
from .foo import bar
The code above is for cones app, what if I call the other model in other app? Do I have to put like this:
from .foo import bar
from .other import sample
OR
from .foo import bar
from test.other import sample
What is the correct way?
I usually use imports like this only for one reason
from .foo import bar
from .other import sample
The reason being
If Tomorrow, my module name changes from say 'test' to 'mytest' then the code does not require a refactoring. The code works without breaking.
Update
All imports starting with a '.' dot, only works within that package.
Cross package imports need require the whole path.
If test is another app,
from .other import sample
wont work.
Update:
You can only use relative imports when you're importing from the same app.
Inside test app
from .other import sample
will work. But you'll still need the complete form
from cones.foo import bar
if you import method defined in foo from test app.
So answering your question the second way is the correct way.