How to import plotly.js into TypeScript?
up vote
6
down vote
favorite
I'm trying to import plotly.js
into TypeScript. Plotly.js is installed using npm
. In my TypeScript file I use
import 'plotly.js';
It's OK but I'm getting error on code like Plotly.<member>
:
error TS2304: Cannot find name 'Plotly'
When I try
import Plotly = require('plotly.js');
I'm getting
error TS2307: Cannot find module 'plotly.js'.
typescript plotly
add a comment |
up vote
6
down vote
favorite
I'm trying to import plotly.js
into TypeScript. Plotly.js is installed using npm
. In my TypeScript file I use
import 'plotly.js';
It's OK but I'm getting error on code like Plotly.<member>
:
error TS2304: Cannot find name 'Plotly'
When I try
import Plotly = require('plotly.js');
I'm getting
error TS2307: Cannot find module 'plotly.js'.
typescript plotly
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I'm trying to import plotly.js
into TypeScript. Plotly.js is installed using npm
. In my TypeScript file I use
import 'plotly.js';
It's OK but I'm getting error on code like Plotly.<member>
:
error TS2304: Cannot find name 'Plotly'
When I try
import Plotly = require('plotly.js');
I'm getting
error TS2307: Cannot find module 'plotly.js'.
typescript plotly
I'm trying to import plotly.js
into TypeScript. Plotly.js is installed using npm
. In my TypeScript file I use
import 'plotly.js';
It's OK but I'm getting error on code like Plotly.<member>
:
error TS2304: Cannot find name 'Plotly'
When I try
import Plotly = require('plotly.js');
I'm getting
error TS2307: Cannot find module 'plotly.js'.
typescript plotly
typescript plotly
asked Aug 22 '16 at 16:26
eXavier
2,59612141
2,59612141
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
10
down vote
There is a Definitely Typed version available for plotly.js (Github link). This allows you to use the plain Javascript version inside Typescript. Follow these steps to setup your environment:
npm install --save plotly.js
npm install --save @types/plotly.js
Then in your code you can do the following (example taken from Github):
import * as Plotly from 'plotly.js';
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
Finally you need to convert the Typescript to javascript with tsc
and prepare the code to be included in you webpage with browserify
.
This also works now with ionic 2! Make sure you manually add the following packages:
npm install --save glslify
npm install --save mapbox-gl
2
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
– thewaywewere
Jun 16 '17 at 6:57
Thanks @thewaywewere! I've modified my answer accordingly.
– Ronald Ligteringen
Jun 16 '17 at 11:14
plotly.js can also be used in ionic 2
– Ronald Ligteringen
Jun 21 '17 at 10:58
I'm getting 404 errors using typed plotly: node_modules/plotly.js/src/traces/bar.js 404 (Not Found), node_modules/plotly.js/src/traces/box.js 404 (Not Found) and so on. Did you have this problem?
– 1gn1ter
Jul 26 '17 at 11:32
add a comment |
up vote
8
down vote
accepted
Ended up with:
declare function require(moduleName: string): any;
var Plotly = require('plotly.js/lib/index-basic.js');
(Referring to index-basic.js as I need just the basic functionality, use index.js for the full library.)
EDIT (Feb 2018):
The preferred way now is adding path into tsconfig.json
file, like this:
"compilerOptions": {
"paths": {
"plotly.js": [
"../node_modules/plotly.js/lib/core.js"
]
}
}
and then import in .ts
file like
import * as Plotly from 'plotly.js';
Note: in the path I included only core.js
, which contains scatter
graphs, you can import other graphs as you need.
(I'm using it with Angular CLI - Angular 5.2 and Typesript 2.7.1)
2
I am doingvar Plotly = require('plotly.js/dist/plotly.js')
which is also working fine. Do you know, how both are different?
– yugantar kumar
Sep 20 '17 at 18:41
2
@yugantarkumar I only include basic module (I needed only scatter graph). You inlcude the full library. Check this link github.com/plotly/plotly.js/blob/master/dist/README.md
– eXavier
Sep 21 '17 at 6:49
I only searched for this issue because Plotly.js was not working on IE11. @yugantarkumar version worked for me on IE11 but some error related tovertx
occured. The final solution for me was as @eXavier added (Feb 2018) but"../node_modules/plotly.js/lib/index-basic.js"
(core.js wasn't enough to render basic charts).
– Arthez
Mar 30 at 14:21
add a comment |
up vote
2
down vote
If the npm library you are importing does not provide type declarations itself, you'll have to import them yourself. The preferred tool is typings.
For plotly, it seems someone already made a declaration file. So once you have typings installed (npm i -g typings
) you can search for the type declaration of plotly (typings search plotly
). You'll see that there is a declaration file available on Definitely Typed. After you initialize typings for your project (typings init
), you can install the declaration file (typings i --global --save plotly.js
).
Notice the --global
. This specific declaration file is a global declaration file. This means that you don't have to import any types, as the types are available everywhere in your project. Eg.
// app.ts
let myPlotlyConfig: PlotlyConfig
Generally, you're right with the typings. The ones for Plotly are just in a very early stadium, just 1 function actually so it is not usable yet. Anyway +1
– eXavier
Aug 31 '16 at 11:25
add a comment |
up vote
2
down vote
TS support
npm install --save-dev @types/plotly.js`
or
yarn add --dev @types/plotly.js`
and
import { PlotData } from "plotly.js"; // or whatever you need
⚠️Don't install @types in dependencies since they are used exclusively for development purposes.
Problem
Since you add types for plotly.js, you expect to import stuff from plotly.js instead of a specific module. This is all good until you want to use it with react i.e. react-plotly + plotly.js ☹️
I know you didn't mention React explicitly, but I figured you or anyone else with a similar issue might find this useful.
I am not sure about your bundling approaches, however, I'm using Webpack 4 and currently investigating a react-plotly + plotly.js-basic-dist combination.
Possible combinations are:
react-plotly + plotly.js-basic-dist – my choice. You can add more bundles if needed- react-plotly + CDN
- react-plotly + custom bundle
More about customising plotly bundles.
This brings the bundle size down significantly and the sun shines upon us once again.
BUT...
Since this approach will require a factory and custom dist, these, as of this writing, don't have types
import Plotly from "plotly.js-basic-dist";
import createPlotlyComponent from "react-plotly.js/factory";
const Plot = createPlotlyComponent(Plotly);
FIX
Create a @types folder
Add typeRoots to tsconfig.json
N.B. The following approach did fix the types for the above imports, but I still have a failing compilation, which I didn't manage to fix following the accepted answer:
Hope you have a better experience!
UPDATE
I fixed the compiling issue. It was because react-plotly.js was being bundled in the 'dependencies`. For my case vendor deps are handled like this:
/**
* Given an array of files this will produce an object which contains the values for the vendor entry point
*/
function makeVendorEntry(config) {
const packageJson = require('../package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule =>
config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1);
return vendorModulesMinusExclusions;
}
exports.makeVendorEntry = makeVendorEntry;
Instead I moved react-plotly.js
to the devDependencies and it works fine now.
add a comment |
up vote
1
down vote
Install plotly.js using NPM:
npm install --save plotly.js
Then in component or service wherever you want to use import like:
import Plotly from 'plotly.js/lib/core';
Plotly.register([
require('plotly.js/lib/heatmap'),
require('plotly.js/lib/bar'),
require('plotly.js/lib/pie') // can include as many plot types as you want
]);
Use plotly lib as below:
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
add a comment |
up vote
0
down vote
plotly.js types usage with the new distribution packages
If you want to use typescript together with the partial npm bundles, starting in v1.39.0
, you can do the following:
As an example, lets assume we just want to use basic functionality of plotly and get along with scatter
, pie
and bar
charts.
So the distribution package plotly.js-basic-dist
is the interesting one (significant less bundle size):
npm install plotly.js-basic-dist
Install the @types for plotly:
npm install --save-dev @types/plotly.js
Then, in your project root tsconfig.json
simply add the following paths
mapping:
"baseUrl": "./",
"paths": {
"plotly.js-basic-dist": [
"node_modules/@types/plotly.js"
]
}
What does it do?
When Typescript resolves module import
statements like import Plotly from 'plotly.js-basic-dist'
, above path mapping is applied. With that, it actually behaves like import Plotly from '<baseUrl>/node_modules/@types/plotly.js'
and the compiler can grab the typings. At runtime plotly.js-basic-dist
still is served, typescript does not transform the source code via this mechanism.
baseUrl
must be specified if paths
is and makes all module imports with absolute names relative to it. When "."
is set as value, baseUrl
points at your project root directory containing tsconfig.json
. So if you have node_modules
directly under project root dir, as in most cases, that value should be appropriate (see here and this helpful stack answer).
Hope, that helps.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39084438%2fhow-to-import-plotly-js-into-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
There is a Definitely Typed version available for plotly.js (Github link). This allows you to use the plain Javascript version inside Typescript. Follow these steps to setup your environment:
npm install --save plotly.js
npm install --save @types/plotly.js
Then in your code you can do the following (example taken from Github):
import * as Plotly from 'plotly.js';
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
Finally you need to convert the Typescript to javascript with tsc
and prepare the code to be included in you webpage with browserify
.
This also works now with ionic 2! Make sure you manually add the following packages:
npm install --save glslify
npm install --save mapbox-gl
2
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
– thewaywewere
Jun 16 '17 at 6:57
Thanks @thewaywewere! I've modified my answer accordingly.
– Ronald Ligteringen
Jun 16 '17 at 11:14
plotly.js can also be used in ionic 2
– Ronald Ligteringen
Jun 21 '17 at 10:58
I'm getting 404 errors using typed plotly: node_modules/plotly.js/src/traces/bar.js 404 (Not Found), node_modules/plotly.js/src/traces/box.js 404 (Not Found) and so on. Did you have this problem?
– 1gn1ter
Jul 26 '17 at 11:32
add a comment |
up vote
10
down vote
There is a Definitely Typed version available for plotly.js (Github link). This allows you to use the plain Javascript version inside Typescript. Follow these steps to setup your environment:
npm install --save plotly.js
npm install --save @types/plotly.js
Then in your code you can do the following (example taken from Github):
import * as Plotly from 'plotly.js';
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
Finally you need to convert the Typescript to javascript with tsc
and prepare the code to be included in you webpage with browserify
.
This also works now with ionic 2! Make sure you manually add the following packages:
npm install --save glslify
npm install --save mapbox-gl
2
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
– thewaywewere
Jun 16 '17 at 6:57
Thanks @thewaywewere! I've modified my answer accordingly.
– Ronald Ligteringen
Jun 16 '17 at 11:14
plotly.js can also be used in ionic 2
– Ronald Ligteringen
Jun 21 '17 at 10:58
I'm getting 404 errors using typed plotly: node_modules/plotly.js/src/traces/bar.js 404 (Not Found), node_modules/plotly.js/src/traces/box.js 404 (Not Found) and so on. Did you have this problem?
– 1gn1ter
Jul 26 '17 at 11:32
add a comment |
up vote
10
down vote
up vote
10
down vote
There is a Definitely Typed version available for plotly.js (Github link). This allows you to use the plain Javascript version inside Typescript. Follow these steps to setup your environment:
npm install --save plotly.js
npm install --save @types/plotly.js
Then in your code you can do the following (example taken from Github):
import * as Plotly from 'plotly.js';
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
Finally you need to convert the Typescript to javascript with tsc
and prepare the code to be included in you webpage with browserify
.
This also works now with ionic 2! Make sure you manually add the following packages:
npm install --save glslify
npm install --save mapbox-gl
There is a Definitely Typed version available for plotly.js (Github link). This allows you to use the plain Javascript version inside Typescript. Follow these steps to setup your environment:
npm install --save plotly.js
npm install --save @types/plotly.js
Then in your code you can do the following (example taken from Github):
import * as Plotly from 'plotly.js';
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
Finally you need to convert the Typescript to javascript with tsc
and prepare the code to be included in you webpage with browserify
.
This also works now with ionic 2! Make sure you manually add the following packages:
npm install --save glslify
npm install --save mapbox-gl
edited Jun 21 '17 at 10:58
answered Jun 16 '17 at 6:30
Ronald Ligteringen
11116
11116
2
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
– thewaywewere
Jun 16 '17 at 6:57
Thanks @thewaywewere! I've modified my answer accordingly.
– Ronald Ligteringen
Jun 16 '17 at 11:14
plotly.js can also be used in ionic 2
– Ronald Ligteringen
Jun 21 '17 at 10:58
I'm getting 404 errors using typed plotly: node_modules/plotly.js/src/traces/bar.js 404 (Not Found), node_modules/plotly.js/src/traces/box.js 404 (Not Found) and so on. Did you have this problem?
– 1gn1ter
Jul 26 '17 at 11:32
add a comment |
2
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
– thewaywewere
Jun 16 '17 at 6:57
Thanks @thewaywewere! I've modified my answer accordingly.
– Ronald Ligteringen
Jun 16 '17 at 11:14
plotly.js can also be used in ionic 2
– Ronald Ligteringen
Jun 21 '17 at 10:58
I'm getting 404 errors using typed plotly: node_modules/plotly.js/src/traces/bar.js 404 (Not Found), node_modules/plotly.js/src/traces/box.js 404 (Not Found) and so on. Did you have this problem?
– 1gn1ter
Jul 26 '17 at 11:32
2
2
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
– thewaywewere
Jun 16 '17 at 6:57
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
– thewaywewere
Jun 16 '17 at 6:57
Thanks @thewaywewere! I've modified my answer accordingly.
– Ronald Ligteringen
Jun 16 '17 at 11:14
Thanks @thewaywewere! I've modified my answer accordingly.
– Ronald Ligteringen
Jun 16 '17 at 11:14
plotly.js can also be used in ionic 2
– Ronald Ligteringen
Jun 21 '17 at 10:58
plotly.js can also be used in ionic 2
– Ronald Ligteringen
Jun 21 '17 at 10:58
I'm getting 404 errors using typed plotly: node_modules/plotly.js/src/traces/bar.js 404 (Not Found), node_modules/plotly.js/src/traces/box.js 404 (Not Found) and so on. Did you have this problem?
– 1gn1ter
Jul 26 '17 at 11:32
I'm getting 404 errors using typed plotly: node_modules/plotly.js/src/traces/bar.js 404 (Not Found), node_modules/plotly.js/src/traces/box.js 404 (Not Found) and so on. Did you have this problem?
– 1gn1ter
Jul 26 '17 at 11:32
add a comment |
up vote
8
down vote
accepted
Ended up with:
declare function require(moduleName: string): any;
var Plotly = require('plotly.js/lib/index-basic.js');
(Referring to index-basic.js as I need just the basic functionality, use index.js for the full library.)
EDIT (Feb 2018):
The preferred way now is adding path into tsconfig.json
file, like this:
"compilerOptions": {
"paths": {
"plotly.js": [
"../node_modules/plotly.js/lib/core.js"
]
}
}
and then import in .ts
file like
import * as Plotly from 'plotly.js';
Note: in the path I included only core.js
, which contains scatter
graphs, you can import other graphs as you need.
(I'm using it with Angular CLI - Angular 5.2 and Typesript 2.7.1)
2
I am doingvar Plotly = require('plotly.js/dist/plotly.js')
which is also working fine. Do you know, how both are different?
– yugantar kumar
Sep 20 '17 at 18:41
2
@yugantarkumar I only include basic module (I needed only scatter graph). You inlcude the full library. Check this link github.com/plotly/plotly.js/blob/master/dist/README.md
– eXavier
Sep 21 '17 at 6:49
I only searched for this issue because Plotly.js was not working on IE11. @yugantarkumar version worked for me on IE11 but some error related tovertx
occured. The final solution for me was as @eXavier added (Feb 2018) but"../node_modules/plotly.js/lib/index-basic.js"
(core.js wasn't enough to render basic charts).
– Arthez
Mar 30 at 14:21
add a comment |
up vote
8
down vote
accepted
Ended up with:
declare function require(moduleName: string): any;
var Plotly = require('plotly.js/lib/index-basic.js');
(Referring to index-basic.js as I need just the basic functionality, use index.js for the full library.)
EDIT (Feb 2018):
The preferred way now is adding path into tsconfig.json
file, like this:
"compilerOptions": {
"paths": {
"plotly.js": [
"../node_modules/plotly.js/lib/core.js"
]
}
}
and then import in .ts
file like
import * as Plotly from 'plotly.js';
Note: in the path I included only core.js
, which contains scatter
graphs, you can import other graphs as you need.
(I'm using it with Angular CLI - Angular 5.2 and Typesript 2.7.1)
2
I am doingvar Plotly = require('plotly.js/dist/plotly.js')
which is also working fine. Do you know, how both are different?
– yugantar kumar
Sep 20 '17 at 18:41
2
@yugantarkumar I only include basic module (I needed only scatter graph). You inlcude the full library. Check this link github.com/plotly/plotly.js/blob/master/dist/README.md
– eXavier
Sep 21 '17 at 6:49
I only searched for this issue because Plotly.js was not working on IE11. @yugantarkumar version worked for me on IE11 but some error related tovertx
occured. The final solution for me was as @eXavier added (Feb 2018) but"../node_modules/plotly.js/lib/index-basic.js"
(core.js wasn't enough to render basic charts).
– Arthez
Mar 30 at 14:21
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
Ended up with:
declare function require(moduleName: string): any;
var Plotly = require('plotly.js/lib/index-basic.js');
(Referring to index-basic.js as I need just the basic functionality, use index.js for the full library.)
EDIT (Feb 2018):
The preferred way now is adding path into tsconfig.json
file, like this:
"compilerOptions": {
"paths": {
"plotly.js": [
"../node_modules/plotly.js/lib/core.js"
]
}
}
and then import in .ts
file like
import * as Plotly from 'plotly.js';
Note: in the path I included only core.js
, which contains scatter
graphs, you can import other graphs as you need.
(I'm using it with Angular CLI - Angular 5.2 and Typesript 2.7.1)
Ended up with:
declare function require(moduleName: string): any;
var Plotly = require('plotly.js/lib/index-basic.js');
(Referring to index-basic.js as I need just the basic functionality, use index.js for the full library.)
EDIT (Feb 2018):
The preferred way now is adding path into tsconfig.json
file, like this:
"compilerOptions": {
"paths": {
"plotly.js": [
"../node_modules/plotly.js/lib/core.js"
]
}
}
and then import in .ts
file like
import * as Plotly from 'plotly.js';
Note: in the path I included only core.js
, which contains scatter
graphs, you can import other graphs as you need.
(I'm using it with Angular CLI - Angular 5.2 and Typesript 2.7.1)
edited Feb 14 at 8:19
answered Aug 31 '16 at 11:30
eXavier
2,59612141
2,59612141
2
I am doingvar Plotly = require('plotly.js/dist/plotly.js')
which is also working fine. Do you know, how both are different?
– yugantar kumar
Sep 20 '17 at 18:41
2
@yugantarkumar I only include basic module (I needed only scatter graph). You inlcude the full library. Check this link github.com/plotly/plotly.js/blob/master/dist/README.md
– eXavier
Sep 21 '17 at 6:49
I only searched for this issue because Plotly.js was not working on IE11. @yugantarkumar version worked for me on IE11 but some error related tovertx
occured. The final solution for me was as @eXavier added (Feb 2018) but"../node_modules/plotly.js/lib/index-basic.js"
(core.js wasn't enough to render basic charts).
– Arthez
Mar 30 at 14:21
add a comment |
2
I am doingvar Plotly = require('plotly.js/dist/plotly.js')
which is also working fine. Do you know, how both are different?
– yugantar kumar
Sep 20 '17 at 18:41
2
@yugantarkumar I only include basic module (I needed only scatter graph). You inlcude the full library. Check this link github.com/plotly/plotly.js/blob/master/dist/README.md
– eXavier
Sep 21 '17 at 6:49
I only searched for this issue because Plotly.js was not working on IE11. @yugantarkumar version worked for me on IE11 but some error related tovertx
occured. The final solution for me was as @eXavier added (Feb 2018) but"../node_modules/plotly.js/lib/index-basic.js"
(core.js wasn't enough to render basic charts).
– Arthez
Mar 30 at 14:21
2
2
I am doing
var Plotly = require('plotly.js/dist/plotly.js')
which is also working fine. Do you know, how both are different?– yugantar kumar
Sep 20 '17 at 18:41
I am doing
var Plotly = require('plotly.js/dist/plotly.js')
which is also working fine. Do you know, how both are different?– yugantar kumar
Sep 20 '17 at 18:41
2
2
@yugantarkumar I only include basic module (I needed only scatter graph). You inlcude the full library. Check this link github.com/plotly/plotly.js/blob/master/dist/README.md
– eXavier
Sep 21 '17 at 6:49
@yugantarkumar I only include basic module (I needed only scatter graph). You inlcude the full library. Check this link github.com/plotly/plotly.js/blob/master/dist/README.md
– eXavier
Sep 21 '17 at 6:49
I only searched for this issue because Plotly.js was not working on IE11. @yugantarkumar version worked for me on IE11 but some error related to
vertx
occured. The final solution for me was as @eXavier added (Feb 2018) but "../node_modules/plotly.js/lib/index-basic.js"
(core.js wasn't enough to render basic charts).– Arthez
Mar 30 at 14:21
I only searched for this issue because Plotly.js was not working on IE11. @yugantarkumar version worked for me on IE11 but some error related to
vertx
occured. The final solution for me was as @eXavier added (Feb 2018) but "../node_modules/plotly.js/lib/index-basic.js"
(core.js wasn't enough to render basic charts).– Arthez
Mar 30 at 14:21
add a comment |
up vote
2
down vote
If the npm library you are importing does not provide type declarations itself, you'll have to import them yourself. The preferred tool is typings.
For plotly, it seems someone already made a declaration file. So once you have typings installed (npm i -g typings
) you can search for the type declaration of plotly (typings search plotly
). You'll see that there is a declaration file available on Definitely Typed. After you initialize typings for your project (typings init
), you can install the declaration file (typings i --global --save plotly.js
).
Notice the --global
. This specific declaration file is a global declaration file. This means that you don't have to import any types, as the types are available everywhere in your project. Eg.
// app.ts
let myPlotlyConfig: PlotlyConfig
Generally, you're right with the typings. The ones for Plotly are just in a very early stadium, just 1 function actually so it is not usable yet. Anyway +1
– eXavier
Aug 31 '16 at 11:25
add a comment |
up vote
2
down vote
If the npm library you are importing does not provide type declarations itself, you'll have to import them yourself. The preferred tool is typings.
For plotly, it seems someone already made a declaration file. So once you have typings installed (npm i -g typings
) you can search for the type declaration of plotly (typings search plotly
). You'll see that there is a declaration file available on Definitely Typed. After you initialize typings for your project (typings init
), you can install the declaration file (typings i --global --save plotly.js
).
Notice the --global
. This specific declaration file is a global declaration file. This means that you don't have to import any types, as the types are available everywhere in your project. Eg.
// app.ts
let myPlotlyConfig: PlotlyConfig
Generally, you're right with the typings. The ones for Plotly are just in a very early stadium, just 1 function actually so it is not usable yet. Anyway +1
– eXavier
Aug 31 '16 at 11:25
add a comment |
up vote
2
down vote
up vote
2
down vote
If the npm library you are importing does not provide type declarations itself, you'll have to import them yourself. The preferred tool is typings.
For plotly, it seems someone already made a declaration file. So once you have typings installed (npm i -g typings
) you can search for the type declaration of plotly (typings search plotly
). You'll see that there is a declaration file available on Definitely Typed. After you initialize typings for your project (typings init
), you can install the declaration file (typings i --global --save plotly.js
).
Notice the --global
. This specific declaration file is a global declaration file. This means that you don't have to import any types, as the types are available everywhere in your project. Eg.
// app.ts
let myPlotlyConfig: PlotlyConfig
If the npm library you are importing does not provide type declarations itself, you'll have to import them yourself. The preferred tool is typings.
For plotly, it seems someone already made a declaration file. So once you have typings installed (npm i -g typings
) you can search for the type declaration of plotly (typings search plotly
). You'll see that there is a declaration file available on Definitely Typed. After you initialize typings for your project (typings init
), you can install the declaration file (typings i --global --save plotly.js
).
Notice the --global
. This specific declaration file is a global declaration file. This means that you don't have to import any types, as the types are available everywhere in your project. Eg.
// app.ts
let myPlotlyConfig: PlotlyConfig
edited Aug 22 '16 at 17:33
answered Aug 22 '16 at 16:58
Pelle Jacobs
1,040914
1,040914
Generally, you're right with the typings. The ones for Plotly are just in a very early stadium, just 1 function actually so it is not usable yet. Anyway +1
– eXavier
Aug 31 '16 at 11:25
add a comment |
Generally, you're right with the typings. The ones for Plotly are just in a very early stadium, just 1 function actually so it is not usable yet. Anyway +1
– eXavier
Aug 31 '16 at 11:25
Generally, you're right with the typings. The ones for Plotly are just in a very early stadium, just 1 function actually so it is not usable yet. Anyway +1
– eXavier
Aug 31 '16 at 11:25
Generally, you're right with the typings. The ones for Plotly are just in a very early stadium, just 1 function actually so it is not usable yet. Anyway +1
– eXavier
Aug 31 '16 at 11:25
add a comment |
up vote
2
down vote
TS support
npm install --save-dev @types/plotly.js`
or
yarn add --dev @types/plotly.js`
and
import { PlotData } from "plotly.js"; // or whatever you need
⚠️Don't install @types in dependencies since they are used exclusively for development purposes.
Problem
Since you add types for plotly.js, you expect to import stuff from plotly.js instead of a specific module. This is all good until you want to use it with react i.e. react-plotly + plotly.js ☹️
I know you didn't mention React explicitly, but I figured you or anyone else with a similar issue might find this useful.
I am not sure about your bundling approaches, however, I'm using Webpack 4 and currently investigating a react-plotly + plotly.js-basic-dist combination.
Possible combinations are:
react-plotly + plotly.js-basic-dist – my choice. You can add more bundles if needed- react-plotly + CDN
- react-plotly + custom bundle
More about customising plotly bundles.
This brings the bundle size down significantly and the sun shines upon us once again.
BUT...
Since this approach will require a factory and custom dist, these, as of this writing, don't have types
import Plotly from "plotly.js-basic-dist";
import createPlotlyComponent from "react-plotly.js/factory";
const Plot = createPlotlyComponent(Plotly);
FIX
Create a @types folder
Add typeRoots to tsconfig.json
N.B. The following approach did fix the types for the above imports, but I still have a failing compilation, which I didn't manage to fix following the accepted answer:
Hope you have a better experience!
UPDATE
I fixed the compiling issue. It was because react-plotly.js was being bundled in the 'dependencies`. For my case vendor deps are handled like this:
/**
* Given an array of files this will produce an object which contains the values for the vendor entry point
*/
function makeVendorEntry(config) {
const packageJson = require('../package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule =>
config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1);
return vendorModulesMinusExclusions;
}
exports.makeVendorEntry = makeVendorEntry;
Instead I moved react-plotly.js
to the devDependencies and it works fine now.
add a comment |
up vote
2
down vote
TS support
npm install --save-dev @types/plotly.js`
or
yarn add --dev @types/plotly.js`
and
import { PlotData } from "plotly.js"; // or whatever you need
⚠️Don't install @types in dependencies since they are used exclusively for development purposes.
Problem
Since you add types for plotly.js, you expect to import stuff from plotly.js instead of a specific module. This is all good until you want to use it with react i.e. react-plotly + plotly.js ☹️
I know you didn't mention React explicitly, but I figured you or anyone else with a similar issue might find this useful.
I am not sure about your bundling approaches, however, I'm using Webpack 4 and currently investigating a react-plotly + plotly.js-basic-dist combination.
Possible combinations are:
react-plotly + plotly.js-basic-dist – my choice. You can add more bundles if needed- react-plotly + CDN
- react-plotly + custom bundle
More about customising plotly bundles.
This brings the bundle size down significantly and the sun shines upon us once again.
BUT...
Since this approach will require a factory and custom dist, these, as of this writing, don't have types
import Plotly from "plotly.js-basic-dist";
import createPlotlyComponent from "react-plotly.js/factory";
const Plot = createPlotlyComponent(Plotly);
FIX
Create a @types folder
Add typeRoots to tsconfig.json
N.B. The following approach did fix the types for the above imports, but I still have a failing compilation, which I didn't manage to fix following the accepted answer:
Hope you have a better experience!
UPDATE
I fixed the compiling issue. It was because react-plotly.js was being bundled in the 'dependencies`. For my case vendor deps are handled like this:
/**
* Given an array of files this will produce an object which contains the values for the vendor entry point
*/
function makeVendorEntry(config) {
const packageJson = require('../package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule =>
config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1);
return vendorModulesMinusExclusions;
}
exports.makeVendorEntry = makeVendorEntry;
Instead I moved react-plotly.js
to the devDependencies and it works fine now.
add a comment |
up vote
2
down vote
up vote
2
down vote
TS support
npm install --save-dev @types/plotly.js`
or
yarn add --dev @types/plotly.js`
and
import { PlotData } from "plotly.js"; // or whatever you need
⚠️Don't install @types in dependencies since they are used exclusively for development purposes.
Problem
Since you add types for plotly.js, you expect to import stuff from plotly.js instead of a specific module. This is all good until you want to use it with react i.e. react-plotly + plotly.js ☹️
I know you didn't mention React explicitly, but I figured you or anyone else with a similar issue might find this useful.
I am not sure about your bundling approaches, however, I'm using Webpack 4 and currently investigating a react-plotly + plotly.js-basic-dist combination.
Possible combinations are:
react-plotly + plotly.js-basic-dist – my choice. You can add more bundles if needed- react-plotly + CDN
- react-plotly + custom bundle
More about customising plotly bundles.
This brings the bundle size down significantly and the sun shines upon us once again.
BUT...
Since this approach will require a factory and custom dist, these, as of this writing, don't have types
import Plotly from "plotly.js-basic-dist";
import createPlotlyComponent from "react-plotly.js/factory";
const Plot = createPlotlyComponent(Plotly);
FIX
Create a @types folder
Add typeRoots to tsconfig.json
N.B. The following approach did fix the types for the above imports, but I still have a failing compilation, which I didn't manage to fix following the accepted answer:
Hope you have a better experience!
UPDATE
I fixed the compiling issue. It was because react-plotly.js was being bundled in the 'dependencies`. For my case vendor deps are handled like this:
/**
* Given an array of files this will produce an object which contains the values for the vendor entry point
*/
function makeVendorEntry(config) {
const packageJson = require('../package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule =>
config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1);
return vendorModulesMinusExclusions;
}
exports.makeVendorEntry = makeVendorEntry;
Instead I moved react-plotly.js
to the devDependencies and it works fine now.
TS support
npm install --save-dev @types/plotly.js`
or
yarn add --dev @types/plotly.js`
and
import { PlotData } from "plotly.js"; // or whatever you need
⚠️Don't install @types in dependencies since they are used exclusively for development purposes.
Problem
Since you add types for plotly.js, you expect to import stuff from plotly.js instead of a specific module. This is all good until you want to use it with react i.e. react-plotly + plotly.js ☹️
I know you didn't mention React explicitly, but I figured you or anyone else with a similar issue might find this useful.
I am not sure about your bundling approaches, however, I'm using Webpack 4 and currently investigating a react-plotly + plotly.js-basic-dist combination.
Possible combinations are:
react-plotly + plotly.js-basic-dist – my choice. You can add more bundles if needed- react-plotly + CDN
- react-plotly + custom bundle
More about customising plotly bundles.
This brings the bundle size down significantly and the sun shines upon us once again.
BUT...
Since this approach will require a factory and custom dist, these, as of this writing, don't have types
import Plotly from "plotly.js-basic-dist";
import createPlotlyComponent from "react-plotly.js/factory";
const Plot = createPlotlyComponent(Plotly);
FIX
Create a @types folder
Add typeRoots to tsconfig.json
N.B. The following approach did fix the types for the above imports, but I still have a failing compilation, which I didn't manage to fix following the accepted answer:
Hope you have a better experience!
UPDATE
I fixed the compiling issue. It was because react-plotly.js was being bundled in the 'dependencies`. For my case vendor deps are handled like this:
/**
* Given an array of files this will produce an object which contains the values for the vendor entry point
*/
function makeVendorEntry(config) {
const packageJson = require('../package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule =>
config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1);
return vendorModulesMinusExclusions;
}
exports.makeVendorEntry = makeVendorEntry;
Instead I moved react-plotly.js
to the devDependencies and it works fine now.
/**
* Given an array of files this will produce an object which contains the values for the vendor entry point
*/
function makeVendorEntry(config) {
const packageJson = require('../package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule =>
config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1);
return vendorModulesMinusExclusions;
}
exports.makeVendorEntry = makeVendorEntry;
/**
* Given an array of files this will produce an object which contains the values for the vendor entry point
*/
function makeVendorEntry(config) {
const packageJson = require('../package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule =>
config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1);
return vendorModulesMinusExclusions;
}
exports.makeVendorEntry = makeVendorEntry;
edited Oct 25 at 14:26
answered Oct 25 at 10:56
Roland Jegorov
309416
309416
add a comment |
add a comment |
up vote
1
down vote
Install plotly.js using NPM:
npm install --save plotly.js
Then in component or service wherever you want to use import like:
import Plotly from 'plotly.js/lib/core';
Plotly.register([
require('plotly.js/lib/heatmap'),
require('plotly.js/lib/bar'),
require('plotly.js/lib/pie') // can include as many plot types as you want
]);
Use plotly lib as below:
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
add a comment |
up vote
1
down vote
Install plotly.js using NPM:
npm install --save plotly.js
Then in component or service wherever you want to use import like:
import Plotly from 'plotly.js/lib/core';
Plotly.register([
require('plotly.js/lib/heatmap'),
require('plotly.js/lib/bar'),
require('plotly.js/lib/pie') // can include as many plot types as you want
]);
Use plotly lib as below:
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
add a comment |
up vote
1
down vote
up vote
1
down vote
Install plotly.js using NPM:
npm install --save plotly.js
Then in component or service wherever you want to use import like:
import Plotly from 'plotly.js/lib/core';
Plotly.register([
require('plotly.js/lib/heatmap'),
require('plotly.js/lib/bar'),
require('plotly.js/lib/pie') // can include as many plot types as you want
]);
Use plotly lib as below:
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
Install plotly.js using NPM:
npm install --save plotly.js
Then in component or service wherever you want to use import like:
import Plotly from 'plotly.js/lib/core';
Plotly.register([
require('plotly.js/lib/heatmap'),
require('plotly.js/lib/bar'),
require('plotly.js/lib/pie') // can include as many plot types as you want
]);
Use plotly lib as below:
const data: Plotly.BarData = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('test', data);
answered Dec 18 '17 at 16:37
Priya Gupta
191
191
add a comment |
add a comment |
up vote
0
down vote
plotly.js types usage with the new distribution packages
If you want to use typescript together with the partial npm bundles, starting in v1.39.0
, you can do the following:
As an example, lets assume we just want to use basic functionality of plotly and get along with scatter
, pie
and bar
charts.
So the distribution package plotly.js-basic-dist
is the interesting one (significant less bundle size):
npm install plotly.js-basic-dist
Install the @types for plotly:
npm install --save-dev @types/plotly.js
Then, in your project root tsconfig.json
simply add the following paths
mapping:
"baseUrl": "./",
"paths": {
"plotly.js-basic-dist": [
"node_modules/@types/plotly.js"
]
}
What does it do?
When Typescript resolves module import
statements like import Plotly from 'plotly.js-basic-dist'
, above path mapping is applied. With that, it actually behaves like import Plotly from '<baseUrl>/node_modules/@types/plotly.js'
and the compiler can grab the typings. At runtime plotly.js-basic-dist
still is served, typescript does not transform the source code via this mechanism.
baseUrl
must be specified if paths
is and makes all module imports with absolute names relative to it. When "."
is set as value, baseUrl
points at your project root directory containing tsconfig.json
. So if you have node_modules
directly under project root dir, as in most cases, that value should be appropriate (see here and this helpful stack answer).
Hope, that helps.
add a comment |
up vote
0
down vote
plotly.js types usage with the new distribution packages
If you want to use typescript together with the partial npm bundles, starting in v1.39.0
, you can do the following:
As an example, lets assume we just want to use basic functionality of plotly and get along with scatter
, pie
and bar
charts.
So the distribution package plotly.js-basic-dist
is the interesting one (significant less bundle size):
npm install plotly.js-basic-dist
Install the @types for plotly:
npm install --save-dev @types/plotly.js
Then, in your project root tsconfig.json
simply add the following paths
mapping:
"baseUrl": "./",
"paths": {
"plotly.js-basic-dist": [
"node_modules/@types/plotly.js"
]
}
What does it do?
When Typescript resolves module import
statements like import Plotly from 'plotly.js-basic-dist'
, above path mapping is applied. With that, it actually behaves like import Plotly from '<baseUrl>/node_modules/@types/plotly.js'
and the compiler can grab the typings. At runtime plotly.js-basic-dist
still is served, typescript does not transform the source code via this mechanism.
baseUrl
must be specified if paths
is and makes all module imports with absolute names relative to it. When "."
is set as value, baseUrl
points at your project root directory containing tsconfig.json
. So if you have node_modules
directly under project root dir, as in most cases, that value should be appropriate (see here and this helpful stack answer).
Hope, that helps.
add a comment |
up vote
0
down vote
up vote
0
down vote
plotly.js types usage with the new distribution packages
If you want to use typescript together with the partial npm bundles, starting in v1.39.0
, you can do the following:
As an example, lets assume we just want to use basic functionality of plotly and get along with scatter
, pie
and bar
charts.
So the distribution package plotly.js-basic-dist
is the interesting one (significant less bundle size):
npm install plotly.js-basic-dist
Install the @types for plotly:
npm install --save-dev @types/plotly.js
Then, in your project root tsconfig.json
simply add the following paths
mapping:
"baseUrl": "./",
"paths": {
"plotly.js-basic-dist": [
"node_modules/@types/plotly.js"
]
}
What does it do?
When Typescript resolves module import
statements like import Plotly from 'plotly.js-basic-dist'
, above path mapping is applied. With that, it actually behaves like import Plotly from '<baseUrl>/node_modules/@types/plotly.js'
and the compiler can grab the typings. At runtime plotly.js-basic-dist
still is served, typescript does not transform the source code via this mechanism.
baseUrl
must be specified if paths
is and makes all module imports with absolute names relative to it. When "."
is set as value, baseUrl
points at your project root directory containing tsconfig.json
. So if you have node_modules
directly under project root dir, as in most cases, that value should be appropriate (see here and this helpful stack answer).
Hope, that helps.
plotly.js types usage with the new distribution packages
If you want to use typescript together with the partial npm bundles, starting in v1.39.0
, you can do the following:
As an example, lets assume we just want to use basic functionality of plotly and get along with scatter
, pie
and bar
charts.
So the distribution package plotly.js-basic-dist
is the interesting one (significant less bundle size):
npm install plotly.js-basic-dist
Install the @types for plotly:
npm install --save-dev @types/plotly.js
Then, in your project root tsconfig.json
simply add the following paths
mapping:
"baseUrl": "./",
"paths": {
"plotly.js-basic-dist": [
"node_modules/@types/plotly.js"
]
}
What does it do?
When Typescript resolves module import
statements like import Plotly from 'plotly.js-basic-dist'
, above path mapping is applied. With that, it actually behaves like import Plotly from '<baseUrl>/node_modules/@types/plotly.js'
and the compiler can grab the typings. At runtime plotly.js-basic-dist
still is served, typescript does not transform the source code via this mechanism.
baseUrl
must be specified if paths
is and makes all module imports with absolute names relative to it. When "."
is set as value, baseUrl
points at your project root directory containing tsconfig.json
. So if you have node_modules
directly under project root dir, as in most cases, that value should be appropriate (see here and this helpful stack answer).
Hope, that helps.
edited Nov 16 at 13:26
answered Nov 11 at 17:07
ford04
112211
112211
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39084438%2fhow-to-import-plotly-js-into-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown