{"id":20233,"date":"2020-11-18T11:23:22","date_gmt":"2020-11-18T10:23:22","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=20233"},"modified":"2026-01-13T11:16:29","modified_gmt":"2026-01-13T11:16:29","slug":"working-with-data-api","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/tutorials\/working-with-data-api\/","title":{"rendered":"Working With Data &#038; API"},"content":{"rendered":"<p>In this article, we will compare two ways to visualize data from a CSV file:<\/p>\n<ol>\n<li>The featured Highcharts <a href=\"https:\/\/www.highcharts.com\/docs\/working-with-data\/custom-preprocessing#preprocess-data-using-csv\" target=\"_blank\" rel=\"noopener noreferrer\">data-module API<\/a> solution using the <a href=\"https:\/\/api.highcharts.com\/highcharts\/data.csvURL\" target=\"_blank\" rel=\"noopener noreferrer\">data.cvsURL<\/a>.<\/li>\n<li>A custom parser written in pure JavaScript, for more flexibility in advanced cases.<\/li>\n<\/ol>\n<p>Let\u2019s get started \ud83d\ude42<\/p>\n<p>Whichever way you choose, you have to go through the basics when dealing with data, such as find the data source, load the data, and process the data.<\/p>\n<p><i><b>Remark<\/b><br \/>\nIn our <a href=\"https:\/\/www.highcharts.com\/docs\/working-with-data\/data-module\" target=\"_blank\" rel=\"noopener noreferrer\">documentation<\/a> and <a href=\"https:\/\/www.highcharts.com\/docs\/working-with-data\/custom-preprocessing#preprocess-data-using-csv\" target=\"_blank\" rel=\"noopener noreferrer\">API<\/a>, you can find many examples and solutions that allow you to start working\/processing data. <\/i><\/p>\n<p>We will use the data set from NASA (National Aeronautics and Space Administration), particularly from the <a href=\"https:\/\/www.giss.nasa.gov\/\" target=\"_blank\" rel=\"noopener noreferrer\">NASA Goddard Institute for Space Studies<\/a>. The idea is to visualize the CSV file that includes the global average temperature from 1880 to 2019.<\/p>\n<h2>1. Data source<\/h2>\n<p>Let&#8217;s grab the <a href=\"https:\/\/data.giss.nasa.gov\/gistemp\/tabledata_v4\/ZonAnn.Ts+dSST.csv\" target=\"_blank\" rel=\"noopener noreferrer\">Zonal annual means CSV file<\/a>, store it on <a href=\"https:\/\/github.com\/mekhatria\/demo_highcharts\/blob\/master\/globalTemperatureChange.csv\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a> to be used for the dedicated Highcharts solution, and locally be used for the custom way. The CSV file has several columns: Year, Glob, Nhem, northern hemisphere, etc. The columns are separated by commas. The file isn&#8217;t really meant to be human-readable; there are ways of viewing a CSV that&#8217;s more human-readable. For example, in the custom demo, you can see how the data looks in a spreadsheet format using the Highcharts <a href=\"https:\/\/www.highcharts.com\/docs\/export-module\/export-module-overview\" target=\"_blank\" rel=\"noopener noreferrer\">export-data module<\/a>, which we highly recommend using for that purpose.<\/p>\n<h2>2. Data loading<\/h2>\n<p>To start working with the data, we need to load it into the script. For the built-in Highcharts solution, this is as easy as setting the <a href=\"https:\/\/api.highcharts.com\/highcharts\/data.csvURL\" target=\"_blank\" rel=\"noopener noreferrer\">data.cvsURL<\/a> option.<\/p>\n<p>For the custom parser, we will use the Fetch API standard. We will also use the await and async syntax for the custom solution supported in modern browsers. Remember to check the browser&#8217;s capacity when working with the newest features of JavaScript. When you want to get support for older browsers it&#8217;s best to compile your code to the older ES5 syntax.<\/p>\n<h2>3. Data parsing<\/h2>\n<p>If you go with the built-in Highcharts solution, the data module will spare you the pain to go through the data parsing and you can skip directly to the data visualization section (see section <i><b>4. Data visualization<\/b><\/i>).<\/p>\n<p>However, for the <em>custom solution<\/em>, there are various JavaScript libraries to parse a CSV. By parsing, we mean to figure out where all the commas are located, break up the data, and put it into objects to make it usable. For this data set, it&#8217;s simple enough to do the parsing manually with the split function. Btw, a piece of text in a JavaScript variable is a string object and has a function called split. That function allows you to take any arbitrary text and split it up into different elements of an array. And that&#8217;s basically what we want to do; we want to split up all the rows, and then in each row, we split up all the columns. The split function requires a single argument &#8211; a separator or otherwise known as a delimiter. And in this case, we have two kinds of delimiters. For each row, the delimiter, which differentiates one row from another, is a line break. So first, let&#8217;s call split with a line break. Also, we don&#8217;t need this first row &#8211; the first row is really useful information for us as human beings to think about what the data is, but for that use, we want to take years as a category.<\/p>\n<p>Now it&#8217;s important to know this data is clean: no empty date, no mistakes, no empty pieces. But, if the data has commas, this parsing system is going to break down. Even though there are conventions for CSV files to use quotes around the information that shouldn&#8217;t be split, where there is a comma in there, you always have to check the data. You also might find that your data isn&#8217;t already in CSV format, so be sure to get rid of the unnecessary spaces and commas in the data either manually or through CSV converter\/cleaner tools.. This might be a lot of work that can go into parsing and cleaning data for your projects.<\/p>\n<h2>4. Data visualization<\/h2>\n<p>Now, we&#8217;re ready to visualize the data using a simple line chart.<br \/>\nFor the dedicated Highcharts solution, the data is hosted in <a href=\"https:\/\/github.com\/mekhatria\/demo_highcharts\/blob\/master\/globalTemperatureChange.csv\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a>, and it takes only one line of code to fetch the data (thanks to the <a href=\"https:\/\/www.highcharts.com\/docs\/working-with-data\/data-module\" target=\"_blank\" rel=\"noopener noreferrer\">data-module<\/a>):<\/p>\n<pre>data: {\r\n  csvURL: 'https:\/\/raw.githubusercontent.com\/mekhatria\/demo_highcharts\/master\/globalTemperatureChange.csv',\r\n}<\/pre>\n<p>The Highcharts data module does all the heavy lifting for you to fetch, process, and prepare the data to be visualized. Yes, it is that simple, and the result is in the <a href=\"https:\/\/codepen.io\/mushigh\/pen\/dyXRKOw\" target=\"_blank\" rel=\"noopener noreferrer\">demo below<\/a>:<\/p>\n<p class=\"demo-comtainer\"><iframe style=\"width: 100%;\" title=\"Global temperature change\" src=\"https:\/\/codepen.io\/mushigh\/embed\/dyXRKOw?height=265&amp;theme-id=light&amp;default-tab=result\" height=\"450\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\" title=\"A line chart displays the global temperature change from 1880 to 2019. By Piotr Madej\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/mushigh\/pen\/dyXRKOw\">Global temperature change<\/a> by mustapha mekhatria<br \/>\n(<a href=\"https:\/\/codepen.io\/mushigh\">@mushigh<\/a>) on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<p>Below, you can see the same data and chart type using a customised code (see <a href=\"https:\/\/madepiet.github.io\/highcharts-demos\/highcharts-csv\/manual-parsing-demo\/\" target=\"_blank\" rel=\"noopener noreferrer\">chart below<\/a>):<br \/>\n<a href=\"https:\/\/madepiet.github.io\/highcharts-demos\/highcharts-csv\/manual-parsing-demo\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20354 aligncenter\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/11\/18145013\/Screenshot-2020-11-18-at-14.49.34.jpg\" alt=\"Working With Data &amp; API\" width=\"664\" height=\"821\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/11\/18145013\/Screenshot-2020-11-18-at-14.49.34.jpg 664w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/11\/18145013\/Screenshot-2020-11-18-at-14.49.34-356x440.jpg 356w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/11\/18145013\/Screenshot-2020-11-18-at-14.49.34-477x590.jpg 477w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/11\/18145013\/Screenshot-2020-11-18-at-14.49.34-226x280.jpg 226w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/a><br \/>\n<!--iframe src=\"https:\/\/madepiet.github.io\/highcharts-demos\/highcharts-csv\/manual-parsing-demo\/\" height=\"650\" style=\"width: 100%;\"><\/iframe--><\/p>\n<p>Even though the charts look similar, the code behind the scene is different. Instead of one line to fetch the data using the data module, now, there is a whole function for the same job:<\/p>\n<pre>async function getData() {\r\n  const date = [],\r\n    globalTemp = [],\r\n    northernTemp = [],\r\n    southernTemp = [],\r\n    response = await fetch('..\/ZonAnn.Ts+dSST.csv'),\r\n    data = await response.text(),\r\n    rows = data.split(\/\\n\/).slice(1);\r\n\r\n  rows.forEach(element =&gt; {\r\n    const row = element.split(','),\r\n      year = row[0],\r\n      gt = +row[1],\r\n      nt = +row[2],\r\n      st = +row[3];\r\n\r\n    date.push(year);\r\n    globalTemp.push(gt);\r\n    northernTemp.push(nt);\r\n    southernTemp.push(st);\r\n  })\r\n\r\n  return {\r\n    date,\r\n    globalTemp,\r\n    northernTemp,\r\n    southernTemp\r\n  }\r\n}<\/pre>\n<p>The function above fetches the data from local storage, cleans it, processes it, and returns the result in four different arrays.<\/p>\n<p>For both examples, the idea is to load tabular data in the form of a CSV and visualize it. We had to go through the data processing, then display the data on the charts. In the first demo, the Highcharts data module offers an out-of-the-box solution to fetch and process the data. In most cases, we recommend using this option. Nevertheless, if you need to make specific processing, you can always go with a custom code.<\/p>\n<p>We encourage you to use what you learned in this article to create interactive charts with different data and techniques. You are always welcome to leave your questions and comments under this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create an interactive chart with Highcharts and data from a CSV file.<\/p>\n","protected":false},"author":255,"featured_media":21948,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[210],"tags":[1098,1094],"coauthors":[844],"class_list":["post-20233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-data-connectors","tag-highcharts-core"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/255"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=20233"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20233\/revisions"}],"predecessor-version":[{"id":29330,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20233\/revisions\/29330"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/21948"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=20233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=20233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=20233"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=20233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}