Introduction
This is a series of 2 articles where we will see the process of preparing the C# Corner Statistics charts. You can read the first part here -
In this article, we will learn how to prepare the chart data from raw JSON from C# Corner statistics data and draw a chart using the
Toast UI(TUI) Charts Library.
Description
In the previous article, we have learned how to grab or generate C# Corner statistics using web scraping with the help of
cheerio.js.
Let's start Step by step to draw charts using Toast UI(TUI) Charts Library.
Step 1
Install or add the prerequisite references for drawing charts
As we are generating charts, we obviously need JavaScript Charts Library and its dependent JS and CSS files.
- <link rel="stylesheet" href="css/tui-chart.min.css">
- <script src="js/jquery-3.3.1.min.js" type='text/javascript'></script>
- <script type='text/javascript' src='js/tui-code-snippet.min.js'></script>
- <script type='text/javascript' src='js/raphael.min.js'></script>
- <script src="js/tui-chart.min.js"></script>
- <script src="js/lodash.min.js"></script>
Here, tui-chart.js is dependent on tui-code-snippet and Raphael.js so first, we need to add references to these JavaScript libraries before adding tui-chart JS.
Lodash.js is the most popular and useful JavaScript Utility Library that we will use here for preparing the chart.
What does Wikipedia say about lodash?
Lodash is a JavaScript library that provides utility functions for common programming tasks using the functional programming paradigm.
To generate the data for any chart, we have to make some manipulation (Conversion) on raw JSON data before passing that data to a chart.
Using lodash utility function, it will be easy to generate the chart's series data for any Chart Library.
Step 2
Add the required HTML controls or selectors for charts.
- <body>
- <h3>Sample TUI Bar charts based on c-sharpcorner web scrapping data.</h3>
-
- <div id="tui-chart1"></div>
- <br/>
- <div id="tui-chart2"></div>
- <br/>
- <div id="tui-chart3"></div>
- <br/>
-
- </body>
From Web scraping data from c-sharp corner website, we found total 152 Technology categories and for that Big, JSON Data is generated. For this article, I have grabbed the top 10 technology categories based on their article count. So, this JSON data is our raw data for preparing the meaningful visualization charts.
Step 3
Code snippet get top 10 technology categories based on article count using Lodash.js from Big JSON Data.
- var categoryData = ["Articles", "Blogs", "Links", "Videos", "News"];
- var topData = _.chain(AllData).sortBy(function(item) {
- return -1 * parseFloat(item.categoryData[categoryData.indexOf("Articles")].count);
- }).take(10).value();
Reference or add sample JSON data using script tag as below. Reference this JSON object to the page before drawing any charts so it is available before use.
- <script>
- var topArticleData = [{
- "category": "ASP.NET",
- "url": "https://www.c-sharpcorner.com/technologies/asp-dot-net-programming",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "4256"
- }, {
- "categoryType": "Blogs",
- "count": "1380"
- }, {
- "categoryType": "Links",
- "count": "111"
- }, {
- "categoryType": "Videos",
- "count": "77"
- }, {
- "categoryType": "News",
- "count": "18"
- }]
- }, {
- "category": "SharePoint",
- "url": "https://www.c-sharpcorner.com/technologies/sharepoint",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "3944"
- }, {
- "categoryType": "Blogs",
- "count": "2318"
- }, {
- "categoryType": "Links",
- "count": "54"
- }, {
- "categoryType": "Videos",
- "count": "37"
- }, {
- "categoryType": "News",
- "count": "22"
- }]
- }, {
- "category": "C#",
- "url": "https://www.c-sharpcorner.com/technologies/csharp-programming",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "2567"
- }, {
- "categoryType": "Blogs",
- "count": "1684"
- }, {
- "categoryType": "Links",
- "count": "71"
- }, {
- "categoryType": "Videos",
- "count": "98"
- }, {
- "categoryType": "News",
- "count": "14"
- }]
- }, {
- "category": "Azure",
- "url": "https://www.c-sharpcorner.com/technologies/azure",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "1491"
- }, {
- "categoryType": "Blogs",
- "count": "174"
- }, {
- "categoryType": "Links",
- "count": "62"
- }, {
- "categoryType": "Videos",
- "count": "90"
- }, {
- "categoryType": "News",
- "count": "141"
- }]
- }, {
- "category": "SQL Server",
- "url": "https://www.c-sharpcorner.com/technologies/sql-server",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "1205"
- }, {
- "categoryType": "Blogs",
- "count": "1175"
- }, {
- "categoryType": "Links",
- "count": "62"
- }, {
- "categoryType": "Videos",
- "count": "25"
- }, {
- "categoryType": "News",
- "count": "25"
- }]
- }, {
- "category": "Angular",
- "url": "https://www.c-sharpcorner.com/technologies/angularjs",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "1000"
- }, {
- "categoryType": "Blogs",
- "count": "269"
- }, {
- "categoryType": "Links",
- "count": "14"
- }, {
- "categoryType": "Videos",
- "count": "41"
- }, {
- "categoryType": "News",
- "count": "14"
- }]
- }, {
- "category": "Windows 10",
- "url": "https://www.c-sharpcorner.com/technologies/windows_10",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "915"
- }, {
- "categoryType": "Blogs",
- "count": "114"
- }, {
- "categoryType": "Links",
- "count": "46"
- }, {
- "categoryType": "Videos",
- "count": "24"
- }, {
- "categoryType": "News",
- "count": "312"
- }]
- }, {
- "category": "UWP",
- "url": "https://www.c-sharpcorner.com/technologies/universal-windows-apps",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "887"
- }, {
- "categoryType": "Blogs",
- "count": "86"
- }, {
- "categoryType": "Links",
- "count": "28"
- }, {
- "categoryType": "Videos",
- "count": "9"
- }, {
- "categoryType": "News",
- "count": "18"
- }]
- }, {
- "category": ".NET",
- "url": "https://www.c-sharpcorner.com/technologies/dot_net_2015",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "818"
- }, {
- "categoryType": "Blogs",
- "count": "710"
- }, {
- "categoryType": "Links",
- "count": "332"
- }, {
- "categoryType": "Videos",
- "count": "91"
- }, {
- "categoryType": "News",
- "count": "93"
- }]
- }, {
- "category": "Visual Studio",
- "url": "https://www.c-sharpcorner.com/technologies/visual-studio",
- "categoryData": [{
- "categoryType": "Articles",
- "count": "737"
- }, {
- "categoryType": "Blogs",
- "count": "231"
- }, {
- "categoryType": "Links",
- "count": "51"
- }, {
- "categoryType": "Videos",
- "count": "73"
- }, {
- "categoryType": "News",
- "count": "115"
- }]
- }];
- </script>
Step 4
Prepare the charts' data and draw charts.
- <script>
-
-
- function drawBarChartUsingTUILibrary(options) {
-
- var chartOptions = {
- chartExportMenu: {
- visible: false
- },
- chart: {
- width: 470,
- height: 250,
- title: 'C-SharpCorner Articles Technology Category',
- format: '1,000'
- },
- yAxis: {
- title: 'Y Axis'
- },
- xAxis: {
- title: 'X Axis',
- min: 0,
-
-
- },
- series: {
- showLabel: true
- }
- };
-
-
- var categoryData = ["Articles", "Blogs", "Links", "Videos", "News"];
-
-
- var data = {
-
- categories: [options.category],
-
- series: _.map(options.data, function(e) {
-
- var ele = _.find(e.categoryData, function(e) {
- return e.categoryType === options.category ? e.count : 0;
- });
- return {
- name: e.category,
- data: parseInt(ele.count)
- };
- })
- };
-
-
- _.set(chartOptions, "chart.title", options.chartTitle);
- if (options.isColumnChart) {
-
-
- tui.chart.columnChart(document.getElementById(options.id), data, chartOptions);
- } else {
-
- tui.chart.barChart(document.getElementById(options.id), data, chartOptions);
- }
- }
-
- $(document).ready(function() {
- console.log("Dom is ready, Now call a function to draw charts");
-
- drawBarChartUsingTUILibrary({
- id: "tui-chart1",
- chartTitle: 'C-SharpCorner Top 10 Contribution by Articles Count',
- data: topArticleData,
- category: "Articles"
- });
- drawBarChartUsingTUILibrary({
- id: "tui-chart2",
- chartTitle: 'C-SharpCorner Top 10 Contribution by Blogs Count',
- data: topArticleData,
- category: "Blogs"
- });
- drawBarChartUsingTUILibrary({
- id: "tui-chart3",
- chartTitle: 'C-SharpCorner Top 10 Contribution by News Count',
- data: topArticleData,
- category: "News",
- isColumnChart: true
- });
- });
- </script>
In the above JavaScript code snippet, I have created a reusable function to draw charts based on the provided parameters. I have used some of the Lodash utility functions like _.map, _.find, _.set. to prepare the charts series data.
Conclusion
In this article, we learned how to draw charts and generate charts data from any raw JSON data using lodash js and Toast UI Charts. I hope you liked it.