{"id":16460,"date":"2023-04-11T18:24:21","date_gmt":"2023-04-11T18:24:21","guid":{"rendered":"https:\/\/www.anychart.com\/blog\/?p=16460"},"modified":"2023-06-15T18:32:49","modified_gmt":"2023-06-15T18:32:49","slug":"3d-surface-plot","status":"publish","type":"post","link":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/","title":{"rendered":"Building 3D Surface Plot in JavaScript"},"content":{"rendered":"<p><a href=\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\"><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot.png\" alt=\"3D Surface Plot in JavaScript\" width=\"100%\" class=\"alignnone size-full wp-image-16469\" \/><\/a>Welcome to this <a href=\"https:\/\/www.anychart.com\/blog\/category\/javascript-chart-tutorials\/\">tutorial<\/a> on creating a visually stunning and interactive <strong>3D surface plot<\/strong> using <strong>JavaScript<\/strong>! If you&#8217;re passionate about <a href=\"https:\/\/www.anychart.com\/blog\/2018\/11\/20\/data-visualization-definition-history-examples\/\">data visualization<\/a> and want to expand your skills to the next level, this guide is for you. Here, we&#8217;ll take you through a step-by-step process to create an engaging 3D surface plot that will make your data come alive.<\/p>\n<p>Sometimes, representing complex information using traditional 2D charts can be difficult, especially when working with large datasets or data that varies over three or more dimensions. This is where a 3D surface plot (or surface chart) can prove to be <a href=\"https:\/\/www.anychart.com\/chartopedia\/chart-type\/surface-chart\/\">useful<\/a>. In this tutorial, we&#8217;ll be using real-world population data to create our surface plot, showcasing the <strong>population growth trends of the top 50 most populous countries<\/strong> in the world. You don&#8217;t need to be a data expert to follow this guide, as we&#8217;ll cover everything in detail.<\/p>\n<p>So, sit back, grab your favorite beverage, and let&#8217;s dive into the world of data visualization with a 3D surface plot. By the end of this tutorial, you&#8217;ll have an impressive chart that you can showcase to your colleagues or friends. Let&#8217;s get started!<\/p>\n<p><!--more Read the JS charting tutorial \u00bb--><\/p>\n<h2>3D Surface Plot That Will Be Created<\/h2>\n<p>In this tutorial, I will guide you through creating a beautiful custom 3D surface plot using JavaScript. By following the step-by-step instructions will give you the skills to build your own visually stunning charts. The result will be an impressive surface chart that you can use to showcase your data meaningfully.<\/p>\n<p>Look at the example chart below to see what you&#8217;ll be creating by the end of this tutorial. So let&#8217;s get started and create something amazing together!<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/surface-chart-preview.png\" alt=\"3D Surface Plot Preview\" width=\"100%\" class=\"alignnone size-full wp-image-16468\" srcset=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/surface-chart-preview.png 1342w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/surface-chart-preview-300x167.png 300w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/surface-chart-preview-768x428.png 768w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/surface-chart-preview-1024x571.png 1024w\" sizes=\"(max-width: 1342px) 100vw, 1342px\" \/><\/p>\n<h2>A. How to Create Basic JS 3D Surface Plot<\/h2>\n<p>Creating a custom 3D surface plot in JavaScript is easier than you might think! It involves the following four fundamental steps:<\/p>\n<ol>\n<li>Create a basic webpage in HTML<\/li>\n<li>Include the required JavaScript files<\/li>\n<li>Add the data<\/li>\n<li>Write a simple JS charting code<\/li>\n<\/ol>\n<p>And voila! You&#8217;ll have a stunning surface chart. Let\u2019s dive deeper into each of these steps.<\/p>\n<h3>1. Create a basic webpage in HTML<\/h3>\n<p>First, we need to whip up a simple HTML webpage. Then, we add a <code>&lt;div&gt;<\/code> element and assign it a unique ID to create a special spot for our 3D surface plot to shine. To make sure the chart fills the entire page, we include CSS code within the <code>&lt;style&gt;<\/code> tag with the width and height set to 100%. Of course, you can always customize the size and shape of your chart to your heart&#8217;s content.<\/p>\n<pre class=\"html\"><code>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;title&gt;JavaScript 3D Surface Plot&lt;\/title&gt;\r\n    &lt;style type=\"text\/css\"&gt;\r\n      html, body, #container {\r\n        width: 100%;\r\n        height: 100%;\r\n        margin: 0;\r\n        padding: 0;\r\n      }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>2. Include the required JavaScript files<\/h3>\n<p>Now, we&#8217;ll add the necessary JavaScript files in the <code>&lt;head&gt;<\/code> section. Luckily, many charting libraries are available that simplify the process of visualizing data. I personally love <a href=\"https:\/\/www.anychart.com\/\">AnyChart<\/a>, as it supports 3D surface plots with its comprehensive <a href=\"https:\/\/docs.anychart.com\" target=\"_blank\" rel=\"nofollow\">documentation<\/a> and is free for non-profit use. Regardless of which library you choose, the basic steps for chart creation remain the same. For the 3D surface plot, we need to include the Core, Surface, and Data Adapter <a href=\"https:\/\/docs.anychart.com\/Quick_Start\/Modules\" target=\"_blank\" rel=\"nofollow\">modules<\/a> in your project.<\/p>\n<pre class=\"html\"><code>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;title&gt;JavaScript 3D surface plot&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-surface.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-data-adapter.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;style type=\"text\/css\"&gt;\r\n      html, body, #container {\r\n        width: 100%;\r\n        height: 100%;\r\n        margin: 0;\r\n        padding: 0;\r\n      }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n      \/\/ All the code for the 3D surface plot will come here.\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>3. Add the data<\/h3>\n<p>For this chart, we&#8217;ll be using data from <a href=\"https:\/\/data.worldbank.org\/indicator\/SP.POP.TOTL?fbclid=IwAR3Sa1zCRdz7wa9N_ODwd-GczCmSi1NbuKx3kb24mm7XTnDPNuwQeshMROI\" target=\"_blank\" rel=\"nofollow\">The World Bank<\/a>.<\/p>\n<p>I&#8217;ve already saved the data in a JSON file, which you can find <a href=\"https:\/\/gist.githubusercontent.com\/awanshrestha\/9d0f4c864817d5ee043126f63f1f4d02\/raw\/91cb71ac4159ce86bbe2eb204c190f1e9ce43ba6\/surface-test.json\" target=\"_blank\" rel=\"nofollow\">here<\/a>. However, I&#8217;ve excluded the data of China and India since their large populations skew the chart data.<\/p>\n<p>To load the data into our surface chart, we&#8217;ll use the <code>anychart.data.loadJsonFile()<\/code> function. Here&#8217;s the code we\u2019ll be using.<\/p>\n<pre class=\"javascript\"><code>anychart.data.loadJsonFile('https:\/\/gist.githubusercontent.com\/awanshrestha\/9d0f4c864817d5ee043126f63f1f4d02\/raw\/91cb71ac4159ce86bbe2eb204c190f1e9ce43ba6\/surface-test.json',);<\/code><\/pre>\n<h3>4. Write a simple JS charting code<\/h3>\n<p>Creating a 3D surface plot is simpler than you might think. With just a few lines of code, you can build your chart in no time when using a good JavaScript charting library. Start by enclosing all the necessary code within an <code>anychart.onDocumentReady()<\/code> function, which ensures that it will only execute once the page is fully loaded.<\/p>\n<pre class=\"html\"><code>&lt;script&gt;\r\n  anychart.onDocumentReady(function () {\r\n    \/\/ The JS 3D surface plot data and code will be in this section.\r\n  });\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>We&#8217;ll add the data from Step 3 and create a dataset result. Then, we&#8217;ll use the <code>anychart.data.loadJsonFile()<\/code> method to load the JSON data from the file and execute a callback function when the data is available. The <code>function (data) { }<\/code> is the callback function that takes the loaded JSON data as its parameter and processes it into a format that the surface chart can understand.<\/p>\n<p>Next, we&#8217;ll pass the result as a parameter to the <code>anychart.surface()<\/code> function to create a 3D surface plot object. This object can be customized and displayed on the page using JavaScript.<\/p>\n<pre class=\"html\"><code>&lt;script&gt;\r\n  anychart.onDocumentReady(function () {\r\n    anychart.data.loadJsonFile('https:\/\/gist.githubusercontent.com\/awanshrestha\/9d0f4c864817d5ee043126f63f1f4d02\/raw\/91cb71ac4159ce86bbe2eb204c190f1e9ce43ba6\/surface-test.json',\r\n      function (data) {\r\n        \/\/ pre-processing of the data\r\n        var result = [];\r\n        for (var x = 0; x &lt; data.x.length; x++) {\r\n          for (var y = 0; y &lt; data.y.length; y++) {\r\n            result.push([x, data.y[y], data.z[x][y]]);\r\n          }\r\n        }\r\n      }\r\n    );\r\n  });\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>We&#8217;ll use the <code>surface()<\/code> function to create our chart, passing the result dataset into it.<\/p>\n<pre class=\"javascript\"><code>var chart = anychart.surface(result);<\/code><\/pre>\n<p>The labels of all axes are enabled by default and can be formatted using the <code>format()<\/code> parameter of the <code>labels()<\/code> method.<\/p>\n<pre class=\"javascript\"><code>chart\r\n  .xAxis()\r\n  .labels()\r\n  .format(function () {\r\n    return data.x[Math.round(this.value)];\r\n  });<\/code><\/pre>\n<p>We can set the axis scale using the appropriate constructor, as shown in the code snippet.<\/p>\n<pre class=\"javascript\"><code>chart.xScale().maximum(data.x.length - 1);<\/code><\/pre>\n<p>We&#8217;ll also set the chart paddings using the <code>padding()<\/code> function.<\/p>\n<pre class=\"javascript\"><code>chart.padding(50, 50, 50, 75);<\/code><\/pre>\n<p>Additionally, we can rotate the 3D surface plot by dragging it with the help of a mouse. We can even set the angles of rotation using <code>rotationY()<\/code> and <code>rotationZ()<\/code> methods.<\/p>\n<pre class=\"javascript\"><code>\/\/ set the default chart rotation by the y-axis\r\nchart.rotationY(40);\r\n\r\n\/\/ set the default chart rotation by the z-axis\r\nchart.rotationZ(130);<\/code><\/pre>\n<p>Finally, we&#8217;ll set the title of the chart and place it into the <code>&lt;div&gt;<\/code> container that was previously specified. We&#8217;ll display it by using the <code>draw()<\/code> function.<\/p>\n<pre class=\"javascript\"><code>\/\/ set the chart title\r\nchart.title('Population Growth of Top 50 Most Populous Countries Over Past 50 Years');\r\n\r\n\/\/ set the container id for the chart\r\nchart.container('container');\r\n\r\n\/\/ initiate the visualization\r\nchart.draw();<\/code><\/pre>\n<p>Ta-da! Our 3D surface plot is complete and can be seen below. For your convenience, I&#8217;ve included the complete code just under the picture. If you want to experiment with the code and see how different modifications can affect the surface chart visualization. Feel free to find the interactive version on the <a href=\"https:\/\/playground.anychart.com\/8F3KBk4L\" target=\"_blank\" rel=\"nofollow\">playground<\/a>. But for now, keep reading because this is just the beginning.<\/p>\n<p><iframe loading=\"lazy\" class=\"anychart-embed anychart-embed-8F3KBk4L\" src=\"https:\/\/playground.anychart.com\/8F3KBk4L\/iframe\" width=\"300\" height=\"150\" frameborder=\"0\" sandbox=\"allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms\" allowfullscreen=\"allowfullscreen\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-8F3KBk4L{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<pre class=\"html\"><code>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;title&gt;JavaScript 3D Surface Plot&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-surface.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-data-adapter.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;style type=\"text\/css\"&gt;\r\n      html, body, #container {\r\n        width: 100%;\r\n        height: 100%;\r\n        margin: 0;\r\n        padding: 0;\r\n      }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n      anychart.onDocumentReady(function () {\r\n        anychart.data.loadJsonFile('https:\/\/gist.githubusercontent.com\/awanshrestha\/9d0f4c864817d5ee043126f63f1f4d02\/raw\/91cb71ac4159ce86bbe2eb204c190f1e9ce43ba6\/surface-test.json',\r\n        function (data) {\r\n          \/\/ pre-process the data\r\n          var result = [];\r\n          for (var x = 0; x &lt; data.x.length; x++) {\r\n            for (var y = 0; y &lt; data.y.length; y++) {\r\n              result.push([x, data.y[y], data.z[x][y]]);\r\n            }\r\n          }\r\n          \/\/ create a surface chart\r\n          var chart = anychart.surface(result);\r\n          \/\/ set the x-axis label format\r\n          chart\r\n            .xAxis()\r\n            .labels()\r\n            .format(function () {\r\n              return data.x[Math.round(this.value)];\r\n            });\r\n          \/\/ set the x-scale maximum\r\n          chart.xScale().maximum(data.x.length - 1);\r\n          \/\/ set the chart padding\r\n          chart.padding(50, 50, 50, 75);\r\n          \/\/ set the default chart rotation by the y-axis\r\n          chart.rotationY(40);\r\n          \/\/ set the default chart rotation by the z-axis\r\n          chart.rotationZ(130);\r\n          \/\/ set the chart title\r\n          chart.title('Population Growth of Top 50 Most Populous Countries Over Past 50 Years');\r\n          \/\/ set the container id for the chart\r\n          chart.container('container');\r\n          \/\/ initiate the visualization\r\n          chart.draw();\r\n        });\r\n      });\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h2>B. How to Customize 3D Surface Plot<\/h2>\n<p>Once we&#8217;ve got a basic 3D surface plot up and running, it&#8217;s time to have some fun with it! Here are some simple, yet effective customizations to take your chart to the next level.<\/p>\n<h3>1. Jazz up the labels and axes<\/h3>\n<p>Let&#8217;s start by giving our X, Y, and Z axis stroke colors a makeover. I personally like black, but you can choose any color you fancy.<\/p>\n<pre class=\"javascript\"><code>chart.xAxis().stroke('#000');\r\nchart.yAxis().stroke('#000');\r\nchart.zAxis().stroke('#000');<\/code><\/pre>\n<p>Next, we can tweak the minimum and maximum scale values of the Y-axis to suit our data.<\/p>\n<pre class=\"javascript\"><code>chart.yScale().minimum(1971);\r\nchart.yScale().maximum(2021);<\/code><\/pre>\n<p>We can also fine-tune the intervals between the ticks on our scales to make them more readable.<\/p>\n<pre class=\"javascript\"><code>chart.xScale().ticks().interval(1);\r\nchart.yScale().ticks().interval(5);<\/code><\/pre>\n<p>Lastly, we can use the <code>rotation()<\/code> parameter of the <code>labels()<\/code> method to adjust the rotation of the X-axis labels, and hide the last label on the Y-axis for a cleaner look.<\/p>\n<pre class=\"javascript\"><code>\/\/ set the x-axis label rotation\r\nchart.xAxis().labels().rotation(90);\r\n\r\n\/\/ hide the last label on the y-axis\r\nchart.yAxis().drawLastLabel(false);<\/code><\/pre>\n<h3>2. Spice up the color palette<\/h3>\n<p>Who says data visualization can&#8217;t be beautiful? Let&#8217;s add some color to our 3D surface chart! We&#8217;ll start by creating a linear color scale using <code>anychart.scales.linearColor()<\/code>, and then set the color palette to our liking.<\/p>\n<pre class=\"javascript\"><code>\/\/ create a color scale\r\nlet customColorScale = anychart.scales.linearColor();\r\ncustomColorScale.colors([\r\n  '#76BA99',\r\n  '#FDFF00',\r\n  '#FD841F',\r\n  '#E14D2A'\r\n]);\r\n\r\n\/\/ set the color scale\r\nchart.colorScale(customColorScale);<\/code><\/pre>\n<p>After our color palette customizations, voil\u00e0! Our 3D surface plot looks stunning, doesn&#8217;t it?<\/p>\n<h3>3. Add a color range<\/h3>\n<p>To make our chart even more user-friendly, we can add a color range indicator to show what each color represents. We can do this using the <code>colorRange()<\/code> method.<\/p>\n<pre class=\"javascript\"><code>chart.colorRange().enabled(true).orientation('right');<\/code><\/pre>\n<p>Feel free to experiment with different <a href=\"https:\/\/api.anychart.com\/v8\/anychart.core.ui.ColorRange\" target=\"_blank\" rel=\"nofollow\">color range settings<\/a>.<\/p>\n<h3>4. Give it some rotation controls<\/h3>\n<p>Now it&#8217;s time to get interactive! We can add rotation controls to our 3D surface plot by inserting the following code in the head section. This creates a simple interface with two range input sliders, each with a label indicating which axis it controls.<\/p>\n<p>When the user moves the Y slider, the onInput event handler <code>rotateY()<\/code> is called, updating the rotation of the chart along the Y-axis. Similarly, when the user moves the Z slider, the onInput event handler <code>rotateZ()<\/code> is called, updating the rotation of the chart along the Z-axis.<\/p>\n<p>We can set the minimum, maximum, and step values for our input sliders in the code snippet below.<\/p>\n<pre class=\"html\"><code>&lt;!-- rotation controls --&gt;\r\n&lt;fieldset&gt;\r\n  &lt;legend&gt;Rotation control&lt;\/legend&gt;\r\n  Y: &lt;input type=\"range\" id=\"rotationY\" min=\"-90\" max=\"90\" step=\"1\" value=\"40\" onInput=\"rotateY()\"&gt;\r\n  Z: &lt;input type=\"range\" id=\"rotationZ\" min=\"-180\" max=\"180\" step=\"1\" value=\"60\" onInput=\"rotateZ()\"&gt;\r\n&lt;\/fieldset&gt;<\/code><\/pre>\n<p>And to make things pretty, we can add some CSS styling for a fieldset element.<\/p>\n<pre class=\"css\"><code>fieldset {\r\n  font-family: Verdana;\r\n  font-size: 13px;\r\n  border: none;\r\n  margin-top: 20px;\r\n}<\/code><\/pre>\n<p>Lastly, we declare a variable <code>theChart<\/code> above the <code>anychart.onDocumentReady()<\/code> function.<\/p>\n<pre class=\"javascript\"><code>let theChart;<\/code><\/pre>\n<p>And we assign it the value of our chart variable from Step 4.<\/p>\n<pre class=\"javascript\"><code>var chart = anychart.surface(result);\r\ntheChart = chart;<\/code><\/pre>\n<p>To make our rotation controls functional, we need to define two functions named <code>rotateY<\/code> and <code>rotateZ<\/code>, which update the rotation of the 3D surface plot on the Y and Z-axes respectively and are called when the user interacts with the corresponding input sliders of the HTML page.<\/p>\n<p>These functions use the <code>document.getElementById()<\/code> method to retrieve the current value of the corresponding range input slider, and convert it to a number using the unary plus operator. The value represents the degree of rotation to be applied to the chart along the corresponding axis. Each function then calls a method on the <code>theChart<\/code> object to update the rotation accordingly.<\/p>\n<pre class=\"javascript\"><code>\/\/ rotation control y\r\nlet rotateY = function() {\r\n  let value = +document.getElementById('rotationY').value;\r\n  theChart.rotationY(value);\r\n};\r\n\r\n\/\/ rotation control z\r\nlet rotateZ = function() {\r\n  let value = +document.getElementById('rotationZ').value;\r\n  theChart.rotationZ(value);\r\n};<\/code><\/pre>\n<p>And there you have it! Our engaging and interactive 3D surface plot is complete &#8211; see it just below! You can check out the full source code on the <a href=\"https:\/\/playground.anychart.com\/ipd0sD6y\" target=\"_blank\" rel=\"nofollow\">playground<\/a> and experiment with colors, interactivity, and other elements to your heart&#8217;s content. Get creative and have fun with it! The full code is also below for your convenience.<\/p>\n<p><iframe loading=\"lazy\" class=\"anychart-embed anychart-embed-ipd0sD6y\" src=\"https:\/\/playground.anychart.com\/ipd0sD6y\/iframe\" width=\"300\" height=\"150\" frameborder=\"0\" sandbox=\"allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms\" allowfullscreen=\"allowfullscreen\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-ipd0sD6y{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<pre class=\"html\"><code>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;title&gt;JavaScript Surface Chart&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-surface.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-data-adapter.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;style type=\"text\/css\"&gt;  \t\r\n      html, body, #container {\r\n        width: 100%;\r\n        height: 100%;\r\n        margin: 0;\r\n        padding: 0;\r\n      }\r\n      fieldset {\r\n        font-family: Verdana;\r\n        font-size: 13px;\r\n        border: none;\r\n        margin-top: 20px;\r\n      }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;!-- rotate controls --&gt;\r\n    &lt;fieldset&gt;\r\n      &lt;legend&gt;Rotation controls:&lt;\/legend&gt;\r\n      Y: &lt;input type=\"range\" id=\"rotationY\" min=\"-90\" max=\"90\" step=\"1\" value=\"40\" onInput=\"rotateY()\"&gt;\r\n      Z: &lt;input type=\"range\" id=\"rotationZ\" min=\"-180\" max=\"180\" step=\"1\" value=\"60\" onInput=\"rotateZ()\"&gt;\r\n    &lt;\/fieldset&gt;\r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n      anychart.onDocumentReady(function () {\r\n        anychart.data.loadJsonFile('https:\/\/gist.githubusercontent.com\/awanshrestha\/9d0f4c864817d5ee043126f63f1f4d02\/raw\/91cb71ac4159ce86bbe2eb204c190f1e9ce43ba6\/surface-test.json',\r\n        function (data) {\r\n            \/\/ pre-processing of the data\r\n            var result = [];\r\n            for (var x = 0; x &lt; data.x.length; x++) {\r\n              for (var y = 0; y &lt; data.y.length; y++) {\r\n                result.push([x, data.y[y], data.z[x][y]]);\r\n              }\r\n            }\r\n            \/\/ create a surface chart\r\n            var chart = anychart.surface(result);\r\n            \/\/ set the x-axis label format\r\n            chart\r\n              .xAxis()\r\n              .labels()\r\n              .format(function () {\r\n                return data.x[Math.round(this.value)];\r\n              });\r\n            \/\/ set the x-axis scale maximum\r\n            chart.xScale().maximum(data.x.length - 1);\r\n            \/\/ set the chart paddings\r\n            chart.padding(50, 50, 50, 75);\r\n            \/\/ set the default chart rotation by the y-axis\r\n            chart.rotationY(40);\r\n            \/\/ set the default chart rotation by the z-axis\r\n            chart.rotationZ(130);\r\n            \/\/ set the chart title\r\n            chart.title('Population Growth of Top 50 Most Populous Countries in Last 50 Years');\r\n            \/\/ set the container id for the chart\r\n            chart.container('container');\r\n            \/\/ initiate the visualization\r\n            chart.draw();\r\n          }\r\n        );\r\n      });\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Congratulations! You have now learned how to create a stunning 3D surface plot using JavaScript. This <a href=\"https:\/\/www.anychart.com\/chartopedia\/\">data visualization technique<\/a> allows you to showcase complex data in an easy-to-understand way.<\/p>\n<p>By following the steps outlined in this tutorial, you can create surface charts that not only look beautiful but also provide valuable insights into your data. Refer to the <a href=\"https:\/\/docs.anychart.com\/3D_Plot\/Surface_Chart\" target=\"_blank\" rel=\"nofollow\">surface chart documentation<\/a> to master more modification options.<\/p>\n<p>If you have any questions or feedback, please feel free to leave a comment. I&#8217;m always happy to help and hear your thoughts.<\/p>\n<p>I hope you enjoyed this tutorial and feel inspired to create your own customizations and experiment with your data. Let&#8217;s continue to explore the vast world of data visualization together!<\/p>\n<hr \/>\n<p><strong><em>AnyChart thanks Awan Shrestha for this awesome <strong>3D Surface Plot<\/strong> tutorial!<\/p>\n<p>See more <a href=\"https:\/\/www.anychart.com\/blog\/category\/javascript-chart-tutorials\/\">JavaScript charting tutorials<\/a> on our blog!<\/p>\n<p>Would you like us to publish your cool guest post? <a href=\"https:\/\/www.anychart.com\/support\/\">Let us know<\/a>!<\/em><\/strong><\/p>\n<hr \/>\n<p><!-- SyntaxHighlighter --><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/default.min.css\"><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/atom-one-light.min.css\"><script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/highlight.min.js\"><\/script><script>hljs.initHighlightingOnLoad();<\/script><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Welcome to this tutorial on creating a visually stunning and interactive 3D surface plot using JavaScript! If you&#8217;re passionate about data visualization and want to expand your skills to the next level, this guide is for you. Here, we&#8217;ll take you through a step-by-step process to create an engaging 3D surface plot that will make [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":22,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,263,23,13,279,4],"tags":[],"class_list":["post-16460","post","type-post","status-publish","format-standard","hentry","category-anychart-charting-component","category-big-data","category-html5","category-javascript","category-javascript-chart-tutorials","category-tips-and-tricks","wpautop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide<\/title>\n<meta name=\"description\" content=\"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide\" \/>\n<meta property=\"og:description\" content=\"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\" \/>\n<meta property=\"og:site_name\" content=\"AnyChart News\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/AnyCharts\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-11T18:24:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-15T18:32:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot-social.png\" \/>\n<meta name=\"author\" content=\"Awan Shrestha\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide\" \/>\n<meta name=\"twitter:description\" content=\"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot-social.png\" \/>\n<meta name=\"twitter:creator\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:site\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Awan Shrestha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\"},\"author\":{\"name\":\"Awan Shrestha\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\"},\"headline\":\"Building 3D Surface Plot in JavaScript\",\"datePublished\":\"2023-04-11T18:24:21+00:00\",\"dateModified\":\"2023-06-15T18:32:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\"},\"wordCount\":1626,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot.png\",\"articleSection\":[\"AnyChart Charting Component\",\"Big Data\",\"HTML5\",\"JavaScript\",\"JavaScript Chart Tutorials\",\"Tips and Tricks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\",\"url\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\",\"name\":\"Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot.png\",\"datePublished\":\"2023-04-11T18:24:21+00:00\",\"dateModified\":\"2023-06-15T18:32:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\"},\"description\":\"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage\",\"url\":\"\",\"contentUrl\":\"\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.anychart.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Building 3D Surface Plot in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\",\"url\":\"https:\/\/www.anychart.com\/blog\/\",\"name\":\"AnyChart News\",\"description\":\"AnyChart JS Charts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.anychart.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\",\"name\":\"Awan Shrestha\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g\",\"caption\":\"Awan Shrestha\"},\"url\":\"https:\/\/www.anychart.com\/blog\/author\/awan-shrestha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide","description":"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/","og_locale":"en_US","og_type":"article","og_title":"Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide","og_description":"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.","og_url":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/","og_site_name":"AnyChart News","article_publisher":"https:\/\/www.facebook.com\/AnyCharts","article_published_time":"2023-04-11T18:24:21+00:00","article_modified_time":"2023-06-15T18:32:49+00:00","og_image":[{"url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot-social.png","type":"","width":"","height":""}],"author":"Awan Shrestha","twitter_card":"summary_large_image","twitter_title":"Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide","twitter_description":"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.","twitter_image":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot-social.png","twitter_creator":"@AnyChart","twitter_site":"@AnyChart","twitter_misc":{"Written by":"Awan Shrestha","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#article","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/"},"author":{"name":"Awan Shrestha","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1"},"headline":"Building 3D Surface Plot in JavaScript","datePublished":"2023-04-11T18:24:21+00:00","dateModified":"2023-06-15T18:32:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/"},"wordCount":1626,"commentCount":0,"image":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot.png","articleSection":["AnyChart Charting Component","Big Data","HTML5","JavaScript","JavaScript Chart Tutorials","Tips and Tricks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/","url":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/","name":"Create Interactive 3D Surface Plot with JavaScript: Step-by-Step Guide","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage"},"image":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/3d-surface-plot.png","datePublished":"2023-04-11T18:24:21+00:00","dateModified":"2023-06-15T18:32:49+00:00","author":{"@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1"},"description":"Learn how to create stunning 3D surface plots using JavaScript. This tutorial takes you through a step-by-step process using real-world population data.","breadcrumb":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/www.anychart.com\/blog\/2023\/04\/11\/3d-surface-plot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.anychart.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Building 3D Surface Plot in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.anychart.com\/blog\/#website","url":"https:\/\/www.anychart.com\/blog\/","name":"AnyChart News","description":"AnyChart JS Charts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.anychart.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1","name":"Awan Shrestha","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g","caption":"Awan Shrestha"},"url":"https:\/\/www.anychart.com\/blog\/author\/awan-shrestha\/"}]}},"_links":{"self":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/comments?post=16460"}],"version-history":[{"count":19,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16460\/revisions"}],"predecessor-version":[{"id":16711,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16460\/revisions\/16711"}],"wp:attachment":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/media?parent=16460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/categories?post=16460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/tags?post=16460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}