There are two Source files that come in the downloaded Zip folder
Download CanvasJS library from here. Extract the downloaded file and put the content in the same folder as the html file.
Include the file inside the <head> tag of your web page.
You can Include Any of the above two Javascript files. While its ok to use uncompressed version on the development machine, its recommended to use compressed version in the production environment.
<head> <script type="text/javascript" src="canvasjs.min.js"></script> </head>
Create an HTML file in the same directory as the library and copy the following code into it.
<!DOCTYPE HTML> <html> <head> <script type="text/javascript" src="canvasjs.min.js"></script> <script type="text/javascript"> window.onload = function () { var chart = new CanvasJS.Chart("chartContainer", { data: [ { type: "column", dataPoints: [ { x: 10, y: 10 }, { x: 20, y: 15 }, { x: 30, y: 25 }, { x: 40, y: 30 }, { x: 50, y: 28 } ] } ] }); chart.render(); } </script> </head> <body> <div id="chartContainer" style="height: 500px; width: 50%;"></div> </body> </html>
Save the file, and Open it in a Browser. A Column Chart Should Render on the browser.
Install CanvasJS Charts Package from NPM package manager.
npm install @canvasjs/charts
As CanvasJS Charts package is installed, it can be imported using different module formats like AMD, CommonJS, etc.
import CanvasJS from '@canvasjs/charts';
Define a div (chart-container) where chart has to be rendered.
<div id="chartContainer"></div>
Create chart by passing the container-id of the div that you created in the previous step and chart-options. Once the chart is created, render the chart by calling chart.render();
//Create Chart var chart = new CanvasJS.Chart("container", { //Chart Options - Check https://canvasjs.com/docs/charts/chart-options/ title:{ text: "Basic Column Chart in JavaScript" }, data: [{ type: "column", dataPoints: [ { label: "apple", y: 10 }, { label: "orange", y: 15 }, { label: "banana", y: 25 }, { label: "mango", y: 30 }, { label: "grape", y: 28 } ] }] }); //Render Chart chart.render();
25 Comments
Hi Sunil,
I want to pass the “chartContainer” dynamically, how can i do that, when i pass it as a varibale its throwing a error
Chart Container Cannot be set dynamically. It should be set in the constructor itself.
Hi,
I am integrating canvasjs charts in oracle apex plugins. I would like to pass the container ID that is system-generated to the CavasJS.Chart. Is there any workaround for this?
// Create and draw the chart
function renderChart() {
var chart = new CanvasJS.Chart( pRegionId, {
theme: “theme2”, // Options: “theme1″,”theme2″, “theme3″
exportEnabled: true,
animationEnabled: true,
title:{
text: “Simple Column Chart”
},
subtitles:[
{ text: “This is a Subtitle”
//Uncomment properties below to see how they behave
//fontColor: “red”,
//fontSize: 30
}
],
axisX:{
title:”Axis X title”,
},
axisY:{
title:”Axis Y Title”,
},
legend: {
horizontalAlign: “left”, // “center” , “right”
verticalAlign: “center”, // “top” , “bottom”
fontSize: 15
},
data: [
{
type: “column”, //change type to bar, line, area, pie, etc
indexLabel: “{y}”,
indexLabelPlacement: “outside”,
indexLabelOrientation: “horizontal”,
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 },
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14 }
]
}
]
});
chart.render();
}
Do the names have to be *exactly* chart, chartcontainer, or can they be different names? So that instead of chart, it could be foo, and with multiple charts on one page, it could be foo1, foocontainer1, foo2, foocontainer2, as suggested here: http://jsfiddle.net/canvasjs/U6DAe/.
Thank you.
You can use any name that you prefer. We are using chart & chartContainer everywhere just to have some consistency
I’ve got 2 “div”, and I want to put 2 charts in that 2 “div”. However, only the first chart can be displayed, the second one cannot be loaded. How can i solve it?
Here is an example http://jsfiddle.net/canvasjs/U6DAe/
Oh… I work it out! I just forgot to change the “id” of “div”…lol
SCRIPT5007: Unable to get property ‘style’ of undefined or null reference
canvasjs.min.js, line 56 character 54
if(“none”!==this._toolBar.style.display)
error occurs here, due to _toolBar is undefined
Can you please create a new thread in forum with a JSFiddle that can help us reproduce the issue? Also please let us know your browser, browser version and OS.
Did you find any solution to this problem? or created any new thread?
Gampala,
We are not getting this error at our end so not able to reproduce the issue. If you are getting the same error, please create a jsfiddle with your code so that we can have a look.
Can I use XML datapoints instead of JSON ? if yes can you please provide me an example
Alim,
Yes, it is possible to create charts from XML data. We’ll update the documentation with an example in couple of days.
I managed to get values from a xml file in my graph give me your mail to send you my solution if you want
alim.ben.helal@gmail.com
I can’t seem to find the documentation to load the data via XML instead of JSON. Can you point me in the right direction?
Possible to use images as labels? If so, how?
Foxhoundn,
This feature is not supported yet.
this is responsive?
pedro,
Yes, whenever the browser’s size changes chart resizes itself based on the container’s new width or height. You can also explicitly call chart.render() to update its size.
it drives me nuts, I am getting Uncaught TypeError: $(…).CanvasJSChart is not a function although I don’t have any errors on the link to your files so my HTML file sees it alright. I am about to give up. This is basic install problem, you guys should make sure those things don’t happen. There is no way I can troubleshoot it.
Alex,
We can surely help you with this. Can you please create a jsfiddle with your code so that we can have a look? And also please mention the Browser & its Version.
M getting same error….so what is the solution?
Pradnya,
We’ll surely help you with the issue if you can reproduce the problem using jsfiddle. Without looking into the code its hard to guess the problem. Also let us know your browser and os.